blob: 33210ef3c03400dfaab1b9ea9bbb31e26c9e5c9c [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>
Simon Glass4d72caa2020-05-10 11:40:01 -060014#include <linux/libfdt.h>
Simon Glass3db71102019-11-14 12:57:16 -070015#include <u-boot/crc.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000016#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050017#include <linux/compiler.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000018#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000019#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050021#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000022#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030023#include <malloc.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000024DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000025#endif /* !USE_HOSTCC*/
26
Julius Wernerb1307f82019-07-24 19:37:55 -070027#include <bootm.h>
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050028#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000029#include <bootstage.h>
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +010030#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000031#include <u-boot/crc.h>
32#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020033#include <u-boot/sha1.h>
34#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120035#include <u-boot/sha512.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000036
37/*****************************************************************************/
38/* New uImage format routines */
39/*****************************************************************************/
40#ifndef USE_HOSTCC
41static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
42 ulong *addr, const char **name)
43{
44 const char *sep;
45
46 *addr = addr_curr;
47 *name = NULL;
48
49 sep = strchr(spec, sepc);
50 if (sep) {
51 if (sep - spec > 0)
52 *addr = simple_strtoul(spec, NULL, 16);
53
54 *name = sep + 1;
55 return 1;
56 }
57
58 return 0;
59}
60
61/**
62 * fit_parse_conf - parse FIT configuration spec
63 * @spec: input string, containing configuration spec
64 * @add_curr: current image address (to be used as a possible default)
65 * @addr: pointer to a ulong variable, will hold FIT image address of a given
66 * configuration
67 * @conf_name double pointer to a char, will hold pointer to a configuration
68 * unit name
69 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090070 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000071 * where <addr> is a FIT image address that contains configuration
72 * with a <conf> unit name.
73 *
74 * Address part is optional, and if omitted default add_curr will
75 * be used instead.
76 *
77 * returns:
78 * 1 if spec is a valid configuration string,
79 * addr and conf_name are set accordingly
80 * 0 otherwise
81 */
82int fit_parse_conf(const char *spec, ulong addr_curr,
83 ulong *addr, const char **conf_name)
84{
85 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
86}
87
88/**
89 * fit_parse_subimage - parse FIT subimage spec
90 * @spec: input string, containing subimage spec
91 * @add_curr: current image address (to be used as a possible default)
92 * @addr: pointer to a ulong variable, will hold FIT image address of a given
93 * subimage
94 * @image_name: double pointer to a char, will hold pointer to a subimage name
95 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090096 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000097 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
98 * subimage with a <subimg> unit name.
99 *
100 * Address part is optional, and if omitted default add_curr will
101 * be used instead.
102 *
103 * returns:
104 * 1 if spec is a valid subimage string,
105 * addr and image_name are set accordingly
106 * 0 otherwise
107 */
108int fit_parse_subimage(const char *spec, ulong addr_curr,
109 ulong *addr, const char **image_name)
110{
111 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
112}
113#endif /* !USE_HOSTCC */
114
Joel Stanley93af80f2020-12-08 14:42:14 +1030115#ifdef USE_HOSTCC
116/* Host tools use these implementations for Cipher and Signature support */
117static void *host_blob;
118
119void image_set_host_blob(void *blob)
120{
121 host_blob = blob;
122}
123
124void *image_get_host_blob(void)
125{
126 return host_blob;
127}
128#endif /* USE_HOSTCC */
129
Simon Glass53fbb7e2013-05-07 06:11:53 +0000130static void fit_get_debug(const void *fit, int noffset,
131 char *prop_name, int err)
132{
133 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
134 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
135 fdt_strerror(err));
136}
137
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200138/**
139 * fit_get_subimage_count - get component (sub-image) count
140 * @fit: pointer to the FIT format image header
141 * @images_noffset: offset of images node
142 *
143 * returns:
144 * number of image components
145 */
146int fit_get_subimage_count(const void *fit, int images_noffset)
147{
148 int noffset;
149 int ndepth;
150 int count = 0;
151
152 /* Process its subnodes, print out component images details */
153 for (ndepth = 0, count = 0,
154 noffset = fdt_next_node(fit, images_noffset, &ndepth);
155 (noffset >= 0) && (ndepth > 0);
156 noffset = fdt_next_node(fit, noffset, &ndepth)) {
157 if (ndepth == 1) {
158 count++;
159 }
160 }
161
162 return count;
163}
164
Ravik Hasija7a018822021-01-27 14:01:48 -0800165#if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000166/**
Tom Rini16c4b162018-05-08 14:34:05 -0400167 * fit_image_print_data() - prints out the hash node details
168 * @fit: pointer to the FIT format image header
169 * @noffset: offset of the hash node
170 * @p: pointer to prefix string
171 * @type: Type of information to print ("hash" or "sign")
172 *
173 * fit_image_print_data() lists properties for the processed hash node
174 *
175 * This function avoid using puts() since it prints a newline on the host
176 * but does not in U-Boot.
177 *
178 * returns:
179 * no returned results
180 */
181static void fit_image_print_data(const void *fit, int noffset, const char *p,
182 const char *type)
183{
184 const char *keyname;
185 uint8_t *value;
186 int value_len;
187 char *algo;
Philippe Reynes20031562018-11-14 13:51:00 +0100188 const char *padding;
Simon Glass72188f52020-03-18 11:44:06 -0600189 bool required;
Tom Rini16c4b162018-05-08 14:34:05 -0400190 int ret, i;
191
192 debug("%s %s node: '%s'\n", p, type,
193 fit_get_name(fit, noffset, NULL));
194 printf("%s %s algo: ", p, type);
195 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
196 printf("invalid/unsupported\n");
197 return;
198 }
199 printf("%s", algo);
Simon Glass72188f52020-03-18 11:44:06 -0600200 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
201 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
Tom Rini16c4b162018-05-08 14:34:05 -0400202 if (keyname)
203 printf(":%s", keyname);
204 if (required)
205 printf(" (required)");
206 printf("\n");
207
Philippe Reynes20031562018-11-14 13:51:00 +0100208 padding = fdt_getprop(fit, noffset, "padding", NULL);
209 if (padding)
210 printf("%s %s padding: %s\n", p, type, padding);
211
Tom Rini16c4b162018-05-08 14:34:05 -0400212 ret = fit_image_hash_get_value(fit, noffset, &value,
213 &value_len);
214 printf("%s %s value: ", p, type);
215 if (ret) {
216 printf("unavailable\n");
217 } else {
218 for (i = 0; i < value_len; i++)
219 printf("%02x", value[i]);
220 printf("\n");
221 }
222
223 debug("%s %s len: %d\n", p, type, value_len);
224
225 /* Signatures have a time stamp */
226 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
227 time_t timestamp;
228
229 printf("%s Timestamp: ", p);
230 if (fit_get_timestamp(fit, noffset, &timestamp))
231 printf("unavailable\n");
232 else
233 genimg_print_time(timestamp);
234 }
235}
236
237/**
238 * fit_image_print_verification_data() - prints out the hash/signature details
239 * @fit: pointer to the FIT format image header
240 * @noffset: offset of the hash or signature node
241 * @p: pointer to prefix string
242 *
243 * This lists properties for the processed hash node
244 *
245 * returns:
246 * no returned results
247 */
248static void fit_image_print_verification_data(const void *fit, int noffset,
249 const char *p)
250{
251 const char *name;
252
253 /*
254 * Check subnode name, must be equal to "hash" or "signature".
255 * Multiple hash/signature nodes require unique unit node
256 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
257 */
258 name = fit_get_name(fit, noffset, NULL);
259 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
260 fit_image_print_data(fit, noffset, p, "Hash");
261 } else if (!strncmp(name, FIT_SIG_NODENAME,
262 strlen(FIT_SIG_NODENAME))) {
263 fit_image_print_data(fit, noffset, p, "Sign");
264 }
265}
266
267/**
268 * fit_conf_print - prints out the FIT configuration details
269 * @fit: pointer to the FIT format image header
270 * @noffset: offset of the configuration node
271 * @p: pointer to prefix string
272 *
273 * fit_conf_print() lists all mandatory properties for the processed
274 * configuration node.
275 *
276 * returns:
277 * no returned results
278 */
279static void fit_conf_print(const void *fit, int noffset, const char *p)
280{
281 char *desc;
282 const char *uname;
283 int ret;
284 int fdt_index, loadables_index;
285 int ndepth;
286
287 /* Mandatory properties */
288 ret = fit_get_desc(fit, noffset, &desc);
289 printf("%s Description: ", p);
290 if (ret)
291 printf("unavailable\n");
292 else
293 printf("%s\n", desc);
294
295 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
296 printf("%s Kernel: ", p);
297 if (!uname)
298 printf("unavailable\n");
299 else
300 printf("%s\n", uname);
301
302 /* Optional properties */
303 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
304 if (uname)
305 printf("%s Init Ramdisk: %s\n", p, uname);
306
307 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
308 if (uname)
309 printf("%s Firmware: %s\n", p, uname);
310
311 for (fdt_index = 0;
312 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
313 fdt_index, NULL), uname;
314 fdt_index++) {
315 if (fdt_index == 0)
316 printf("%s FDT: ", p);
317 else
318 printf("%s ", p);
319 printf("%s\n", uname);
320 }
321
322 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
323 if (uname)
324 printf("%s FPGA: %s\n", p, uname);
325
326 /* Print out all of the specified loadables */
327 for (loadables_index = 0;
328 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
329 loadables_index, NULL), uname;
330 loadables_index++) {
331 if (loadables_index == 0) {
332 printf("%s Loadables: ", p);
333 } else {
334 printf("%s ", p);
335 }
336 printf("%s\n", uname);
337 }
338
339 /* Process all hash subnodes of the component configuration node */
340 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
341 (noffset >= 0) && (ndepth > 0);
342 noffset = fdt_next_node(fit, noffset, &ndepth)) {
343 if (ndepth == 1) {
344 /* Direct child node of the component configuration node */
345 fit_image_print_verification_data(fit, noffset, p);
346 }
347 }
348}
349
350/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000351 * fit_print_contents - prints out the contents of the FIT format image
352 * @fit: pointer to the FIT format image header
353 * @p: pointer to prefix string
354 *
355 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500356 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000357 * the details of each component image.
358 *
359 * returns:
360 * no returned results
361 */
362void fit_print_contents(const void *fit)
363{
364 char *desc;
365 char *uname;
366 int images_noffset;
367 int confs_noffset;
368 int noffset;
369 int ndepth;
370 int count = 0;
371 int ret;
372 const char *p;
373 time_t timestamp;
374
Simon Glass1fe7d932013-05-08 08:05:58 +0000375 /* Indent string is defined in header image.h */
376 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000377
378 /* Root node properties */
379 ret = fit_get_desc(fit, 0, &desc);
380 printf("%sFIT description: ", p);
381 if (ret)
382 printf("unavailable\n");
383 else
384 printf("%s\n", desc);
385
386 if (IMAGE_ENABLE_TIMESTAMP) {
387 ret = fit_get_timestamp(fit, 0, &timestamp);
388 printf("%sCreated: ", p);
389 if (ret)
390 printf("unavailable\n");
391 else
392 genimg_print_time(timestamp);
393 }
394
395 /* Find images parent node offset */
396 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
397 if (images_noffset < 0) {
398 printf("Can't find images parent node '%s' (%s)\n",
399 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
400 return;
401 }
402
403 /* Process its subnodes, print out component images details */
404 for (ndepth = 0, count = 0,
405 noffset = fdt_next_node(fit, images_noffset, &ndepth);
406 (noffset >= 0) && (ndepth > 0);
407 noffset = fdt_next_node(fit, noffset, &ndepth)) {
408 if (ndepth == 1) {
409 /*
410 * Direct child node of the images parent node,
411 * i.e. component image node.
412 */
413 printf("%s Image %u (%s)\n", p, count++,
414 fit_get_name(fit, noffset, NULL));
415
416 fit_image_print(fit, noffset, p);
417 }
418 }
419
420 /* Find configurations parent node offset */
421 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
422 if (confs_noffset < 0) {
423 debug("Can't get configurations parent node '%s' (%s)\n",
424 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
425 return;
426 }
427
428 /* get default configuration unit name from default property */
429 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
430 if (uname)
431 printf("%s Default Configuration: '%s'\n", p, uname);
432
433 /* Process its subnodes, print out configurations details */
434 for (ndepth = 0, count = 0,
435 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
436 (noffset >= 0) && (ndepth > 0);
437 noffset = fdt_next_node(fit, noffset, &ndepth)) {
438 if (ndepth == 1) {
439 /*
440 * Direct child node of the configurations parent node,
441 * i.e. configuration node.
442 */
443 printf("%s Configuration %u (%s)\n", p, count++,
444 fit_get_name(fit, noffset, NULL));
445
446 fit_conf_print(fit, noffset, p);
447 }
448 }
449}
450
451/**
452 * fit_image_print - prints out the FIT component image details
453 * @fit: pointer to the FIT format image header
454 * @image_noffset: offset of the component image node
455 * @p: pointer to prefix string
456 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500457 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000458 * image. If present, hash nodes are printed out as well. Load
459 * address for images of type firmware is also printed out. Since the load
460 * address is not mandatory for firmware images, it will be output as
461 * "unavailable" when not present.
462 *
463 * returns:
464 * no returned results
465 */
466void fit_image_print(const void *fit, int image_noffset, const char *p)
467{
468 char *desc;
469 uint8_t type, arch, os, comp;
470 size_t size;
471 ulong load, entry;
472 const void *data;
473 int noffset;
474 int ndepth;
475 int ret;
476
477 /* Mandatory properties */
478 ret = fit_get_desc(fit, image_noffset, &desc);
479 printf("%s Description: ", p);
480 if (ret)
481 printf("unavailable\n");
482 else
483 printf("%s\n", desc);
484
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700485 if (IMAGE_ENABLE_TIMESTAMP) {
486 time_t timestamp;
487
488 ret = fit_get_timestamp(fit, 0, &timestamp);
489 printf("%s Created: ", p);
490 if (ret)
491 printf("unavailable\n");
492 else
493 genimg_print_time(timestamp);
494 }
495
Simon Glass53fbb7e2013-05-07 06:11:53 +0000496 fit_image_get_type(fit, image_noffset, &type);
497 printf("%s Type: %s\n", p, genimg_get_type_name(type));
498
499 fit_image_get_comp(fit, image_noffset, &comp);
500 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
501
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800502 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000503
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100504 if (!host_build()) {
505 printf("%s Data Start: ", p);
506 if (ret) {
507 printf("unavailable\n");
508 } else {
509 void *vdata = (void *)data;
Simon Glassc6ac13b2013-05-16 13:53:26 +0000510
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100511 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
512 }
Simon Glassc6ac13b2013-05-16 13:53:26 +0000513 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000514
515 printf("%s Data Size: ", p);
516 if (ret)
517 printf("unavailable\n");
518 else
519 genimg_print_size(size);
520
521 /* Remaining, type dependent properties */
522 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
523 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
524 (type == IH_TYPE_FLATDT)) {
525 fit_image_get_arch(fit, image_noffset, &arch);
526 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
527 }
528
Michal Simek6faf4622018-03-26 16:31:27 +0200529 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
530 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000531 fit_image_get_os(fit, image_noffset, &os);
532 printf("%s OS: %s\n", p, genimg_get_os_name(os));
533 }
534
535 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200536 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
537 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000538 ret = fit_image_get_load(fit, image_noffset, &load);
539 printf("%s Load Address: ", p);
540 if (ret)
541 printf("unavailable\n");
542 else
543 printf("0x%08lx\n", load);
544 }
545
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300546 /* optional load address for FDT */
547 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
548 printf("%s Load Address: 0x%08lx\n", p, load);
549
Simon Glass53fbb7e2013-05-07 06:11:53 +0000550 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
551 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800552 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000553 printf("%s Entry Point: ", p);
554 if (ret)
555 printf("unavailable\n");
556 else
557 printf("0x%08lx\n", entry);
558 }
559
560 /* Process all hash subnodes of the component image node */
561 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
562 (noffset >= 0) && (ndepth > 0);
563 noffset = fdt_next_node(fit, noffset, &ndepth)) {
564 if (ndepth == 1) {
565 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000566 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000567 }
568 }
569}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200570#else
571void fit_print_contents(const void *fit) { }
572void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Ravik Hasija7a018822021-01-27 14:01:48 -0800573#endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000574
575/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000576 * fit_get_desc - get node description property
577 * @fit: pointer to the FIT format image header
578 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500579 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000580 *
581 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500582 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000583 *
584 * returns:
585 * 0, on success
586 * -1, on failure
587 */
588int fit_get_desc(const void *fit, int noffset, char **desc)
589{
590 int len;
591
592 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
593 if (*desc == NULL) {
594 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
595 return -1;
596 }
597
598 return 0;
599}
600
601/**
602 * fit_get_timestamp - get node timestamp property
603 * @fit: pointer to the FIT format image header
604 * @noffset: node offset
605 * @timestamp: pointer to the time_t, will hold read timestamp
606 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500607 * fit_get_timestamp() reads timestamp property from given node, if timestamp
608 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000609 * argument.
610 *
611 * returns:
612 * 0, on success
613 * -1, on property read failure
614 * -2, on wrong timestamp size
615 */
616int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
617{
618 int len;
619 const void *data;
620
621 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
622 if (data == NULL) {
623 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
624 return -1;
625 }
626 if (len != sizeof(uint32_t)) {
627 debug("FIT timestamp with incorrect size of (%u)\n", len);
628 return -2;
629 }
630
631 *timestamp = uimage_to_cpu(*((uint32_t *)data));
632 return 0;
633}
634
635/**
636 * fit_image_get_node - get node offset for component image of a given unit name
637 * @fit: pointer to the FIT format image header
638 * @image_uname: component image node unit name
639 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500640 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000641 * node) of a provided unit name. If image is found its node offset is
642 * returned to the caller.
643 *
644 * returns:
645 * image node offset when found (>=0)
646 * negative number on failure (FDT_ERR_* code)
647 */
648int fit_image_get_node(const void *fit, const char *image_uname)
649{
650 int noffset, images_noffset;
651
652 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
653 if (images_noffset < 0) {
654 debug("Can't find images parent node '%s' (%s)\n",
655 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
656 return images_noffset;
657 }
658
659 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
660 if (noffset < 0) {
661 debug("Can't get node offset for image unit name: '%s' (%s)\n",
662 image_uname, fdt_strerror(noffset));
663 }
664
665 return noffset;
666}
667
668/**
669 * fit_image_get_os - get os id for a given component image node
670 * @fit: pointer to the FIT format image header
671 * @noffset: component image node offset
672 * @os: pointer to the uint8_t, will hold os numeric id
673 *
674 * fit_image_get_os() finds os property in a given component image node.
675 * If the property is found, its (string) value is translated to the numeric
676 * id which is returned to the caller.
677 *
678 * returns:
679 * 0, on success
680 * -1, on failure
681 */
682int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
683{
684 int len;
685 const void *data;
686
687 /* Get OS name from property data */
688 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
689 if (data == NULL) {
690 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
691 *os = -1;
692 return -1;
693 }
694
695 /* Translate OS name to id */
696 *os = genimg_get_os_id(data);
697 return 0;
698}
699
700/**
701 * fit_image_get_arch - get arch id for a given component image node
702 * @fit: pointer to the FIT format image header
703 * @noffset: component image node offset
704 * @arch: pointer to the uint8_t, will hold arch numeric id
705 *
706 * fit_image_get_arch() finds arch property in a given component image node.
707 * If the property is found, its (string) value is translated to the numeric
708 * id which is returned to the caller.
709 *
710 * returns:
711 * 0, on success
712 * -1, on failure
713 */
714int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
715{
716 int len;
717 const void *data;
718
719 /* Get architecture name from property data */
720 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
721 if (data == NULL) {
722 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
723 *arch = -1;
724 return -1;
725 }
726
727 /* Translate architecture name to id */
728 *arch = genimg_get_arch_id(data);
729 return 0;
730}
731
732/**
733 * fit_image_get_type - get type id for a given component image node
734 * @fit: pointer to the FIT format image header
735 * @noffset: component image node offset
736 * @type: pointer to the uint8_t, will hold type numeric id
737 *
738 * fit_image_get_type() finds type property in a given component image node.
739 * If the property is found, its (string) value is translated to the numeric
740 * id which is returned to the caller.
741 *
742 * returns:
743 * 0, on success
744 * -1, on failure
745 */
746int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
747{
748 int len;
749 const void *data;
750
751 /* Get image type name from property data */
752 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
753 if (data == NULL) {
754 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
755 *type = -1;
756 return -1;
757 }
758
759 /* Translate image type name to id */
760 *type = genimg_get_type_id(data);
761 return 0;
762}
763
764/**
765 * fit_image_get_comp - get comp id for a given component image node
766 * @fit: pointer to the FIT format image header
767 * @noffset: component image node offset
768 * @comp: pointer to the uint8_t, will hold comp numeric id
769 *
770 * fit_image_get_comp() finds comp property in a given component image node.
771 * If the property is found, its (string) value is translated to the numeric
772 * id which is returned to the caller.
773 *
774 * returns:
775 * 0, on success
776 * -1, on failure
777 */
778int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
779{
780 int len;
781 const void *data;
782
783 /* Get compression name from property data */
784 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
785 if (data == NULL) {
786 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
787 *comp = -1;
788 return -1;
789 }
790
791 /* Translate compression name to id */
792 *comp = genimg_get_comp_id(data);
793 return 0;
794}
795
York Sun60047652016-02-29 15:48:40 -0800796static int fit_image_get_address(const void *fit, int noffset, char *name,
797 ulong *load)
798{
York Sunc1913cb2016-02-29 15:48:41 -0800799 int len, cell_len;
800 const fdt32_t *cell;
801 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800802
York Sunc1913cb2016-02-29 15:48:41 -0800803 cell = fdt_getprop(fit, noffset, name, &len);
804 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800805 fit_get_debug(fit, noffset, name, len);
806 return -1;
807 }
808
York Sunc1913cb2016-02-29 15:48:41 -0800809 cell_len = len >> 2;
810 /* Use load64 to avoid compiling warning for 32-bit target */
811 while (cell_len--) {
812 load64 = (load64 << 32) | uimage_to_cpu(*cell);
813 cell++;
814 }
Michal Simek13d1ca82020-09-03 12:44:51 +0200815
816 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
817 printf("Unsupported %s address size\n", name);
818 return -1;
819 }
820
York Sunc1913cb2016-02-29 15:48:41 -0800821 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800822
823 return 0;
824}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000825/**
826 * fit_image_get_load() - get load addr property for given component image node
827 * @fit: pointer to the FIT format image header
828 * @noffset: component image node offset
829 * @load: pointer to the uint32_t, will hold load address
830 *
831 * fit_image_get_load() finds load address property in a given component
832 * image node. If the property is found, its value is returned to the caller.
833 *
834 * returns:
835 * 0, on success
836 * -1, on failure
837 */
838int fit_image_get_load(const void *fit, int noffset, ulong *load)
839{
York Sun60047652016-02-29 15:48:40 -0800840 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000841}
842
843/**
844 * fit_image_get_entry() - get entry point address property
845 * @fit: pointer to the FIT format image header
846 * @noffset: component image node offset
847 * @entry: pointer to the uint32_t, will hold entry point address
848 *
849 * This gets the entry point address property for a given component image
850 * node.
851 *
852 * fit_image_get_entry() finds entry point address property in a given
853 * component image node. If the property is found, its value is returned
854 * to the caller.
855 *
856 * returns:
857 * 0, on success
858 * -1, on failure
859 */
860int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
861{
York Sun60047652016-02-29 15:48:40 -0800862 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000863}
864
865/**
866 * fit_image_get_data - get data property and its size for a given component image node
867 * @fit: pointer to the FIT format image header
868 * @noffset: component image node offset
869 * @data: double pointer to void, will hold data property's data address
870 * @size: pointer to size_t, will hold data property's data size
871 *
872 * fit_image_get_data() finds data property in a given component image node.
873 * If the property is found its data start address and size are returned to
874 * the caller.
875 *
876 * returns:
877 * 0, on success
878 * -1, on failure
879 */
880int fit_image_get_data(const void *fit, int noffset,
881 const void **data, size_t *size)
882{
883 int len;
884
885 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
886 if (*data == NULL) {
887 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
888 *size = 0;
889 return -1;
890 }
891
892 *size = len;
893 return 0;
894}
895
896/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200897 * Get 'data-offset' property from a given image node.
898 *
899 * @fit: pointer to the FIT image header
900 * @noffset: component image node offset
901 * @data_offset: holds the data-offset property
902 *
903 * returns:
904 * 0, on success
905 * -ENOENT if the property could not be found
906 */
907int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
908{
909 const fdt32_t *val;
910
911 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
912 if (!val)
913 return -ENOENT;
914
915 *data_offset = fdt32_to_cpu(*val);
916
917 return 0;
918}
919
920/**
Peng Fana1be94b2017-12-05 13:20:59 +0800921 * Get 'data-position' property from a given image node.
922 *
923 * @fit: pointer to the FIT image header
924 * @noffset: component image node offset
925 * @data_position: holds the data-position property
926 *
927 * returns:
928 * 0, on success
929 * -ENOENT if the property could not be found
930 */
931int fit_image_get_data_position(const void *fit, int noffset,
932 int *data_position)
933{
934 const fdt32_t *val;
935
936 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
937 if (!val)
938 return -ENOENT;
939
940 *data_position = fdt32_to_cpu(*val);
941
942 return 0;
943}
944
945/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200946 * Get 'data-size' property from a given image node.
947 *
948 * @fit: pointer to the FIT image header
949 * @noffset: component image node offset
950 * @data_size: holds the data-size property
951 *
952 * returns:
953 * 0, on success
954 * -ENOENT if the property could not be found
955 */
956int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
957{
958 const fdt32_t *val;
959
960 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
961 if (!val)
962 return -ENOENT;
963
964 *data_size = fdt32_to_cpu(*val);
965
966 return 0;
967}
968
969/**
Philippe Reynes4df35782019-12-18 18:25:42 +0100970 * Get 'data-size-unciphered' property from a given image node.
971 *
972 * @fit: pointer to the FIT image header
973 * @noffset: component image node offset
974 * @data_size: holds the data-size property
975 *
976 * returns:
977 * 0, on success
978 * -ENOENT if the property could not be found
979 */
980int fit_image_get_data_size_unciphered(const void *fit, int noffset,
981 size_t *data_size)
982{
983 const fdt32_t *val;
984
985 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
986 if (!val)
987 return -ENOENT;
988
989 *data_size = (size_t)fdt32_to_cpu(*val);
990
991 return 0;
992}
993
994/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800995 * fit_image_get_data_and_size - get data and its size including
996 * both embedded and external data
997 * @fit: pointer to the FIT format image header
998 * @noffset: component image node offset
999 * @data: double pointer to void, will hold data property's data address
1000 * @size: pointer to size_t, will hold data property's data size
1001 *
1002 * fit_image_get_data_and_size() finds data and its size including
1003 * both embedded and external data. If the property is found
1004 * its data start address and size are returned to the caller.
1005 *
1006 * returns:
1007 * 0, on success
1008 * otherwise, on failure
1009 */
1010int fit_image_get_data_and_size(const void *fit, int noffset,
1011 const void **data, size_t *size)
1012{
1013 bool external_data = false;
1014 int offset;
1015 int len;
1016 int ret;
1017
1018 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1019 external_data = true;
1020 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1021 external_data = true;
1022 /*
1023 * For FIT with external data, figure out where
1024 * the external images start. This is the base
1025 * for the data-offset properties in each image.
1026 */
1027 offset += ((fdt_totalsize(fit) + 3) & ~3);
1028 }
1029
1030 if (external_data) {
1031 debug("External Data\n");
1032 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt18378042020-03-11 21:51:08 +01001033 if (!ret) {
1034 *data = fit + offset;
1035 *size = len;
1036 }
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001037 } else {
1038 ret = fit_image_get_data(fit, noffset, data, size);
1039 }
1040
1041 return ret;
1042}
1043
1044/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001045 * fit_image_hash_get_algo - get hash algorithm name
1046 * @fit: pointer to the FIT format image header
1047 * @noffset: hash node offset
1048 * @algo: double pointer to char, will hold pointer to the algorithm name
1049 *
1050 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1051 * If the property is found its data start address is returned to the caller.
1052 *
1053 * returns:
1054 * 0, on success
1055 * -1, on failure
1056 */
1057int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1058{
1059 int len;
1060
1061 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1062 if (*algo == NULL) {
1063 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1064 return -1;
1065 }
1066
1067 return 0;
1068}
1069
1070/**
1071 * fit_image_hash_get_value - get hash value and length
1072 * @fit: pointer to the FIT format image header
1073 * @noffset: hash node offset
1074 * @value: double pointer to uint8_t, will hold address of a hash value data
1075 * @value_len: pointer to an int, will hold hash data length
1076 *
1077 * fit_image_hash_get_value() finds hash value property in a given hash node.
1078 * If the property is found its data start address and size are returned to
1079 * the caller.
1080 *
1081 * returns:
1082 * 0, on success
1083 * -1, on failure
1084 */
1085int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1086 int *value_len)
1087{
1088 int len;
1089
1090 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1091 if (*value == NULL) {
1092 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1093 *value_len = 0;
1094 return -1;
1095 }
1096
1097 *value_len = len;
1098 return 0;
1099}
1100
Simon Glass53fbb7e2013-05-07 06:11:53 +00001101/**
1102 * fit_image_hash_get_ignore - get hash ignore flag
1103 * @fit: pointer to the FIT format image header
1104 * @noffset: hash node offset
1105 * @ignore: pointer to an int, will hold hash ignore flag
1106 *
1107 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1108 * If the property is found and non-zero, the hash algorithm is not verified by
1109 * u-boot automatically.
1110 *
1111 * returns:
1112 * 0, on ignore not found
1113 * value, on ignore found
1114 */
Simon Glassab9efc62013-05-07 06:11:58 +00001115static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001116{
1117 int len;
1118 int *value;
1119
1120 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1121 if (value == NULL || len != sizeof(int))
1122 *ignore = 0;
1123 else
1124 *ignore = *value;
1125
1126 return 0;
1127}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001128
Philippe Reynes7298e422019-12-18 18:25:41 +01001129/**
1130 * fit_image_cipher_get_algo - get cipher algorithm name
1131 * @fit: pointer to the FIT format image header
1132 * @noffset: cipher node offset
1133 * @algo: double pointer to char, will hold pointer to the algorithm name
1134 *
1135 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1136 * cipher node. If the property is found its data start address is returned
1137 * to the caller.
1138 *
1139 * returns:
1140 * 0, on success
1141 * -1, on failure
1142 */
1143int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1144{
1145 int len;
1146
1147 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1148 if (!*algo) {
1149 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1150 return -1;
1151 }
1152
1153 return 0;
1154}
1155
Simon Glass7a80de42016-02-24 09:14:42 -07001156ulong fit_get_end(const void *fit)
1157{
1158 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1159}
1160
Simon Glass53fbb7e2013-05-07 06:11:53 +00001161/**
1162 * fit_set_timestamp - set node timestamp property
1163 * @fit: pointer to the FIT format image header
1164 * @noffset: node offset
1165 * @timestamp: timestamp value to be set
1166 *
1167 * fit_set_timestamp() attempts to set timestamp property in the requested
1168 * node and returns operation status to the caller.
1169 *
1170 * returns:
1171 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001172 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001173 */
1174int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1175{
1176 uint32_t t;
1177 int ret;
1178
1179 t = cpu_to_uimage(timestamp);
1180 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1181 sizeof(uint32_t));
1182 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001183 debug("Can't set '%s' property for '%s' node (%s)\n",
1184 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1185 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001186 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001187 }
1188
1189 return 0;
1190}
1191
1192/**
1193 * calculate_hash - calculate and return hash for provided input data
1194 * @data: pointer to the input data
1195 * @data_len: data length
1196 * @algo: requested hash algorithm
1197 * @value: pointer to the char, will hold hash value data (caller must
1198 * allocate enough free space)
1199 * value_len: length of the calculated hash
1200 *
1201 * calculate_hash() computes input data hash according to the requested
1202 * algorithm.
1203 * Resulting hash value is placed in caller provided 'value' buffer, length
1204 * of the calculated hash is returned via value_len pointer argument.
1205 *
1206 * returns:
1207 * 0, on success
1208 * -1, when algo is unsupported
1209 */
Simon Glass604f23d2013-05-07 06:11:54 +00001210int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001211 uint8_t *value, int *value_len)
1212{
Simon Glass87ebee32013-05-08 08:05:59 +00001213 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001214 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1215 CHUNKSZ_CRC32);
1216 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1217 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001218 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001219 sha1_csum_wd((unsigned char *)data, data_len,
1220 (unsigned char *)value, CHUNKSZ_SHA1);
1221 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001222 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1223 sha256_csum_wd((unsigned char *)data, data_len,
1224 (unsigned char *)value, CHUNKSZ_SHA256);
1225 *value_len = SHA256_SUM_LEN;
Reuben Dowled16b38f2020-04-16 17:36:52 +12001226 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1227 sha384_csum_wd((unsigned char *)data, data_len,
1228 (unsigned char *)value, CHUNKSZ_SHA384);
1229 *value_len = SHA384_SUM_LEN;
1230 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1231 sha512_csum_wd((unsigned char *)data, data_len,
1232 (unsigned char *)value, CHUNKSZ_SHA512);
1233 *value_len = SHA512_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001234 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001235 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1236 *value_len = 16;
1237 } else {
1238 debug("Unsupported hash alogrithm\n");
1239 return -1;
1240 }
1241 return 0;
1242}
1243
Simon Glassab9efc62013-05-07 06:11:58 +00001244static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1245 size_t size, char **err_msgp)
1246{
1247 uint8_t value[FIT_MAX_HASH_LEN];
1248 int value_len;
1249 char *algo;
1250 uint8_t *fit_value;
1251 int fit_value_len;
1252 int ignore;
1253
1254 *err_msgp = NULL;
1255
1256 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001257 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001258 return -1;
1259 }
1260 printf("%s", algo);
1261
1262 if (IMAGE_ENABLE_IGNORE) {
1263 fit_image_hash_get_ignore(fit, noffset, &ignore);
1264 if (ignore) {
1265 printf("-skipped ");
1266 return 0;
1267 }
1268 }
1269
1270 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1271 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001272 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001273 return -1;
1274 }
1275
1276 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001277 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001278 return -1;
1279 }
1280
1281 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001282 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001283 return -1;
1284 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001285 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001286 return -1;
1287 }
1288
1289 return 0;
1290}
1291
Jun Nie5c643db2018-02-27 16:55:58 +08001292int fit_image_verify_with_data(const void *fit, int image_noffset,
1293 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001294{
Simon Glass56518e72013-06-13 15:10:01 -07001295 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001296 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001297 int verify_all = 1;
1298 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001299
Simon Glass56518e72013-06-13 15:10:01 -07001300 /* Verify all required signatures */
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001301 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glass56518e72013-06-13 15:10:01 -07001302 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1303 gd_fdt_blob(), &verify_all)) {
1304 err_msg = "Unable to verify required signature";
1305 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001306 }
1307
1308 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001309 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001310 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001311
Simon Glassab9efc62013-05-07 06:11:58 +00001312 /*
1313 * Check subnode name, must be equal to "hash".
1314 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001315 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001316 */
1317 if (!strncmp(name, FIT_HASH_NODENAME,
1318 strlen(FIT_HASH_NODENAME))) {
1319 if (fit_image_check_hash(fit, noffset, data, size,
1320 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001321 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001322 puts("+ ");
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001323 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glass56518e72013-06-13 15:10:01 -07001324 !strncmp(name, FIT_SIG_NODENAME,
1325 strlen(FIT_SIG_NODENAME))) {
1326 ret = fit_image_check_sig(fit, noffset, data,
1327 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001328
1329 /*
1330 * Show an indication on failure, but do not return
1331 * an error. Only keys marked 'required' can cause
1332 * an image validation failure. See the call to
1333 * fit_image_verify_required_sigs() above.
1334 */
1335 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001336 puts("- ");
1337 else
1338 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001339 }
1340 }
1341
1342 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001343 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001344 goto error;
1345 }
1346
1347 return 1;
1348
1349error:
Simon Glasse754da22013-05-07 06:11:59 +00001350 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001351 err_msg, fit_get_name(fit, noffset, NULL),
1352 fit_get_name(fit, image_noffset, NULL));
1353 return 0;
1354}
1355
1356/**
Jun Nie5c643db2018-02-27 16:55:58 +08001357 * fit_image_verify - verify data integrity
1358 * @fit: pointer to the FIT format image header
1359 * @image_noffset: component image node offset
1360 *
1361 * fit_image_verify() goes over component image hash nodes,
1362 * re-calculates each data hash and compares with the value stored in hash
1363 * node.
1364 *
1365 * returns:
1366 * 1, if all hashes are valid
1367 * 0, otherwise (or on error)
1368 */
1369int fit_image_verify(const void *fit, int image_noffset)
1370{
1371 const void *data;
1372 size_t size;
1373 int noffset = 0;
1374 char *err_msg = "";
1375
1376 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001377 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001378 err_msg = "Can't get image data/size";
1379 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1380 err_msg, fit_get_name(fit, noffset, NULL),
1381 fit_get_name(fit, image_noffset, NULL));
1382 return 0;
1383 }
1384
1385 return fit_image_verify_with_data(fit, image_noffset, data, size);
1386}
1387
1388/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001389 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001390 * @fit: pointer to the FIT format image header
1391 *
Simon Glassb8da8362013-05-07 06:11:57 +00001392 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001393 * for every images checks if all it's hashes are valid.
1394 *
1395 * returns:
1396 * 1, if all hashes of all images are valid
1397 * 0, otherwise (or on error)
1398 */
Simon Glassb8da8362013-05-07 06:11:57 +00001399int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001400{
1401 int images_noffset;
1402 int noffset;
1403 int ndepth;
1404 int count;
1405
1406 /* Find images parent node offset */
1407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1408 if (images_noffset < 0) {
1409 printf("Can't find images parent node '%s' (%s)\n",
1410 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1411 return 0;
1412 }
1413
1414 /* Process all image subnodes, check hashes for each */
1415 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1416 (ulong)fit);
1417 for (ndepth = 0, count = 0,
1418 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1419 (noffset >= 0) && (ndepth > 0);
1420 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1421 if (ndepth == 1) {
1422 /*
1423 * Direct child node of the images parent node,
1424 * i.e. component image node.
1425 */
Simon Glass73223f02016-02-22 22:55:43 -07001426 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001427 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001428 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001429
Simon Glassb8da8362013-05-07 06:11:57 +00001430 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001431 return 0;
1432 printf("\n");
1433 }
1434 }
1435 return 1;
1436}
1437
Philippe Reynes4df35782019-12-18 18:25:42 +01001438static int fit_image_uncipher(const void *fit, int image_noffset,
1439 void **data, size_t *size)
1440{
1441 int cipher_noffset, ret;
1442 void *dst;
1443 size_t size_dst;
1444
1445 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1446 FIT_CIPHER_NODENAME);
1447 if (cipher_noffset < 0)
1448 return 0;
1449
1450 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1451 *data, *size, &dst, &size_dst);
1452 if (ret)
1453 goto out;
1454
1455 *data = dst;
1456 *size = size_dst;
1457
1458 out:
1459 return ret;
1460}
Philippe Reynes4df35782019-12-18 18:25:42 +01001461
Simon Glass53fbb7e2013-05-07 06:11:53 +00001462/**
1463 * fit_image_check_os - check whether image node is of a given os type
1464 * @fit: pointer to the FIT format image header
1465 * @noffset: component image node offset
1466 * @os: requested image os
1467 *
1468 * fit_image_check_os() reads image os property and compares its numeric
1469 * id with the requested os. Comparison result is returned to the caller.
1470 *
1471 * returns:
1472 * 1 if image is of given os type
1473 * 0 otherwise (or on error)
1474 */
1475int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1476{
1477 uint8_t image_os;
1478
1479 if (fit_image_get_os(fit, noffset, &image_os))
1480 return 0;
1481 return (os == image_os);
1482}
1483
1484/**
1485 * fit_image_check_arch - check whether image node is of a given arch
1486 * @fit: pointer to the FIT format image header
1487 * @noffset: component image node offset
1488 * @arch: requested imagearch
1489 *
1490 * fit_image_check_arch() reads image arch property and compares its numeric
1491 * id with the requested arch. Comparison result is returned to the caller.
1492 *
1493 * returns:
1494 * 1 if image is of given arch
1495 * 0 otherwise (or on error)
1496 */
1497int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1498{
1499 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001500 int aarch32_support = 0;
1501
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01001502 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1503 aarch32_support = 1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001504
1505 if (fit_image_get_arch(fit, noffset, &image_arch))
1506 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001507 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001508 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1509 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1510 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001511}
1512
1513/**
1514 * fit_image_check_type - check whether image node is of a given type
1515 * @fit: pointer to the FIT format image header
1516 * @noffset: component image node offset
1517 * @type: requested image type
1518 *
1519 * fit_image_check_type() reads image type property and compares its numeric
1520 * id with the requested type. Comparison result is returned to the caller.
1521 *
1522 * returns:
1523 * 1 if image is of given type
1524 * 0 otherwise (or on error)
1525 */
1526int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1527{
1528 uint8_t image_type;
1529
1530 if (fit_image_get_type(fit, noffset, &image_type))
1531 return 0;
1532 return (type == image_type);
1533}
1534
1535/**
1536 * fit_image_check_comp - check whether image node uses given compression
1537 * @fit: pointer to the FIT format image header
1538 * @noffset: component image node offset
1539 * @comp: requested image compression type
1540 *
1541 * fit_image_check_comp() reads image compression property and compares its
1542 * numeric id with the requested compression type. Comparison result is
1543 * returned to the caller.
1544 *
1545 * returns:
1546 * 1 if image uses requested compression
1547 * 0 otherwise (or on error)
1548 */
1549int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1550{
1551 uint8_t image_comp;
1552
1553 if (fit_image_get_comp(fit, noffset, &image_comp))
1554 return 0;
1555 return (comp == image_comp);
1556}
1557
1558/**
1559 * fit_check_format - sanity check FIT image format
1560 * @fit: pointer to the FIT format image header
1561 *
1562 * fit_check_format() runs a basic sanity FIT image verification.
1563 * Routine checks for mandatory properties, nodes, etc.
1564 *
1565 * returns:
1566 * 1, on success
1567 * 0, on failure
1568 */
1569int fit_check_format(const void *fit)
1570{
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001571 /* A FIT image must be a valid FDT */
1572 if (fdt_check_header(fit)) {
1573 debug("Wrong FIT format: not a flattened device tree\n");
1574 return 0;
1575 }
1576
Simon Glass53fbb7e2013-05-07 06:11:53 +00001577 /* mandatory / node 'description' property */
1578 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1579 debug("Wrong FIT format: no description\n");
1580 return 0;
1581 }
1582
1583 if (IMAGE_ENABLE_TIMESTAMP) {
1584 /* mandatory / node 'timestamp' property */
1585 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1586 debug("Wrong FIT format: no timestamp\n");
1587 return 0;
1588 }
1589 }
1590
1591 /* mandatory subimages parent '/images' node */
1592 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1593 debug("Wrong FIT format: no images parent node\n");
1594 return 0;
1595 }
1596
1597 return 1;
1598}
1599
1600
1601/**
1602 * fit_conf_find_compat
1603 * @fit: pointer to the FIT format image header
1604 * @fdt: pointer to the device tree to compare against
1605 *
1606 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1607 * most compatible with the passed in device tree.
1608 *
1609 * Example:
1610 *
1611 * / o image-tree
1612 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001613 * | |-o fdt-1
1614 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001615 * |
1616 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001617 * |-o config-1
1618 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001619 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001620 * |-o config-2
1621 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001622 *
1623 * / o U-Boot fdt
1624 * |-compatible = "foo,bar", "bim,bam"
1625 *
1626 * / o kernel fdt1
1627 * |-compatible = "foo,bar",
1628 *
1629 * / o kernel fdt2
1630 * |-compatible = "bim,bam", "baz,biz"
1631 *
1632 * Configuration 1 would be picked because the first string in U-Boot's
1633 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1634 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1635 *
Julius Werner18cfa612019-07-24 19:37:56 -07001636 * As an optimization, the compatible property from the FDT's root node can be
1637 * copied into the configuration node in the FIT image. This is required to
1638 * match configurations with compressed FDTs.
1639 *
Simon Glass53fbb7e2013-05-07 06:11:53 +00001640 * returns:
1641 * offset to the configuration to use if one was found
1642 * -1 otherwise
1643 */
1644int fit_conf_find_compat(const void *fit, const void *fdt)
1645{
1646 int ndepth = 0;
1647 int noffset, confs_noffset, images_noffset;
1648 const void *fdt_compat;
1649 int fdt_compat_len;
1650 int best_match_offset = 0;
1651 int best_match_pos = 0;
1652
1653 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1654 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1655 if (confs_noffset < 0 || images_noffset < 0) {
1656 debug("Can't find configurations or images nodes.\n");
1657 return -1;
1658 }
1659
1660 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1661 if (!fdt_compat) {
1662 debug("Fdt for comparison has no \"compatible\" property.\n");
1663 return -1;
1664 }
1665
1666 /*
1667 * Loop over the configurations in the FIT image.
1668 */
1669 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1670 (noffset >= 0) && (ndepth > 0);
1671 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001672 const void *fdt;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001673 const char *kfdt_name;
Julius Werner18cfa612019-07-24 19:37:56 -07001674 int kfdt_noffset, compat_noffset;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001675 const char *cur_fdt_compat;
1676 int len;
Julius Werner18cfa612019-07-24 19:37:56 -07001677 size_t sz;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001678 int i;
1679
1680 if (ndepth > 1)
1681 continue;
1682
Julius Werner18cfa612019-07-24 19:37:56 -07001683 /* If there's a compat property in the config node, use that. */
1684 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1685 fdt = fit; /* search in FIT image */
1686 compat_noffset = noffset; /* search under config node */
1687 } else { /* Otherwise extract it from the kernel FDT. */
1688 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1689 if (!kfdt_name) {
1690 debug("No fdt property found.\n");
1691 continue;
1692 }
1693 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1694 kfdt_name);
1695 if (kfdt_noffset < 0) {
1696 debug("No image node named \"%s\" found.\n",
1697 kfdt_name);
1698 continue;
1699 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001700
Julius Werner18cfa612019-07-24 19:37:56 -07001701 if (!fit_image_check_comp(fit, kfdt_noffset,
1702 IH_COMP_NONE)) {
1703 debug("Can't extract compat from \"%s\" "
1704 "(compressed)\n", kfdt_name);
1705 continue;
1706 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001707
Julius Werner18cfa612019-07-24 19:37:56 -07001708 /* search in this config's kernel FDT */
1709 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1710 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1711 continue;
1712 }
1713
1714 compat_noffset = 0; /* search kFDT under root node */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001715 }
1716
1717 len = fdt_compat_len;
1718 cur_fdt_compat = fdt_compat;
1719 /*
1720 * Look for a match for each U-Boot compatibility string in
Julius Werner18cfa612019-07-24 19:37:56 -07001721 * turn in the compat string property.
Simon Glass53fbb7e2013-05-07 06:11:53 +00001722 */
1723 for (i = 0; len > 0 &&
1724 (!best_match_offset || best_match_pos > i); i++) {
1725 int cur_len = strlen(cur_fdt_compat) + 1;
1726
Julius Werner18cfa612019-07-24 19:37:56 -07001727 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001728 cur_fdt_compat)) {
1729 best_match_offset = noffset;
1730 best_match_pos = i;
1731 break;
1732 }
1733 len -= cur_len;
1734 cur_fdt_compat += cur_len;
1735 }
1736 }
1737 if (!best_match_offset) {
1738 debug("No match found.\n");
1739 return -1;
1740 }
1741
1742 return best_match_offset;
1743}
1744
Simon Glass53fbb7e2013-05-07 06:11:53 +00001745int fit_conf_get_node(const void *fit, const char *conf_uname)
1746{
1747 int noffset, confs_noffset;
1748 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001749 const char *s;
1750 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001751
1752 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1753 if (confs_noffset < 0) {
1754 debug("Can't find configurations parent node '%s' (%s)\n",
1755 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1756 return confs_noffset;
1757 }
1758
1759 if (conf_uname == NULL) {
1760 /* get configuration unit name from the default property */
1761 debug("No configuration specified, trying default...\n");
Sebastian Reicheld8ab0fe2021-01-04 20:48:04 +01001762 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1763 noffset = fit_find_config_node(fit);
1764 if (noffset < 0)
1765 return noffset;
1766 conf_uname = fdt_get_name(fit, noffset, NULL);
1767 } else {
1768 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1769 FIT_DEFAULT_PROP, &len);
1770 if (conf_uname == NULL) {
1771 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1772 len);
1773 return len;
1774 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001775 }
1776 debug("Found default configuration: '%s'\n", conf_uname);
1777 }
1778
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001779 s = strchr(conf_uname, '#');
1780 if (s) {
1781 len = s - conf_uname;
1782 conf_uname_copy = malloc(len + 1);
1783 if (!conf_uname_copy) {
1784 debug("Can't allocate uname copy: '%s'\n",
1785 conf_uname);
1786 return -ENOMEM;
1787 }
1788 memcpy(conf_uname_copy, conf_uname, len);
1789 conf_uname_copy[len] = '\0';
1790 conf_uname = conf_uname_copy;
1791 }
1792
Simon Glass53fbb7e2013-05-07 06:11:53 +00001793 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1794 if (noffset < 0) {
1795 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1796 conf_uname, fdt_strerror(noffset));
1797 }
1798
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001799 if (conf_uname_copy)
1800 free(conf_uname_copy);
1801
Simon Glass53fbb7e2013-05-07 06:11:53 +00001802 return noffset;
1803}
1804
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001805int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001806 const char *prop_name)
1807{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001808 return fdt_stringlist_count(fit, noffset, prop_name);
1809}
1810
1811int fit_conf_get_prop_node_index(const void *fit, int noffset,
1812 const char *prop_name, int index)
1813{
1814 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001815 int len;
1816
1817 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001818 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001819 if (uname == NULL)
1820 return len;
1821
1822 return fit_image_get_node(fit, uname);
1823}
1824
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001825int fit_conf_get_prop_node(const void *fit, int noffset,
1826 const char *prop_name)
1827{
1828 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1829}
1830
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001831static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001832{
1833 fit_image_print(fit, rd_noffset, " ");
1834
1835 if (verify) {
1836 puts(" Verifying Hash Integrity ... ");
1837 if (!fit_image_verify(fit, rd_noffset)) {
1838 puts("Bad Data Hash\n");
1839 return -EACCES;
1840 }
1841 puts("OK\n");
1842 }
1843
1844 return 0;
1845}
1846
Simon Glass782cfbb2013-05-16 13:53:21 +00001847int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1848 ulong addr)
1849{
1850 int cfg_noffset;
1851 void *fit_hdr;
1852 int noffset;
1853
1854 debug("* %s: using config '%s' from image at 0x%08lx\n",
1855 prop_name, images->fit_uname_cfg, addr);
1856
1857 /* Check whether configuration has this property defined */
1858 fit_hdr = map_sysmem(addr, 0);
1859 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1860 if (cfg_noffset < 0) {
1861 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001862 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001863 }
1864
1865 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1866 if (noffset < 0) {
1867 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001868 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001869 }
1870
1871 return noffset;
1872}
1873
Simon Glass126cc862014-06-12 07:24:47 -06001874/**
1875 * fit_get_image_type_property() - get property name for IH_TYPE_...
1876 *
1877 * @return the properly name where we expect to find the image in the
1878 * config node
1879 */
1880static const char *fit_get_image_type_property(int type)
1881{
1882 /*
1883 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001884 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001885 * anyway. So let's just keep it here.
1886 */
1887 switch (type) {
1888 case IH_TYPE_FLATDT:
1889 return FIT_FDT_PROP;
1890 case IH_TYPE_KERNEL:
1891 return FIT_KERNEL_PROP;
1892 case IH_TYPE_RAMDISK:
1893 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001894 case IH_TYPE_X86_SETUP:
1895 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001896 case IH_TYPE_LOADABLE:
1897 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001898 case IH_TYPE_FPGA:
1899 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001900 case IH_TYPE_STANDALONE:
1901 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001902 }
1903
1904 return "unknown";
1905}
1906
1907int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001908 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001909 int arch, int image_type, int bootstage_id,
1910 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1911{
1912 int cfg_noffset, noffset;
1913 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001914 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001915 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001916 const void *fit;
Julius Wernerb1307f82019-07-24 19:37:55 -07001917 void *buf;
1918 void *loadbuf;
Simon Glass782cfbb2013-05-16 13:53:21 +00001919 size_t size;
1920 int type_ok, os_ok;
Julius Wernerb1307f82019-07-24 19:37:55 -07001921 ulong load, load_end, data, len;
1922 uint8_t os, comp;
Alison Wangec6617c2016-11-10 10:49:03 +08001923#ifndef USE_HOSTCC
1924 uint8_t os_arch;
1925#endif
Simon Glass126cc862014-06-12 07:24:47 -06001926 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001927 int ret;
1928
1929 fit = map_sysmem(addr, 0);
1930 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001931 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001932 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001933 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001934 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1935
1936 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1937 if (!fit_check_format(fit)) {
1938 printf("Bad FIT %s image format!\n", prop_name);
1939 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1940 return -ENOEXEC;
1941 }
1942 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1943 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001944 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1946 noffset = fit_image_get_node(fit, fit_uname);
1947 } else {
1948 /*
1949 * no image node unit name, try to get config
1950 * node first. If config unit node name is NULL
1951 * fit_conf_get_node() will try to find default config node
1952 */
1953 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1954 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1955 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1956 } else {
1957 cfg_noffset = fit_conf_get_node(fit,
1958 fit_uname_config);
1959 }
1960 if (cfg_noffset < 0) {
1961 puts("Could not find configuration node\n");
1962 bootstage_error(bootstage_id +
1963 BOOTSTAGE_SUB_NO_UNIT_NAME);
1964 return -ENOENT;
1965 }
Marek Vasut078e5582018-05-31 17:59:07 +02001966
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001967 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1968 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02001969 /* Remember this config */
1970 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001971 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02001972
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001973 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut078e5582018-05-31 17:59:07 +02001974 puts(" Verifying Hash Integrity ... ");
1975 if (fit_config_verify(fit, cfg_noffset)) {
1976 puts("Bad Data Hash\n");
1977 bootstage_error(bootstage_id +
1978 BOOTSTAGE_SUB_HASH);
1979 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00001980 }
Marek Vasut078e5582018-05-31 17:59:07 +02001981 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00001982 }
1983
Marek Vasut078e5582018-05-31 17:59:07 +02001984 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1985
Simon Glass782cfbb2013-05-16 13:53:21 +00001986 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1987 prop_name);
1988 fit_uname = fit_get_name(fit, noffset, NULL);
1989 }
1990 if (noffset < 0) {
Simon Glass382cf622020-03-18 11:43:56 -06001991 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass782cfbb2013-05-16 13:53:21 +00001992 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1993 return -ENOENT;
1994 }
1995
1996 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1997
1998 ret = fit_image_select(fit, noffset, images->verify);
1999 if (ret) {
2000 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2001 return ret;
2002 }
2003
2004 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002005 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2006 if (!fit_image_check_target_arch(fit, noffset)) {
2007 puts("Unsupported Architecture\n");
2008 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2009 return -ENOEXEC;
2010 }
Simon Glass782cfbb2013-05-16 13:53:21 +00002011 }
Alison Wangec6617c2016-11-10 10:49:03 +08002012
2013#ifndef USE_HOSTCC
2014 fit_image_get_arch(fit, noffset, &os_arch);
2015 images->os.arch = os_arch;
2016#endif
2017
Simon Glass782cfbb2013-05-16 13:53:21 +00002018 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2019 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002020 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2021 (image_type == IH_TYPE_KERNEL &&
2022 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01002023
Andreas Bießmann950fe262016-08-14 20:31:24 +02002024 os_ok = image_type == IH_TYPE_FLATDT ||
2025 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01002026 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002027 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Cristian Ciocalteaa031b032019-12-24 18:05:38 +02002028 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao0df27d62020-03-18 07:32:07 -07002029 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2030 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
Karl Apsite84a07db2015-05-21 09:52:48 -04002031
2032 /*
2033 * If either of the checks fail, we should report an error, but
2034 * if the image type is coming from the "loadables" field, we
2035 * don't care what it is
2036 */
2037 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01002038 fit_image_get_os(fit, noffset, &os);
2039 printf("No %s %s %s Image\n",
2040 genimg_get_os_name(os),
2041 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00002042 genimg_get_type_name(image_type));
2043 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2044 return -EIO;
2045 }
2046
2047 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2048
2049 /* get image data address and length */
Julius Wernerb1307f82019-07-24 19:37:55 -07002050 if (fit_image_get_data_and_size(fit, noffset,
2051 (const void **)&buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002052 printf("Could not find %s subimage data!\n", prop_name);
2053 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07002054 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00002055 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002056
Philippe Reynes4df35782019-12-18 18:25:42 +01002057 /* Decrypt data before uncompress/move */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002058 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
Philippe Reynes4df35782019-12-18 18:25:42 +01002059 puts(" Decrypting Data ... ");
2060 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2061 puts("Error\n");
2062 return -EACCES;
2063 }
2064 puts("OK\n");
2065 }
Philippe Reynes4df35782019-12-18 18:25:42 +01002066
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002067 /* perform any post-processing on the image data */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002068 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2069 board_fit_image_post_process(&buf, &size);
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002070
Simon Glass782cfbb2013-05-16 13:53:21 +00002071 len = (ulong)size;
2072
Simon Glass782cfbb2013-05-16 13:53:21 +00002073 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2074
Julius Wernerb1307f82019-07-24 19:37:55 -07002075 data = map_to_sysmem(buf);
2076 load = data;
Simon Glass782cfbb2013-05-16 13:53:21 +00002077 if (load_op == FIT_LOAD_IGNORED) {
2078 /* Don't load */
2079 } else if (fit_image_get_load(fit, noffset, &load)) {
2080 if (load_op == FIT_LOAD_REQUIRED) {
2081 printf("Can't get %s subimage load address!\n",
2082 prop_name);
2083 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2084 return -EBADF;
2085 }
Simon Glassfe20a812014-08-22 14:26:43 -06002086 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002087 ulong image_start, image_end;
Simon Glass782cfbb2013-05-16 13:53:21 +00002088
2089 /*
2090 * move image data to the load address,
2091 * make sure we don't overwrite initial image
2092 */
2093 image_start = addr;
2094 image_end = addr + fit_get_size(fit);
2095
2096 load_end = load + len;
2097 if (image_type != IH_TYPE_KERNEL &&
2098 load < image_end && load_end > image_start) {
2099 printf("Error: %s overwritten\n", prop_name);
2100 return -EXDEV;
2101 }
2102
2103 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2104 prop_name, data, load);
Julius Wernerb1307f82019-07-24 19:37:55 -07002105 } else {
2106 load = data; /* No load address specified */
Simon Glass782cfbb2013-05-16 13:53:21 +00002107 }
Julius Wernerb1307f82019-07-24 19:37:55 -07002108
2109 comp = IH_COMP_NONE;
2110 loadbuf = buf;
2111 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Wernerbddd9852019-08-02 15:52:28 -07002112 if (!fit_image_get_comp(fit, noffset, &comp) &&
2113 comp != IH_COMP_NONE &&
2114 !(image_type == IH_TYPE_KERNEL ||
2115 image_type == IH_TYPE_KERNEL_NOLOAD ||
2116 image_type == IH_TYPE_RAMDISK)) {
Julius Wernerb1307f82019-07-24 19:37:55 -07002117 ulong max_decomp_len = len * 20;
2118 if (load == data) {
2119 loadbuf = malloc(max_decomp_len);
2120 load = map_to_sysmem(loadbuf);
2121 } else {
2122 loadbuf = map_sysmem(load, max_decomp_len);
2123 }
2124 if (image_decomp(comp, load, data, image_type,
2125 loadbuf, buf, len, max_decomp_len, &load_end)) {
2126 printf("Error decompressing %s\n", prop_name);
2127
2128 return -ENOEXEC;
2129 }
2130 len = load_end - load;
2131 } else if (load != data) {
2132 loadbuf = map_sysmem(load, len);
2133 memcpy(loadbuf, buf, len);
2134 }
2135
Julius Wernerbddd9852019-08-02 15:52:28 -07002136 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2137 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2138 " please fix your .its file!\n");
2139
Julius Wernerb1307f82019-07-24 19:37:55 -07002140 /* verify that image data is a proper FDT blob */
2141 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2142 puts("Subimage data is not a FDT");
2143 return -ENOEXEC;
2144 }
2145
Simon Glass782cfbb2013-05-16 13:53:21 +00002146 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2147
Julius Wernerb1307f82019-07-24 19:37:55 -07002148 *datap = load;
Simon Glass782cfbb2013-05-16 13:53:21 +00002149 *lenp = len;
2150 if (fit_unamep)
2151 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002152 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002153 *fit_uname_configp = (char *)(fit_uname_config ? :
2154 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002155
2156 return noffset;
2157}
Simon Glass90268b82014-10-19 21:11:24 -06002158
2159int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2160 ulong *setup_start, ulong *setup_len)
2161{
2162 int noffset;
2163 ulong addr;
2164 ulong len;
2165 int ret;
2166
2167 addr = map_to_sysmem(images->fit_hdr_os);
2168 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2169 if (noffset < 0)
2170 return noffset;
2171
2172 ret = fit_image_load(images, addr, NULL, NULL, arch,
2173 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2174 FIT_LOAD_REQUIRED, setup_start, &len);
2175
2176 return ret;
2177}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002178
2179#ifndef USE_HOSTCC
2180int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2181 const char **fit_unamep, const char **fit_uname_configp,
2182 int arch, ulong *datap, ulong *lenp)
2183{
2184 int fdt_noffset, cfg_noffset, count;
2185 const void *fit;
2186 const char *fit_uname = NULL;
2187 const char *fit_uname_config = NULL;
2188 char *fit_uname_config_copy = NULL;
2189 char *next_config = NULL;
2190 ulong load, len;
2191#ifdef CONFIG_OF_LIBFDT_OVERLAY
2192 ulong image_start, image_end;
2193 ulong ovload, ovlen;
2194 const char *uconfig;
2195 const char *uname;
2196 void *base, *ov;
2197 int i, err, noffset, ov_noffset;
2198#endif
2199
2200 fit_uname = fit_unamep ? *fit_unamep : NULL;
2201
2202 if (fit_uname_configp && *fit_uname_configp) {
2203 fit_uname_config_copy = strdup(*fit_uname_configp);
2204 if (!fit_uname_config_copy)
2205 return -ENOMEM;
2206
2207 next_config = strchr(fit_uname_config_copy, '#');
2208 if (next_config)
2209 *next_config++ = '\0';
2210 if (next_config - 1 > fit_uname_config_copy)
2211 fit_uname_config = fit_uname_config_copy;
2212 }
2213
2214 fdt_noffset = fit_image_load(images,
2215 addr, &fit_uname, &fit_uname_config,
2216 arch, IH_TYPE_FLATDT,
2217 BOOTSTAGE_ID_FIT_FDT_START,
2218 FIT_LOAD_OPTIONAL, &load, &len);
2219
2220 if (fdt_noffset < 0)
2221 goto out;
2222
2223 debug("fit_uname=%s, fit_uname_config=%s\n",
2224 fit_uname ? fit_uname : "<NULL>",
2225 fit_uname_config ? fit_uname_config : "<NULL>");
2226
2227 fit = map_sysmem(addr, 0);
2228
2229 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2230
2231 /* single blob, or error just return as well */
2232 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2233 if (count <= 1 && !next_config)
2234 goto out;
2235
2236 /* we need to apply overlays */
2237
2238#ifdef CONFIG_OF_LIBFDT_OVERLAY
2239 image_start = addr;
2240 image_end = addr + fit_get_size(fit);
2241 /* verify that relocation took place by load address not being in fit */
2242 if (load >= image_start && load < image_end) {
2243 /* check is simplified; fit load checks for overlaps */
2244 printf("Overlayed FDT requires relocation\n");
2245 fdt_noffset = -EBADF;
2246 goto out;
2247 }
2248
2249 base = map_sysmem(load, len);
2250
2251 /* apply extra configs in FIT first, followed by args */
2252 for (i = 1; ; i++) {
2253 if (i < count) {
2254 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2255 FIT_FDT_PROP, i);
2256 uname = fit_get_name(fit, noffset, NULL);
2257 uconfig = NULL;
2258 } else {
2259 if (!next_config)
2260 break;
2261 uconfig = next_config;
2262 next_config = strchr(next_config, '#');
2263 if (next_config)
2264 *next_config++ = '\0';
2265 uname = NULL;
Peter Ujfalusi35c75272019-03-06 15:52:27 +02002266
2267 /*
2268 * fit_image_load() would load the first FDT from the
2269 * extra config only when uconfig is specified.
2270 * Check if the extra config contains multiple FDTs and
2271 * if so, load them.
2272 */
2273 cfg_noffset = fit_conf_get_node(fit, uconfig);
2274
2275 i = 0;
2276 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2277 FIT_FDT_PROP);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002278 }
2279
2280 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2281
2282 ov_noffset = fit_image_load(images,
2283 addr, &uname, &uconfig,
2284 arch, IH_TYPE_FLATDT,
2285 BOOTSTAGE_ID_FIT_FDT_START,
2286 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2287 if (ov_noffset < 0) {
2288 printf("load of %s failed\n", uname);
2289 continue;
2290 }
2291 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2292 uname, ovload, ovlen);
2293 ov = map_sysmem(ovload, ovlen);
2294
2295 base = map_sysmem(load, len + ovlen);
2296 err = fdt_open_into(base, base, len + ovlen);
2297 if (err < 0) {
2298 printf("failed on fdt_open_into\n");
2299 fdt_noffset = err;
2300 goto out;
2301 }
2302 /* the verbose method prints out messages on error */
2303 err = fdt_overlay_apply_verbose(base, ov);
2304 if (err < 0) {
2305 fdt_noffset = err;
2306 goto out;
2307 }
2308 fdt_pack(base);
2309 len = fdt_totalsize(base);
2310 }
2311#else
2312 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2313 fdt_noffset = -EBADF;
2314#endif
2315
2316out:
2317 if (datap)
2318 *datap = load;
2319 if (lenp)
2320 *lenp = len;
2321 if (fit_unamep)
2322 *fit_unamep = fit_uname;
2323 if (fit_uname_configp)
2324 *fit_uname_configp = fit_uname_config;
2325
2326 if (fit_uname_config_copy)
2327 free(fit_uname_config_copy);
2328 return fdt_noffset;
2329}
2330#endif