blob: 109ecfaaccb79f2a6401938e2180c511e3af9eef [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>
York Sun020198b2016-11-23 09:25:09 -080017#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000018#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000019#include <errno.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000021#include <asm/io.h>
22DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000023#endif /* !USE_HOSTCC*/
24
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050025#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000026#include <bootstage.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000027#include <u-boot/crc.h>
28#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020029#include <u-boot/sha1.h>
30#include <u-boot/sha256.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000031
32/*****************************************************************************/
33/* New uImage format routines */
34/*****************************************************************************/
35#ifndef USE_HOSTCC
36static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
37 ulong *addr, const char **name)
38{
39 const char *sep;
40
41 *addr = addr_curr;
42 *name = NULL;
43
44 sep = strchr(spec, sepc);
45 if (sep) {
46 if (sep - spec > 0)
47 *addr = simple_strtoul(spec, NULL, 16);
48
49 *name = sep + 1;
50 return 1;
51 }
52
53 return 0;
54}
55
56/**
57 * fit_parse_conf - parse FIT configuration spec
58 * @spec: input string, containing configuration spec
59 * @add_curr: current image address (to be used as a possible default)
60 * @addr: pointer to a ulong variable, will hold FIT image address of a given
61 * configuration
62 * @conf_name double pointer to a char, will hold pointer to a configuration
63 * unit name
64 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090065 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000066 * where <addr> is a FIT image address that contains configuration
67 * with a <conf> unit name.
68 *
69 * Address part is optional, and if omitted default add_curr will
70 * be used instead.
71 *
72 * returns:
73 * 1 if spec is a valid configuration string,
74 * addr and conf_name are set accordingly
75 * 0 otherwise
76 */
77int fit_parse_conf(const char *spec, ulong addr_curr,
78 ulong *addr, const char **conf_name)
79{
80 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
81}
82
83/**
84 * fit_parse_subimage - parse FIT subimage spec
85 * @spec: input string, containing subimage spec
86 * @add_curr: current image address (to be used as a possible default)
87 * @addr: pointer to a ulong variable, will hold FIT image address of a given
88 * subimage
89 * @image_name: double pointer to a char, will hold pointer to a subimage name
90 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090091 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000092 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
93 * subimage with a <subimg> unit name.
94 *
95 * Address part is optional, and if omitted default add_curr will
96 * be used instead.
97 *
98 * returns:
99 * 1 if spec is a valid subimage string,
100 * addr and image_name are set accordingly
101 * 0 otherwise
102 */
103int fit_parse_subimage(const char *spec, ulong addr_curr,
104 ulong *addr, const char **image_name)
105{
106 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
107}
108#endif /* !USE_HOSTCC */
109
110static void fit_get_debug(const void *fit, int noffset,
111 char *prop_name, int err)
112{
113 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
114 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
115 fdt_strerror(err));
116}
117
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200118/**
119 * fit_get_subimage_count - get component (sub-image) count
120 * @fit: pointer to the FIT format image header
121 * @images_noffset: offset of images node
122 *
123 * returns:
124 * number of image components
125 */
126int fit_get_subimage_count(const void *fit, int images_noffset)
127{
128 int noffset;
129 int ndepth;
130 int count = 0;
131
132 /* Process its subnodes, print out component images details */
133 for (ndepth = 0, count = 0,
134 noffset = fdt_next_node(fit, images_noffset, &ndepth);
135 (noffset >= 0) && (ndepth > 0);
136 noffset = fdt_next_node(fit, noffset, &ndepth)) {
137 if (ndepth == 1) {
138 count++;
139 }
140 }
141
142 return count;
143}
144
Simon Glass87ebee32013-05-08 08:05:59 +0000145#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000146/**
147 * fit_print_contents - prints out the contents of the FIT format image
148 * @fit: pointer to the FIT format image header
149 * @p: pointer to prefix string
150 *
151 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500152 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000153 * the details of each component image.
154 *
155 * returns:
156 * no returned results
157 */
158void fit_print_contents(const void *fit)
159{
160 char *desc;
161 char *uname;
162 int images_noffset;
163 int confs_noffset;
164 int noffset;
165 int ndepth;
166 int count = 0;
167 int ret;
168 const char *p;
169 time_t timestamp;
170
Simon Glass1fe7d932013-05-08 08:05:58 +0000171 /* Indent string is defined in header image.h */
172 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000173
174 /* Root node properties */
175 ret = fit_get_desc(fit, 0, &desc);
176 printf("%sFIT description: ", p);
177 if (ret)
178 printf("unavailable\n");
179 else
180 printf("%s\n", desc);
181
182 if (IMAGE_ENABLE_TIMESTAMP) {
183 ret = fit_get_timestamp(fit, 0, &timestamp);
184 printf("%sCreated: ", p);
185 if (ret)
186 printf("unavailable\n");
187 else
188 genimg_print_time(timestamp);
189 }
190
191 /* Find images parent node offset */
192 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
193 if (images_noffset < 0) {
194 printf("Can't find images parent node '%s' (%s)\n",
195 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
196 return;
197 }
198
199 /* Process its subnodes, print out component images details */
200 for (ndepth = 0, count = 0,
201 noffset = fdt_next_node(fit, images_noffset, &ndepth);
202 (noffset >= 0) && (ndepth > 0);
203 noffset = fdt_next_node(fit, noffset, &ndepth)) {
204 if (ndepth == 1) {
205 /*
206 * Direct child node of the images parent node,
207 * i.e. component image node.
208 */
209 printf("%s Image %u (%s)\n", p, count++,
210 fit_get_name(fit, noffset, NULL));
211
212 fit_image_print(fit, noffset, p);
213 }
214 }
215
216 /* Find configurations parent node offset */
217 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
218 if (confs_noffset < 0) {
219 debug("Can't get configurations parent node '%s' (%s)\n",
220 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
221 return;
222 }
223
224 /* get default configuration unit name from default property */
225 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
226 if (uname)
227 printf("%s Default Configuration: '%s'\n", p, uname);
228
229 /* Process its subnodes, print out configurations details */
230 for (ndepth = 0, count = 0,
231 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
232 (noffset >= 0) && (ndepth > 0);
233 noffset = fdt_next_node(fit, noffset, &ndepth)) {
234 if (ndepth == 1) {
235 /*
236 * Direct child node of the configurations parent node,
237 * i.e. configuration node.
238 */
239 printf("%s Configuration %u (%s)\n", p, count++,
240 fit_get_name(fit, noffset, NULL));
241
242 fit_conf_print(fit, noffset, p);
243 }
244 }
245}
246
247/**
Simon Glassd8b75362013-05-07 06:12:02 +0000248 * fit_image_print_data() - prints out the hash node details
249 * @fit: pointer to the FIT format image header
250 * @noffset: offset of the hash node
251 * @p: pointer to prefix string
Simon Glass56518e72013-06-13 15:10:01 -0700252 * @type: Type of information to print ("hash" or "sign")
Simon Glassd8b75362013-05-07 06:12:02 +0000253 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500254 * fit_image_print_data() lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000255 *
Simon Glass56518e72013-06-13 15:10:01 -0700256 * This function avoid using puts() since it prints a newline on the host
257 * but does not in U-Boot.
258 *
Simon Glassd8b75362013-05-07 06:12:02 +0000259 * returns:
260 * no returned results
261 */
Simon Glass56518e72013-06-13 15:10:01 -0700262static void fit_image_print_data(const void *fit, int noffset, const char *p,
263 const char *type)
Simon Glassd8b75362013-05-07 06:12:02 +0000264{
Simon Glass56518e72013-06-13 15:10:01 -0700265 const char *keyname;
Simon Glassd8b75362013-05-07 06:12:02 +0000266 uint8_t *value;
267 int value_len;
Simon Glass56518e72013-06-13 15:10:01 -0700268 char *algo;
269 int required;
270 int ret, i;
Simon Glassd8b75362013-05-07 06:12:02 +0000271
Simon Glass56518e72013-06-13 15:10:01 -0700272 debug("%s %s node: '%s'\n", p, type,
Simon Glassd8b75362013-05-07 06:12:02 +0000273 fit_get_name(fit, noffset, NULL));
Simon Glass56518e72013-06-13 15:10:01 -0700274 printf("%s %s algo: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000275 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
276 printf("invalid/unsupported\n");
277 return;
278 }
Simon Glass56518e72013-06-13 15:10:01 -0700279 printf("%s", algo);
280 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
281 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
282 if (keyname)
283 printf(":%s", keyname);
284 if (required)
285 printf(" (required)");
286 printf("\n");
Simon Glassd8b75362013-05-07 06:12:02 +0000287
288 ret = fit_image_hash_get_value(fit, noffset, &value,
289 &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700290 printf("%s %s value: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000291 if (ret) {
292 printf("unavailable\n");
293 } else {
294 for (i = 0; i < value_len; i++)
295 printf("%02x", value[i]);
296 printf("\n");
297 }
298
Simon Glass56518e72013-06-13 15:10:01 -0700299 debug("%s %s len: %d\n", p, type, value_len);
300
301 /* Signatures have a time stamp */
302 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
303 time_t timestamp;
304
305 printf("%s Timestamp: ", p);
306 if (fit_get_timestamp(fit, noffset, &timestamp))
307 printf("unavailable\n");
308 else
309 genimg_print_time(timestamp);
310 }
Simon Glassd8b75362013-05-07 06:12:02 +0000311}
312
313/**
314 * fit_image_print_verification_data() - prints out the hash/signature details
315 * @fit: pointer to the FIT format image header
316 * @noffset: offset of the hash or signature node
317 * @p: pointer to prefix string
318 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500319 * This lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000320 *
321 * returns:
322 * no returned results
323 */
324static void fit_image_print_verification_data(const void *fit, int noffset,
325 const char *p)
326{
327 const char *name;
328
329 /*
330 * Check subnode name, must be equal to "hash" or "signature".
331 * Multiple hash/signature nodes require unique unit node
332 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
333 */
334 name = fit_get_name(fit, noffset, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700335 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
336 fit_image_print_data(fit, noffset, p, "Hash");
337 } else if (!strncmp(name, FIT_SIG_NODENAME,
338 strlen(FIT_SIG_NODENAME))) {
339 fit_image_print_data(fit, noffset, p, "Sign");
340 }
Simon Glassd8b75362013-05-07 06:12:02 +0000341}
342
343/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000344 * fit_image_print - prints out the FIT component image details
345 * @fit: pointer to the FIT format image header
346 * @image_noffset: offset of the component image node
347 * @p: pointer to prefix string
348 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500349 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000350 * image. If present, hash nodes are printed out as well. Load
351 * address for images of type firmware is also printed out. Since the load
352 * address is not mandatory for firmware images, it will be output as
353 * "unavailable" when not present.
354 *
355 * returns:
356 * no returned results
357 */
358void fit_image_print(const void *fit, int image_noffset, const char *p)
359{
360 char *desc;
361 uint8_t type, arch, os, comp;
362 size_t size;
363 ulong load, entry;
364 const void *data;
365 int noffset;
366 int ndepth;
367 int ret;
368
369 /* Mandatory properties */
370 ret = fit_get_desc(fit, image_noffset, &desc);
371 printf("%s Description: ", p);
372 if (ret)
373 printf("unavailable\n");
374 else
375 printf("%s\n", desc);
376
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700377 if (IMAGE_ENABLE_TIMESTAMP) {
378 time_t timestamp;
379
380 ret = fit_get_timestamp(fit, 0, &timestamp);
381 printf("%s Created: ", p);
382 if (ret)
383 printf("unavailable\n");
384 else
385 genimg_print_time(timestamp);
386 }
387
Simon Glass53fbb7e2013-05-07 06:11:53 +0000388 fit_image_get_type(fit, image_noffset, &type);
389 printf("%s Type: %s\n", p, genimg_get_type_name(type));
390
391 fit_image_get_comp(fit, image_noffset, &comp);
392 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
393
394 ret = fit_image_get_data(fit, image_noffset, &data, &size);
395
396#ifndef USE_HOSTCC
397 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000398 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000399 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000400 } else {
401 void *vdata = (void *)data;
402
403 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
404 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000405#endif
406
407 printf("%s Data Size: ", p);
408 if (ret)
409 printf("unavailable\n");
410 else
411 genimg_print_size(size);
412
413 /* Remaining, type dependent properties */
414 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
415 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
416 (type == IH_TYPE_FLATDT)) {
417 fit_image_get_arch(fit, image_noffset, &arch);
418 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
419 }
420
421 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
422 fit_image_get_os(fit, image_noffset, &os);
423 printf("%s OS: %s\n", p, genimg_get_os_name(os));
424 }
425
426 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200427 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
428 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000429 ret = fit_image_get_load(fit, image_noffset, &load);
430 printf("%s Load Address: ", p);
431 if (ret)
432 printf("unavailable\n");
433 else
434 printf("0x%08lx\n", load);
435 }
436
437 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
438 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800439 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000440 printf("%s Entry Point: ", p);
441 if (ret)
442 printf("unavailable\n");
443 else
444 printf("0x%08lx\n", entry);
445 }
446
447 /* Process all hash subnodes of the component image node */
448 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
449 (noffset >= 0) && (ndepth > 0);
450 noffset = fdt_next_node(fit, noffset, &ndepth)) {
451 if (ndepth == 1) {
452 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000453 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000454 }
455 }
456}
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200457
458#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000459
460/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000461 * fit_get_desc - get node description property
462 * @fit: pointer to the FIT format image header
463 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500464 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000465 *
466 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500467 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000468 *
469 * returns:
470 * 0, on success
471 * -1, on failure
472 */
473int fit_get_desc(const void *fit, int noffset, char **desc)
474{
475 int len;
476
477 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
478 if (*desc == NULL) {
479 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
480 return -1;
481 }
482
483 return 0;
484}
485
486/**
487 * fit_get_timestamp - get node timestamp property
488 * @fit: pointer to the FIT format image header
489 * @noffset: node offset
490 * @timestamp: pointer to the time_t, will hold read timestamp
491 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500492 * fit_get_timestamp() reads timestamp property from given node, if timestamp
493 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000494 * argument.
495 *
496 * returns:
497 * 0, on success
498 * -1, on property read failure
499 * -2, on wrong timestamp size
500 */
501int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
502{
503 int len;
504 const void *data;
505
506 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
507 if (data == NULL) {
508 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
509 return -1;
510 }
511 if (len != sizeof(uint32_t)) {
512 debug("FIT timestamp with incorrect size of (%u)\n", len);
513 return -2;
514 }
515
516 *timestamp = uimage_to_cpu(*((uint32_t *)data));
517 return 0;
518}
519
520/**
521 * fit_image_get_node - get node offset for component image of a given unit name
522 * @fit: pointer to the FIT format image header
523 * @image_uname: component image node unit name
524 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500525 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000526 * node) of a provided unit name. If image is found its node offset is
527 * returned to the caller.
528 *
529 * returns:
530 * image node offset when found (>=0)
531 * negative number on failure (FDT_ERR_* code)
532 */
533int fit_image_get_node(const void *fit, const char *image_uname)
534{
535 int noffset, images_noffset;
536
537 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
538 if (images_noffset < 0) {
539 debug("Can't find images parent node '%s' (%s)\n",
540 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
541 return images_noffset;
542 }
543
544 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
545 if (noffset < 0) {
546 debug("Can't get node offset for image unit name: '%s' (%s)\n",
547 image_uname, fdt_strerror(noffset));
548 }
549
550 return noffset;
551}
552
553/**
554 * fit_image_get_os - get os id for a given component image node
555 * @fit: pointer to the FIT format image header
556 * @noffset: component image node offset
557 * @os: pointer to the uint8_t, will hold os numeric id
558 *
559 * fit_image_get_os() finds os property in a given component image node.
560 * If the property is found, its (string) value is translated to the numeric
561 * id which is returned to the caller.
562 *
563 * returns:
564 * 0, on success
565 * -1, on failure
566 */
567int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
568{
569 int len;
570 const void *data;
571
572 /* Get OS name from property data */
573 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
574 if (data == NULL) {
575 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
576 *os = -1;
577 return -1;
578 }
579
580 /* Translate OS name to id */
581 *os = genimg_get_os_id(data);
582 return 0;
583}
584
585/**
586 * fit_image_get_arch - get arch id for a given component image node
587 * @fit: pointer to the FIT format image header
588 * @noffset: component image node offset
589 * @arch: pointer to the uint8_t, will hold arch numeric id
590 *
591 * fit_image_get_arch() finds arch property in a given component image node.
592 * If the property is found, its (string) value is translated to the numeric
593 * id which is returned to the caller.
594 *
595 * returns:
596 * 0, on success
597 * -1, on failure
598 */
599int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
600{
601 int len;
602 const void *data;
603
604 /* Get architecture name from property data */
605 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
606 if (data == NULL) {
607 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
608 *arch = -1;
609 return -1;
610 }
611
612 /* Translate architecture name to id */
613 *arch = genimg_get_arch_id(data);
614 return 0;
615}
616
617/**
618 * fit_image_get_type - get type id for a given component image node
619 * @fit: pointer to the FIT format image header
620 * @noffset: component image node offset
621 * @type: pointer to the uint8_t, will hold type numeric id
622 *
623 * fit_image_get_type() finds type property in a given component image node.
624 * If the property is found, its (string) value is translated to the numeric
625 * id which is returned to the caller.
626 *
627 * returns:
628 * 0, on success
629 * -1, on failure
630 */
631int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
632{
633 int len;
634 const void *data;
635
636 /* Get image type name from property data */
637 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
638 if (data == NULL) {
639 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
640 *type = -1;
641 return -1;
642 }
643
644 /* Translate image type name to id */
645 *type = genimg_get_type_id(data);
646 return 0;
647}
648
649/**
650 * fit_image_get_comp - get comp id for a given component image node
651 * @fit: pointer to the FIT format image header
652 * @noffset: component image node offset
653 * @comp: pointer to the uint8_t, will hold comp numeric id
654 *
655 * fit_image_get_comp() finds comp property in a given component image node.
656 * If the property is found, its (string) value is translated to the numeric
657 * id which is returned to the caller.
658 *
659 * returns:
660 * 0, on success
661 * -1, on failure
662 */
663int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
664{
665 int len;
666 const void *data;
667
668 /* Get compression name from property data */
669 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
670 if (data == NULL) {
671 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
672 *comp = -1;
673 return -1;
674 }
675
676 /* Translate compression name to id */
677 *comp = genimg_get_comp_id(data);
678 return 0;
679}
680
York Sun60047652016-02-29 15:48:40 -0800681static int fit_image_get_address(const void *fit, int noffset, char *name,
682 ulong *load)
683{
York Sunc1913cb2016-02-29 15:48:41 -0800684 int len, cell_len;
685 const fdt32_t *cell;
686 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800687
York Sunc1913cb2016-02-29 15:48:41 -0800688 cell = fdt_getprop(fit, noffset, name, &len);
689 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800690 fit_get_debug(fit, noffset, name, len);
691 return -1;
692 }
693
York Sunc1913cb2016-02-29 15:48:41 -0800694 if (len > sizeof(ulong)) {
695 printf("Unsupported %s address size\n", name);
696 return -1;
697 }
698
699 cell_len = len >> 2;
700 /* Use load64 to avoid compiling warning for 32-bit target */
701 while (cell_len--) {
702 load64 = (load64 << 32) | uimage_to_cpu(*cell);
703 cell++;
704 }
705 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800706
707 return 0;
708}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000709/**
710 * fit_image_get_load() - get load addr property for given component image node
711 * @fit: pointer to the FIT format image header
712 * @noffset: component image node offset
713 * @load: pointer to the uint32_t, will hold load address
714 *
715 * fit_image_get_load() finds load address property in a given component
716 * image node. If the property is found, its value is returned to the caller.
717 *
718 * returns:
719 * 0, on success
720 * -1, on failure
721 */
722int fit_image_get_load(const void *fit, int noffset, ulong *load)
723{
York Sun60047652016-02-29 15:48:40 -0800724 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000725}
726
727/**
728 * fit_image_get_entry() - get entry point address property
729 * @fit: pointer to the FIT format image header
730 * @noffset: component image node offset
731 * @entry: pointer to the uint32_t, will hold entry point address
732 *
733 * This gets the entry point address property for a given component image
734 * node.
735 *
736 * fit_image_get_entry() finds entry point address property in a given
737 * component image node. If the property is found, its value is returned
738 * to the caller.
739 *
740 * returns:
741 * 0, on success
742 * -1, on failure
743 */
744int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
745{
York Sun60047652016-02-29 15:48:40 -0800746 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000747}
748
749/**
750 * fit_image_get_data - get data property and its size for a given component image node
751 * @fit: pointer to the FIT format image header
752 * @noffset: component image node offset
753 * @data: double pointer to void, will hold data property's data address
754 * @size: pointer to size_t, will hold data property's data size
755 *
756 * fit_image_get_data() finds data property in a given component image node.
757 * If the property is found its data start address and size are returned to
758 * the caller.
759 *
760 * returns:
761 * 0, on success
762 * -1, on failure
763 */
764int fit_image_get_data(const void *fit, int noffset,
765 const void **data, size_t *size)
766{
767 int len;
768
769 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
770 if (*data == NULL) {
771 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
772 *size = 0;
773 return -1;
774 }
775
776 *size = len;
777 return 0;
778}
779
780/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200781 * Get 'data-offset' property from a given image node.
782 *
783 * @fit: pointer to the FIT image header
784 * @noffset: component image node offset
785 * @data_offset: holds the data-offset property
786 *
787 * returns:
788 * 0, on success
789 * -ENOENT if the property could not be found
790 */
791int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
792{
793 const fdt32_t *val;
794
795 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
796 if (!val)
797 return -ENOENT;
798
799 *data_offset = fdt32_to_cpu(*val);
800
801 return 0;
802}
803
804/**
805 * Get 'data-size' property from a given image node.
806 *
807 * @fit: pointer to the FIT image header
808 * @noffset: component image node offset
809 * @data_size: holds the data-size property
810 *
811 * returns:
812 * 0, on success
813 * -ENOENT if the property could not be found
814 */
815int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
816{
817 const fdt32_t *val;
818
819 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
820 if (!val)
821 return -ENOENT;
822
823 *data_size = fdt32_to_cpu(*val);
824
825 return 0;
826}
827
828/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000829 * fit_image_hash_get_algo - get hash algorithm name
830 * @fit: pointer to the FIT format image header
831 * @noffset: hash node offset
832 * @algo: double pointer to char, will hold pointer to the algorithm name
833 *
834 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
835 * If the property is found its data start address is returned to the caller.
836 *
837 * returns:
838 * 0, on success
839 * -1, on failure
840 */
841int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
842{
843 int len;
844
845 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
846 if (*algo == NULL) {
847 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
848 return -1;
849 }
850
851 return 0;
852}
853
854/**
855 * fit_image_hash_get_value - get hash value and length
856 * @fit: pointer to the FIT format image header
857 * @noffset: hash node offset
858 * @value: double pointer to uint8_t, will hold address of a hash value data
859 * @value_len: pointer to an int, will hold hash data length
860 *
861 * fit_image_hash_get_value() finds hash value property in a given hash node.
862 * If the property is found its data start address and size are returned to
863 * the caller.
864 *
865 * returns:
866 * 0, on success
867 * -1, on failure
868 */
869int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
870 int *value_len)
871{
872 int len;
873
874 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
875 if (*value == NULL) {
876 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
877 *value_len = 0;
878 return -1;
879 }
880
881 *value_len = len;
882 return 0;
883}
884
Simon Glass53fbb7e2013-05-07 06:11:53 +0000885/**
886 * fit_image_hash_get_ignore - get hash ignore flag
887 * @fit: pointer to the FIT format image header
888 * @noffset: hash node offset
889 * @ignore: pointer to an int, will hold hash ignore flag
890 *
891 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
892 * If the property is found and non-zero, the hash algorithm is not verified by
893 * u-boot automatically.
894 *
895 * returns:
896 * 0, on ignore not found
897 * value, on ignore found
898 */
Simon Glassab9efc62013-05-07 06:11:58 +0000899static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000900{
901 int len;
902 int *value;
903
904 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
905 if (value == NULL || len != sizeof(int))
906 *ignore = 0;
907 else
908 *ignore = *value;
909
910 return 0;
911}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000912
Simon Glass7a80de42016-02-24 09:14:42 -0700913ulong fit_get_end(const void *fit)
914{
915 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
916}
917
Simon Glass53fbb7e2013-05-07 06:11:53 +0000918/**
919 * fit_set_timestamp - set node timestamp property
920 * @fit: pointer to the FIT format image header
921 * @noffset: node offset
922 * @timestamp: timestamp value to be set
923 *
924 * fit_set_timestamp() attempts to set timestamp property in the requested
925 * node and returns operation status to the caller.
926 *
927 * returns:
928 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -0600929 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +0000930 */
931int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
932{
933 uint32_t t;
934 int ret;
935
936 t = cpu_to_uimage(timestamp);
937 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
938 sizeof(uint32_t));
939 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -0600940 debug("Can't set '%s' property for '%s' node (%s)\n",
941 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
942 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -0600943 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000944 }
945
946 return 0;
947}
948
949/**
950 * calculate_hash - calculate and return hash for provided input data
951 * @data: pointer to the input data
952 * @data_len: data length
953 * @algo: requested hash algorithm
954 * @value: pointer to the char, will hold hash value data (caller must
955 * allocate enough free space)
956 * value_len: length of the calculated hash
957 *
958 * calculate_hash() computes input data hash according to the requested
959 * algorithm.
960 * Resulting hash value is placed in caller provided 'value' buffer, length
961 * of the calculated hash is returned via value_len pointer argument.
962 *
963 * returns:
964 * 0, on success
965 * -1, when algo is unsupported
966 */
Simon Glass604f23d2013-05-07 06:11:54 +0000967int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000968 uint8_t *value, int *value_len)
969{
Simon Glass87ebee32013-05-08 08:05:59 +0000970 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000971 *((uint32_t *)value) = crc32_wd(0, data, data_len,
972 CHUNKSZ_CRC32);
973 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
974 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +0000975 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000976 sha1_csum_wd((unsigned char *)data, data_len,
977 (unsigned char *)value, CHUNKSZ_SHA1);
978 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +0100979 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
980 sha256_csum_wd((unsigned char *)data, data_len,
981 (unsigned char *)value, CHUNKSZ_SHA256);
982 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +0000983 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000984 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
985 *value_len = 16;
986 } else {
987 debug("Unsupported hash alogrithm\n");
988 return -1;
989 }
990 return 0;
991}
992
Simon Glassab9efc62013-05-07 06:11:58 +0000993static int fit_image_check_hash(const void *fit, int noffset, const void *data,
994 size_t size, char **err_msgp)
995{
996 uint8_t value[FIT_MAX_HASH_LEN];
997 int value_len;
998 char *algo;
999 uint8_t *fit_value;
1000 int fit_value_len;
1001 int ignore;
1002
1003 *err_msgp = NULL;
1004
1005 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001006 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001007 return -1;
1008 }
1009 printf("%s", algo);
1010
1011 if (IMAGE_ENABLE_IGNORE) {
1012 fit_image_hash_get_ignore(fit, noffset, &ignore);
1013 if (ignore) {
1014 printf("-skipped ");
1015 return 0;
1016 }
1017 }
1018
1019 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1020 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001021 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001022 return -1;
1023 }
1024
1025 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001026 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001027 return -1;
1028 }
1029
1030 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001031 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001032 return -1;
1033 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001034 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001035 return -1;
1036 }
1037
1038 return 0;
1039}
1040
Simon Glass53fbb7e2013-05-07 06:11:53 +00001041/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001042 * fit_image_verify - verify data integrity
Simon Glass53fbb7e2013-05-07 06:11:53 +00001043 * @fit: pointer to the FIT format image header
1044 * @image_noffset: component image node offset
1045 *
Simon Glassb8da8362013-05-07 06:11:57 +00001046 * fit_image_verify() goes over component image hash nodes,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001047 * re-calculates each data hash and compares with the value stored in hash
1048 * node.
1049 *
1050 * returns:
1051 * 1, if all hashes are valid
1052 * 0, otherwise (or on error)
1053 */
Simon Glassb8da8362013-05-07 06:11:57 +00001054int fit_image_verify(const void *fit, int image_noffset)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001055{
1056 const void *data;
1057 size_t size;
Simon Glass56518e72013-06-13 15:10:01 -07001058 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001059 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001060 int verify_all = 1;
1061 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001062
1063 /* Get image data and data length */
1064 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Simon Glasse754da22013-05-07 06:11:59 +00001065 err_msg = "Can't get image data/size";
Simon Glass56518e72013-06-13 15:10:01 -07001066 goto error;
1067 }
1068
1069 /* Verify all required signatures */
1070 if (IMAGE_ENABLE_VERIFY &&
1071 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1072 gd_fdt_blob(), &verify_all)) {
1073 err_msg = "Unable to verify required signature";
1074 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001075 }
1076
1077 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001078 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001079 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001080
Simon Glassab9efc62013-05-07 06:11:58 +00001081 /*
1082 * Check subnode name, must be equal to "hash".
1083 * Multiple hash nodes require unique unit node
1084 * names, e.g. hash@1, hash@2, etc.
1085 */
1086 if (!strncmp(name, FIT_HASH_NODENAME,
1087 strlen(FIT_HASH_NODENAME))) {
1088 if (fit_image_check_hash(fit, noffset, data, size,
1089 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001090 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001091 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001092 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1093 !strncmp(name, FIT_SIG_NODENAME,
1094 strlen(FIT_SIG_NODENAME))) {
1095 ret = fit_image_check_sig(fit, noffset, data,
1096 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001097
1098 /*
1099 * Show an indication on failure, but do not return
1100 * an error. Only keys marked 'required' can cause
1101 * an image validation failure. See the call to
1102 * fit_image_verify_required_sigs() above.
1103 */
1104 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001105 puts("- ");
1106 else
1107 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001108 }
1109 }
1110
1111 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001112 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001113 goto error;
1114 }
1115
1116 return 1;
1117
1118error:
Simon Glasse754da22013-05-07 06:11:59 +00001119 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001120 err_msg, fit_get_name(fit, noffset, NULL),
1121 fit_get_name(fit, image_noffset, NULL));
1122 return 0;
1123}
1124
1125/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001126 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001127 * @fit: pointer to the FIT format image header
1128 *
Simon Glassb8da8362013-05-07 06:11:57 +00001129 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001130 * for every images checks if all it's hashes are valid.
1131 *
1132 * returns:
1133 * 1, if all hashes of all images are valid
1134 * 0, otherwise (or on error)
1135 */
Simon Glassb8da8362013-05-07 06:11:57 +00001136int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001137{
1138 int images_noffset;
1139 int noffset;
1140 int ndepth;
1141 int count;
1142
1143 /* Find images parent node offset */
1144 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1145 if (images_noffset < 0) {
1146 printf("Can't find images parent node '%s' (%s)\n",
1147 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1148 return 0;
1149 }
1150
1151 /* Process all image subnodes, check hashes for each */
1152 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1153 (ulong)fit);
1154 for (ndepth = 0, count = 0,
1155 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1156 (noffset >= 0) && (ndepth > 0);
1157 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1158 if (ndepth == 1) {
1159 /*
1160 * Direct child node of the images parent node,
1161 * i.e. component image node.
1162 */
Simon Glass73223f02016-02-22 22:55:43 -07001163 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001164 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001165 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001166
Simon Glassb8da8362013-05-07 06:11:57 +00001167 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001168 return 0;
1169 printf("\n");
1170 }
1171 }
1172 return 1;
1173}
1174
1175/**
1176 * fit_image_check_os - check whether image node is of a given os type
1177 * @fit: pointer to the FIT format image header
1178 * @noffset: component image node offset
1179 * @os: requested image os
1180 *
1181 * fit_image_check_os() reads image os property and compares its numeric
1182 * id with the requested os. Comparison result is returned to the caller.
1183 *
1184 * returns:
1185 * 1 if image is of given os type
1186 * 0 otherwise (or on error)
1187 */
1188int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1189{
1190 uint8_t image_os;
1191
1192 if (fit_image_get_os(fit, noffset, &image_os))
1193 return 0;
1194 return (os == image_os);
1195}
1196
1197/**
1198 * fit_image_check_arch - check whether image node is of a given arch
1199 * @fit: pointer to the FIT format image header
1200 * @noffset: component image node offset
1201 * @arch: requested imagearch
1202 *
1203 * fit_image_check_arch() reads image arch property and compares its numeric
1204 * id with the requested arch. Comparison result is returned to the caller.
1205 *
1206 * returns:
1207 * 1 if image is of given arch
1208 * 0 otherwise (or on error)
1209 */
1210int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1211{
1212 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001213 int aarch32_support = 0;
1214
1215#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1216 aarch32_support = 1;
1217#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001218
1219 if (fit_image_get_arch(fit, noffset, &image_arch))
1220 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001221 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001222 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1223 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1224 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001225}
1226
1227/**
1228 * fit_image_check_type - check whether image node is of a given type
1229 * @fit: pointer to the FIT format image header
1230 * @noffset: component image node offset
1231 * @type: requested image type
1232 *
1233 * fit_image_check_type() reads image type property and compares its numeric
1234 * id with the requested type. Comparison result is returned to the caller.
1235 *
1236 * returns:
1237 * 1 if image is of given type
1238 * 0 otherwise (or on error)
1239 */
1240int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1241{
1242 uint8_t image_type;
1243
1244 if (fit_image_get_type(fit, noffset, &image_type))
1245 return 0;
1246 return (type == image_type);
1247}
1248
1249/**
1250 * fit_image_check_comp - check whether image node uses given compression
1251 * @fit: pointer to the FIT format image header
1252 * @noffset: component image node offset
1253 * @comp: requested image compression type
1254 *
1255 * fit_image_check_comp() reads image compression property and compares its
1256 * numeric id with the requested compression type. Comparison result is
1257 * returned to the caller.
1258 *
1259 * returns:
1260 * 1 if image uses requested compression
1261 * 0 otherwise (or on error)
1262 */
1263int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1264{
1265 uint8_t image_comp;
1266
1267 if (fit_image_get_comp(fit, noffset, &image_comp))
1268 return 0;
1269 return (comp == image_comp);
1270}
1271
1272/**
1273 * fit_check_format - sanity check FIT image format
1274 * @fit: pointer to the FIT format image header
1275 *
1276 * fit_check_format() runs a basic sanity FIT image verification.
1277 * Routine checks for mandatory properties, nodes, etc.
1278 *
1279 * returns:
1280 * 1, on success
1281 * 0, on failure
1282 */
1283int fit_check_format(const void *fit)
1284{
1285 /* mandatory / node 'description' property */
1286 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1287 debug("Wrong FIT format: no description\n");
1288 return 0;
1289 }
1290
1291 if (IMAGE_ENABLE_TIMESTAMP) {
1292 /* mandatory / node 'timestamp' property */
1293 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1294 debug("Wrong FIT format: no timestamp\n");
1295 return 0;
1296 }
1297 }
1298
1299 /* mandatory subimages parent '/images' node */
1300 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1301 debug("Wrong FIT format: no images parent node\n");
1302 return 0;
1303 }
1304
1305 return 1;
1306}
1307
1308
1309/**
1310 * fit_conf_find_compat
1311 * @fit: pointer to the FIT format image header
1312 * @fdt: pointer to the device tree to compare against
1313 *
1314 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1315 * most compatible with the passed in device tree.
1316 *
1317 * Example:
1318 *
1319 * / o image-tree
1320 * |-o images
1321 * | |-o fdt@1
1322 * | |-o fdt@2
1323 * |
1324 * |-o configurations
1325 * |-o config@1
1326 * | |-fdt = fdt@1
1327 * |
1328 * |-o config@2
1329 * |-fdt = fdt@2
1330 *
1331 * / o U-Boot fdt
1332 * |-compatible = "foo,bar", "bim,bam"
1333 *
1334 * / o kernel fdt1
1335 * |-compatible = "foo,bar",
1336 *
1337 * / o kernel fdt2
1338 * |-compatible = "bim,bam", "baz,biz"
1339 *
1340 * Configuration 1 would be picked because the first string in U-Boot's
1341 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1342 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1343 *
1344 * returns:
1345 * offset to the configuration to use if one was found
1346 * -1 otherwise
1347 */
1348int fit_conf_find_compat(const void *fit, const void *fdt)
1349{
1350 int ndepth = 0;
1351 int noffset, confs_noffset, images_noffset;
1352 const void *fdt_compat;
1353 int fdt_compat_len;
1354 int best_match_offset = 0;
1355 int best_match_pos = 0;
1356
1357 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1358 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1359 if (confs_noffset < 0 || images_noffset < 0) {
1360 debug("Can't find configurations or images nodes.\n");
1361 return -1;
1362 }
1363
1364 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1365 if (!fdt_compat) {
1366 debug("Fdt for comparison has no \"compatible\" property.\n");
1367 return -1;
1368 }
1369
1370 /*
1371 * Loop over the configurations in the FIT image.
1372 */
1373 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1374 (noffset >= 0) && (ndepth > 0);
1375 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1376 const void *kfdt;
1377 const char *kfdt_name;
1378 int kfdt_noffset;
1379 const char *cur_fdt_compat;
1380 int len;
1381 size_t size;
1382 int i;
1383
1384 if (ndepth > 1)
1385 continue;
1386
1387 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1388 if (!kfdt_name) {
1389 debug("No fdt property found.\n");
1390 continue;
1391 }
1392 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1393 kfdt_name);
1394 if (kfdt_noffset < 0) {
1395 debug("No image node named \"%s\" found.\n",
1396 kfdt_name);
1397 continue;
1398 }
1399 /*
1400 * Get a pointer to this configuration's fdt.
1401 */
1402 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1403 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1404 continue;
1405 }
1406
1407 len = fdt_compat_len;
1408 cur_fdt_compat = fdt_compat;
1409 /*
1410 * Look for a match for each U-Boot compatibility string in
1411 * turn in this configuration's fdt.
1412 */
1413 for (i = 0; len > 0 &&
1414 (!best_match_offset || best_match_pos > i); i++) {
1415 int cur_len = strlen(cur_fdt_compat) + 1;
1416
1417 if (!fdt_node_check_compatible(kfdt, 0,
1418 cur_fdt_compat)) {
1419 best_match_offset = noffset;
1420 best_match_pos = i;
1421 break;
1422 }
1423 len -= cur_len;
1424 cur_fdt_compat += cur_len;
1425 }
1426 }
1427 if (!best_match_offset) {
1428 debug("No match found.\n");
1429 return -1;
1430 }
1431
1432 return best_match_offset;
1433}
1434
1435/**
1436 * fit_conf_get_node - get node offset for configuration of a given unit name
1437 * @fit: pointer to the FIT format image header
1438 * @conf_uname: configuration node unit name
1439 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001440 * fit_conf_get_node() finds a configuration (within the '/configurations'
1441 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001442 * offset is returned to the caller.
1443 *
1444 * When NULL is provided in second argument fit_conf_get_node() will search
1445 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001446 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001447 * node.
1448 *
1449 * returns:
1450 * configuration node offset when found (>=0)
1451 * negative number on failure (FDT_ERR_* code)
1452 */
1453int fit_conf_get_node(const void *fit, const char *conf_uname)
1454{
1455 int noffset, confs_noffset;
1456 int len;
1457
1458 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1459 if (confs_noffset < 0) {
1460 debug("Can't find configurations parent node '%s' (%s)\n",
1461 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1462 return confs_noffset;
1463 }
1464
1465 if (conf_uname == NULL) {
1466 /* get configuration unit name from the default property */
1467 debug("No configuration specified, trying default...\n");
1468 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1469 FIT_DEFAULT_PROP, &len);
1470 if (conf_uname == NULL) {
1471 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1472 len);
1473 return len;
1474 }
1475 debug("Found default configuration: '%s'\n", conf_uname);
1476 }
1477
1478 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1479 if (noffset < 0) {
1480 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1481 conf_uname, fdt_strerror(noffset));
1482 }
1483
1484 return noffset;
1485}
1486
Simon Glass003efd72013-05-07 06:12:00 +00001487int fit_conf_get_prop_node(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001488 const char *prop_name)
1489{
1490 char *uname;
1491 int len;
1492
1493 /* get kernel image unit name from configuration kernel property */
1494 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1495 if (uname == NULL)
1496 return len;
1497
1498 return fit_image_get_node(fit, uname);
1499}
1500
1501/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001502 * fit_conf_print - prints out the FIT configuration details
1503 * @fit: pointer to the FIT format image header
1504 * @noffset: offset of the configuration node
1505 * @p: pointer to prefix string
1506 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001507 * fit_conf_print() lists all mandatory properties for the processed
Simon Glass53fbb7e2013-05-07 06:11:53 +00001508 * configuration node.
1509 *
1510 * returns:
1511 * no returned results
1512 */
1513void fit_conf_print(const void *fit, int noffset, const char *p)
1514{
1515 char *desc;
Simon Glassb02e4042016-10-02 17:59:28 -06001516 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001517 int ret;
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001518 int loadables_index;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001519
1520 /* Mandatory properties */
1521 ret = fit_get_desc(fit, noffset, &desc);
1522 printf("%s Description: ", p);
1523 if (ret)
1524 printf("unavailable\n");
1525 else
1526 printf("%s\n", desc);
1527
Simon Glassb02e4042016-10-02 17:59:28 -06001528 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001529 printf("%s Kernel: ", p);
1530 if (uname == NULL)
1531 printf("unavailable\n");
1532 else
1533 printf("%s\n", uname);
1534
1535 /* Optional properties */
Simon Glassb02e4042016-10-02 17:59:28 -06001536 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001537 if (uname)
1538 printf("%s Init Ramdisk: %s\n", p, uname);
1539
Simon Glassb02e4042016-10-02 17:59:28 -06001540 uname = fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001541 if (uname)
1542 printf("%s FDT: %s\n", p, uname);
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001543
Simon Glassb02e4042016-10-02 17:59:28 -06001544 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
Michal Simeked0cea72016-05-17 13:58:44 +02001545 if (uname)
1546 printf("%s FPGA: %s\n", p, uname);
1547
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001548 /* Print out all of the specified loadables */
1549 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001550 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1551 loadables_index, NULL), uname;
1552 loadables_index++) {
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001553 if (loadables_index == 0) {
1554 printf("%s Loadables: ", p);
1555 } else {
1556 printf("%s ", p);
1557 }
1558 printf("%s\n", uname);
1559 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001560}
1561
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001562static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001563{
1564 fit_image_print(fit, rd_noffset, " ");
1565
1566 if (verify) {
1567 puts(" Verifying Hash Integrity ... ");
1568 if (!fit_image_verify(fit, rd_noffset)) {
1569 puts("Bad Data Hash\n");
1570 return -EACCES;
1571 }
1572 puts("OK\n");
1573 }
1574
1575 return 0;
1576}
1577
Simon Glass782cfbb2013-05-16 13:53:21 +00001578int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1579 ulong addr)
1580{
1581 int cfg_noffset;
1582 void *fit_hdr;
1583 int noffset;
1584
1585 debug("* %s: using config '%s' from image at 0x%08lx\n",
1586 prop_name, images->fit_uname_cfg, addr);
1587
1588 /* Check whether configuration has this property defined */
1589 fit_hdr = map_sysmem(addr, 0);
1590 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1591 if (cfg_noffset < 0) {
1592 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001593 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001594 }
1595
1596 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1597 if (noffset < 0) {
1598 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001599 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001600 }
1601
1602 return noffset;
1603}
1604
Simon Glass126cc862014-06-12 07:24:47 -06001605/**
1606 * fit_get_image_type_property() - get property name for IH_TYPE_...
1607 *
1608 * @return the properly name where we expect to find the image in the
1609 * config node
1610 */
1611static const char *fit_get_image_type_property(int type)
1612{
1613 /*
1614 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001615 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001616 * anyway. So let's just keep it here.
1617 */
1618 switch (type) {
1619 case IH_TYPE_FLATDT:
1620 return FIT_FDT_PROP;
1621 case IH_TYPE_KERNEL:
1622 return FIT_KERNEL_PROP;
1623 case IH_TYPE_RAMDISK:
1624 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001625 case IH_TYPE_X86_SETUP:
1626 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001627 case IH_TYPE_LOADABLE:
1628 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001629 case IH_TYPE_FPGA:
1630 return FIT_FPGA_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001631 }
1632
1633 return "unknown";
1634}
1635
1636int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001637 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001638 int arch, int image_type, int bootstage_id,
1639 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1640{
1641 int cfg_noffset, noffset;
1642 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001643 const char *fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001644 const void *fit;
1645 const void *buf;
1646 size_t size;
1647 int type_ok, os_ok;
1648 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001649 uint8_t os;
Alison Wangec6617c2016-11-10 10:49:03 +08001650#ifndef USE_HOSTCC
1651 uint8_t os_arch;
1652#endif
Simon Glass126cc862014-06-12 07:24:47 -06001653 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001654 int ret;
1655
1656 fit = map_sysmem(addr, 0);
1657 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001658 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001659 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001660 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1661
1662 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1663 if (!fit_check_format(fit)) {
1664 printf("Bad FIT %s image format!\n", prop_name);
1665 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1666 return -ENOEXEC;
1667 }
1668 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1669 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001670 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001671 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1672 noffset = fit_image_get_node(fit, fit_uname);
1673 } else {
1674 /*
1675 * no image node unit name, try to get config
1676 * node first. If config unit node name is NULL
1677 * fit_conf_get_node() will try to find default config node
1678 */
1679 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1680 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1681 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1682 } else {
1683 cfg_noffset = fit_conf_get_node(fit,
1684 fit_uname_config);
1685 }
1686 if (cfg_noffset < 0) {
1687 puts("Could not find configuration node\n");
1688 bootstage_error(bootstage_id +
1689 BOOTSTAGE_SUB_NO_UNIT_NAME);
1690 return -ENOENT;
1691 }
1692 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1693 printf(" Using '%s' configuration\n", fit_uname_config);
1694 if (image_type == IH_TYPE_KERNEL) {
1695 /* Remember (and possibly verify) this config */
1696 images->fit_uname_cfg = fit_uname_config;
1697 if (IMAGE_ENABLE_VERIFY && images->verify) {
1698 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001699 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001700 puts("Bad Data Hash\n");
1701 bootstage_error(bootstage_id +
1702 BOOTSTAGE_SUB_HASH);
1703 return -EACCES;
1704 }
1705 puts("OK\n");
1706 }
1707 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1708 }
1709
1710 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1711 prop_name);
1712 fit_uname = fit_get_name(fit, noffset, NULL);
1713 }
1714 if (noffset < 0) {
1715 puts("Could not find subimage node\n");
1716 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1717 return -ENOENT;
1718 }
1719
1720 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1721
1722 ret = fit_image_select(fit, noffset, images->verify);
1723 if (ret) {
1724 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1725 return ret;
1726 }
1727
1728 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001729#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001730 if (!fit_image_check_target_arch(fit, noffset)) {
1731 puts("Unsupported Architecture\n");
1732 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1733 return -ENOEXEC;
1734 }
Simon Glassce1400f2014-06-12 07:24:53 -06001735#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001736
1737#ifndef USE_HOSTCC
1738 fit_image_get_arch(fit, noffset, &os_arch);
1739 images->os.arch = os_arch;
1740#endif
1741
Simon Glass782cfbb2013-05-16 13:53:21 +00001742 if (image_type == IH_TYPE_FLATDT &&
1743 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1744 puts("FDT image is compressed");
1745 return -EPROTONOSUPPORT;
1746 }
1747
1748 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1749 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001750 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1751 (image_type == IH_TYPE_KERNEL &&
1752 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001753
Andreas Bießmann950fe262016-08-14 20:31:24 +02001754 os_ok = image_type == IH_TYPE_FLATDT ||
1755 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001756 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001757 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001758 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001759
1760 /*
1761 * If either of the checks fail, we should report an error, but
1762 * if the image type is coming from the "loadables" field, we
1763 * don't care what it is
1764 */
1765 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001766 fit_image_get_os(fit, noffset, &os);
1767 printf("No %s %s %s Image\n",
1768 genimg_get_os_name(os),
1769 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001770 genimg_get_type_name(image_type));
1771 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1772 return -EIO;
1773 }
1774
1775 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1776
1777 /* get image data address and length */
1778 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1779 printf("Could not find %s subimage data!\n", prop_name);
1780 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001781 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001782 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06001783
1784#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1785 /* perform any post-processing on the image data */
1786 board_fit_image_post_process((void **)&buf, &size);
1787#endif
1788
Simon Glass782cfbb2013-05-16 13:53:21 +00001789 len = (ulong)size;
1790
1791 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001792 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001793 puts("Subimage data is not a FDT");
1794 return -ENOEXEC;
1795 }
1796
1797 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1798
1799 /*
1800 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001801 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001802 * warning: initialization discards qualifiers from pointer target type
1803 */
1804 {
1805 void *vbuf = (void *)buf;
1806
1807 data = map_to_sysmem(vbuf);
1808 }
1809
1810 if (load_op == FIT_LOAD_IGNORED) {
1811 /* Don't load */
1812 } else if (fit_image_get_load(fit, noffset, &load)) {
1813 if (load_op == FIT_LOAD_REQUIRED) {
1814 printf("Can't get %s subimage load address!\n",
1815 prop_name);
1816 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1817 return -EBADF;
1818 }
Simon Glassfe20a812014-08-22 14:26:43 -06001819 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001820 ulong image_start, image_end;
1821 ulong load_end;
1822 void *dst;
1823
1824 /*
1825 * move image data to the load address,
1826 * make sure we don't overwrite initial image
1827 */
1828 image_start = addr;
1829 image_end = addr + fit_get_size(fit);
1830
1831 load_end = load + len;
1832 if (image_type != IH_TYPE_KERNEL &&
1833 load < image_end && load_end > image_start) {
1834 printf("Error: %s overwritten\n", prop_name);
1835 return -EXDEV;
1836 }
1837
1838 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1839 prop_name, data, load);
1840
1841 dst = map_sysmem(load, len);
1842 memmove(dst, buf, len);
1843 data = load;
1844 }
1845 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1846
1847 *datap = data;
1848 *lenp = len;
1849 if (fit_unamep)
1850 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001851 if (fit_uname_configp)
1852 *fit_uname_configp = (char *)fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001853
1854 return noffset;
1855}
Simon Glass90268b82014-10-19 21:11:24 -06001856
1857int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1858 ulong *setup_start, ulong *setup_len)
1859{
1860 int noffset;
1861 ulong addr;
1862 ulong len;
1863 int ret;
1864
1865 addr = map_to_sysmem(images->fit_hdr_os);
1866 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1867 if (noffset < 0)
1868 return noffset;
1869
1870 ret = fit_image_load(images, addr, NULL, NULL, arch,
1871 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1872 FIT_LOAD_REQUIRED, setup_start, &len);
1873
1874 return ret;
1875}