blob: ac901e131ca1edca2b9d7ebc0d18bf1c75060859 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass53fbb7e2013-05-07 06:11:53 +00002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass53fbb7e2013-05-07 06:11:53 +00009 */
10
11#ifdef USE_HOSTCC
12#include "mkimage.h"
Simon Glass53fbb7e2013-05-07 06:11:53 +000013#include <time.h>
14#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050015#include <linux/compiler.h>
York Sun020198b2016-11-23 09:25:09 -080016#include <linux/kconfig.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>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030021#include <malloc.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000022DECLARE_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
Marek Vasutb527b9c2018-05-13 00:22:52 +0200145#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000146/**
Tom Rini16c4b162018-05-08 14:34:05 -0400147 * fit_image_print_data() - prints out the hash node details
148 * @fit: pointer to the FIT format image header
149 * @noffset: offset of the hash node
150 * @p: pointer to prefix string
151 * @type: Type of information to print ("hash" or "sign")
152 *
153 * fit_image_print_data() lists properties for the processed hash node
154 *
155 * This function avoid using puts() since it prints a newline on the host
156 * but does not in U-Boot.
157 *
158 * returns:
159 * no returned results
160 */
161static void fit_image_print_data(const void *fit, int noffset, const char *p,
162 const char *type)
163{
164 const char *keyname;
165 uint8_t *value;
166 int value_len;
167 char *algo;
Philippe Reynes20031562018-11-14 13:51:00 +0100168 const char *padding;
Tom Rini16c4b162018-05-08 14:34:05 -0400169 int required;
170 int ret, i;
171
172 debug("%s %s node: '%s'\n", p, type,
173 fit_get_name(fit, noffset, NULL));
174 printf("%s %s algo: ", p, type);
175 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
176 printf("invalid/unsupported\n");
177 return;
178 }
179 printf("%s", algo);
180 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
181 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
182 if (keyname)
183 printf(":%s", keyname);
184 if (required)
185 printf(" (required)");
186 printf("\n");
187
Philippe Reynes20031562018-11-14 13:51:00 +0100188 padding = fdt_getprop(fit, noffset, "padding", NULL);
189 if (padding)
190 printf("%s %s padding: %s\n", p, type, padding);
191
Tom Rini16c4b162018-05-08 14:34:05 -0400192 ret = fit_image_hash_get_value(fit, noffset, &value,
193 &value_len);
194 printf("%s %s value: ", p, type);
195 if (ret) {
196 printf("unavailable\n");
197 } else {
198 for (i = 0; i < value_len; i++)
199 printf("%02x", value[i]);
200 printf("\n");
201 }
202
203 debug("%s %s len: %d\n", p, type, value_len);
204
205 /* Signatures have a time stamp */
206 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
207 time_t timestamp;
208
209 printf("%s Timestamp: ", p);
210 if (fit_get_timestamp(fit, noffset, &timestamp))
211 printf("unavailable\n");
212 else
213 genimg_print_time(timestamp);
214 }
215}
216
217/**
218 * fit_image_print_verification_data() - prints out the hash/signature details
219 * @fit: pointer to the FIT format image header
220 * @noffset: offset of the hash or signature node
221 * @p: pointer to prefix string
222 *
223 * This lists properties for the processed hash node
224 *
225 * returns:
226 * no returned results
227 */
228static void fit_image_print_verification_data(const void *fit, int noffset,
229 const char *p)
230{
231 const char *name;
232
233 /*
234 * Check subnode name, must be equal to "hash" or "signature".
235 * Multiple hash/signature nodes require unique unit node
236 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
237 */
238 name = fit_get_name(fit, noffset, NULL);
239 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
240 fit_image_print_data(fit, noffset, p, "Hash");
241 } else if (!strncmp(name, FIT_SIG_NODENAME,
242 strlen(FIT_SIG_NODENAME))) {
243 fit_image_print_data(fit, noffset, p, "Sign");
244 }
245}
246
247/**
248 * fit_conf_print - prints out the FIT configuration details
249 * @fit: pointer to the FIT format image header
250 * @noffset: offset of the configuration node
251 * @p: pointer to prefix string
252 *
253 * fit_conf_print() lists all mandatory properties for the processed
254 * configuration node.
255 *
256 * returns:
257 * no returned results
258 */
259static void fit_conf_print(const void *fit, int noffset, const char *p)
260{
261 char *desc;
262 const char *uname;
263 int ret;
264 int fdt_index, loadables_index;
265 int ndepth;
266
267 /* Mandatory properties */
268 ret = fit_get_desc(fit, noffset, &desc);
269 printf("%s Description: ", p);
270 if (ret)
271 printf("unavailable\n");
272 else
273 printf("%s\n", desc);
274
275 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
276 printf("%s Kernel: ", p);
277 if (!uname)
278 printf("unavailable\n");
279 else
280 printf("%s\n", uname);
281
282 /* Optional properties */
283 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
284 if (uname)
285 printf("%s Init Ramdisk: %s\n", p, uname);
286
287 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
288 if (uname)
289 printf("%s Firmware: %s\n", p, uname);
290
291 for (fdt_index = 0;
292 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
293 fdt_index, NULL), uname;
294 fdt_index++) {
295 if (fdt_index == 0)
296 printf("%s FDT: ", p);
297 else
298 printf("%s ", p);
299 printf("%s\n", uname);
300 }
301
302 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
303 if (uname)
304 printf("%s FPGA: %s\n", p, uname);
305
306 /* Print out all of the specified loadables */
307 for (loadables_index = 0;
308 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
309 loadables_index, NULL), uname;
310 loadables_index++) {
311 if (loadables_index == 0) {
312 printf("%s Loadables: ", p);
313 } else {
314 printf("%s ", p);
315 }
316 printf("%s\n", uname);
317 }
318
319 /* Process all hash subnodes of the component configuration node */
320 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
321 (noffset >= 0) && (ndepth > 0);
322 noffset = fdt_next_node(fit, noffset, &ndepth)) {
323 if (ndepth == 1) {
324 /* Direct child node of the component configuration node */
325 fit_image_print_verification_data(fit, noffset, p);
326 }
327 }
328}
329
330/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000331 * fit_print_contents - prints out the contents of the FIT format image
332 * @fit: pointer to the FIT format image header
333 * @p: pointer to prefix string
334 *
335 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500336 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000337 * the details of each component image.
338 *
339 * returns:
340 * no returned results
341 */
342void fit_print_contents(const void *fit)
343{
344 char *desc;
345 char *uname;
346 int images_noffset;
347 int confs_noffset;
348 int noffset;
349 int ndepth;
350 int count = 0;
351 int ret;
352 const char *p;
353 time_t timestamp;
354
Simon Glass1fe7d932013-05-08 08:05:58 +0000355 /* Indent string is defined in header image.h */
356 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000357
358 /* Root node properties */
359 ret = fit_get_desc(fit, 0, &desc);
360 printf("%sFIT description: ", p);
361 if (ret)
362 printf("unavailable\n");
363 else
364 printf("%s\n", desc);
365
366 if (IMAGE_ENABLE_TIMESTAMP) {
367 ret = fit_get_timestamp(fit, 0, &timestamp);
368 printf("%sCreated: ", p);
369 if (ret)
370 printf("unavailable\n");
371 else
372 genimg_print_time(timestamp);
373 }
374
375 /* Find images parent node offset */
376 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
377 if (images_noffset < 0) {
378 printf("Can't find images parent node '%s' (%s)\n",
379 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
380 return;
381 }
382
383 /* Process its subnodes, print out component images details */
384 for (ndepth = 0, count = 0,
385 noffset = fdt_next_node(fit, images_noffset, &ndepth);
386 (noffset >= 0) && (ndepth > 0);
387 noffset = fdt_next_node(fit, noffset, &ndepth)) {
388 if (ndepth == 1) {
389 /*
390 * Direct child node of the images parent node,
391 * i.e. component image node.
392 */
393 printf("%s Image %u (%s)\n", p, count++,
394 fit_get_name(fit, noffset, NULL));
395
396 fit_image_print(fit, noffset, p);
397 }
398 }
399
400 /* Find configurations parent node offset */
401 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
402 if (confs_noffset < 0) {
403 debug("Can't get configurations parent node '%s' (%s)\n",
404 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
405 return;
406 }
407
408 /* get default configuration unit name from default property */
409 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
410 if (uname)
411 printf("%s Default Configuration: '%s'\n", p, uname);
412
413 /* Process its subnodes, print out configurations details */
414 for (ndepth = 0, count = 0,
415 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
416 (noffset >= 0) && (ndepth > 0);
417 noffset = fdt_next_node(fit, noffset, &ndepth)) {
418 if (ndepth == 1) {
419 /*
420 * Direct child node of the configurations parent node,
421 * i.e. configuration node.
422 */
423 printf("%s Configuration %u (%s)\n", p, count++,
424 fit_get_name(fit, noffset, NULL));
425
426 fit_conf_print(fit, noffset, p);
427 }
428 }
429}
430
431/**
432 * fit_image_print - prints out the FIT component image details
433 * @fit: pointer to the FIT format image header
434 * @image_noffset: offset of the component image node
435 * @p: pointer to prefix string
436 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500437 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000438 * image. If present, hash nodes are printed out as well. Load
439 * address for images of type firmware is also printed out. Since the load
440 * address is not mandatory for firmware images, it will be output as
441 * "unavailable" when not present.
442 *
443 * returns:
444 * no returned results
445 */
446void fit_image_print(const void *fit, int image_noffset, const char *p)
447{
448 char *desc;
449 uint8_t type, arch, os, comp;
450 size_t size;
451 ulong load, entry;
452 const void *data;
453 int noffset;
454 int ndepth;
455 int ret;
456
457 /* Mandatory properties */
458 ret = fit_get_desc(fit, image_noffset, &desc);
459 printf("%s Description: ", p);
460 if (ret)
461 printf("unavailable\n");
462 else
463 printf("%s\n", desc);
464
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700465 if (IMAGE_ENABLE_TIMESTAMP) {
466 time_t timestamp;
467
468 ret = fit_get_timestamp(fit, 0, &timestamp);
469 printf("%s Created: ", p);
470 if (ret)
471 printf("unavailable\n");
472 else
473 genimg_print_time(timestamp);
474 }
475
Simon Glass53fbb7e2013-05-07 06:11:53 +0000476 fit_image_get_type(fit, image_noffset, &type);
477 printf("%s Type: %s\n", p, genimg_get_type_name(type));
478
479 fit_image_get_comp(fit, image_noffset, &comp);
480 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
481
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800482 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000483
484#ifndef USE_HOSTCC
485 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000486 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000487 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000488 } else {
489 void *vdata = (void *)data;
490
491 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
492 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000493#endif
494
495 printf("%s Data Size: ", p);
496 if (ret)
497 printf("unavailable\n");
498 else
499 genimg_print_size(size);
500
501 /* Remaining, type dependent properties */
502 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
503 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
504 (type == IH_TYPE_FLATDT)) {
505 fit_image_get_arch(fit, image_noffset, &arch);
506 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
507 }
508
Michal Simek6faf4622018-03-26 16:31:27 +0200509 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
510 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000511 fit_image_get_os(fit, image_noffset, &os);
512 printf("%s OS: %s\n", p, genimg_get_os_name(os));
513 }
514
515 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200516 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
517 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000518 ret = fit_image_get_load(fit, image_noffset, &load);
519 printf("%s Load Address: ", p);
520 if (ret)
521 printf("unavailable\n");
522 else
523 printf("0x%08lx\n", load);
524 }
525
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300526 /* optional load address for FDT */
527 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
528 printf("%s Load Address: 0x%08lx\n", p, load);
529
Simon Glass53fbb7e2013-05-07 06:11:53 +0000530 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
531 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800532 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000533 printf("%s Entry Point: ", p);
534 if (ret)
535 printf("unavailable\n");
536 else
537 printf("0x%08lx\n", entry);
538 }
539
540 /* Process all hash subnodes of the component image node */
541 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
542 (noffset >= 0) && (ndepth > 0);
543 noffset = fdt_next_node(fit, noffset, &ndepth)) {
544 if (ndepth == 1) {
545 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000546 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000547 }
548 }
549}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200550#else
551void fit_print_contents(const void *fit) { }
552void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Marek Vasutb527b9c2018-05-13 00:22:52 +0200553#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000554
555/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000556 * fit_get_desc - get node description property
557 * @fit: pointer to the FIT format image header
558 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500559 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000560 *
561 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500562 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000563 *
564 * returns:
565 * 0, on success
566 * -1, on failure
567 */
568int fit_get_desc(const void *fit, int noffset, char **desc)
569{
570 int len;
571
572 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
573 if (*desc == NULL) {
574 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
575 return -1;
576 }
577
578 return 0;
579}
580
581/**
582 * fit_get_timestamp - get node timestamp property
583 * @fit: pointer to the FIT format image header
584 * @noffset: node offset
585 * @timestamp: pointer to the time_t, will hold read timestamp
586 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500587 * fit_get_timestamp() reads timestamp property from given node, if timestamp
588 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000589 * argument.
590 *
591 * returns:
592 * 0, on success
593 * -1, on property read failure
594 * -2, on wrong timestamp size
595 */
596int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
597{
598 int len;
599 const void *data;
600
601 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
602 if (data == NULL) {
603 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
604 return -1;
605 }
606 if (len != sizeof(uint32_t)) {
607 debug("FIT timestamp with incorrect size of (%u)\n", len);
608 return -2;
609 }
610
611 *timestamp = uimage_to_cpu(*((uint32_t *)data));
612 return 0;
613}
614
615/**
616 * fit_image_get_node - get node offset for component image of a given unit name
617 * @fit: pointer to the FIT format image header
618 * @image_uname: component image node unit name
619 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500620 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000621 * node) of a provided unit name. If image is found its node offset is
622 * returned to the caller.
623 *
624 * returns:
625 * image node offset when found (>=0)
626 * negative number on failure (FDT_ERR_* code)
627 */
628int fit_image_get_node(const void *fit, const char *image_uname)
629{
630 int noffset, images_noffset;
631
632 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
633 if (images_noffset < 0) {
634 debug("Can't find images parent node '%s' (%s)\n",
635 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
636 return images_noffset;
637 }
638
639 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
640 if (noffset < 0) {
641 debug("Can't get node offset for image unit name: '%s' (%s)\n",
642 image_uname, fdt_strerror(noffset));
643 }
644
645 return noffset;
646}
647
648/**
649 * fit_image_get_os - get os id for a given component image node
650 * @fit: pointer to the FIT format image header
651 * @noffset: component image node offset
652 * @os: pointer to the uint8_t, will hold os numeric id
653 *
654 * fit_image_get_os() finds os 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_os(const void *fit, int noffset, uint8_t *os)
663{
664 int len;
665 const void *data;
666
667 /* Get OS name from property data */
668 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
669 if (data == NULL) {
670 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
671 *os = -1;
672 return -1;
673 }
674
675 /* Translate OS name to id */
676 *os = genimg_get_os_id(data);
677 return 0;
678}
679
680/**
681 * fit_image_get_arch - get arch id for a given component image node
682 * @fit: pointer to the FIT format image header
683 * @noffset: component image node offset
684 * @arch: pointer to the uint8_t, will hold arch numeric id
685 *
686 * fit_image_get_arch() finds arch property in a given component image node.
687 * If the property is found, its (string) value is translated to the numeric
688 * id which is returned to the caller.
689 *
690 * returns:
691 * 0, on success
692 * -1, on failure
693 */
694int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
695{
696 int len;
697 const void *data;
698
699 /* Get architecture name from property data */
700 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
701 if (data == NULL) {
702 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
703 *arch = -1;
704 return -1;
705 }
706
707 /* Translate architecture name to id */
708 *arch = genimg_get_arch_id(data);
709 return 0;
710}
711
712/**
713 * fit_image_get_type - get type id for a given component image node
714 * @fit: pointer to the FIT format image header
715 * @noffset: component image node offset
716 * @type: pointer to the uint8_t, will hold type numeric id
717 *
718 * fit_image_get_type() finds type property in a given component image node.
719 * If the property is found, its (string) value is translated to the numeric
720 * id which is returned to the caller.
721 *
722 * returns:
723 * 0, on success
724 * -1, on failure
725 */
726int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
727{
728 int len;
729 const void *data;
730
731 /* Get image type name from property data */
732 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
733 if (data == NULL) {
734 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
735 *type = -1;
736 return -1;
737 }
738
739 /* Translate image type name to id */
740 *type = genimg_get_type_id(data);
741 return 0;
742}
743
744/**
745 * fit_image_get_comp - get comp id for a given component image node
746 * @fit: pointer to the FIT format image header
747 * @noffset: component image node offset
748 * @comp: pointer to the uint8_t, will hold comp numeric id
749 *
750 * fit_image_get_comp() finds comp property in a given component image node.
751 * If the property is found, its (string) value is translated to the numeric
752 * id which is returned to the caller.
753 *
754 * returns:
755 * 0, on success
756 * -1, on failure
757 */
758int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
759{
760 int len;
761 const void *data;
762
763 /* Get compression name from property data */
764 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
765 if (data == NULL) {
766 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
767 *comp = -1;
768 return -1;
769 }
770
771 /* Translate compression name to id */
772 *comp = genimg_get_comp_id(data);
773 return 0;
774}
775
York Sun60047652016-02-29 15:48:40 -0800776static int fit_image_get_address(const void *fit, int noffset, char *name,
777 ulong *load)
778{
York Sunc1913cb2016-02-29 15:48:41 -0800779 int len, cell_len;
780 const fdt32_t *cell;
781 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800782
York Sunc1913cb2016-02-29 15:48:41 -0800783 cell = fdt_getprop(fit, noffset, name, &len);
784 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800785 fit_get_debug(fit, noffset, name, len);
786 return -1;
787 }
788
York Sunc1913cb2016-02-29 15:48:41 -0800789 if (len > sizeof(ulong)) {
790 printf("Unsupported %s address size\n", name);
791 return -1;
792 }
793
794 cell_len = len >> 2;
795 /* Use load64 to avoid compiling warning for 32-bit target */
796 while (cell_len--) {
797 load64 = (load64 << 32) | uimage_to_cpu(*cell);
798 cell++;
799 }
800 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800801
802 return 0;
803}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000804/**
805 * fit_image_get_load() - get load addr property for given component image node
806 * @fit: pointer to the FIT format image header
807 * @noffset: component image node offset
808 * @load: pointer to the uint32_t, will hold load address
809 *
810 * fit_image_get_load() finds load address property in a given component
811 * image node. If the property is found, its value is returned to the caller.
812 *
813 * returns:
814 * 0, on success
815 * -1, on failure
816 */
817int fit_image_get_load(const void *fit, int noffset, ulong *load)
818{
York Sun60047652016-02-29 15:48:40 -0800819 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000820}
821
822/**
823 * fit_image_get_entry() - get entry point address property
824 * @fit: pointer to the FIT format image header
825 * @noffset: component image node offset
826 * @entry: pointer to the uint32_t, will hold entry point address
827 *
828 * This gets the entry point address property for a given component image
829 * node.
830 *
831 * fit_image_get_entry() finds entry point address property in a given
832 * component image node. If the property is found, its value is returned
833 * to the caller.
834 *
835 * returns:
836 * 0, on success
837 * -1, on failure
838 */
839int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
840{
York Sun60047652016-02-29 15:48:40 -0800841 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000842}
843
844/**
845 * fit_image_get_data - get data property and its size for a given component image node
846 * @fit: pointer to the FIT format image header
847 * @noffset: component image node offset
848 * @data: double pointer to void, will hold data property's data address
849 * @size: pointer to size_t, will hold data property's data size
850 *
851 * fit_image_get_data() finds data property in a given component image node.
852 * If the property is found its data start address and size are returned to
853 * the caller.
854 *
855 * returns:
856 * 0, on success
857 * -1, on failure
858 */
859int fit_image_get_data(const void *fit, int noffset,
860 const void **data, size_t *size)
861{
862 int len;
863
864 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
865 if (*data == NULL) {
866 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
867 *size = 0;
868 return -1;
869 }
870
871 *size = len;
872 return 0;
873}
874
875/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200876 * Get 'data-offset' property from a given image node.
877 *
878 * @fit: pointer to the FIT image header
879 * @noffset: component image node offset
880 * @data_offset: holds the data-offset property
881 *
882 * returns:
883 * 0, on success
884 * -ENOENT if the property could not be found
885 */
886int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
887{
888 const fdt32_t *val;
889
890 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
891 if (!val)
892 return -ENOENT;
893
894 *data_offset = fdt32_to_cpu(*val);
895
896 return 0;
897}
898
899/**
Peng Fana1be94b2017-12-05 13:20:59 +0800900 * Get 'data-position' property from a given image node.
901 *
902 * @fit: pointer to the FIT image header
903 * @noffset: component image node offset
904 * @data_position: holds the data-position property
905 *
906 * returns:
907 * 0, on success
908 * -ENOENT if the property could not be found
909 */
910int fit_image_get_data_position(const void *fit, int noffset,
911 int *data_position)
912{
913 const fdt32_t *val;
914
915 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
916 if (!val)
917 return -ENOENT;
918
919 *data_position = fdt32_to_cpu(*val);
920
921 return 0;
922}
923
924/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200925 * Get 'data-size' property from a given image node.
926 *
927 * @fit: pointer to the FIT image header
928 * @noffset: component image node offset
929 * @data_size: holds the data-size property
930 *
931 * returns:
932 * 0, on success
933 * -ENOENT if the property could not be found
934 */
935int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
936{
937 const fdt32_t *val;
938
939 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
940 if (!val)
941 return -ENOENT;
942
943 *data_size = fdt32_to_cpu(*val);
944
945 return 0;
946}
947
948/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800949 * fit_image_get_data_and_size - get data and its size including
950 * both embedded and external data
951 * @fit: pointer to the FIT format image header
952 * @noffset: component image node offset
953 * @data: double pointer to void, will hold data property's data address
954 * @size: pointer to size_t, will hold data property's data size
955 *
956 * fit_image_get_data_and_size() finds data and its size including
957 * both embedded and external data. If the property is found
958 * its data start address and size are returned to the caller.
959 *
960 * returns:
961 * 0, on success
962 * otherwise, on failure
963 */
964int fit_image_get_data_and_size(const void *fit, int noffset,
965 const void **data, size_t *size)
966{
967 bool external_data = false;
968 int offset;
969 int len;
970 int ret;
971
972 if (!fit_image_get_data_position(fit, noffset, &offset)) {
973 external_data = true;
974 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
975 external_data = true;
976 /*
977 * For FIT with external data, figure out where
978 * the external images start. This is the base
979 * for the data-offset properties in each image.
980 */
981 offset += ((fdt_totalsize(fit) + 3) & ~3);
982 }
983
984 if (external_data) {
985 debug("External Data\n");
986 ret = fit_image_get_data_size(fit, noffset, &len);
987 *data = fit + offset;
988 *size = len;
989 } else {
990 ret = fit_image_get_data(fit, noffset, data, size);
991 }
992
993 return ret;
994}
995
996/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000997 * fit_image_hash_get_algo - get hash algorithm name
998 * @fit: pointer to the FIT format image header
999 * @noffset: hash node offset
1000 * @algo: double pointer to char, will hold pointer to the algorithm name
1001 *
1002 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1003 * If the property is found its data start address is returned to the caller.
1004 *
1005 * returns:
1006 * 0, on success
1007 * -1, on failure
1008 */
1009int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1010{
1011 int len;
1012
1013 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1014 if (*algo == NULL) {
1015 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1016 return -1;
1017 }
1018
1019 return 0;
1020}
1021
1022/**
1023 * fit_image_hash_get_value - get hash value and length
1024 * @fit: pointer to the FIT format image header
1025 * @noffset: hash node offset
1026 * @value: double pointer to uint8_t, will hold address of a hash value data
1027 * @value_len: pointer to an int, will hold hash data length
1028 *
1029 * fit_image_hash_get_value() finds hash value property in a given hash node.
1030 * If the property is found its data start address and size are returned to
1031 * the caller.
1032 *
1033 * returns:
1034 * 0, on success
1035 * -1, on failure
1036 */
1037int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1038 int *value_len)
1039{
1040 int len;
1041
1042 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1043 if (*value == NULL) {
1044 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1045 *value_len = 0;
1046 return -1;
1047 }
1048
1049 *value_len = len;
1050 return 0;
1051}
1052
Simon Glass53fbb7e2013-05-07 06:11:53 +00001053/**
1054 * fit_image_hash_get_ignore - get hash ignore flag
1055 * @fit: pointer to the FIT format image header
1056 * @noffset: hash node offset
1057 * @ignore: pointer to an int, will hold hash ignore flag
1058 *
1059 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1060 * If the property is found and non-zero, the hash algorithm is not verified by
1061 * u-boot automatically.
1062 *
1063 * returns:
1064 * 0, on ignore not found
1065 * value, on ignore found
1066 */
Simon Glassab9efc62013-05-07 06:11:58 +00001067static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001068{
1069 int len;
1070 int *value;
1071
1072 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1073 if (value == NULL || len != sizeof(int))
1074 *ignore = 0;
1075 else
1076 *ignore = *value;
1077
1078 return 0;
1079}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001080
Simon Glass7a80de42016-02-24 09:14:42 -07001081ulong fit_get_end(const void *fit)
1082{
1083 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1084}
1085
Simon Glass53fbb7e2013-05-07 06:11:53 +00001086/**
1087 * fit_set_timestamp - set node timestamp property
1088 * @fit: pointer to the FIT format image header
1089 * @noffset: node offset
1090 * @timestamp: timestamp value to be set
1091 *
1092 * fit_set_timestamp() attempts to set timestamp property in the requested
1093 * node and returns operation status to the caller.
1094 *
1095 * returns:
1096 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001097 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001098 */
1099int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1100{
1101 uint32_t t;
1102 int ret;
1103
1104 t = cpu_to_uimage(timestamp);
1105 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1106 sizeof(uint32_t));
1107 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001108 debug("Can't set '%s' property for '%s' node (%s)\n",
1109 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1110 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001111 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001112 }
1113
1114 return 0;
1115}
1116
1117/**
1118 * calculate_hash - calculate and return hash for provided input data
1119 * @data: pointer to the input data
1120 * @data_len: data length
1121 * @algo: requested hash algorithm
1122 * @value: pointer to the char, will hold hash value data (caller must
1123 * allocate enough free space)
1124 * value_len: length of the calculated hash
1125 *
1126 * calculate_hash() computes input data hash according to the requested
1127 * algorithm.
1128 * Resulting hash value is placed in caller provided 'value' buffer, length
1129 * of the calculated hash is returned via value_len pointer argument.
1130 *
1131 * returns:
1132 * 0, on success
1133 * -1, when algo is unsupported
1134 */
Simon Glass604f23d2013-05-07 06:11:54 +00001135int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001136 uint8_t *value, int *value_len)
1137{
Simon Glass87ebee32013-05-08 08:05:59 +00001138 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001139 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1140 CHUNKSZ_CRC32);
1141 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1142 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001143 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001144 sha1_csum_wd((unsigned char *)data, data_len,
1145 (unsigned char *)value, CHUNKSZ_SHA1);
1146 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001147 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1148 sha256_csum_wd((unsigned char *)data, data_len,
1149 (unsigned char *)value, CHUNKSZ_SHA256);
1150 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001151 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001152 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1153 *value_len = 16;
1154 } else {
1155 debug("Unsupported hash alogrithm\n");
1156 return -1;
1157 }
1158 return 0;
1159}
1160
Simon Glassab9efc62013-05-07 06:11:58 +00001161static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1162 size_t size, char **err_msgp)
1163{
1164 uint8_t value[FIT_MAX_HASH_LEN];
1165 int value_len;
1166 char *algo;
1167 uint8_t *fit_value;
1168 int fit_value_len;
1169 int ignore;
1170
1171 *err_msgp = NULL;
1172
1173 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001174 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001175 return -1;
1176 }
1177 printf("%s", algo);
1178
1179 if (IMAGE_ENABLE_IGNORE) {
1180 fit_image_hash_get_ignore(fit, noffset, &ignore);
1181 if (ignore) {
1182 printf("-skipped ");
1183 return 0;
1184 }
1185 }
1186
1187 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1188 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001189 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001190 return -1;
1191 }
1192
1193 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001194 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001195 return -1;
1196 }
1197
1198 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001199 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001200 return -1;
1201 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001202 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001203 return -1;
1204 }
1205
1206 return 0;
1207}
1208
Jun Nie5c643db2018-02-27 16:55:58 +08001209int fit_image_verify_with_data(const void *fit, int image_noffset,
1210 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001211{
Simon Glass56518e72013-06-13 15:10:01 -07001212 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001213 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001214 int verify_all = 1;
1215 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001216
Simon Glass56518e72013-06-13 15:10:01 -07001217 /* Verify all required signatures */
1218 if (IMAGE_ENABLE_VERIFY &&
1219 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1220 gd_fdt_blob(), &verify_all)) {
1221 err_msg = "Unable to verify required signature";
1222 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001223 }
1224
1225 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001226 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001227 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001228
Simon Glassab9efc62013-05-07 06:11:58 +00001229 /*
1230 * Check subnode name, must be equal to "hash".
1231 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001232 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001233 */
1234 if (!strncmp(name, FIT_HASH_NODENAME,
1235 strlen(FIT_HASH_NODENAME))) {
1236 if (fit_image_check_hash(fit, noffset, data, size,
1237 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001238 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001239 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001240 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1241 !strncmp(name, FIT_SIG_NODENAME,
1242 strlen(FIT_SIG_NODENAME))) {
1243 ret = fit_image_check_sig(fit, noffset, data,
1244 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001245
1246 /*
1247 * Show an indication on failure, but do not return
1248 * an error. Only keys marked 'required' can cause
1249 * an image validation failure. See the call to
1250 * fit_image_verify_required_sigs() above.
1251 */
1252 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001253 puts("- ");
1254 else
1255 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001256 }
1257 }
1258
1259 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001260 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001261 goto error;
1262 }
1263
1264 return 1;
1265
1266error:
Simon Glasse754da22013-05-07 06:11:59 +00001267 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001268 err_msg, fit_get_name(fit, noffset, NULL),
1269 fit_get_name(fit, image_noffset, NULL));
1270 return 0;
1271}
1272
1273/**
Jun Nie5c643db2018-02-27 16:55:58 +08001274 * fit_image_verify - verify data integrity
1275 * @fit: pointer to the FIT format image header
1276 * @image_noffset: component image node offset
1277 *
1278 * fit_image_verify() goes over component image hash nodes,
1279 * re-calculates each data hash and compares with the value stored in hash
1280 * node.
1281 *
1282 * returns:
1283 * 1, if all hashes are valid
1284 * 0, otherwise (or on error)
1285 */
1286int fit_image_verify(const void *fit, int image_noffset)
1287{
1288 const void *data;
1289 size_t size;
1290 int noffset = 0;
1291 char *err_msg = "";
1292
1293 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001294 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001295 err_msg = "Can't get image data/size";
1296 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1297 err_msg, fit_get_name(fit, noffset, NULL),
1298 fit_get_name(fit, image_noffset, NULL));
1299 return 0;
1300 }
1301
1302 return fit_image_verify_with_data(fit, image_noffset, data, size);
1303}
1304
1305/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001306 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001307 * @fit: pointer to the FIT format image header
1308 *
Simon Glassb8da8362013-05-07 06:11:57 +00001309 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001310 * for every images checks if all it's hashes are valid.
1311 *
1312 * returns:
1313 * 1, if all hashes of all images are valid
1314 * 0, otherwise (or on error)
1315 */
Simon Glassb8da8362013-05-07 06:11:57 +00001316int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001317{
1318 int images_noffset;
1319 int noffset;
1320 int ndepth;
1321 int count;
1322
1323 /* Find images parent node offset */
1324 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1325 if (images_noffset < 0) {
1326 printf("Can't find images parent node '%s' (%s)\n",
1327 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1328 return 0;
1329 }
1330
1331 /* Process all image subnodes, check hashes for each */
1332 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1333 (ulong)fit);
1334 for (ndepth = 0, count = 0,
1335 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1336 (noffset >= 0) && (ndepth > 0);
1337 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1338 if (ndepth == 1) {
1339 /*
1340 * Direct child node of the images parent node,
1341 * i.e. component image node.
1342 */
Simon Glass73223f02016-02-22 22:55:43 -07001343 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001344 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001345 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001346
Simon Glassb8da8362013-05-07 06:11:57 +00001347 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001348 return 0;
1349 printf("\n");
1350 }
1351 }
1352 return 1;
1353}
1354
1355/**
1356 * fit_image_check_os - check whether image node is of a given os type
1357 * @fit: pointer to the FIT format image header
1358 * @noffset: component image node offset
1359 * @os: requested image os
1360 *
1361 * fit_image_check_os() reads image os property and compares its numeric
1362 * id with the requested os. Comparison result is returned to the caller.
1363 *
1364 * returns:
1365 * 1 if image is of given os type
1366 * 0 otherwise (or on error)
1367 */
1368int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1369{
1370 uint8_t image_os;
1371
1372 if (fit_image_get_os(fit, noffset, &image_os))
1373 return 0;
1374 return (os == image_os);
1375}
1376
1377/**
1378 * fit_image_check_arch - check whether image node is of a given arch
1379 * @fit: pointer to the FIT format image header
1380 * @noffset: component image node offset
1381 * @arch: requested imagearch
1382 *
1383 * fit_image_check_arch() reads image arch property and compares its numeric
1384 * id with the requested arch. Comparison result is returned to the caller.
1385 *
1386 * returns:
1387 * 1 if image is of given arch
1388 * 0 otherwise (or on error)
1389 */
1390int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1391{
1392 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001393 int aarch32_support = 0;
1394
1395#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1396 aarch32_support = 1;
1397#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001398
1399 if (fit_image_get_arch(fit, noffset, &image_arch))
1400 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001401 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001402 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1403 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1404 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001405}
1406
1407/**
1408 * fit_image_check_type - check whether image node is of a given type
1409 * @fit: pointer to the FIT format image header
1410 * @noffset: component image node offset
1411 * @type: requested image type
1412 *
1413 * fit_image_check_type() reads image type property and compares its numeric
1414 * id with the requested type. Comparison result is returned to the caller.
1415 *
1416 * returns:
1417 * 1 if image is of given type
1418 * 0 otherwise (or on error)
1419 */
1420int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1421{
1422 uint8_t image_type;
1423
1424 if (fit_image_get_type(fit, noffset, &image_type))
1425 return 0;
1426 return (type == image_type);
1427}
1428
1429/**
1430 * fit_image_check_comp - check whether image node uses given compression
1431 * @fit: pointer to the FIT format image header
1432 * @noffset: component image node offset
1433 * @comp: requested image compression type
1434 *
1435 * fit_image_check_comp() reads image compression property and compares its
1436 * numeric id with the requested compression type. Comparison result is
1437 * returned to the caller.
1438 *
1439 * returns:
1440 * 1 if image uses requested compression
1441 * 0 otherwise (or on error)
1442 */
1443int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1444{
1445 uint8_t image_comp;
1446
1447 if (fit_image_get_comp(fit, noffset, &image_comp))
1448 return 0;
1449 return (comp == image_comp);
1450}
1451
1452/**
1453 * fit_check_format - sanity check FIT image format
1454 * @fit: pointer to the FIT format image header
1455 *
1456 * fit_check_format() runs a basic sanity FIT image verification.
1457 * Routine checks for mandatory properties, nodes, etc.
1458 *
1459 * returns:
1460 * 1, on success
1461 * 0, on failure
1462 */
1463int fit_check_format(const void *fit)
1464{
1465 /* mandatory / node 'description' property */
1466 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1467 debug("Wrong FIT format: no description\n");
1468 return 0;
1469 }
1470
1471 if (IMAGE_ENABLE_TIMESTAMP) {
1472 /* mandatory / node 'timestamp' property */
1473 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1474 debug("Wrong FIT format: no timestamp\n");
1475 return 0;
1476 }
1477 }
1478
1479 /* mandatory subimages parent '/images' node */
1480 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1481 debug("Wrong FIT format: no images parent node\n");
1482 return 0;
1483 }
1484
1485 return 1;
1486}
1487
1488
1489/**
1490 * fit_conf_find_compat
1491 * @fit: pointer to the FIT format image header
1492 * @fdt: pointer to the device tree to compare against
1493 *
1494 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1495 * most compatible with the passed in device tree.
1496 *
1497 * Example:
1498 *
1499 * / o image-tree
1500 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001501 * | |-o fdt-1
1502 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001503 * |
1504 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001505 * |-o config-1
1506 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001507 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001508 * |-o config-2
1509 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001510 *
1511 * / o U-Boot fdt
1512 * |-compatible = "foo,bar", "bim,bam"
1513 *
1514 * / o kernel fdt1
1515 * |-compatible = "foo,bar",
1516 *
1517 * / o kernel fdt2
1518 * |-compatible = "bim,bam", "baz,biz"
1519 *
1520 * Configuration 1 would be picked because the first string in U-Boot's
1521 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1522 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1523 *
1524 * returns:
1525 * offset to the configuration to use if one was found
1526 * -1 otherwise
1527 */
1528int fit_conf_find_compat(const void *fit, const void *fdt)
1529{
1530 int ndepth = 0;
1531 int noffset, confs_noffset, images_noffset;
1532 const void *fdt_compat;
1533 int fdt_compat_len;
1534 int best_match_offset = 0;
1535 int best_match_pos = 0;
1536
1537 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1538 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1539 if (confs_noffset < 0 || images_noffset < 0) {
1540 debug("Can't find configurations or images nodes.\n");
1541 return -1;
1542 }
1543
1544 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1545 if (!fdt_compat) {
1546 debug("Fdt for comparison has no \"compatible\" property.\n");
1547 return -1;
1548 }
1549
1550 /*
1551 * Loop over the configurations in the FIT image.
1552 */
1553 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1554 (noffset >= 0) && (ndepth > 0);
1555 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1556 const void *kfdt;
1557 const char *kfdt_name;
1558 int kfdt_noffset;
1559 const char *cur_fdt_compat;
1560 int len;
1561 size_t size;
1562 int i;
1563
1564 if (ndepth > 1)
1565 continue;
1566
1567 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1568 if (!kfdt_name) {
1569 debug("No fdt property found.\n");
1570 continue;
1571 }
1572 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1573 kfdt_name);
1574 if (kfdt_noffset < 0) {
1575 debug("No image node named \"%s\" found.\n",
1576 kfdt_name);
1577 continue;
1578 }
1579 /*
1580 * Get a pointer to this configuration's fdt.
1581 */
1582 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1583 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1584 continue;
1585 }
1586
1587 len = fdt_compat_len;
1588 cur_fdt_compat = fdt_compat;
1589 /*
1590 * Look for a match for each U-Boot compatibility string in
1591 * turn in this configuration's fdt.
1592 */
1593 for (i = 0; len > 0 &&
1594 (!best_match_offset || best_match_pos > i); i++) {
1595 int cur_len = strlen(cur_fdt_compat) + 1;
1596
1597 if (!fdt_node_check_compatible(kfdt, 0,
1598 cur_fdt_compat)) {
1599 best_match_offset = noffset;
1600 best_match_pos = i;
1601 break;
1602 }
1603 len -= cur_len;
1604 cur_fdt_compat += cur_len;
1605 }
1606 }
1607 if (!best_match_offset) {
1608 debug("No match found.\n");
1609 return -1;
1610 }
1611
1612 return best_match_offset;
1613}
1614
1615/**
1616 * fit_conf_get_node - get node offset for configuration of a given unit name
1617 * @fit: pointer to the FIT format image header
1618 * @conf_uname: configuration node unit name
1619 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001620 * fit_conf_get_node() finds a configuration (within the '/configurations'
1621 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001622 * offset is returned to the caller.
1623 *
1624 * When NULL is provided in second argument fit_conf_get_node() will search
1625 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001626 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001627 * node.
1628 *
1629 * returns:
1630 * configuration node offset when found (>=0)
1631 * negative number on failure (FDT_ERR_* code)
1632 */
1633int fit_conf_get_node(const void *fit, const char *conf_uname)
1634{
1635 int noffset, confs_noffset;
1636 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001637 const char *s;
1638 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001639
1640 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1641 if (confs_noffset < 0) {
1642 debug("Can't find configurations parent node '%s' (%s)\n",
1643 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1644 return confs_noffset;
1645 }
1646
1647 if (conf_uname == NULL) {
1648 /* get configuration unit name from the default property */
1649 debug("No configuration specified, trying default...\n");
1650 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1651 FIT_DEFAULT_PROP, &len);
1652 if (conf_uname == NULL) {
1653 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1654 len);
1655 return len;
1656 }
1657 debug("Found default configuration: '%s'\n", conf_uname);
1658 }
1659
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001660 s = strchr(conf_uname, '#');
1661 if (s) {
1662 len = s - conf_uname;
1663 conf_uname_copy = malloc(len + 1);
1664 if (!conf_uname_copy) {
1665 debug("Can't allocate uname copy: '%s'\n",
1666 conf_uname);
1667 return -ENOMEM;
1668 }
1669 memcpy(conf_uname_copy, conf_uname, len);
1670 conf_uname_copy[len] = '\0';
1671 conf_uname = conf_uname_copy;
1672 }
1673
Simon Glass53fbb7e2013-05-07 06:11:53 +00001674 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1675 if (noffset < 0) {
1676 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1677 conf_uname, fdt_strerror(noffset));
1678 }
1679
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001680 if (conf_uname_copy)
1681 free(conf_uname_copy);
1682
Simon Glass53fbb7e2013-05-07 06:11:53 +00001683 return noffset;
1684}
1685
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001686int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001687 const char *prop_name)
1688{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001689 return fdt_stringlist_count(fit, noffset, prop_name);
1690}
1691
1692int fit_conf_get_prop_node_index(const void *fit, int noffset,
1693 const char *prop_name, int index)
1694{
1695 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001696 int len;
1697
1698 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001699 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001700 if (uname == NULL)
1701 return len;
1702
1703 return fit_image_get_node(fit, uname);
1704}
1705
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001706int fit_conf_get_prop_node(const void *fit, int noffset,
1707 const char *prop_name)
1708{
1709 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1710}
1711
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001712static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001713{
1714 fit_image_print(fit, rd_noffset, " ");
1715
1716 if (verify) {
1717 puts(" Verifying Hash Integrity ... ");
1718 if (!fit_image_verify(fit, rd_noffset)) {
1719 puts("Bad Data Hash\n");
1720 return -EACCES;
1721 }
1722 puts("OK\n");
1723 }
1724
1725 return 0;
1726}
1727
Simon Glass782cfbb2013-05-16 13:53:21 +00001728int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1729 ulong addr)
1730{
1731 int cfg_noffset;
1732 void *fit_hdr;
1733 int noffset;
1734
1735 debug("* %s: using config '%s' from image at 0x%08lx\n",
1736 prop_name, images->fit_uname_cfg, addr);
1737
1738 /* Check whether configuration has this property defined */
1739 fit_hdr = map_sysmem(addr, 0);
1740 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1741 if (cfg_noffset < 0) {
1742 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001743 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001744 }
1745
1746 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1747 if (noffset < 0) {
1748 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001749 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001750 }
1751
1752 return noffset;
1753}
1754
Simon Glass126cc862014-06-12 07:24:47 -06001755/**
1756 * fit_get_image_type_property() - get property name for IH_TYPE_...
1757 *
1758 * @return the properly name where we expect to find the image in the
1759 * config node
1760 */
1761static const char *fit_get_image_type_property(int type)
1762{
1763 /*
1764 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001765 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001766 * anyway. So let's just keep it here.
1767 */
1768 switch (type) {
1769 case IH_TYPE_FLATDT:
1770 return FIT_FDT_PROP;
1771 case IH_TYPE_KERNEL:
1772 return FIT_KERNEL_PROP;
1773 case IH_TYPE_RAMDISK:
1774 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001775 case IH_TYPE_X86_SETUP:
1776 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001777 case IH_TYPE_LOADABLE:
1778 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001779 case IH_TYPE_FPGA:
1780 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001781 case IH_TYPE_STANDALONE:
1782 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001783 }
1784
1785 return "unknown";
1786}
1787
1788int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001789 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001790 int arch, int image_type, int bootstage_id,
1791 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1792{
1793 int cfg_noffset, noffset;
1794 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001795 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001796 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001797 const void *fit;
1798 const void *buf;
1799 size_t size;
1800 int type_ok, os_ok;
1801 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001802 uint8_t os;
Alison Wangec6617c2016-11-10 10:49:03 +08001803#ifndef USE_HOSTCC
1804 uint8_t os_arch;
1805#endif
Simon Glass126cc862014-06-12 07:24:47 -06001806 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001807 int ret;
1808
1809 fit = map_sysmem(addr, 0);
1810 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001811 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001812 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001813 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001814 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1815
1816 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1817 if (!fit_check_format(fit)) {
1818 printf("Bad FIT %s image format!\n", prop_name);
1819 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1820 return -ENOEXEC;
1821 }
1822 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1823 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001824 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001825 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1826 noffset = fit_image_get_node(fit, fit_uname);
1827 } else {
1828 /*
1829 * no image node unit name, try to get config
1830 * node first. If config unit node name is NULL
1831 * fit_conf_get_node() will try to find default config node
1832 */
1833 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1834 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1835 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1836 } else {
1837 cfg_noffset = fit_conf_get_node(fit,
1838 fit_uname_config);
1839 }
1840 if (cfg_noffset < 0) {
1841 puts("Could not find configuration node\n");
1842 bootstage_error(bootstage_id +
1843 BOOTSTAGE_SUB_NO_UNIT_NAME);
1844 return -ENOENT;
1845 }
Marek Vasut078e5582018-05-31 17:59:07 +02001846
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001847 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1848 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02001849 /* Remember this config */
1850 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001851 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02001852
1853 if (IMAGE_ENABLE_VERIFY && images->verify) {
1854 puts(" Verifying Hash Integrity ... ");
1855 if (fit_config_verify(fit, cfg_noffset)) {
1856 puts("Bad Data Hash\n");
1857 bootstage_error(bootstage_id +
1858 BOOTSTAGE_SUB_HASH);
1859 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00001860 }
Marek Vasut078e5582018-05-31 17:59:07 +02001861 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00001862 }
1863
Marek Vasut078e5582018-05-31 17:59:07 +02001864 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1865
Simon Glass782cfbb2013-05-16 13:53:21 +00001866 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1867 prop_name);
1868 fit_uname = fit_get_name(fit, noffset, NULL);
1869 }
1870 if (noffset < 0) {
1871 puts("Could not find subimage node\n");
1872 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1873 return -ENOENT;
1874 }
1875
1876 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1877
1878 ret = fit_image_select(fit, noffset, images->verify);
1879 if (ret) {
1880 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1881 return ret;
1882 }
1883
1884 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001885#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001886 if (!fit_image_check_target_arch(fit, noffset)) {
1887 puts("Unsupported Architecture\n");
1888 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1889 return -ENOEXEC;
1890 }
Simon Glassce1400f2014-06-12 07:24:53 -06001891#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001892
1893#ifndef USE_HOSTCC
1894 fit_image_get_arch(fit, noffset, &os_arch);
1895 images->os.arch = os_arch;
1896#endif
1897
Simon Glass782cfbb2013-05-16 13:53:21 +00001898 if (image_type == IH_TYPE_FLATDT &&
1899 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1900 puts("FDT image is compressed");
1901 return -EPROTONOSUPPORT;
1902 }
1903
1904 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1905 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001906 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1907 (image_type == IH_TYPE_KERNEL &&
1908 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001909
Andreas Bießmann950fe262016-08-14 20:31:24 +02001910 os_ok = image_type == IH_TYPE_FLATDT ||
1911 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001912 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001913 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001914 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001915
1916 /*
1917 * If either of the checks fail, we should report an error, but
1918 * if the image type is coming from the "loadables" field, we
1919 * don't care what it is
1920 */
1921 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001922 fit_image_get_os(fit, noffset, &os);
1923 printf("No %s %s %s Image\n",
1924 genimg_get_os_name(os),
1925 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001926 genimg_get_type_name(image_type));
1927 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1928 return -EIO;
1929 }
1930
1931 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1932
1933 /* get image data address and length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001934 if (fit_image_get_data_and_size(fit, noffset, &buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001935 printf("Could not find %s subimage data!\n", prop_name);
1936 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001937 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001938 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06001939
1940#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1941 /* perform any post-processing on the image data */
1942 board_fit_image_post_process((void **)&buf, &size);
1943#endif
1944
Simon Glass782cfbb2013-05-16 13:53:21 +00001945 len = (ulong)size;
1946
1947 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001948 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001949 puts("Subimage data is not a FDT");
1950 return -ENOEXEC;
1951 }
1952
1953 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1954
1955 /*
1956 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001957 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001958 * warning: initialization discards qualifiers from pointer target type
1959 */
1960 {
1961 void *vbuf = (void *)buf;
1962
1963 data = map_to_sysmem(vbuf);
1964 }
1965
1966 if (load_op == FIT_LOAD_IGNORED) {
1967 /* Don't load */
1968 } else if (fit_image_get_load(fit, noffset, &load)) {
1969 if (load_op == FIT_LOAD_REQUIRED) {
1970 printf("Can't get %s subimage load address!\n",
1971 prop_name);
1972 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1973 return -EBADF;
1974 }
Simon Glassfe20a812014-08-22 14:26:43 -06001975 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001976 ulong image_start, image_end;
1977 ulong load_end;
1978 void *dst;
1979
1980 /*
1981 * move image data to the load address,
1982 * make sure we don't overwrite initial image
1983 */
1984 image_start = addr;
1985 image_end = addr + fit_get_size(fit);
1986
1987 load_end = load + len;
1988 if (image_type != IH_TYPE_KERNEL &&
1989 load < image_end && load_end > image_start) {
1990 printf("Error: %s overwritten\n", prop_name);
1991 return -EXDEV;
1992 }
1993
1994 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1995 prop_name, data, load);
1996
1997 dst = map_sysmem(load, len);
1998 memmove(dst, buf, len);
1999 data = load;
2000 }
2001 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2002
2003 *datap = data;
2004 *lenp = len;
2005 if (fit_unamep)
2006 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002007 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002008 *fit_uname_configp = (char *)(fit_uname_config ? :
2009 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002010
2011 return noffset;
2012}
Simon Glass90268b82014-10-19 21:11:24 -06002013
2014int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2015 ulong *setup_start, ulong *setup_len)
2016{
2017 int noffset;
2018 ulong addr;
2019 ulong len;
2020 int ret;
2021
2022 addr = map_to_sysmem(images->fit_hdr_os);
2023 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2024 if (noffset < 0)
2025 return noffset;
2026
2027 ret = fit_image_load(images, addr, NULL, NULL, arch,
2028 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2029 FIT_LOAD_REQUIRED, setup_start, &len);
2030
2031 return ret;
2032}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002033
2034#ifndef USE_HOSTCC
2035int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2036 const char **fit_unamep, const char **fit_uname_configp,
2037 int arch, ulong *datap, ulong *lenp)
2038{
2039 int fdt_noffset, cfg_noffset, count;
2040 const void *fit;
2041 const char *fit_uname = NULL;
2042 const char *fit_uname_config = NULL;
2043 char *fit_uname_config_copy = NULL;
2044 char *next_config = NULL;
2045 ulong load, len;
2046#ifdef CONFIG_OF_LIBFDT_OVERLAY
2047 ulong image_start, image_end;
2048 ulong ovload, ovlen;
2049 const char *uconfig;
2050 const char *uname;
2051 void *base, *ov;
2052 int i, err, noffset, ov_noffset;
2053#endif
2054
2055 fit_uname = fit_unamep ? *fit_unamep : NULL;
2056
2057 if (fit_uname_configp && *fit_uname_configp) {
2058 fit_uname_config_copy = strdup(*fit_uname_configp);
2059 if (!fit_uname_config_copy)
2060 return -ENOMEM;
2061
2062 next_config = strchr(fit_uname_config_copy, '#');
2063 if (next_config)
2064 *next_config++ = '\0';
2065 if (next_config - 1 > fit_uname_config_copy)
2066 fit_uname_config = fit_uname_config_copy;
2067 }
2068
2069 fdt_noffset = fit_image_load(images,
2070 addr, &fit_uname, &fit_uname_config,
2071 arch, IH_TYPE_FLATDT,
2072 BOOTSTAGE_ID_FIT_FDT_START,
2073 FIT_LOAD_OPTIONAL, &load, &len);
2074
2075 if (fdt_noffset < 0)
2076 goto out;
2077
2078 debug("fit_uname=%s, fit_uname_config=%s\n",
2079 fit_uname ? fit_uname : "<NULL>",
2080 fit_uname_config ? fit_uname_config : "<NULL>");
2081
2082 fit = map_sysmem(addr, 0);
2083
2084 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2085
2086 /* single blob, or error just return as well */
2087 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2088 if (count <= 1 && !next_config)
2089 goto out;
2090
2091 /* we need to apply overlays */
2092
2093#ifdef CONFIG_OF_LIBFDT_OVERLAY
2094 image_start = addr;
2095 image_end = addr + fit_get_size(fit);
2096 /* verify that relocation took place by load address not being in fit */
2097 if (load >= image_start && load < image_end) {
2098 /* check is simplified; fit load checks for overlaps */
2099 printf("Overlayed FDT requires relocation\n");
2100 fdt_noffset = -EBADF;
2101 goto out;
2102 }
2103
2104 base = map_sysmem(load, len);
2105
2106 /* apply extra configs in FIT first, followed by args */
2107 for (i = 1; ; i++) {
2108 if (i < count) {
2109 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2110 FIT_FDT_PROP, i);
2111 uname = fit_get_name(fit, noffset, NULL);
2112 uconfig = NULL;
2113 } else {
2114 if (!next_config)
2115 break;
2116 uconfig = next_config;
2117 next_config = strchr(next_config, '#');
2118 if (next_config)
2119 *next_config++ = '\0';
2120 uname = NULL;
2121 }
2122
2123 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2124
2125 ov_noffset = fit_image_load(images,
2126 addr, &uname, &uconfig,
2127 arch, IH_TYPE_FLATDT,
2128 BOOTSTAGE_ID_FIT_FDT_START,
2129 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2130 if (ov_noffset < 0) {
2131 printf("load of %s failed\n", uname);
2132 continue;
2133 }
2134 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2135 uname, ovload, ovlen);
2136 ov = map_sysmem(ovload, ovlen);
2137
2138 base = map_sysmem(load, len + ovlen);
2139 err = fdt_open_into(base, base, len + ovlen);
2140 if (err < 0) {
2141 printf("failed on fdt_open_into\n");
2142 fdt_noffset = err;
2143 goto out;
2144 }
2145 /* the verbose method prints out messages on error */
2146 err = fdt_overlay_apply_verbose(base, ov);
2147 if (err < 0) {
2148 fdt_noffset = err;
2149 goto out;
2150 }
2151 fdt_pack(base);
2152 len = fdt_totalsize(base);
2153 }
2154#else
2155 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2156 fdt_noffset = -EBADF;
2157#endif
2158
2159out:
2160 if (datap)
2161 *datap = load;
2162 if (lenp)
2163 *lenp = len;
2164 if (fit_unamep)
2165 *fit_unamep = fit_uname;
2166 if (fit_uname_configp)
2167 *fit_uname_configp = fit_uname_config;
2168
2169 if (fit_uname_config_copy)
2170 free(fit_uname_config_copy);
2171 return fdt_noffset;
2172}
2173#endif