blob: e3577e2dd73d4344d1ddb142ca7e67cfb98496e7 [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;
168 int required;
169 int ret, i;
170
171 debug("%s %s node: '%s'\n", p, type,
172 fit_get_name(fit, noffset, NULL));
173 printf("%s %s algo: ", p, type);
174 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
175 printf("invalid/unsupported\n");
176 return;
177 }
178 printf("%s", algo);
179 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
180 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
181 if (keyname)
182 printf(":%s", keyname);
183 if (required)
184 printf(" (required)");
185 printf("\n");
186
187 ret = fit_image_hash_get_value(fit, noffset, &value,
188 &value_len);
189 printf("%s %s value: ", p, type);
190 if (ret) {
191 printf("unavailable\n");
192 } else {
193 for (i = 0; i < value_len; i++)
194 printf("%02x", value[i]);
195 printf("\n");
196 }
197
198 debug("%s %s len: %d\n", p, type, value_len);
199
200 /* Signatures have a time stamp */
201 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
202 time_t timestamp;
203
204 printf("%s Timestamp: ", p);
205 if (fit_get_timestamp(fit, noffset, &timestamp))
206 printf("unavailable\n");
207 else
208 genimg_print_time(timestamp);
209 }
210}
211
212/**
213 * fit_image_print_verification_data() - prints out the hash/signature details
214 * @fit: pointer to the FIT format image header
215 * @noffset: offset of the hash or signature node
216 * @p: pointer to prefix string
217 *
218 * This lists properties for the processed hash node
219 *
220 * returns:
221 * no returned results
222 */
223static void fit_image_print_verification_data(const void *fit, int noffset,
224 const char *p)
225{
226 const char *name;
227
228 /*
229 * Check subnode name, must be equal to "hash" or "signature".
230 * Multiple hash/signature nodes require unique unit node
231 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
232 */
233 name = fit_get_name(fit, noffset, NULL);
234 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
235 fit_image_print_data(fit, noffset, p, "Hash");
236 } else if (!strncmp(name, FIT_SIG_NODENAME,
237 strlen(FIT_SIG_NODENAME))) {
238 fit_image_print_data(fit, noffset, p, "Sign");
239 }
240}
241
242/**
243 * fit_conf_print - prints out the FIT configuration details
244 * @fit: pointer to the FIT format image header
245 * @noffset: offset of the configuration node
246 * @p: pointer to prefix string
247 *
248 * fit_conf_print() lists all mandatory properties for the processed
249 * configuration node.
250 *
251 * returns:
252 * no returned results
253 */
254static void fit_conf_print(const void *fit, int noffset, const char *p)
255{
256 char *desc;
257 const char *uname;
258 int ret;
259 int fdt_index, loadables_index;
260 int ndepth;
261
262 /* Mandatory properties */
263 ret = fit_get_desc(fit, noffset, &desc);
264 printf("%s Description: ", p);
265 if (ret)
266 printf("unavailable\n");
267 else
268 printf("%s\n", desc);
269
270 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
271 printf("%s Kernel: ", p);
272 if (!uname)
273 printf("unavailable\n");
274 else
275 printf("%s\n", uname);
276
277 /* Optional properties */
278 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
279 if (uname)
280 printf("%s Init Ramdisk: %s\n", p, uname);
281
282 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
283 if (uname)
284 printf("%s Firmware: %s\n", p, uname);
285
286 for (fdt_index = 0;
287 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
288 fdt_index, NULL), uname;
289 fdt_index++) {
290 if (fdt_index == 0)
291 printf("%s FDT: ", p);
292 else
293 printf("%s ", p);
294 printf("%s\n", uname);
295 }
296
297 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
298 if (uname)
299 printf("%s FPGA: %s\n", p, uname);
300
301 /* Print out all of the specified loadables */
302 for (loadables_index = 0;
303 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
304 loadables_index, NULL), uname;
305 loadables_index++) {
306 if (loadables_index == 0) {
307 printf("%s Loadables: ", p);
308 } else {
309 printf("%s ", p);
310 }
311 printf("%s\n", uname);
312 }
313
314 /* Process all hash subnodes of the component configuration node */
315 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
316 (noffset >= 0) && (ndepth > 0);
317 noffset = fdt_next_node(fit, noffset, &ndepth)) {
318 if (ndepth == 1) {
319 /* Direct child node of the component configuration node */
320 fit_image_print_verification_data(fit, noffset, p);
321 }
322 }
323}
324
325/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000326 * fit_print_contents - prints out the contents of the FIT format image
327 * @fit: pointer to the FIT format image header
328 * @p: pointer to prefix string
329 *
330 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500331 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000332 * the details of each component image.
333 *
334 * returns:
335 * no returned results
336 */
337void fit_print_contents(const void *fit)
338{
339 char *desc;
340 char *uname;
341 int images_noffset;
342 int confs_noffset;
343 int noffset;
344 int ndepth;
345 int count = 0;
346 int ret;
347 const char *p;
348 time_t timestamp;
349
Simon Glass1fe7d932013-05-08 08:05:58 +0000350 /* Indent string is defined in header image.h */
351 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000352
353 /* Root node properties */
354 ret = fit_get_desc(fit, 0, &desc);
355 printf("%sFIT description: ", p);
356 if (ret)
357 printf("unavailable\n");
358 else
359 printf("%s\n", desc);
360
361 if (IMAGE_ENABLE_TIMESTAMP) {
362 ret = fit_get_timestamp(fit, 0, &timestamp);
363 printf("%sCreated: ", p);
364 if (ret)
365 printf("unavailable\n");
366 else
367 genimg_print_time(timestamp);
368 }
369
370 /* Find images parent node offset */
371 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
372 if (images_noffset < 0) {
373 printf("Can't find images parent node '%s' (%s)\n",
374 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
375 return;
376 }
377
378 /* Process its subnodes, print out component images details */
379 for (ndepth = 0, count = 0,
380 noffset = fdt_next_node(fit, images_noffset, &ndepth);
381 (noffset >= 0) && (ndepth > 0);
382 noffset = fdt_next_node(fit, noffset, &ndepth)) {
383 if (ndepth == 1) {
384 /*
385 * Direct child node of the images parent node,
386 * i.e. component image node.
387 */
388 printf("%s Image %u (%s)\n", p, count++,
389 fit_get_name(fit, noffset, NULL));
390
391 fit_image_print(fit, noffset, p);
392 }
393 }
394
395 /* Find configurations parent node offset */
396 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
397 if (confs_noffset < 0) {
398 debug("Can't get configurations parent node '%s' (%s)\n",
399 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
400 return;
401 }
402
403 /* get default configuration unit name from default property */
404 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
405 if (uname)
406 printf("%s Default Configuration: '%s'\n", p, uname);
407
408 /* Process its subnodes, print out configurations details */
409 for (ndepth = 0, count = 0,
410 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
411 (noffset >= 0) && (ndepth > 0);
412 noffset = fdt_next_node(fit, noffset, &ndepth)) {
413 if (ndepth == 1) {
414 /*
415 * Direct child node of the configurations parent node,
416 * i.e. configuration node.
417 */
418 printf("%s Configuration %u (%s)\n", p, count++,
419 fit_get_name(fit, noffset, NULL));
420
421 fit_conf_print(fit, noffset, p);
422 }
423 }
424}
425
426/**
427 * fit_image_print - prints out the FIT component image details
428 * @fit: pointer to the FIT format image header
429 * @image_noffset: offset of the component image node
430 * @p: pointer to prefix string
431 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500432 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000433 * image. If present, hash nodes are printed out as well. Load
434 * address for images of type firmware is also printed out. Since the load
435 * address is not mandatory for firmware images, it will be output as
436 * "unavailable" when not present.
437 *
438 * returns:
439 * no returned results
440 */
441void fit_image_print(const void *fit, int image_noffset, const char *p)
442{
443 char *desc;
444 uint8_t type, arch, os, comp;
445 size_t size;
446 ulong load, entry;
447 const void *data;
448 int noffset;
449 int ndepth;
450 int ret;
451
452 /* Mandatory properties */
453 ret = fit_get_desc(fit, image_noffset, &desc);
454 printf("%s Description: ", p);
455 if (ret)
456 printf("unavailable\n");
457 else
458 printf("%s\n", desc);
459
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700460 if (IMAGE_ENABLE_TIMESTAMP) {
461 time_t timestamp;
462
463 ret = fit_get_timestamp(fit, 0, &timestamp);
464 printf("%s Created: ", p);
465 if (ret)
466 printf("unavailable\n");
467 else
468 genimg_print_time(timestamp);
469 }
470
Simon Glass53fbb7e2013-05-07 06:11:53 +0000471 fit_image_get_type(fit, image_noffset, &type);
472 printf("%s Type: %s\n", p, genimg_get_type_name(type));
473
474 fit_image_get_comp(fit, image_noffset, &comp);
475 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
476
477 ret = fit_image_get_data(fit, image_noffset, &data, &size);
478
479#ifndef USE_HOSTCC
480 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000481 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000482 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000483 } else {
484 void *vdata = (void *)data;
485
486 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
487 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000488#endif
489
490 printf("%s Data Size: ", p);
491 if (ret)
492 printf("unavailable\n");
493 else
494 genimg_print_size(size);
495
496 /* Remaining, type dependent properties */
497 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
498 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
499 (type == IH_TYPE_FLATDT)) {
500 fit_image_get_arch(fit, image_noffset, &arch);
501 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
502 }
503
Michal Simek6faf4622018-03-26 16:31:27 +0200504 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
505 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000506 fit_image_get_os(fit, image_noffset, &os);
507 printf("%s OS: %s\n", p, genimg_get_os_name(os));
508 }
509
510 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200511 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
512 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000513 ret = fit_image_get_load(fit, image_noffset, &load);
514 printf("%s Load Address: ", p);
515 if (ret)
516 printf("unavailable\n");
517 else
518 printf("0x%08lx\n", load);
519 }
520
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300521 /* optional load address for FDT */
522 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
523 printf("%s Load Address: 0x%08lx\n", p, load);
524
Simon Glass53fbb7e2013-05-07 06:11:53 +0000525 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
526 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800527 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000528 printf("%s Entry Point: ", p);
529 if (ret)
530 printf("unavailable\n");
531 else
532 printf("0x%08lx\n", entry);
533 }
534
535 /* Process all hash subnodes of the component image node */
536 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
537 (noffset >= 0) && (ndepth > 0);
538 noffset = fdt_next_node(fit, noffset, &ndepth)) {
539 if (ndepth == 1) {
540 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000541 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000542 }
543 }
544}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200545#else
546void fit_print_contents(const void *fit) { }
547void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Marek Vasutb527b9c2018-05-13 00:22:52 +0200548#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000549
550/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000551 * fit_get_desc - get node description property
552 * @fit: pointer to the FIT format image header
553 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500554 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000555 *
556 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500557 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000558 *
559 * returns:
560 * 0, on success
561 * -1, on failure
562 */
563int fit_get_desc(const void *fit, int noffset, char **desc)
564{
565 int len;
566
567 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
568 if (*desc == NULL) {
569 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
570 return -1;
571 }
572
573 return 0;
574}
575
576/**
577 * fit_get_timestamp - get node timestamp property
578 * @fit: pointer to the FIT format image header
579 * @noffset: node offset
580 * @timestamp: pointer to the time_t, will hold read timestamp
581 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500582 * fit_get_timestamp() reads timestamp property from given node, if timestamp
583 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000584 * argument.
585 *
586 * returns:
587 * 0, on success
588 * -1, on property read failure
589 * -2, on wrong timestamp size
590 */
591int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
592{
593 int len;
594 const void *data;
595
596 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
597 if (data == NULL) {
598 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
599 return -1;
600 }
601 if (len != sizeof(uint32_t)) {
602 debug("FIT timestamp with incorrect size of (%u)\n", len);
603 return -2;
604 }
605
606 *timestamp = uimage_to_cpu(*((uint32_t *)data));
607 return 0;
608}
609
610/**
611 * fit_image_get_node - get node offset for component image of a given unit name
612 * @fit: pointer to the FIT format image header
613 * @image_uname: component image node unit name
614 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500615 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000616 * node) of a provided unit name. If image is found its node offset is
617 * returned to the caller.
618 *
619 * returns:
620 * image node offset when found (>=0)
621 * negative number on failure (FDT_ERR_* code)
622 */
623int fit_image_get_node(const void *fit, const char *image_uname)
624{
625 int noffset, images_noffset;
626
627 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
628 if (images_noffset < 0) {
629 debug("Can't find images parent node '%s' (%s)\n",
630 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
631 return images_noffset;
632 }
633
634 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
635 if (noffset < 0) {
636 debug("Can't get node offset for image unit name: '%s' (%s)\n",
637 image_uname, fdt_strerror(noffset));
638 }
639
640 return noffset;
641}
642
643/**
644 * fit_image_get_os - get os id for a given component image node
645 * @fit: pointer to the FIT format image header
646 * @noffset: component image node offset
647 * @os: pointer to the uint8_t, will hold os numeric id
648 *
649 * fit_image_get_os() finds os property in a given component image node.
650 * If the property is found, its (string) value is translated to the numeric
651 * id which is returned to the caller.
652 *
653 * returns:
654 * 0, on success
655 * -1, on failure
656 */
657int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
658{
659 int len;
660 const void *data;
661
662 /* Get OS name from property data */
663 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
664 if (data == NULL) {
665 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
666 *os = -1;
667 return -1;
668 }
669
670 /* Translate OS name to id */
671 *os = genimg_get_os_id(data);
672 return 0;
673}
674
675/**
676 * fit_image_get_arch - get arch id for a given component image node
677 * @fit: pointer to the FIT format image header
678 * @noffset: component image node offset
679 * @arch: pointer to the uint8_t, will hold arch numeric id
680 *
681 * fit_image_get_arch() finds arch property in a given component image node.
682 * If the property is found, its (string) value is translated to the numeric
683 * id which is returned to the caller.
684 *
685 * returns:
686 * 0, on success
687 * -1, on failure
688 */
689int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
690{
691 int len;
692 const void *data;
693
694 /* Get architecture name from property data */
695 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
696 if (data == NULL) {
697 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
698 *arch = -1;
699 return -1;
700 }
701
702 /* Translate architecture name to id */
703 *arch = genimg_get_arch_id(data);
704 return 0;
705}
706
707/**
708 * fit_image_get_type - get type id for a given component image node
709 * @fit: pointer to the FIT format image header
710 * @noffset: component image node offset
711 * @type: pointer to the uint8_t, will hold type numeric id
712 *
713 * fit_image_get_type() finds type property in a given component image node.
714 * If the property is found, its (string) value is translated to the numeric
715 * id which is returned to the caller.
716 *
717 * returns:
718 * 0, on success
719 * -1, on failure
720 */
721int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
722{
723 int len;
724 const void *data;
725
726 /* Get image type name from property data */
727 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
728 if (data == NULL) {
729 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
730 *type = -1;
731 return -1;
732 }
733
734 /* Translate image type name to id */
735 *type = genimg_get_type_id(data);
736 return 0;
737}
738
739/**
740 * fit_image_get_comp - get comp id for a given component image node
741 * @fit: pointer to the FIT format image header
742 * @noffset: component image node offset
743 * @comp: pointer to the uint8_t, will hold comp numeric id
744 *
745 * fit_image_get_comp() finds comp property in a given component image node.
746 * If the property is found, its (string) value is translated to the numeric
747 * id which is returned to the caller.
748 *
749 * returns:
750 * 0, on success
751 * -1, on failure
752 */
753int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
754{
755 int len;
756 const void *data;
757
758 /* Get compression name from property data */
759 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
760 if (data == NULL) {
761 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
762 *comp = -1;
763 return -1;
764 }
765
766 /* Translate compression name to id */
767 *comp = genimg_get_comp_id(data);
768 return 0;
769}
770
York Sun60047652016-02-29 15:48:40 -0800771static int fit_image_get_address(const void *fit, int noffset, char *name,
772 ulong *load)
773{
York Sunc1913cb2016-02-29 15:48:41 -0800774 int len, cell_len;
775 const fdt32_t *cell;
776 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800777
York Sunc1913cb2016-02-29 15:48:41 -0800778 cell = fdt_getprop(fit, noffset, name, &len);
779 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800780 fit_get_debug(fit, noffset, name, len);
781 return -1;
782 }
783
York Sunc1913cb2016-02-29 15:48:41 -0800784 if (len > sizeof(ulong)) {
785 printf("Unsupported %s address size\n", name);
786 return -1;
787 }
788
789 cell_len = len >> 2;
790 /* Use load64 to avoid compiling warning for 32-bit target */
791 while (cell_len--) {
792 load64 = (load64 << 32) | uimage_to_cpu(*cell);
793 cell++;
794 }
795 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800796
797 return 0;
798}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000799/**
800 * fit_image_get_load() - get load addr property for given component image node
801 * @fit: pointer to the FIT format image header
802 * @noffset: component image node offset
803 * @load: pointer to the uint32_t, will hold load address
804 *
805 * fit_image_get_load() finds load address property in a given component
806 * image node. If the property is found, its value is returned to the caller.
807 *
808 * returns:
809 * 0, on success
810 * -1, on failure
811 */
812int fit_image_get_load(const void *fit, int noffset, ulong *load)
813{
York Sun60047652016-02-29 15:48:40 -0800814 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000815}
816
817/**
818 * fit_image_get_entry() - get entry point address property
819 * @fit: pointer to the FIT format image header
820 * @noffset: component image node offset
821 * @entry: pointer to the uint32_t, will hold entry point address
822 *
823 * This gets the entry point address property for a given component image
824 * node.
825 *
826 * fit_image_get_entry() finds entry point address property in a given
827 * component image node. If the property is found, its value is returned
828 * to the caller.
829 *
830 * returns:
831 * 0, on success
832 * -1, on failure
833 */
834int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
835{
York Sun60047652016-02-29 15:48:40 -0800836 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000837}
838
839/**
840 * fit_image_get_data - get data property and its size for a given component image node
841 * @fit: pointer to the FIT format image header
842 * @noffset: component image node offset
843 * @data: double pointer to void, will hold data property's data address
844 * @size: pointer to size_t, will hold data property's data size
845 *
846 * fit_image_get_data() finds data property in a given component image node.
847 * If the property is found its data start address and size are returned to
848 * the caller.
849 *
850 * returns:
851 * 0, on success
852 * -1, on failure
853 */
854int fit_image_get_data(const void *fit, int noffset,
855 const void **data, size_t *size)
856{
857 int len;
858
859 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
860 if (*data == NULL) {
861 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
862 *size = 0;
863 return -1;
864 }
865
866 *size = len;
867 return 0;
868}
869
870/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200871 * Get 'data-offset' property from a given image node.
872 *
873 * @fit: pointer to the FIT image header
874 * @noffset: component image node offset
875 * @data_offset: holds the data-offset property
876 *
877 * returns:
878 * 0, on success
879 * -ENOENT if the property could not be found
880 */
881int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
882{
883 const fdt32_t *val;
884
885 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
886 if (!val)
887 return -ENOENT;
888
889 *data_offset = fdt32_to_cpu(*val);
890
891 return 0;
892}
893
894/**
Peng Fana1be94b2017-12-05 13:20:59 +0800895 * Get 'data-position' property from a given image node.
896 *
897 * @fit: pointer to the FIT image header
898 * @noffset: component image node offset
899 * @data_position: holds the data-position property
900 *
901 * returns:
902 * 0, on success
903 * -ENOENT if the property could not be found
904 */
905int fit_image_get_data_position(const void *fit, int noffset,
906 int *data_position)
907{
908 const fdt32_t *val;
909
910 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
911 if (!val)
912 return -ENOENT;
913
914 *data_position = fdt32_to_cpu(*val);
915
916 return 0;
917}
918
919/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200920 * Get 'data-size' property from a given image node.
921 *
922 * @fit: pointer to the FIT image header
923 * @noffset: component image node offset
924 * @data_size: holds the data-size property
925 *
926 * returns:
927 * 0, on success
928 * -ENOENT if the property could not be found
929 */
930int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
931{
932 const fdt32_t *val;
933
934 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
935 if (!val)
936 return -ENOENT;
937
938 *data_size = fdt32_to_cpu(*val);
939
940 return 0;
941}
942
943/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000944 * fit_image_hash_get_algo - get hash algorithm name
945 * @fit: pointer to the FIT format image header
946 * @noffset: hash node offset
947 * @algo: double pointer to char, will hold pointer to the algorithm name
948 *
949 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
950 * If the property is found its data start address is returned to the caller.
951 *
952 * returns:
953 * 0, on success
954 * -1, on failure
955 */
956int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
957{
958 int len;
959
960 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
961 if (*algo == NULL) {
962 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
963 return -1;
964 }
965
966 return 0;
967}
968
969/**
970 * fit_image_hash_get_value - get hash value and length
971 * @fit: pointer to the FIT format image header
972 * @noffset: hash node offset
973 * @value: double pointer to uint8_t, will hold address of a hash value data
974 * @value_len: pointer to an int, will hold hash data length
975 *
976 * fit_image_hash_get_value() finds hash value property in a given hash node.
977 * If the property is found its data start address and size are returned to
978 * the caller.
979 *
980 * returns:
981 * 0, on success
982 * -1, on failure
983 */
984int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
985 int *value_len)
986{
987 int len;
988
989 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
990 if (*value == NULL) {
991 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
992 *value_len = 0;
993 return -1;
994 }
995
996 *value_len = len;
997 return 0;
998}
999
Simon Glass53fbb7e2013-05-07 06:11:53 +00001000/**
1001 * fit_image_hash_get_ignore - get hash ignore flag
1002 * @fit: pointer to the FIT format image header
1003 * @noffset: hash node offset
1004 * @ignore: pointer to an int, will hold hash ignore flag
1005 *
1006 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1007 * If the property is found and non-zero, the hash algorithm is not verified by
1008 * u-boot automatically.
1009 *
1010 * returns:
1011 * 0, on ignore not found
1012 * value, on ignore found
1013 */
Simon Glassab9efc62013-05-07 06:11:58 +00001014static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001015{
1016 int len;
1017 int *value;
1018
1019 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1020 if (value == NULL || len != sizeof(int))
1021 *ignore = 0;
1022 else
1023 *ignore = *value;
1024
1025 return 0;
1026}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001027
Simon Glass7a80de42016-02-24 09:14:42 -07001028ulong fit_get_end(const void *fit)
1029{
1030 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1031}
1032
Simon Glass53fbb7e2013-05-07 06:11:53 +00001033/**
1034 * fit_set_timestamp - set node timestamp property
1035 * @fit: pointer to the FIT format image header
1036 * @noffset: node offset
1037 * @timestamp: timestamp value to be set
1038 *
1039 * fit_set_timestamp() attempts to set timestamp property in the requested
1040 * node and returns operation status to the caller.
1041 *
1042 * returns:
1043 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001044 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001045 */
1046int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1047{
1048 uint32_t t;
1049 int ret;
1050
1051 t = cpu_to_uimage(timestamp);
1052 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1053 sizeof(uint32_t));
1054 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001055 debug("Can't set '%s' property for '%s' node (%s)\n",
1056 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1057 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001058 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001059 }
1060
1061 return 0;
1062}
1063
1064/**
1065 * calculate_hash - calculate and return hash for provided input data
1066 * @data: pointer to the input data
1067 * @data_len: data length
1068 * @algo: requested hash algorithm
1069 * @value: pointer to the char, will hold hash value data (caller must
1070 * allocate enough free space)
1071 * value_len: length of the calculated hash
1072 *
1073 * calculate_hash() computes input data hash according to the requested
1074 * algorithm.
1075 * Resulting hash value is placed in caller provided 'value' buffer, length
1076 * of the calculated hash is returned via value_len pointer argument.
1077 *
1078 * returns:
1079 * 0, on success
1080 * -1, when algo is unsupported
1081 */
Simon Glass604f23d2013-05-07 06:11:54 +00001082int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001083 uint8_t *value, int *value_len)
1084{
Simon Glass87ebee32013-05-08 08:05:59 +00001085 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001086 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1087 CHUNKSZ_CRC32);
1088 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1089 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001090 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001091 sha1_csum_wd((unsigned char *)data, data_len,
1092 (unsigned char *)value, CHUNKSZ_SHA1);
1093 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001094 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1095 sha256_csum_wd((unsigned char *)data, data_len,
1096 (unsigned char *)value, CHUNKSZ_SHA256);
1097 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001098 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001099 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1100 *value_len = 16;
1101 } else {
1102 debug("Unsupported hash alogrithm\n");
1103 return -1;
1104 }
1105 return 0;
1106}
1107
Simon Glassab9efc62013-05-07 06:11:58 +00001108static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1109 size_t size, char **err_msgp)
1110{
1111 uint8_t value[FIT_MAX_HASH_LEN];
1112 int value_len;
1113 char *algo;
1114 uint8_t *fit_value;
1115 int fit_value_len;
1116 int ignore;
1117
1118 *err_msgp = NULL;
1119
1120 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001121 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001122 return -1;
1123 }
1124 printf("%s", algo);
1125
1126 if (IMAGE_ENABLE_IGNORE) {
1127 fit_image_hash_get_ignore(fit, noffset, &ignore);
1128 if (ignore) {
1129 printf("-skipped ");
1130 return 0;
1131 }
1132 }
1133
1134 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1135 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001136 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001137 return -1;
1138 }
1139
1140 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001141 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001142 return -1;
1143 }
1144
1145 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001146 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001147 return -1;
1148 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001149 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001150 return -1;
1151 }
1152
1153 return 0;
1154}
1155
Jun Nie5c643db2018-02-27 16:55:58 +08001156int fit_image_verify_with_data(const void *fit, int image_noffset,
1157 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001158{
Simon Glass56518e72013-06-13 15:10:01 -07001159 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001160 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001161 int verify_all = 1;
1162 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001163
Simon Glass56518e72013-06-13 15:10:01 -07001164 /* Verify all required signatures */
1165 if (IMAGE_ENABLE_VERIFY &&
1166 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1167 gd_fdt_blob(), &verify_all)) {
1168 err_msg = "Unable to verify required signature";
1169 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001170 }
1171
1172 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001173 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001174 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001175
Simon Glassab9efc62013-05-07 06:11:58 +00001176 /*
1177 * Check subnode name, must be equal to "hash".
1178 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001179 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001180 */
1181 if (!strncmp(name, FIT_HASH_NODENAME,
1182 strlen(FIT_HASH_NODENAME))) {
1183 if (fit_image_check_hash(fit, noffset, data, size,
1184 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001185 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001186 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001187 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1188 !strncmp(name, FIT_SIG_NODENAME,
1189 strlen(FIT_SIG_NODENAME))) {
1190 ret = fit_image_check_sig(fit, noffset, data,
1191 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001192
1193 /*
1194 * Show an indication on failure, but do not return
1195 * an error. Only keys marked 'required' can cause
1196 * an image validation failure. See the call to
1197 * fit_image_verify_required_sigs() above.
1198 */
1199 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001200 puts("- ");
1201 else
1202 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001203 }
1204 }
1205
1206 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001207 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001208 goto error;
1209 }
1210
1211 return 1;
1212
1213error:
Simon Glasse754da22013-05-07 06:11:59 +00001214 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001215 err_msg, fit_get_name(fit, noffset, NULL),
1216 fit_get_name(fit, image_noffset, NULL));
1217 return 0;
1218}
1219
1220/**
Jun Nie5c643db2018-02-27 16:55:58 +08001221 * fit_image_verify - verify data integrity
1222 * @fit: pointer to the FIT format image header
1223 * @image_noffset: component image node offset
1224 *
1225 * fit_image_verify() goes over component image hash nodes,
1226 * re-calculates each data hash and compares with the value stored in hash
1227 * node.
1228 *
1229 * returns:
1230 * 1, if all hashes are valid
1231 * 0, otherwise (or on error)
1232 */
1233int fit_image_verify(const void *fit, int image_noffset)
1234{
1235 const void *data;
1236 size_t size;
1237 int noffset = 0;
1238 char *err_msg = "";
1239
1240 /* Get image data and data length */
1241 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1242 err_msg = "Can't get image data/size";
1243 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1244 err_msg, fit_get_name(fit, noffset, NULL),
1245 fit_get_name(fit, image_noffset, NULL));
1246 return 0;
1247 }
1248
1249 return fit_image_verify_with_data(fit, image_noffset, data, size);
1250}
1251
1252/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001253 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001254 * @fit: pointer to the FIT format image header
1255 *
Simon Glassb8da8362013-05-07 06:11:57 +00001256 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001257 * for every images checks if all it's hashes are valid.
1258 *
1259 * returns:
1260 * 1, if all hashes of all images are valid
1261 * 0, otherwise (or on error)
1262 */
Simon Glassb8da8362013-05-07 06:11:57 +00001263int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001264{
1265 int images_noffset;
1266 int noffset;
1267 int ndepth;
1268 int count;
1269
1270 /* Find images parent node offset */
1271 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1272 if (images_noffset < 0) {
1273 printf("Can't find images parent node '%s' (%s)\n",
1274 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1275 return 0;
1276 }
1277
1278 /* Process all image subnodes, check hashes for each */
1279 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1280 (ulong)fit);
1281 for (ndepth = 0, count = 0,
1282 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1283 (noffset >= 0) && (ndepth > 0);
1284 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1285 if (ndepth == 1) {
1286 /*
1287 * Direct child node of the images parent node,
1288 * i.e. component image node.
1289 */
Simon Glass73223f02016-02-22 22:55:43 -07001290 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001291 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001292 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001293
Simon Glassb8da8362013-05-07 06:11:57 +00001294 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001295 return 0;
1296 printf("\n");
1297 }
1298 }
1299 return 1;
1300}
1301
1302/**
1303 * fit_image_check_os - check whether image node is of a given os type
1304 * @fit: pointer to the FIT format image header
1305 * @noffset: component image node offset
1306 * @os: requested image os
1307 *
1308 * fit_image_check_os() reads image os property and compares its numeric
1309 * id with the requested os. Comparison result is returned to the caller.
1310 *
1311 * returns:
1312 * 1 if image is of given os type
1313 * 0 otherwise (or on error)
1314 */
1315int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1316{
1317 uint8_t image_os;
1318
1319 if (fit_image_get_os(fit, noffset, &image_os))
1320 return 0;
1321 return (os == image_os);
1322}
1323
1324/**
1325 * fit_image_check_arch - check whether image node is of a given arch
1326 * @fit: pointer to the FIT format image header
1327 * @noffset: component image node offset
1328 * @arch: requested imagearch
1329 *
1330 * fit_image_check_arch() reads image arch property and compares its numeric
1331 * id with the requested arch. Comparison result is returned to the caller.
1332 *
1333 * returns:
1334 * 1 if image is of given arch
1335 * 0 otherwise (or on error)
1336 */
1337int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1338{
1339 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001340 int aarch32_support = 0;
1341
1342#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1343 aarch32_support = 1;
1344#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001345
1346 if (fit_image_get_arch(fit, noffset, &image_arch))
1347 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001348 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001349 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1350 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1351 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001352}
1353
1354/**
1355 * fit_image_check_type - check whether image node is of a given type
1356 * @fit: pointer to the FIT format image header
1357 * @noffset: component image node offset
1358 * @type: requested image type
1359 *
1360 * fit_image_check_type() reads image type property and compares its numeric
1361 * id with the requested type. Comparison result is returned to the caller.
1362 *
1363 * returns:
1364 * 1 if image is of given type
1365 * 0 otherwise (or on error)
1366 */
1367int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1368{
1369 uint8_t image_type;
1370
1371 if (fit_image_get_type(fit, noffset, &image_type))
1372 return 0;
1373 return (type == image_type);
1374}
1375
1376/**
1377 * fit_image_check_comp - check whether image node uses given compression
1378 * @fit: pointer to the FIT format image header
1379 * @noffset: component image node offset
1380 * @comp: requested image compression type
1381 *
1382 * fit_image_check_comp() reads image compression property and compares its
1383 * numeric id with the requested compression type. Comparison result is
1384 * returned to the caller.
1385 *
1386 * returns:
1387 * 1 if image uses requested compression
1388 * 0 otherwise (or on error)
1389 */
1390int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1391{
1392 uint8_t image_comp;
1393
1394 if (fit_image_get_comp(fit, noffset, &image_comp))
1395 return 0;
1396 return (comp == image_comp);
1397}
1398
1399/**
1400 * fit_check_format - sanity check FIT image format
1401 * @fit: pointer to the FIT format image header
1402 *
1403 * fit_check_format() runs a basic sanity FIT image verification.
1404 * Routine checks for mandatory properties, nodes, etc.
1405 *
1406 * returns:
1407 * 1, on success
1408 * 0, on failure
1409 */
1410int fit_check_format(const void *fit)
1411{
1412 /* mandatory / node 'description' property */
1413 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1414 debug("Wrong FIT format: no description\n");
1415 return 0;
1416 }
1417
1418 if (IMAGE_ENABLE_TIMESTAMP) {
1419 /* mandatory / node 'timestamp' property */
1420 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1421 debug("Wrong FIT format: no timestamp\n");
1422 return 0;
1423 }
1424 }
1425
1426 /* mandatory subimages parent '/images' node */
1427 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1428 debug("Wrong FIT format: no images parent node\n");
1429 return 0;
1430 }
1431
1432 return 1;
1433}
1434
1435
1436/**
1437 * fit_conf_find_compat
1438 * @fit: pointer to the FIT format image header
1439 * @fdt: pointer to the device tree to compare against
1440 *
1441 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1442 * most compatible with the passed in device tree.
1443 *
1444 * Example:
1445 *
1446 * / o image-tree
1447 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001448 * | |-o fdt-1
1449 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001450 * |
1451 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001452 * |-o config-1
1453 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001454 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001455 * |-o config-2
1456 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001457 *
1458 * / o U-Boot fdt
1459 * |-compatible = "foo,bar", "bim,bam"
1460 *
1461 * / o kernel fdt1
1462 * |-compatible = "foo,bar",
1463 *
1464 * / o kernel fdt2
1465 * |-compatible = "bim,bam", "baz,biz"
1466 *
1467 * Configuration 1 would be picked because the first string in U-Boot's
1468 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1469 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1470 *
1471 * returns:
1472 * offset to the configuration to use if one was found
1473 * -1 otherwise
1474 */
1475int fit_conf_find_compat(const void *fit, const void *fdt)
1476{
1477 int ndepth = 0;
1478 int noffset, confs_noffset, images_noffset;
1479 const void *fdt_compat;
1480 int fdt_compat_len;
1481 int best_match_offset = 0;
1482 int best_match_pos = 0;
1483
1484 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1485 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1486 if (confs_noffset < 0 || images_noffset < 0) {
1487 debug("Can't find configurations or images nodes.\n");
1488 return -1;
1489 }
1490
1491 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1492 if (!fdt_compat) {
1493 debug("Fdt for comparison has no \"compatible\" property.\n");
1494 return -1;
1495 }
1496
1497 /*
1498 * Loop over the configurations in the FIT image.
1499 */
1500 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1501 (noffset >= 0) && (ndepth > 0);
1502 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1503 const void *kfdt;
1504 const char *kfdt_name;
1505 int kfdt_noffset;
1506 const char *cur_fdt_compat;
1507 int len;
1508 size_t size;
1509 int i;
1510
1511 if (ndepth > 1)
1512 continue;
1513
1514 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1515 if (!kfdt_name) {
1516 debug("No fdt property found.\n");
1517 continue;
1518 }
1519 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1520 kfdt_name);
1521 if (kfdt_noffset < 0) {
1522 debug("No image node named \"%s\" found.\n",
1523 kfdt_name);
1524 continue;
1525 }
1526 /*
1527 * Get a pointer to this configuration's fdt.
1528 */
1529 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1530 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1531 continue;
1532 }
1533
1534 len = fdt_compat_len;
1535 cur_fdt_compat = fdt_compat;
1536 /*
1537 * Look for a match for each U-Boot compatibility string in
1538 * turn in this configuration's fdt.
1539 */
1540 for (i = 0; len > 0 &&
1541 (!best_match_offset || best_match_pos > i); i++) {
1542 int cur_len = strlen(cur_fdt_compat) + 1;
1543
1544 if (!fdt_node_check_compatible(kfdt, 0,
1545 cur_fdt_compat)) {
1546 best_match_offset = noffset;
1547 best_match_pos = i;
1548 break;
1549 }
1550 len -= cur_len;
1551 cur_fdt_compat += cur_len;
1552 }
1553 }
1554 if (!best_match_offset) {
1555 debug("No match found.\n");
1556 return -1;
1557 }
1558
1559 return best_match_offset;
1560}
1561
1562/**
1563 * fit_conf_get_node - get node offset for configuration of a given unit name
1564 * @fit: pointer to the FIT format image header
1565 * @conf_uname: configuration node unit name
1566 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001567 * fit_conf_get_node() finds a configuration (within the '/configurations'
1568 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001569 * offset is returned to the caller.
1570 *
1571 * When NULL is provided in second argument fit_conf_get_node() will search
1572 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001573 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001574 * node.
1575 *
1576 * returns:
1577 * configuration node offset when found (>=0)
1578 * negative number on failure (FDT_ERR_* code)
1579 */
1580int fit_conf_get_node(const void *fit, const char *conf_uname)
1581{
1582 int noffset, confs_noffset;
1583 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001584 const char *s;
1585 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001586
1587 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1588 if (confs_noffset < 0) {
1589 debug("Can't find configurations parent node '%s' (%s)\n",
1590 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1591 return confs_noffset;
1592 }
1593
1594 if (conf_uname == NULL) {
1595 /* get configuration unit name from the default property */
1596 debug("No configuration specified, trying default...\n");
1597 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1598 FIT_DEFAULT_PROP, &len);
1599 if (conf_uname == NULL) {
1600 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1601 len);
1602 return len;
1603 }
1604 debug("Found default configuration: '%s'\n", conf_uname);
1605 }
1606
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001607 s = strchr(conf_uname, '#');
1608 if (s) {
1609 len = s - conf_uname;
1610 conf_uname_copy = malloc(len + 1);
1611 if (!conf_uname_copy) {
1612 debug("Can't allocate uname copy: '%s'\n",
1613 conf_uname);
1614 return -ENOMEM;
1615 }
1616 memcpy(conf_uname_copy, conf_uname, len);
1617 conf_uname_copy[len] = '\0';
1618 conf_uname = conf_uname_copy;
1619 }
1620
Simon Glass53fbb7e2013-05-07 06:11:53 +00001621 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1622 if (noffset < 0) {
1623 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1624 conf_uname, fdt_strerror(noffset));
1625 }
1626
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001627 if (conf_uname_copy)
1628 free(conf_uname_copy);
1629
Simon Glass53fbb7e2013-05-07 06:11:53 +00001630 return noffset;
1631}
1632
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001633int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001634 const char *prop_name)
1635{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001636 return fdt_stringlist_count(fit, noffset, prop_name);
1637}
1638
1639int fit_conf_get_prop_node_index(const void *fit, int noffset,
1640 const char *prop_name, int index)
1641{
1642 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001643 int len;
1644
1645 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001646 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001647 if (uname == NULL)
1648 return len;
1649
1650 return fit_image_get_node(fit, uname);
1651}
1652
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001653int fit_conf_get_prop_node(const void *fit, int noffset,
1654 const char *prop_name)
1655{
1656 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1657}
1658
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001659static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001660{
1661 fit_image_print(fit, rd_noffset, " ");
1662
1663 if (verify) {
1664 puts(" Verifying Hash Integrity ... ");
1665 if (!fit_image_verify(fit, rd_noffset)) {
1666 puts("Bad Data Hash\n");
1667 return -EACCES;
1668 }
1669 puts("OK\n");
1670 }
1671
1672 return 0;
1673}
1674
Simon Glass782cfbb2013-05-16 13:53:21 +00001675int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1676 ulong addr)
1677{
1678 int cfg_noffset;
1679 void *fit_hdr;
1680 int noffset;
1681
1682 debug("* %s: using config '%s' from image at 0x%08lx\n",
1683 prop_name, images->fit_uname_cfg, addr);
1684
1685 /* Check whether configuration has this property defined */
1686 fit_hdr = map_sysmem(addr, 0);
1687 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1688 if (cfg_noffset < 0) {
1689 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001690 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001691 }
1692
1693 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1694 if (noffset < 0) {
1695 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001696 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001697 }
1698
1699 return noffset;
1700}
1701
Simon Glass126cc862014-06-12 07:24:47 -06001702/**
1703 * fit_get_image_type_property() - get property name for IH_TYPE_...
1704 *
1705 * @return the properly name where we expect to find the image in the
1706 * config node
1707 */
1708static const char *fit_get_image_type_property(int type)
1709{
1710 /*
1711 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001712 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001713 * anyway. So let's just keep it here.
1714 */
1715 switch (type) {
1716 case IH_TYPE_FLATDT:
1717 return FIT_FDT_PROP;
1718 case IH_TYPE_KERNEL:
1719 return FIT_KERNEL_PROP;
1720 case IH_TYPE_RAMDISK:
1721 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001722 case IH_TYPE_X86_SETUP:
1723 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001724 case IH_TYPE_LOADABLE:
1725 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001726 case IH_TYPE_FPGA:
1727 return FIT_FPGA_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001728 }
1729
1730 return "unknown";
1731}
1732
1733int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001734 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001735 int arch, int image_type, int bootstage_id,
1736 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1737{
1738 int cfg_noffset, noffset;
1739 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001740 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001741 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001742 const void *fit;
1743 const void *buf;
1744 size_t size;
1745 int type_ok, os_ok;
1746 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001747 uint8_t os;
Alison Wangec6617c2016-11-10 10:49:03 +08001748#ifndef USE_HOSTCC
1749 uint8_t os_arch;
1750#endif
Simon Glass126cc862014-06-12 07:24:47 -06001751 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001752 int ret;
1753
1754 fit = map_sysmem(addr, 0);
1755 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001756 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001757 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001758 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001759 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1760
1761 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1762 if (!fit_check_format(fit)) {
1763 printf("Bad FIT %s image format!\n", prop_name);
1764 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1765 return -ENOEXEC;
1766 }
1767 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1768 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001769 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001770 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1771 noffset = fit_image_get_node(fit, fit_uname);
1772 } else {
1773 /*
1774 * no image node unit name, try to get config
1775 * node first. If config unit node name is NULL
1776 * fit_conf_get_node() will try to find default config node
1777 */
1778 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1779 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1780 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1781 } else {
1782 cfg_noffset = fit_conf_get_node(fit,
1783 fit_uname_config);
1784 }
1785 if (cfg_noffset < 0) {
1786 puts("Could not find configuration node\n");
1787 bootstage_error(bootstage_id +
1788 BOOTSTAGE_SUB_NO_UNIT_NAME);
1789 return -ENOENT;
1790 }
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001791 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1792 printf(" Using '%s' configuration\n", fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001793 if (image_type == IH_TYPE_KERNEL) {
1794 /* Remember (and possibly verify) this config */
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001795 images->fit_uname_cfg = fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001796 if (IMAGE_ENABLE_VERIFY && images->verify) {
1797 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001798 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001799 puts("Bad Data Hash\n");
1800 bootstage_error(bootstage_id +
1801 BOOTSTAGE_SUB_HASH);
1802 return -EACCES;
1803 }
1804 puts("OK\n");
1805 }
1806 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1807 }
1808
1809 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1810 prop_name);
1811 fit_uname = fit_get_name(fit, noffset, NULL);
1812 }
1813 if (noffset < 0) {
1814 puts("Could not find subimage node\n");
1815 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1816 return -ENOENT;
1817 }
1818
1819 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1820
1821 ret = fit_image_select(fit, noffset, images->verify);
1822 if (ret) {
1823 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1824 return ret;
1825 }
1826
1827 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001828#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001829 if (!fit_image_check_target_arch(fit, noffset)) {
1830 puts("Unsupported Architecture\n");
1831 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1832 return -ENOEXEC;
1833 }
Simon Glassce1400f2014-06-12 07:24:53 -06001834#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001835
1836#ifndef USE_HOSTCC
1837 fit_image_get_arch(fit, noffset, &os_arch);
1838 images->os.arch = os_arch;
1839#endif
1840
Simon Glass782cfbb2013-05-16 13:53:21 +00001841 if (image_type == IH_TYPE_FLATDT &&
1842 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1843 puts("FDT image is compressed");
1844 return -EPROTONOSUPPORT;
1845 }
1846
1847 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1848 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001849 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1850 (image_type == IH_TYPE_KERNEL &&
1851 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001852
Andreas Bießmann950fe262016-08-14 20:31:24 +02001853 os_ok = image_type == IH_TYPE_FLATDT ||
1854 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001855 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001856 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001857 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001858
1859 /*
1860 * If either of the checks fail, we should report an error, but
1861 * if the image type is coming from the "loadables" field, we
1862 * don't care what it is
1863 */
1864 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001865 fit_image_get_os(fit, noffset, &os);
1866 printf("No %s %s %s Image\n",
1867 genimg_get_os_name(os),
1868 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001869 genimg_get_type_name(image_type));
1870 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1871 return -EIO;
1872 }
1873
1874 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1875
1876 /* get image data address and length */
1877 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1878 printf("Could not find %s subimage data!\n", prop_name);
1879 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001880 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001881 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06001882
1883#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1884 /* perform any post-processing on the image data */
1885 board_fit_image_post_process((void **)&buf, &size);
1886#endif
1887
Simon Glass782cfbb2013-05-16 13:53:21 +00001888 len = (ulong)size;
1889
1890 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001891 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001892 puts("Subimage data is not a FDT");
1893 return -ENOEXEC;
1894 }
1895
1896 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1897
1898 /*
1899 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001900 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001901 * warning: initialization discards qualifiers from pointer target type
1902 */
1903 {
1904 void *vbuf = (void *)buf;
1905
1906 data = map_to_sysmem(vbuf);
1907 }
1908
1909 if (load_op == FIT_LOAD_IGNORED) {
1910 /* Don't load */
1911 } else if (fit_image_get_load(fit, noffset, &load)) {
1912 if (load_op == FIT_LOAD_REQUIRED) {
1913 printf("Can't get %s subimage load address!\n",
1914 prop_name);
1915 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1916 return -EBADF;
1917 }
Simon Glassfe20a812014-08-22 14:26:43 -06001918 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001919 ulong image_start, image_end;
1920 ulong load_end;
1921 void *dst;
1922
1923 /*
1924 * move image data to the load address,
1925 * make sure we don't overwrite initial image
1926 */
1927 image_start = addr;
1928 image_end = addr + fit_get_size(fit);
1929
1930 load_end = load + len;
1931 if (image_type != IH_TYPE_KERNEL &&
1932 load < image_end && load_end > image_start) {
1933 printf("Error: %s overwritten\n", prop_name);
1934 return -EXDEV;
1935 }
1936
1937 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1938 prop_name, data, load);
1939
1940 dst = map_sysmem(load, len);
1941 memmove(dst, buf, len);
1942 data = load;
1943 }
1944 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1945
1946 *datap = data;
1947 *lenp = len;
1948 if (fit_unamep)
1949 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001950 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001951 *fit_uname_configp = (char *)(fit_uname_config ? :
1952 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001953
1954 return noffset;
1955}
Simon Glass90268b82014-10-19 21:11:24 -06001956
1957int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1958 ulong *setup_start, ulong *setup_len)
1959{
1960 int noffset;
1961 ulong addr;
1962 ulong len;
1963 int ret;
1964
1965 addr = map_to_sysmem(images->fit_hdr_os);
1966 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1967 if (noffset < 0)
1968 return noffset;
1969
1970 ret = fit_image_load(images, addr, NULL, NULL, arch,
1971 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1972 FIT_LOAD_REQUIRED, setup_start, &len);
1973
1974 return ret;
1975}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001976
1977#ifndef USE_HOSTCC
1978int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1979 const char **fit_unamep, const char **fit_uname_configp,
1980 int arch, ulong *datap, ulong *lenp)
1981{
1982 int fdt_noffset, cfg_noffset, count;
1983 const void *fit;
1984 const char *fit_uname = NULL;
1985 const char *fit_uname_config = NULL;
1986 char *fit_uname_config_copy = NULL;
1987 char *next_config = NULL;
1988 ulong load, len;
1989#ifdef CONFIG_OF_LIBFDT_OVERLAY
1990 ulong image_start, image_end;
1991 ulong ovload, ovlen;
1992 const char *uconfig;
1993 const char *uname;
1994 void *base, *ov;
1995 int i, err, noffset, ov_noffset;
1996#endif
1997
1998 fit_uname = fit_unamep ? *fit_unamep : NULL;
1999
2000 if (fit_uname_configp && *fit_uname_configp) {
2001 fit_uname_config_copy = strdup(*fit_uname_configp);
2002 if (!fit_uname_config_copy)
2003 return -ENOMEM;
2004
2005 next_config = strchr(fit_uname_config_copy, '#');
2006 if (next_config)
2007 *next_config++ = '\0';
2008 if (next_config - 1 > fit_uname_config_copy)
2009 fit_uname_config = fit_uname_config_copy;
2010 }
2011
2012 fdt_noffset = fit_image_load(images,
2013 addr, &fit_uname, &fit_uname_config,
2014 arch, IH_TYPE_FLATDT,
2015 BOOTSTAGE_ID_FIT_FDT_START,
2016 FIT_LOAD_OPTIONAL, &load, &len);
2017
2018 if (fdt_noffset < 0)
2019 goto out;
2020
2021 debug("fit_uname=%s, fit_uname_config=%s\n",
2022 fit_uname ? fit_uname : "<NULL>",
2023 fit_uname_config ? fit_uname_config : "<NULL>");
2024
2025 fit = map_sysmem(addr, 0);
2026
2027 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2028
2029 /* single blob, or error just return as well */
2030 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2031 if (count <= 1 && !next_config)
2032 goto out;
2033
2034 /* we need to apply overlays */
2035
2036#ifdef CONFIG_OF_LIBFDT_OVERLAY
2037 image_start = addr;
2038 image_end = addr + fit_get_size(fit);
2039 /* verify that relocation took place by load address not being in fit */
2040 if (load >= image_start && load < image_end) {
2041 /* check is simplified; fit load checks for overlaps */
2042 printf("Overlayed FDT requires relocation\n");
2043 fdt_noffset = -EBADF;
2044 goto out;
2045 }
2046
2047 base = map_sysmem(load, len);
2048
2049 /* apply extra configs in FIT first, followed by args */
2050 for (i = 1; ; i++) {
2051 if (i < count) {
2052 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2053 FIT_FDT_PROP, i);
2054 uname = fit_get_name(fit, noffset, NULL);
2055 uconfig = NULL;
2056 } else {
2057 if (!next_config)
2058 break;
2059 uconfig = next_config;
2060 next_config = strchr(next_config, '#');
2061 if (next_config)
2062 *next_config++ = '\0';
2063 uname = NULL;
2064 }
2065
2066 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2067
2068 ov_noffset = fit_image_load(images,
2069 addr, &uname, &uconfig,
2070 arch, IH_TYPE_FLATDT,
2071 BOOTSTAGE_ID_FIT_FDT_START,
2072 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2073 if (ov_noffset < 0) {
2074 printf("load of %s failed\n", uname);
2075 continue;
2076 }
2077 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2078 uname, ovload, ovlen);
2079 ov = map_sysmem(ovload, ovlen);
2080
2081 base = map_sysmem(load, len + ovlen);
2082 err = fdt_open_into(base, base, len + ovlen);
2083 if (err < 0) {
2084 printf("failed on fdt_open_into\n");
2085 fdt_noffset = err;
2086 goto out;
2087 }
2088 /* the verbose method prints out messages on error */
2089 err = fdt_overlay_apply_verbose(base, ov);
2090 if (err < 0) {
2091 fdt_noffset = err;
2092 goto out;
2093 }
2094 fdt_pack(base);
2095 len = fdt_totalsize(base);
2096 }
2097#else
2098 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2099 fdt_noffset = -EBADF;
2100#endif
2101
2102out:
2103 if (datap)
2104 *datap = load;
2105 if (lenp)
2106 *lenp = len;
2107 if (fit_unamep)
2108 *fit_unamep = fit_uname;
2109 if (fit_uname_configp)
2110 *fit_uname_configp = fit_uname_config;
2111
2112 if (fit_uname_config_copy)
2113 free(fit_uname_config_copy);
2114 return fdt_noffset;
2115}
2116#endif