blob: 5a0a0cc200727890c32ae797c2dc207681089c97 [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
Simon Glassc5819702021-02-15 17:08:09 -070011#define LOG_CATEGORY LOGC_BOOT
12
Simon Glass53fbb7e2013-05-07 06:11:53 +000013#ifdef USE_HOSTCC
14#include "mkimage.h"
Simon Glass53fbb7e2013-05-07 06:11:53 +000015#include <time.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060016#include <linux/libfdt.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000018#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050019#include <linux/compiler.h>
Marek Vasut4c531d92021-06-11 04:09:56 +020020#include <linux/sizes.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000021#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000022#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050024#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000025#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030026#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060027#include <asm/global_data.h>
Chia-Wei Wangca479552021-07-30 09:08:05 +080028#ifdef CONFIG_DM_HASH
29#include <dm.h>
30#include <u-boot/hash.h>
31#endif
Simon Glass782cfbb2013-05-16 13:53:21 +000032DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000033#endif /* !USE_HOSTCC*/
34
Julius Wernerb1307f82019-07-24 19:37:55 -070035#include <bootm.h>
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050036#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000037#include <bootstage.h>
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +010038#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000039#include <u-boot/crc.h>
40#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020041#include <u-boot/sha1.h>
42#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120043#include <u-boot/sha512.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000044
45/*****************************************************************************/
46/* New uImage format routines */
47/*****************************************************************************/
48#ifndef USE_HOSTCC
49static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
50 ulong *addr, const char **name)
51{
52 const char *sep;
53
54 *addr = addr_curr;
55 *name = NULL;
56
57 sep = strchr(spec, sepc);
58 if (sep) {
59 if (sep - spec > 0)
Simon Glass7e5f4602021-07-24 09:03:29 -060060 *addr = hextoul(spec, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +000061
62 *name = sep + 1;
63 return 1;
64 }
65
66 return 0;
67}
68
69/**
70 * fit_parse_conf - parse FIT configuration spec
71 * @spec: input string, containing configuration spec
72 * @add_curr: current image address (to be used as a possible default)
73 * @addr: pointer to a ulong variable, will hold FIT image address of a given
74 * configuration
75 * @conf_name double pointer to a char, will hold pointer to a configuration
76 * unit name
77 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090078 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000079 * where <addr> is a FIT image address that contains configuration
80 * with a <conf> unit name.
81 *
82 * Address part is optional, and if omitted default add_curr will
83 * be used instead.
84 *
85 * returns:
86 * 1 if spec is a valid configuration string,
87 * addr and conf_name are set accordingly
88 * 0 otherwise
89 */
90int fit_parse_conf(const char *spec, ulong addr_curr,
91 ulong *addr, const char **conf_name)
92{
93 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
94}
95
96/**
97 * fit_parse_subimage - parse FIT subimage spec
98 * @spec: input string, containing subimage spec
99 * @add_curr: current image address (to be used as a possible default)
100 * @addr: pointer to a ulong variable, will hold FIT image address of a given
101 * subimage
102 * @image_name: double pointer to a char, will hold pointer to a subimage name
103 *
Masahiro Yamada069d5942013-09-18 09:36:38 +0900104 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +0000105 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
106 * subimage with a <subimg> unit name.
107 *
108 * Address part is optional, and if omitted default add_curr will
109 * be used instead.
110 *
111 * returns:
112 * 1 if spec is a valid subimage string,
113 * addr and image_name are set accordingly
114 * 0 otherwise
115 */
116int fit_parse_subimage(const char *spec, ulong addr_curr,
117 ulong *addr, const char **image_name)
118{
119 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
120}
121#endif /* !USE_HOSTCC */
122
Joel Stanley93af80f2020-12-08 14:42:14 +1030123#ifdef USE_HOSTCC
124/* Host tools use these implementations for Cipher and Signature support */
125static void *host_blob;
126
127void image_set_host_blob(void *blob)
128{
129 host_blob = blob;
130}
131
132void *image_get_host_blob(void)
133{
134 return host_blob;
135}
136#endif /* USE_HOSTCC */
137
Simon Glass53fbb7e2013-05-07 06:11:53 +0000138static void fit_get_debug(const void *fit, int noffset,
139 char *prop_name, int err)
140{
141 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
142 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
143 fdt_strerror(err));
144}
145
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200146/**
147 * fit_get_subimage_count - get component (sub-image) count
148 * @fit: pointer to the FIT format image header
149 * @images_noffset: offset of images node
150 *
151 * returns:
152 * number of image components
153 */
154int fit_get_subimage_count(const void *fit, int images_noffset)
155{
156 int noffset;
157 int ndepth;
158 int count = 0;
159
160 /* Process its subnodes, print out component images details */
161 for (ndepth = 0, count = 0,
162 noffset = fdt_next_node(fit, images_noffset, &ndepth);
163 (noffset >= 0) && (ndepth > 0);
164 noffset = fdt_next_node(fit, noffset, &ndepth)) {
165 if (ndepth == 1) {
166 count++;
167 }
168 }
169
170 return count;
171}
172
Ravik Hasija7a018822021-01-27 14:01:48 -0800173#if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000174/**
Tom Rini16c4b162018-05-08 14:34:05 -0400175 * fit_image_print_data() - prints out the hash node details
176 * @fit: pointer to the FIT format image header
177 * @noffset: offset of the hash node
178 * @p: pointer to prefix string
179 * @type: Type of information to print ("hash" or "sign")
180 *
181 * fit_image_print_data() lists properties for the processed hash node
182 *
183 * This function avoid using puts() since it prints a newline on the host
184 * but does not in U-Boot.
185 *
186 * returns:
187 * no returned results
188 */
189static void fit_image_print_data(const void *fit, int noffset, const char *p,
190 const char *type)
191{
192 const char *keyname;
193 uint8_t *value;
194 int value_len;
195 char *algo;
Philippe Reynes20031562018-11-14 13:51:00 +0100196 const char *padding;
Simon Glass72188f52020-03-18 11:44:06 -0600197 bool required;
Tom Rini16c4b162018-05-08 14:34:05 -0400198 int ret, i;
199
200 debug("%s %s node: '%s'\n", p, type,
201 fit_get_name(fit, noffset, NULL));
202 printf("%s %s algo: ", p, type);
203 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
204 printf("invalid/unsupported\n");
205 return;
206 }
207 printf("%s", algo);
Simon Glass72188f52020-03-18 11:44:06 -0600208 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
209 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
Tom Rini16c4b162018-05-08 14:34:05 -0400210 if (keyname)
211 printf(":%s", keyname);
212 if (required)
213 printf(" (required)");
214 printf("\n");
215
Philippe Reynes20031562018-11-14 13:51:00 +0100216 padding = fdt_getprop(fit, noffset, "padding", NULL);
217 if (padding)
218 printf("%s %s padding: %s\n", p, type, padding);
219
Tom Rini16c4b162018-05-08 14:34:05 -0400220 ret = fit_image_hash_get_value(fit, noffset, &value,
221 &value_len);
222 printf("%s %s value: ", p, type);
223 if (ret) {
224 printf("unavailable\n");
225 } else {
226 for (i = 0; i < value_len; i++)
227 printf("%02x", value[i]);
228 printf("\n");
229 }
230
231 debug("%s %s len: %d\n", p, type, value_len);
232
233 /* Signatures have a time stamp */
234 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
235 time_t timestamp;
236
237 printf("%s Timestamp: ", p);
238 if (fit_get_timestamp(fit, noffset, &timestamp))
239 printf("unavailable\n");
240 else
241 genimg_print_time(timestamp);
242 }
243}
244
245/**
246 * fit_image_print_verification_data() - prints out the hash/signature details
247 * @fit: pointer to the FIT format image header
248 * @noffset: offset of the hash or signature node
249 * @p: pointer to prefix string
250 *
251 * This lists properties for the processed hash node
252 *
253 * returns:
254 * no returned results
255 */
256static void fit_image_print_verification_data(const void *fit, int noffset,
257 const char *p)
258{
259 const char *name;
260
261 /*
262 * Check subnode name, must be equal to "hash" or "signature".
263 * Multiple hash/signature nodes require unique unit node
264 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
265 */
266 name = fit_get_name(fit, noffset, NULL);
267 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
268 fit_image_print_data(fit, noffset, p, "Hash");
269 } else if (!strncmp(name, FIT_SIG_NODENAME,
270 strlen(FIT_SIG_NODENAME))) {
271 fit_image_print_data(fit, noffset, p, "Sign");
272 }
273}
274
275/**
276 * fit_conf_print - prints out the FIT configuration details
277 * @fit: pointer to the FIT format image header
278 * @noffset: offset of the configuration node
279 * @p: pointer to prefix string
280 *
281 * fit_conf_print() lists all mandatory properties for the processed
282 * configuration node.
283 *
284 * returns:
285 * no returned results
286 */
287static void fit_conf_print(const void *fit, int noffset, const char *p)
288{
289 char *desc;
290 const char *uname;
291 int ret;
292 int fdt_index, loadables_index;
293 int ndepth;
294
295 /* Mandatory properties */
296 ret = fit_get_desc(fit, noffset, &desc);
297 printf("%s Description: ", p);
298 if (ret)
299 printf("unavailable\n");
300 else
301 printf("%s\n", desc);
302
303 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
304 printf("%s Kernel: ", p);
305 if (!uname)
306 printf("unavailable\n");
307 else
308 printf("%s\n", uname);
309
310 /* Optional properties */
311 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
312 if (uname)
313 printf("%s Init Ramdisk: %s\n", p, uname);
314
315 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
316 if (uname)
317 printf("%s Firmware: %s\n", p, uname);
318
319 for (fdt_index = 0;
320 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
321 fdt_index, NULL), uname;
322 fdt_index++) {
323 if (fdt_index == 0)
324 printf("%s FDT: ", p);
325 else
326 printf("%s ", p);
327 printf("%s\n", uname);
328 }
329
330 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
331 if (uname)
332 printf("%s FPGA: %s\n", p, uname);
333
334 /* Print out all of the specified loadables */
335 for (loadables_index = 0;
336 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
337 loadables_index, NULL), uname;
338 loadables_index++) {
339 if (loadables_index == 0) {
340 printf("%s Loadables: ", p);
341 } else {
342 printf("%s ", p);
343 }
344 printf("%s\n", uname);
345 }
346
347 /* Process all hash subnodes of the component configuration node */
348 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
349 (noffset >= 0) && (ndepth > 0);
350 noffset = fdt_next_node(fit, noffset, &ndepth)) {
351 if (ndepth == 1) {
352 /* Direct child node of the component configuration node */
353 fit_image_print_verification_data(fit, noffset, p);
354 }
355 }
356}
357
358/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000359 * fit_print_contents - prints out the contents of the FIT format image
360 * @fit: pointer to the FIT format image header
361 * @p: pointer to prefix string
362 *
363 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500364 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000365 * the details of each component image.
366 *
367 * returns:
368 * no returned results
369 */
370void fit_print_contents(const void *fit)
371{
372 char *desc;
373 char *uname;
374 int images_noffset;
375 int confs_noffset;
376 int noffset;
377 int ndepth;
378 int count = 0;
379 int ret;
380 const char *p;
381 time_t timestamp;
382
Simon Glass1fe7d932013-05-08 08:05:58 +0000383 /* Indent string is defined in header image.h */
384 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000385
386 /* Root node properties */
387 ret = fit_get_desc(fit, 0, &desc);
388 printf("%sFIT description: ", p);
389 if (ret)
390 printf("unavailable\n");
391 else
392 printf("%s\n", desc);
393
394 if (IMAGE_ENABLE_TIMESTAMP) {
395 ret = fit_get_timestamp(fit, 0, &timestamp);
396 printf("%sCreated: ", p);
397 if (ret)
398 printf("unavailable\n");
399 else
400 genimg_print_time(timestamp);
401 }
402
403 /* Find images parent node offset */
404 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
405 if (images_noffset < 0) {
406 printf("Can't find images parent node '%s' (%s)\n",
407 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
408 return;
409 }
410
411 /* Process its subnodes, print out component images details */
412 for (ndepth = 0, count = 0,
413 noffset = fdt_next_node(fit, images_noffset, &ndepth);
414 (noffset >= 0) && (ndepth > 0);
415 noffset = fdt_next_node(fit, noffset, &ndepth)) {
416 if (ndepth == 1) {
417 /*
418 * Direct child node of the images parent node,
419 * i.e. component image node.
420 */
421 printf("%s Image %u (%s)\n", p, count++,
422 fit_get_name(fit, noffset, NULL));
423
424 fit_image_print(fit, noffset, p);
425 }
426 }
427
428 /* Find configurations parent node offset */
429 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
430 if (confs_noffset < 0) {
431 debug("Can't get configurations parent node '%s' (%s)\n",
432 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
433 return;
434 }
435
436 /* get default configuration unit name from default property */
437 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
438 if (uname)
439 printf("%s Default Configuration: '%s'\n", p, uname);
440
441 /* Process its subnodes, print out configurations details */
442 for (ndepth = 0, count = 0,
443 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
444 (noffset >= 0) && (ndepth > 0);
445 noffset = fdt_next_node(fit, noffset, &ndepth)) {
446 if (ndepth == 1) {
447 /*
448 * Direct child node of the configurations parent node,
449 * i.e. configuration node.
450 */
451 printf("%s Configuration %u (%s)\n", p, count++,
452 fit_get_name(fit, noffset, NULL));
453
454 fit_conf_print(fit, noffset, p);
455 }
456 }
457}
458
459/**
460 * fit_image_print - prints out the FIT component image details
461 * @fit: pointer to the FIT format image header
462 * @image_noffset: offset of the component image node
463 * @p: pointer to prefix string
464 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500465 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000466 * image. If present, hash nodes are printed out as well. Load
467 * address for images of type firmware is also printed out. Since the load
468 * address is not mandatory for firmware images, it will be output as
469 * "unavailable" when not present.
470 *
471 * returns:
472 * no returned results
473 */
474void fit_image_print(const void *fit, int image_noffset, const char *p)
475{
476 char *desc;
477 uint8_t type, arch, os, comp;
478 size_t size;
479 ulong load, entry;
480 const void *data;
481 int noffset;
482 int ndepth;
483 int ret;
484
485 /* Mandatory properties */
486 ret = fit_get_desc(fit, image_noffset, &desc);
487 printf("%s Description: ", p);
488 if (ret)
489 printf("unavailable\n");
490 else
491 printf("%s\n", desc);
492
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700493 if (IMAGE_ENABLE_TIMESTAMP) {
494 time_t timestamp;
495
496 ret = fit_get_timestamp(fit, 0, &timestamp);
497 printf("%s Created: ", p);
498 if (ret)
499 printf("unavailable\n");
500 else
501 genimg_print_time(timestamp);
502 }
503
Simon Glass53fbb7e2013-05-07 06:11:53 +0000504 fit_image_get_type(fit, image_noffset, &type);
505 printf("%s Type: %s\n", p, genimg_get_type_name(type));
506
507 fit_image_get_comp(fit, image_noffset, &comp);
508 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
509
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800510 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000511
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100512 if (!host_build()) {
513 printf("%s Data Start: ", p);
514 if (ret) {
515 printf("unavailable\n");
516 } else {
517 void *vdata = (void *)data;
Simon Glassc6ac13b2013-05-16 13:53:26 +0000518
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100519 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
520 }
Simon Glassc6ac13b2013-05-16 13:53:26 +0000521 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000522
523 printf("%s Data Size: ", p);
524 if (ret)
525 printf("unavailable\n");
526 else
527 genimg_print_size(size);
528
529 /* Remaining, type dependent properties */
530 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
531 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
532 (type == IH_TYPE_FLATDT)) {
533 fit_image_get_arch(fit, image_noffset, &arch);
534 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
535 }
536
Michal Simek6faf4622018-03-26 16:31:27 +0200537 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
538 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000539 fit_image_get_os(fit, image_noffset, &os);
540 printf("%s OS: %s\n", p, genimg_get_os_name(os));
541 }
542
543 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200544 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
545 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000546 ret = fit_image_get_load(fit, image_noffset, &load);
547 printf("%s Load Address: ", p);
548 if (ret)
549 printf("unavailable\n");
550 else
551 printf("0x%08lx\n", load);
552 }
553
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300554 /* optional load address for FDT */
555 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
556 printf("%s Load Address: 0x%08lx\n", p, load);
557
Simon Glass53fbb7e2013-05-07 06:11:53 +0000558 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
559 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800560 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000561 printf("%s Entry Point: ", p);
562 if (ret)
563 printf("unavailable\n");
564 else
565 printf("0x%08lx\n", entry);
566 }
567
568 /* Process all hash subnodes of the component image node */
569 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
570 (noffset >= 0) && (ndepth > 0);
571 noffset = fdt_next_node(fit, noffset, &ndepth)) {
572 if (ndepth == 1) {
573 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000574 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000575 }
576 }
577}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200578#else
579void fit_print_contents(const void *fit) { }
580void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Ravik Hasija7a018822021-01-27 14:01:48 -0800581#endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000582
583/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000584 * fit_get_desc - get node description property
585 * @fit: pointer to the FIT format image header
586 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500587 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000588 *
589 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500590 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000591 *
592 * returns:
593 * 0, on success
594 * -1, on failure
595 */
596int fit_get_desc(const void *fit, int noffset, char **desc)
597{
598 int len;
599
600 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
601 if (*desc == NULL) {
602 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
603 return -1;
604 }
605
606 return 0;
607}
608
609/**
610 * fit_get_timestamp - get node timestamp property
611 * @fit: pointer to the FIT format image header
612 * @noffset: node offset
613 * @timestamp: pointer to the time_t, will hold read timestamp
614 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500615 * fit_get_timestamp() reads timestamp property from given node, if timestamp
616 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000617 * argument.
618 *
619 * returns:
620 * 0, on success
621 * -1, on property read failure
622 * -2, on wrong timestamp size
623 */
624int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
625{
626 int len;
627 const void *data;
628
629 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
630 if (data == NULL) {
631 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
632 return -1;
633 }
634 if (len != sizeof(uint32_t)) {
635 debug("FIT timestamp with incorrect size of (%u)\n", len);
636 return -2;
637 }
638
639 *timestamp = uimage_to_cpu(*((uint32_t *)data));
640 return 0;
641}
642
643/**
644 * fit_image_get_node - get node offset for component image of a given unit name
645 * @fit: pointer to the FIT format image header
646 * @image_uname: component image node unit name
647 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500648 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000649 * node) of a provided unit name. If image is found its node offset is
650 * returned to the caller.
651 *
652 * returns:
653 * image node offset when found (>=0)
654 * negative number on failure (FDT_ERR_* code)
655 */
656int fit_image_get_node(const void *fit, const char *image_uname)
657{
658 int noffset, images_noffset;
659
660 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
661 if (images_noffset < 0) {
662 debug("Can't find images parent node '%s' (%s)\n",
663 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
664 return images_noffset;
665 }
666
667 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
668 if (noffset < 0) {
669 debug("Can't get node offset for image unit name: '%s' (%s)\n",
670 image_uname, fdt_strerror(noffset));
671 }
672
673 return noffset;
674}
675
676/**
677 * fit_image_get_os - get os id for a given component image node
678 * @fit: pointer to the FIT format image header
679 * @noffset: component image node offset
680 * @os: pointer to the uint8_t, will hold os numeric id
681 *
682 * fit_image_get_os() finds os property in a given component image node.
683 * If the property is found, its (string) value is translated to the numeric
684 * id which is returned to the caller.
685 *
686 * returns:
687 * 0, on success
688 * -1, on failure
689 */
690int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
691{
692 int len;
693 const void *data;
694
695 /* Get OS name from property data */
696 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
697 if (data == NULL) {
698 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
699 *os = -1;
700 return -1;
701 }
702
703 /* Translate OS name to id */
704 *os = genimg_get_os_id(data);
705 return 0;
706}
707
708/**
709 * fit_image_get_arch - get arch id for a given component image node
710 * @fit: pointer to the FIT format image header
711 * @noffset: component image node offset
712 * @arch: pointer to the uint8_t, will hold arch numeric id
713 *
714 * fit_image_get_arch() finds arch property in a given component image node.
715 * If the property is found, its (string) value is translated to the numeric
716 * id which is returned to the caller.
717 *
718 * returns:
719 * 0, on success
720 * -1, on failure
721 */
722int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
723{
724 int len;
725 const void *data;
726
727 /* Get architecture name from property data */
728 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
729 if (data == NULL) {
730 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
731 *arch = -1;
732 return -1;
733 }
734
735 /* Translate architecture name to id */
736 *arch = genimg_get_arch_id(data);
737 return 0;
738}
739
740/**
741 * fit_image_get_type - get type id for a given component image node
742 * @fit: pointer to the FIT format image header
743 * @noffset: component image node offset
744 * @type: pointer to the uint8_t, will hold type numeric id
745 *
746 * fit_image_get_type() finds type property in a given component image node.
747 * If the property is found, its (string) value is translated to the numeric
748 * id which is returned to the caller.
749 *
750 * returns:
751 * 0, on success
752 * -1, on failure
753 */
754int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
755{
756 int len;
757 const void *data;
758
759 /* Get image type name from property data */
760 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
761 if (data == NULL) {
762 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
763 *type = -1;
764 return -1;
765 }
766
767 /* Translate image type name to id */
768 *type = genimg_get_type_id(data);
769 return 0;
770}
771
772/**
773 * fit_image_get_comp - get comp id for a given component image node
774 * @fit: pointer to the FIT format image header
775 * @noffset: component image node offset
776 * @comp: pointer to the uint8_t, will hold comp numeric id
777 *
778 * fit_image_get_comp() finds comp property in a given component image node.
779 * If the property is found, its (string) value is translated to the numeric
780 * id which is returned to the caller.
781 *
782 * returns:
783 * 0, on success
784 * -1, on failure
785 */
786int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
787{
788 int len;
789 const void *data;
790
791 /* Get compression name from property data */
792 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
793 if (data == NULL) {
794 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
795 *comp = -1;
796 return -1;
797 }
798
799 /* Translate compression name to id */
800 *comp = genimg_get_comp_id(data);
801 return 0;
802}
803
York Sun60047652016-02-29 15:48:40 -0800804static int fit_image_get_address(const void *fit, int noffset, char *name,
805 ulong *load)
806{
York Sunc1913cb2016-02-29 15:48:41 -0800807 int len, cell_len;
808 const fdt32_t *cell;
809 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800810
York Sunc1913cb2016-02-29 15:48:41 -0800811 cell = fdt_getprop(fit, noffset, name, &len);
812 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800813 fit_get_debug(fit, noffset, name, len);
814 return -1;
815 }
816
York Sunc1913cb2016-02-29 15:48:41 -0800817 cell_len = len >> 2;
818 /* Use load64 to avoid compiling warning for 32-bit target */
819 while (cell_len--) {
820 load64 = (load64 << 32) | uimage_to_cpu(*cell);
821 cell++;
822 }
Michal Simek13d1ca82020-09-03 12:44:51 +0200823
824 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
825 printf("Unsupported %s address size\n", name);
826 return -1;
827 }
828
York Sunc1913cb2016-02-29 15:48:41 -0800829 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800830
831 return 0;
832}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000833/**
834 * fit_image_get_load() - get load addr property for given component image node
835 * @fit: pointer to the FIT format image header
836 * @noffset: component image node offset
837 * @load: pointer to the uint32_t, will hold load address
838 *
839 * fit_image_get_load() finds load address property in a given component
840 * image node. If the property is found, its value is returned to the caller.
841 *
842 * returns:
843 * 0, on success
844 * -1, on failure
845 */
846int fit_image_get_load(const void *fit, int noffset, ulong *load)
847{
York Sun60047652016-02-29 15:48:40 -0800848 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000849}
850
851/**
852 * fit_image_get_entry() - get entry point address property
853 * @fit: pointer to the FIT format image header
854 * @noffset: component image node offset
855 * @entry: pointer to the uint32_t, will hold entry point address
856 *
857 * This gets the entry point address property for a given component image
858 * node.
859 *
860 * fit_image_get_entry() finds entry point address property in a given
861 * component image node. If the property is found, its value is returned
862 * to the caller.
863 *
864 * returns:
865 * 0, on success
866 * -1, on failure
867 */
868int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
869{
York Sun60047652016-02-29 15:48:40 -0800870 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000871}
872
873/**
874 * fit_image_get_data - get data property and its size for a given component image node
875 * @fit: pointer to the FIT format image header
876 * @noffset: component image node offset
877 * @data: double pointer to void, will hold data property's data address
878 * @size: pointer to size_t, will hold data property's data size
879 *
880 * fit_image_get_data() finds data property in a given component image node.
881 * If the property is found its data start address and size are returned to
882 * the caller.
883 *
884 * returns:
885 * 0, on success
886 * -1, on failure
887 */
888int fit_image_get_data(const void *fit, int noffset,
889 const void **data, size_t *size)
890{
891 int len;
892
893 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
894 if (*data == NULL) {
895 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
896 *size = 0;
897 return -1;
898 }
899
900 *size = len;
901 return 0;
902}
903
904/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200905 * Get 'data-offset' property from a given image node.
906 *
907 * @fit: pointer to the FIT image header
908 * @noffset: component image node offset
909 * @data_offset: holds the data-offset property
910 *
911 * returns:
912 * 0, on success
913 * -ENOENT if the property could not be found
914 */
915int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
916{
917 const fdt32_t *val;
918
919 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
920 if (!val)
921 return -ENOENT;
922
923 *data_offset = fdt32_to_cpu(*val);
924
925 return 0;
926}
927
928/**
Peng Fana1be94b2017-12-05 13:20:59 +0800929 * Get 'data-position' property from a given image node.
930 *
931 * @fit: pointer to the FIT image header
932 * @noffset: component image node offset
933 * @data_position: holds the data-position property
934 *
935 * returns:
936 * 0, on success
937 * -ENOENT if the property could not be found
938 */
939int fit_image_get_data_position(const void *fit, int noffset,
940 int *data_position)
941{
942 const fdt32_t *val;
943
944 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
945 if (!val)
946 return -ENOENT;
947
948 *data_position = fdt32_to_cpu(*val);
949
950 return 0;
951}
952
953/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200954 * Get 'data-size' property from a given image node.
955 *
956 * @fit: pointer to the FIT image header
957 * @noffset: component image node offset
958 * @data_size: holds the data-size property
959 *
960 * returns:
961 * 0, on success
962 * -ENOENT if the property could not be found
963 */
964int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
965{
966 const fdt32_t *val;
967
968 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
969 if (!val)
970 return -ENOENT;
971
972 *data_size = fdt32_to_cpu(*val);
973
974 return 0;
975}
976
977/**
Philippe Reynes4df35782019-12-18 18:25:42 +0100978 * Get 'data-size-unciphered' property from a given image node.
979 *
980 * @fit: pointer to the FIT image header
981 * @noffset: component image node offset
982 * @data_size: holds the data-size property
983 *
984 * returns:
985 * 0, on success
986 * -ENOENT if the property could not be found
987 */
988int fit_image_get_data_size_unciphered(const void *fit, int noffset,
989 size_t *data_size)
990{
991 const fdt32_t *val;
992
993 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
994 if (!val)
995 return -ENOENT;
996
997 *data_size = (size_t)fdt32_to_cpu(*val);
998
999 return 0;
1000}
1001
1002/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001003 * fit_image_get_data_and_size - get data and its size including
1004 * both embedded and external data
1005 * @fit: pointer to the FIT format image header
1006 * @noffset: component image node offset
1007 * @data: double pointer to void, will hold data property's data address
1008 * @size: pointer to size_t, will hold data property's data size
1009 *
1010 * fit_image_get_data_and_size() finds data and its size including
1011 * both embedded and external data. If the property is found
1012 * its data start address and size are returned to the caller.
1013 *
1014 * returns:
1015 * 0, on success
1016 * otherwise, on failure
1017 */
1018int fit_image_get_data_and_size(const void *fit, int noffset,
1019 const void **data, size_t *size)
1020{
1021 bool external_data = false;
1022 int offset;
1023 int len;
1024 int ret;
1025
1026 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1027 external_data = true;
1028 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1029 external_data = true;
1030 /*
1031 * For FIT with external data, figure out where
1032 * the external images start. This is the base
1033 * for the data-offset properties in each image.
1034 */
1035 offset += ((fdt_totalsize(fit) + 3) & ~3);
1036 }
1037
1038 if (external_data) {
1039 debug("External Data\n");
1040 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt18378042020-03-11 21:51:08 +01001041 if (!ret) {
1042 *data = fit + offset;
1043 *size = len;
1044 }
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001045 } else {
1046 ret = fit_image_get_data(fit, noffset, data, size);
1047 }
1048
1049 return ret;
1050}
1051
1052/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001053 * fit_image_hash_get_algo - get hash algorithm name
1054 * @fit: pointer to the FIT format image header
1055 * @noffset: hash node offset
1056 * @algo: double pointer to char, will hold pointer to the algorithm name
1057 *
1058 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1059 * If the property is found its data start address is returned to the caller.
1060 *
1061 * returns:
1062 * 0, on success
1063 * -1, on failure
1064 */
1065int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1066{
1067 int len;
1068
1069 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1070 if (*algo == NULL) {
1071 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1072 return -1;
1073 }
1074
1075 return 0;
1076}
1077
1078/**
1079 * fit_image_hash_get_value - get hash value and length
1080 * @fit: pointer to the FIT format image header
1081 * @noffset: hash node offset
1082 * @value: double pointer to uint8_t, will hold address of a hash value data
1083 * @value_len: pointer to an int, will hold hash data length
1084 *
1085 * fit_image_hash_get_value() finds hash value property in a given hash node.
1086 * If the property is found its data start address and size are returned to
1087 * the caller.
1088 *
1089 * returns:
1090 * 0, on success
1091 * -1, on failure
1092 */
1093int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1094 int *value_len)
1095{
1096 int len;
1097
1098 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1099 if (*value == NULL) {
1100 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1101 *value_len = 0;
1102 return -1;
1103 }
1104
1105 *value_len = len;
1106 return 0;
1107}
1108
Simon Glass53fbb7e2013-05-07 06:11:53 +00001109/**
1110 * fit_image_hash_get_ignore - get hash ignore flag
1111 * @fit: pointer to the FIT format image header
1112 * @noffset: hash node offset
1113 * @ignore: pointer to an int, will hold hash ignore flag
1114 *
1115 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1116 * If the property is found and non-zero, the hash algorithm is not verified by
1117 * u-boot automatically.
1118 *
1119 * returns:
1120 * 0, on ignore not found
1121 * value, on ignore found
1122 */
Simon Glassab9efc62013-05-07 06:11:58 +00001123static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001124{
1125 int len;
1126 int *value;
1127
1128 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1129 if (value == NULL || len != sizeof(int))
1130 *ignore = 0;
1131 else
1132 *ignore = *value;
1133
1134 return 0;
1135}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001136
Philippe Reynes7298e422019-12-18 18:25:41 +01001137/**
1138 * fit_image_cipher_get_algo - get cipher algorithm name
1139 * @fit: pointer to the FIT format image header
1140 * @noffset: cipher node offset
1141 * @algo: double pointer to char, will hold pointer to the algorithm name
1142 *
1143 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1144 * cipher node. If the property is found its data start address is returned
1145 * to the caller.
1146 *
1147 * returns:
1148 * 0, on success
1149 * -1, on failure
1150 */
1151int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1152{
1153 int len;
1154
1155 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1156 if (!*algo) {
1157 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1158 return -1;
1159 }
1160
1161 return 0;
1162}
1163
Simon Glass7a80de42016-02-24 09:14:42 -07001164ulong fit_get_end(const void *fit)
1165{
1166 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1167}
1168
Simon Glass53fbb7e2013-05-07 06:11:53 +00001169/**
1170 * fit_set_timestamp - set node timestamp property
1171 * @fit: pointer to the FIT format image header
1172 * @noffset: node offset
1173 * @timestamp: timestamp value to be set
1174 *
1175 * fit_set_timestamp() attempts to set timestamp property in the requested
1176 * node and returns operation status to the caller.
1177 *
1178 * returns:
1179 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001180 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001181 */
1182int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1183{
1184 uint32_t t;
1185 int ret;
1186
1187 t = cpu_to_uimage(timestamp);
1188 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1189 sizeof(uint32_t));
1190 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001191 debug("Can't set '%s' property for '%s' node (%s)\n",
1192 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1193 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001194 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001195 }
1196
1197 return 0;
1198}
1199
1200/**
1201 * calculate_hash - calculate and return hash for provided input data
1202 * @data: pointer to the input data
1203 * @data_len: data length
1204 * @algo: requested hash algorithm
1205 * @value: pointer to the char, will hold hash value data (caller must
1206 * allocate enough free space)
1207 * value_len: length of the calculated hash
1208 *
1209 * calculate_hash() computes input data hash according to the requested
1210 * algorithm.
1211 * Resulting hash value is placed in caller provided 'value' buffer, length
1212 * of the calculated hash is returned via value_len pointer argument.
1213 *
1214 * returns:
1215 * 0, on success
1216 * -1, when algo is unsupported
1217 */
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001218int calculate_hash(const void *data, int data_len, const char *name,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001219 uint8_t *value, int *value_len)
1220{
Chia-Wei Wangca479552021-07-30 09:08:05 +08001221#if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
1222 int rc;
1223 enum HASH_ALGO hash_algo;
1224 struct udevice *dev;
1225
1226 rc = uclass_get_device(UCLASS_HASH, 0, &dev);
1227 if (rc) {
1228 debug("failed to get hash device, rc=%d\n", rc);
1229 return -1;
1230 }
1231
1232 hash_algo = hash_algo_lookup_by_name(algo);
1233 if (hash_algo == HASH_ALGO_INVALID) {
1234 debug("Unsupported hash algorithm\n");
1235 return -1;
1236 };
1237
1238 rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
1239 if (rc) {
1240 debug("failed to get hash value, rc=%d\n", rc);
1241 return -1;
1242 }
1243
1244 *value_len = hash_algo_digest_size(hash_algo);
1245#else
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001246 struct hash_algo *algo;
1247 int ret;
1248
1249 ret = hash_lookup_algo(name, &algo);
1250 if (ret < 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001251 debug("Unsupported hash alogrithm\n");
1252 return -1;
1253 }
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001254
1255 algo->hash_func_ws(data, data_len, value, algo->chunk_size);
1256 *value_len = algo->digest_size;
Chia-Wei Wangca479552021-07-30 09:08:05 +08001257#endif
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001258
Simon Glass53fbb7e2013-05-07 06:11:53 +00001259 return 0;
1260}
1261
Simon Glassab9efc62013-05-07 06:11:58 +00001262static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1263 size_t size, char **err_msgp)
1264{
1265 uint8_t value[FIT_MAX_HASH_LEN];
1266 int value_len;
1267 char *algo;
1268 uint8_t *fit_value;
1269 int fit_value_len;
1270 int ignore;
1271
1272 *err_msgp = NULL;
1273
1274 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001275 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001276 return -1;
1277 }
1278 printf("%s", algo);
1279
1280 if (IMAGE_ENABLE_IGNORE) {
1281 fit_image_hash_get_ignore(fit, noffset, &ignore);
1282 if (ignore) {
1283 printf("-skipped ");
1284 return 0;
1285 }
1286 }
1287
1288 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1289 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001290 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001291 return -1;
1292 }
1293
1294 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001295 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001296 return -1;
1297 }
1298
1299 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001300 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001301 return -1;
1302 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001303 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001304 return -1;
1305 }
1306
1307 return 0;
1308}
1309
Jun Nie5c643db2018-02-27 16:55:58 +08001310int fit_image_verify_with_data(const void *fit, int image_noffset,
1311 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001312{
Simon Glass56518e72013-06-13 15:10:01 -07001313 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001314 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001315 int verify_all = 1;
1316 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001317
Simon Glass56518e72013-06-13 15:10:01 -07001318 /* Verify all required signatures */
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001319 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glass56518e72013-06-13 15:10:01 -07001320 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1321 gd_fdt_blob(), &verify_all)) {
1322 err_msg = "Unable to verify required signature";
1323 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001324 }
1325
1326 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001327 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001328 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001329
Simon Glassab9efc62013-05-07 06:11:58 +00001330 /*
1331 * Check subnode name, must be equal to "hash".
1332 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001333 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001334 */
1335 if (!strncmp(name, FIT_HASH_NODENAME,
1336 strlen(FIT_HASH_NODENAME))) {
1337 if (fit_image_check_hash(fit, noffset, data, size,
1338 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001339 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001340 puts("+ ");
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001341 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glass56518e72013-06-13 15:10:01 -07001342 !strncmp(name, FIT_SIG_NODENAME,
1343 strlen(FIT_SIG_NODENAME))) {
1344 ret = fit_image_check_sig(fit, noffset, data,
1345 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001346
1347 /*
1348 * Show an indication on failure, but do not return
1349 * an error. Only keys marked 'required' can cause
1350 * an image validation failure. See the call to
1351 * fit_image_verify_required_sigs() above.
1352 */
1353 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001354 puts("- ");
1355 else
1356 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001357 }
1358 }
1359
1360 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001361 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001362 goto error;
1363 }
1364
1365 return 1;
1366
1367error:
Simon Glasse754da22013-05-07 06:11:59 +00001368 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001369 err_msg, fit_get_name(fit, noffset, NULL),
1370 fit_get_name(fit, image_noffset, NULL));
1371 return 0;
1372}
1373
1374/**
Jun Nie5c643db2018-02-27 16:55:58 +08001375 * fit_image_verify - verify data integrity
1376 * @fit: pointer to the FIT format image header
1377 * @image_noffset: component image node offset
1378 *
1379 * fit_image_verify() goes over component image hash nodes,
1380 * re-calculates each data hash and compares with the value stored in hash
1381 * node.
1382 *
1383 * returns:
1384 * 1, if all hashes are valid
1385 * 0, otherwise (or on error)
1386 */
1387int fit_image_verify(const void *fit, int image_noffset)
1388{
Simon Glass79af75f2021-02-15 17:08:06 -07001389 const char *name = fit_get_name(fit, image_noffset, NULL);
Jun Nie5c643db2018-02-27 16:55:58 +08001390 const void *data;
1391 size_t size;
Jun Nie5c643db2018-02-27 16:55:58 +08001392 char *err_msg = "";
1393
Simon Glass1ac9c4c2021-07-05 16:32:56 -06001394 if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) {
Simon Glass79af75f2021-02-15 17:08:06 -07001395 /*
1396 * We don't support this since libfdt considers names with the
1397 * name root but different @ suffix to be equal
1398 */
1399 err_msg = "Node name contains @";
1400 goto err;
1401 }
Jun Nie5c643db2018-02-27 16:55:58 +08001402 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001403 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001404 err_msg = "Can't get image data/size";
Simon Glass79af75f2021-02-15 17:08:06 -07001405 goto err;
Jun Nie5c643db2018-02-27 16:55:58 +08001406 }
1407
1408 return fit_image_verify_with_data(fit, image_noffset, data, size);
Simon Glass79af75f2021-02-15 17:08:06 -07001409
1410err:
1411 printf("error!\n%s in '%s' image node\n", err_msg,
1412 fit_get_name(fit, image_noffset, NULL));
1413 return 0;
Jun Nie5c643db2018-02-27 16:55:58 +08001414}
1415
1416/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001417 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001418 * @fit: pointer to the FIT format image header
1419 *
Simon Glassb8da8362013-05-07 06:11:57 +00001420 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001421 * for every images checks if all it's hashes are valid.
1422 *
1423 * returns:
1424 * 1, if all hashes of all images are valid
1425 * 0, otherwise (or on error)
1426 */
Simon Glassb8da8362013-05-07 06:11:57 +00001427int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001428{
1429 int images_noffset;
1430 int noffset;
1431 int ndepth;
1432 int count;
1433
1434 /* Find images parent node offset */
1435 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1436 if (images_noffset < 0) {
1437 printf("Can't find images parent node '%s' (%s)\n",
1438 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1439 return 0;
1440 }
1441
1442 /* Process all image subnodes, check hashes for each */
1443 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1444 (ulong)fit);
1445 for (ndepth = 0, count = 0,
1446 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1447 (noffset >= 0) && (ndepth > 0);
1448 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1449 if (ndepth == 1) {
1450 /*
1451 * Direct child node of the images parent node,
1452 * i.e. component image node.
1453 */
Simon Glass73223f02016-02-22 22:55:43 -07001454 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001455 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001456 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001457
Simon Glassb8da8362013-05-07 06:11:57 +00001458 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001459 return 0;
1460 printf("\n");
1461 }
1462 }
1463 return 1;
1464}
1465
Philippe Reynes4df35782019-12-18 18:25:42 +01001466static int fit_image_uncipher(const void *fit, int image_noffset,
1467 void **data, size_t *size)
1468{
1469 int cipher_noffset, ret;
1470 void *dst;
1471 size_t size_dst;
1472
1473 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1474 FIT_CIPHER_NODENAME);
1475 if (cipher_noffset < 0)
1476 return 0;
1477
1478 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1479 *data, *size, &dst, &size_dst);
1480 if (ret)
1481 goto out;
1482
1483 *data = dst;
1484 *size = size_dst;
1485
1486 out:
1487 return ret;
1488}
Philippe Reynes4df35782019-12-18 18:25:42 +01001489
Simon Glass53fbb7e2013-05-07 06:11:53 +00001490/**
1491 * fit_image_check_os - check whether image node is of a given os type
1492 * @fit: pointer to the FIT format image header
1493 * @noffset: component image node offset
1494 * @os: requested image os
1495 *
1496 * fit_image_check_os() reads image os property and compares its numeric
1497 * id with the requested os. Comparison result is returned to the caller.
1498 *
1499 * returns:
1500 * 1 if image is of given os type
1501 * 0 otherwise (or on error)
1502 */
1503int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1504{
1505 uint8_t image_os;
1506
1507 if (fit_image_get_os(fit, noffset, &image_os))
1508 return 0;
1509 return (os == image_os);
1510}
1511
1512/**
1513 * fit_image_check_arch - check whether image node is of a given arch
1514 * @fit: pointer to the FIT format image header
1515 * @noffset: component image node offset
1516 * @arch: requested imagearch
1517 *
1518 * fit_image_check_arch() reads image arch property and compares its numeric
1519 * id with the requested arch. Comparison result is returned to the caller.
1520 *
1521 * returns:
1522 * 1 if image is of given arch
1523 * 0 otherwise (or on error)
1524 */
1525int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1526{
1527 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001528 int aarch32_support = 0;
1529
Simon Glasse2734d62021-03-15 18:11:12 +13001530 /* Let's assume that sandbox can load any architecture */
1531 if (IS_ENABLED(CONFIG_SANDBOX))
1532 return true;
1533
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01001534 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1535 aarch32_support = 1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001536
1537 if (fit_image_get_arch(fit, noffset, &image_arch))
1538 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001539 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001540 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1541 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1542 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001543}
1544
1545/**
1546 * fit_image_check_type - check whether image node is of a given type
1547 * @fit: pointer to the FIT format image header
1548 * @noffset: component image node offset
1549 * @type: requested image type
1550 *
1551 * fit_image_check_type() reads image type property and compares its numeric
1552 * id with the requested type. Comparison result is returned to the caller.
1553 *
1554 * returns:
1555 * 1 if image is of given type
1556 * 0 otherwise (or on error)
1557 */
1558int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1559{
1560 uint8_t image_type;
1561
1562 if (fit_image_get_type(fit, noffset, &image_type))
1563 return 0;
1564 return (type == image_type);
1565}
1566
1567/**
1568 * fit_image_check_comp - check whether image node uses given compression
1569 * @fit: pointer to the FIT format image header
1570 * @noffset: component image node offset
1571 * @comp: requested image compression type
1572 *
1573 * fit_image_check_comp() reads image compression property and compares its
1574 * numeric id with the requested compression type. Comparison result is
1575 * returned to the caller.
1576 *
1577 * returns:
1578 * 1 if image uses requested compression
1579 * 0 otherwise (or on error)
1580 */
1581int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1582{
1583 uint8_t image_comp;
1584
1585 if (fit_image_get_comp(fit, noffset, &image_comp))
1586 return 0;
1587 return (comp == image_comp);
1588}
1589
Simon Glass3f04db82021-02-15 17:08:12 -07001590/**
1591 * fdt_check_no_at() - Check for nodes whose names contain '@'
1592 *
1593 * This checks the parent node and all subnodes recursively
1594 *
1595 * @fit: FIT to check
1596 * @parent: Parent node to check
1597 * @return 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1598 */
1599static int fdt_check_no_at(const void *fit, int parent)
1600{
1601 const char *name;
1602 int node;
1603 int ret;
1604
1605 name = fdt_get_name(fit, parent, NULL);
1606 if (!name || strchr(name, '@'))
1607 return -EADDRNOTAVAIL;
1608
1609 fdt_for_each_subnode(node, fit, parent) {
1610 ret = fdt_check_no_at(fit, node);
1611 if (ret)
1612 return ret;
1613 }
1614
1615 return 0;
1616}
1617
Simon Glassc5819702021-02-15 17:08:09 -07001618int fit_check_format(const void *fit, ulong size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001619{
Simon Glassc5819702021-02-15 17:08:09 -07001620 int ret;
1621
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001622 /* A FIT image must be a valid FDT */
Simon Glassc5819702021-02-15 17:08:09 -07001623 ret = fdt_check_header(fit);
1624 if (ret) {
1625 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1626 ret);
1627 return -ENOEXEC;
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001628 }
1629
Simon Glass6f3c2d82021-02-15 17:08:10 -07001630 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1631 /*
1632 * If we are not given the size, make do wtih calculating it.
1633 * This is not as secure, so we should consider a flag to
1634 * control this.
1635 */
1636 if (size == IMAGE_SIZE_INVAL)
1637 size = fdt_totalsize(fit);
1638 ret = fdt_check_full(fit, size);
Simon Glass3f04db82021-02-15 17:08:12 -07001639 if (ret)
1640 ret = -EINVAL;
Simon Glass6f3c2d82021-02-15 17:08:10 -07001641
Simon Glass3f04db82021-02-15 17:08:12 -07001642 /*
1643 * U-Boot stopped using unit addressed in 2017. Since libfdt
1644 * can match nodes ignoring any unit address, signature
1645 * verification can see the wrong node if one is inserted with
1646 * the same name as a valid node but with a unit address
1647 * attached. Protect against this by disallowing unit addresses.
1648 */
1649 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1650 ret = fdt_check_no_at(fit, 0);
1651
1652 if (ret) {
1653 log_debug("FIT check error %d\n", ret);
1654 return ret;
1655 }
1656 }
Simon Glass6f3c2d82021-02-15 17:08:10 -07001657 if (ret) {
1658 log_debug("FIT check error %d\n", ret);
Simon Glass3f04db82021-02-15 17:08:12 -07001659 return ret;
Simon Glass6f3c2d82021-02-15 17:08:10 -07001660 }
1661 }
1662
Simon Glass53fbb7e2013-05-07 06:11:53 +00001663 /* mandatory / node 'description' property */
Simon Glassc5819702021-02-15 17:08:09 -07001664 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1665 log_debug("Wrong FIT format: no description\n");
1666 return -ENOMSG;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001667 }
1668
1669 if (IMAGE_ENABLE_TIMESTAMP) {
1670 /* mandatory / node 'timestamp' property */
Simon Glassc5819702021-02-15 17:08:09 -07001671 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1672 log_debug("Wrong FIT format: no timestamp\n");
Simon Glass29cbc4b2021-02-24 08:50:32 -05001673 return -EBADMSG;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001674 }
1675 }
1676
1677 /* mandatory subimages parent '/images' node */
1678 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
Simon Glassc5819702021-02-15 17:08:09 -07001679 log_debug("Wrong FIT format: no images parent node\n");
1680 return -ENOENT;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001681 }
1682
Simon Glassc5819702021-02-15 17:08:09 -07001683 return 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001684}
1685
Simon Glass53fbb7e2013-05-07 06:11:53 +00001686/**
1687 * fit_conf_find_compat
1688 * @fit: pointer to the FIT format image header
1689 * @fdt: pointer to the device tree to compare against
1690 *
1691 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1692 * most compatible with the passed in device tree.
1693 *
1694 * Example:
1695 *
1696 * / o image-tree
1697 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001698 * | |-o fdt-1
1699 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001700 * |
1701 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001702 * |-o config-1
1703 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001704 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001705 * |-o config-2
1706 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001707 *
1708 * / o U-Boot fdt
1709 * |-compatible = "foo,bar", "bim,bam"
1710 *
1711 * / o kernel fdt1
1712 * |-compatible = "foo,bar",
1713 *
1714 * / o kernel fdt2
1715 * |-compatible = "bim,bam", "baz,biz"
1716 *
1717 * Configuration 1 would be picked because the first string in U-Boot's
1718 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1719 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1720 *
Julius Werner18cfa612019-07-24 19:37:56 -07001721 * As an optimization, the compatible property from the FDT's root node can be
1722 * copied into the configuration node in the FIT image. This is required to
1723 * match configurations with compressed FDTs.
1724 *
Simon Glass53fbb7e2013-05-07 06:11:53 +00001725 * returns:
1726 * offset to the configuration to use if one was found
1727 * -1 otherwise
1728 */
1729int fit_conf_find_compat(const void *fit, const void *fdt)
1730{
1731 int ndepth = 0;
1732 int noffset, confs_noffset, images_noffset;
1733 const void *fdt_compat;
1734 int fdt_compat_len;
1735 int best_match_offset = 0;
1736 int best_match_pos = 0;
1737
1738 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1739 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1740 if (confs_noffset < 0 || images_noffset < 0) {
1741 debug("Can't find configurations or images nodes.\n");
1742 return -1;
1743 }
1744
1745 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1746 if (!fdt_compat) {
1747 debug("Fdt for comparison has no \"compatible\" property.\n");
1748 return -1;
1749 }
1750
1751 /*
1752 * Loop over the configurations in the FIT image.
1753 */
1754 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1755 (noffset >= 0) && (ndepth > 0);
1756 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001757 const void *fdt;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001758 const char *kfdt_name;
Julius Werner18cfa612019-07-24 19:37:56 -07001759 int kfdt_noffset, compat_noffset;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001760 const char *cur_fdt_compat;
1761 int len;
Julius Werner18cfa612019-07-24 19:37:56 -07001762 size_t sz;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001763 int i;
1764
1765 if (ndepth > 1)
1766 continue;
1767
Julius Werner18cfa612019-07-24 19:37:56 -07001768 /* If there's a compat property in the config node, use that. */
1769 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1770 fdt = fit; /* search in FIT image */
1771 compat_noffset = noffset; /* search under config node */
1772 } else { /* Otherwise extract it from the kernel FDT. */
1773 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1774 if (!kfdt_name) {
1775 debug("No fdt property found.\n");
1776 continue;
1777 }
1778 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1779 kfdt_name);
1780 if (kfdt_noffset < 0) {
1781 debug("No image node named \"%s\" found.\n",
1782 kfdt_name);
1783 continue;
1784 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001785
Julius Werner18cfa612019-07-24 19:37:56 -07001786 if (!fit_image_check_comp(fit, kfdt_noffset,
1787 IH_COMP_NONE)) {
1788 debug("Can't extract compat from \"%s\" "
1789 "(compressed)\n", kfdt_name);
1790 continue;
1791 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001792
Julius Werner18cfa612019-07-24 19:37:56 -07001793 /* search in this config's kernel FDT */
John Keeping650bf002021-06-25 17:58:04 +01001794 if (fit_image_get_data_and_size(fit, kfdt_noffset,
1795 &fdt, &sz)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001796 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1797 continue;
1798 }
1799
1800 compat_noffset = 0; /* search kFDT under root node */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001801 }
1802
1803 len = fdt_compat_len;
1804 cur_fdt_compat = fdt_compat;
1805 /*
1806 * Look for a match for each U-Boot compatibility string in
Julius Werner18cfa612019-07-24 19:37:56 -07001807 * turn in the compat string property.
Simon Glass53fbb7e2013-05-07 06:11:53 +00001808 */
1809 for (i = 0; len > 0 &&
1810 (!best_match_offset || best_match_pos > i); i++) {
1811 int cur_len = strlen(cur_fdt_compat) + 1;
1812
Julius Werner18cfa612019-07-24 19:37:56 -07001813 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001814 cur_fdt_compat)) {
1815 best_match_offset = noffset;
1816 best_match_pos = i;
1817 break;
1818 }
1819 len -= cur_len;
1820 cur_fdt_compat += cur_len;
1821 }
1822 }
1823 if (!best_match_offset) {
1824 debug("No match found.\n");
1825 return -1;
1826 }
1827
1828 return best_match_offset;
1829}
1830
Simon Glass53fbb7e2013-05-07 06:11:53 +00001831int fit_conf_get_node(const void *fit, const char *conf_uname)
1832{
1833 int noffset, confs_noffset;
1834 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001835 const char *s;
1836 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001837
1838 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1839 if (confs_noffset < 0) {
1840 debug("Can't find configurations parent node '%s' (%s)\n",
1841 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1842 return confs_noffset;
1843 }
1844
1845 if (conf_uname == NULL) {
1846 /* get configuration unit name from the default property */
1847 debug("No configuration specified, trying default...\n");
Sebastian Reicheld8ab0fe2021-01-04 20:48:04 +01001848 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1849 noffset = fit_find_config_node(fit);
1850 if (noffset < 0)
1851 return noffset;
1852 conf_uname = fdt_get_name(fit, noffset, NULL);
1853 } else {
1854 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1855 FIT_DEFAULT_PROP, &len);
1856 if (conf_uname == NULL) {
1857 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1858 len);
1859 return len;
1860 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001861 }
1862 debug("Found default configuration: '%s'\n", conf_uname);
1863 }
1864
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001865 s = strchr(conf_uname, '#');
1866 if (s) {
1867 len = s - conf_uname;
1868 conf_uname_copy = malloc(len + 1);
1869 if (!conf_uname_copy) {
1870 debug("Can't allocate uname copy: '%s'\n",
1871 conf_uname);
1872 return -ENOMEM;
1873 }
1874 memcpy(conf_uname_copy, conf_uname, len);
1875 conf_uname_copy[len] = '\0';
1876 conf_uname = conf_uname_copy;
1877 }
1878
Simon Glass53fbb7e2013-05-07 06:11:53 +00001879 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1880 if (noffset < 0) {
1881 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1882 conf_uname, fdt_strerror(noffset));
1883 }
1884
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001885 if (conf_uname_copy)
1886 free(conf_uname_copy);
1887
Simon Glass53fbb7e2013-05-07 06:11:53 +00001888 return noffset;
1889}
1890
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001891int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001892 const char *prop_name)
1893{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001894 return fdt_stringlist_count(fit, noffset, prop_name);
1895}
1896
1897int fit_conf_get_prop_node_index(const void *fit, int noffset,
1898 const char *prop_name, int index)
1899{
1900 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001901 int len;
1902
1903 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001904 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001905 if (uname == NULL)
1906 return len;
1907
1908 return fit_image_get_node(fit, uname);
1909}
1910
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001911int fit_conf_get_prop_node(const void *fit, int noffset,
1912 const char *prop_name)
1913{
1914 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1915}
1916
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001917static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001918{
1919 fit_image_print(fit, rd_noffset, " ");
1920
1921 if (verify) {
1922 puts(" Verifying Hash Integrity ... ");
1923 if (!fit_image_verify(fit, rd_noffset)) {
1924 puts("Bad Data Hash\n");
1925 return -EACCES;
1926 }
1927 puts("OK\n");
1928 }
1929
1930 return 0;
1931}
1932
Simon Glass782cfbb2013-05-16 13:53:21 +00001933int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1934 ulong addr)
1935{
1936 int cfg_noffset;
1937 void *fit_hdr;
1938 int noffset;
1939
1940 debug("* %s: using config '%s' from image at 0x%08lx\n",
1941 prop_name, images->fit_uname_cfg, addr);
1942
1943 /* Check whether configuration has this property defined */
1944 fit_hdr = map_sysmem(addr, 0);
1945 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1946 if (cfg_noffset < 0) {
1947 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001948 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001949 }
1950
1951 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1952 if (noffset < 0) {
1953 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001954 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001955 }
1956
1957 return noffset;
1958}
1959
Simon Glass126cc862014-06-12 07:24:47 -06001960/**
1961 * fit_get_image_type_property() - get property name for IH_TYPE_...
1962 *
1963 * @return the properly name where we expect to find the image in the
1964 * config node
1965 */
1966static const char *fit_get_image_type_property(int type)
1967{
1968 /*
1969 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001970 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001971 * anyway. So let's just keep it here.
1972 */
1973 switch (type) {
1974 case IH_TYPE_FLATDT:
1975 return FIT_FDT_PROP;
1976 case IH_TYPE_KERNEL:
1977 return FIT_KERNEL_PROP;
Alexandru Gagniuc47b6f7f2021-04-01 13:25:30 -05001978 case IH_TYPE_FIRMWARE:
1979 return FIT_FIRMWARE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001980 case IH_TYPE_RAMDISK:
1981 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001982 case IH_TYPE_X86_SETUP:
1983 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001984 case IH_TYPE_LOADABLE:
1985 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001986 case IH_TYPE_FPGA:
1987 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001988 case IH_TYPE_STANDALONE:
1989 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001990 }
1991
1992 return "unknown";
1993}
1994
1995int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001996 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001997 int arch, int image_type, int bootstage_id,
1998 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1999{
2000 int cfg_noffset, noffset;
2001 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002002 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002003 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00002004 const void *fit;
Julius Wernerb1307f82019-07-24 19:37:55 -07002005 void *buf;
2006 void *loadbuf;
Simon Glass782cfbb2013-05-16 13:53:21 +00002007 size_t size;
2008 int type_ok, os_ok;
Julius Wernerb1307f82019-07-24 19:37:55 -07002009 ulong load, load_end, data, len;
2010 uint8_t os, comp;
Alison Wangec6617c2016-11-10 10:49:03 +08002011#ifndef USE_HOSTCC
2012 uint8_t os_arch;
2013#endif
Simon Glass126cc862014-06-12 07:24:47 -06002014 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00002015 int ret;
2016
2017 fit = map_sysmem(addr, 0);
2018 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07002019 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002020 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06002021 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00002022 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2023
2024 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glass3f04db82021-02-15 17:08:12 -07002025 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2026 if (ret) {
2027 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2028 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2029 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00002030 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glass3f04db82021-02-15 17:08:12 -07002031 return ret;
Simon Glass782cfbb2013-05-16 13:53:21 +00002032 }
2033 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2034 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09002035 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00002036 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2037 noffset = fit_image_get_node(fit, fit_uname);
2038 } else {
2039 /*
2040 * no image node unit name, try to get config
2041 * node first. If config unit node name is NULL
2042 * fit_conf_get_node() will try to find default config node
2043 */
2044 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
Simon Glass70c1c892021-07-14 17:05:36 -05002045 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002046 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2047 } else {
2048 cfg_noffset = fit_conf_get_node(fit,
2049 fit_uname_config);
2050 }
2051 if (cfg_noffset < 0) {
2052 puts("Could not find configuration node\n");
2053 bootstage_error(bootstage_id +
2054 BOOTSTAGE_SUB_NO_UNIT_NAME);
2055 return -ENOENT;
2056 }
Marek Vasut078e5582018-05-31 17:59:07 +02002057
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002058 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2059 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02002060 /* Remember this config */
2061 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002062 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02002063
AKASHI Takahirob983cc22020-02-21 15:12:55 +09002064 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut078e5582018-05-31 17:59:07 +02002065 puts(" Verifying Hash Integrity ... ");
2066 if (fit_config_verify(fit, cfg_noffset)) {
2067 puts("Bad Data Hash\n");
2068 bootstage_error(bootstage_id +
2069 BOOTSTAGE_SUB_HASH);
2070 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00002071 }
Marek Vasut078e5582018-05-31 17:59:07 +02002072 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00002073 }
2074
Marek Vasut078e5582018-05-31 17:59:07 +02002075 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2076
Simon Glass782cfbb2013-05-16 13:53:21 +00002077 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2078 prop_name);
2079 fit_uname = fit_get_name(fit, noffset, NULL);
2080 }
2081 if (noffset < 0) {
Simon Glass382cf622020-03-18 11:43:56 -06002082 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass782cfbb2013-05-16 13:53:21 +00002083 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2084 return -ENOENT;
2085 }
2086
2087 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2088
2089 ret = fit_image_select(fit, noffset, images->verify);
2090 if (ret) {
2091 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2092 return ret;
2093 }
2094
2095 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002096 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2097 if (!fit_image_check_target_arch(fit, noffset)) {
2098 puts("Unsupported Architecture\n");
2099 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2100 return -ENOEXEC;
2101 }
Simon Glass782cfbb2013-05-16 13:53:21 +00002102 }
Alison Wangec6617c2016-11-10 10:49:03 +08002103
2104#ifndef USE_HOSTCC
2105 fit_image_get_arch(fit, noffset, &os_arch);
2106 images->os.arch = os_arch;
2107#endif
2108
Simon Glass782cfbb2013-05-16 13:53:21 +00002109 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2110 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002111 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
Alexandru Gagniuc033ac4e2021-04-01 13:25:31 -05002112 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002113 (image_type == IH_TYPE_KERNEL &&
2114 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01002115
Andreas Bießmann950fe262016-08-14 20:31:24 +02002116 os_ok = image_type == IH_TYPE_FLATDT ||
2117 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01002118 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002119 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Alexandru Gagniuc033ac4e2021-04-01 13:25:31 -05002120 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
Cristian Ciocalteaa031b032019-12-24 18:05:38 +02002121 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao0df27d62020-03-18 07:32:07 -07002122 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2123 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
Karl Apsite84a07db2015-05-21 09:52:48 -04002124
2125 /*
2126 * If either of the checks fail, we should report an error, but
2127 * if the image type is coming from the "loadables" field, we
2128 * don't care what it is
2129 */
2130 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01002131 fit_image_get_os(fit, noffset, &os);
2132 printf("No %s %s %s Image\n",
2133 genimg_get_os_name(os),
2134 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00002135 genimg_get_type_name(image_type));
2136 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2137 return -EIO;
2138 }
2139
2140 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2141
2142 /* get image data address and length */
Julius Wernerb1307f82019-07-24 19:37:55 -07002143 if (fit_image_get_data_and_size(fit, noffset,
2144 (const void **)&buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002145 printf("Could not find %s subimage data!\n", prop_name);
2146 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07002147 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00002148 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002149
Philippe Reynes4df35782019-12-18 18:25:42 +01002150 /* Decrypt data before uncompress/move */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002151 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
Philippe Reynes4df35782019-12-18 18:25:42 +01002152 puts(" Decrypting Data ... ");
2153 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2154 puts("Error\n");
2155 return -EACCES;
2156 }
2157 puts("OK\n");
2158 }
Philippe Reynes4df35782019-12-18 18:25:42 +01002159
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002160 /* perform any post-processing on the image data */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002161 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
Lokesh Vutla481d3942021-06-11 11:45:05 +03002162 board_fit_image_post_process(fit, noffset, &buf, &size);
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002163
Simon Glass782cfbb2013-05-16 13:53:21 +00002164 len = (ulong)size;
2165
Simon Glass782cfbb2013-05-16 13:53:21 +00002166 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2167
Julius Wernerb1307f82019-07-24 19:37:55 -07002168 data = map_to_sysmem(buf);
2169 load = data;
Simon Glass782cfbb2013-05-16 13:53:21 +00002170 if (load_op == FIT_LOAD_IGNORED) {
2171 /* Don't load */
2172 } else if (fit_image_get_load(fit, noffset, &load)) {
2173 if (load_op == FIT_LOAD_REQUIRED) {
2174 printf("Can't get %s subimage load address!\n",
2175 prop_name);
2176 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2177 return -EBADF;
2178 }
Simon Glassfe20a812014-08-22 14:26:43 -06002179 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002180 ulong image_start, image_end;
Simon Glass782cfbb2013-05-16 13:53:21 +00002181
2182 /*
2183 * move image data to the load address,
2184 * make sure we don't overwrite initial image
2185 */
2186 image_start = addr;
2187 image_end = addr + fit_get_size(fit);
2188
2189 load_end = load + len;
2190 if (image_type != IH_TYPE_KERNEL &&
2191 load < image_end && load_end > image_start) {
2192 printf("Error: %s overwritten\n", prop_name);
2193 return -EXDEV;
2194 }
2195
2196 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2197 prop_name, data, load);
Julius Wernerb1307f82019-07-24 19:37:55 -07002198 } else {
2199 load = data; /* No load address specified */
Simon Glass782cfbb2013-05-16 13:53:21 +00002200 }
Julius Wernerb1307f82019-07-24 19:37:55 -07002201
2202 comp = IH_COMP_NONE;
2203 loadbuf = buf;
2204 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Wernerbddd9852019-08-02 15:52:28 -07002205 if (!fit_image_get_comp(fit, noffset, &comp) &&
2206 comp != IH_COMP_NONE &&
2207 !(image_type == IH_TYPE_KERNEL ||
2208 image_type == IH_TYPE_KERNEL_NOLOAD ||
2209 image_type == IH_TYPE_RAMDISK)) {
Julius Wernerb1307f82019-07-24 19:37:55 -07002210 ulong max_decomp_len = len * 20;
2211 if (load == data) {
2212 loadbuf = malloc(max_decomp_len);
2213 load = map_to_sysmem(loadbuf);
2214 } else {
2215 loadbuf = map_sysmem(load, max_decomp_len);
2216 }
2217 if (image_decomp(comp, load, data, image_type,
2218 loadbuf, buf, len, max_decomp_len, &load_end)) {
2219 printf("Error decompressing %s\n", prop_name);
2220
2221 return -ENOEXEC;
2222 }
2223 len = load_end - load;
2224 } else if (load != data) {
2225 loadbuf = map_sysmem(load, len);
2226 memcpy(loadbuf, buf, len);
2227 }
2228
Julius Wernerbddd9852019-08-02 15:52:28 -07002229 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2230 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2231 " please fix your .its file!\n");
2232
Julius Wernerb1307f82019-07-24 19:37:55 -07002233 /* verify that image data is a proper FDT blob */
2234 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2235 puts("Subimage data is not a FDT");
2236 return -ENOEXEC;
2237 }
2238
Simon Glass782cfbb2013-05-16 13:53:21 +00002239 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2240
Julius Wernerb1307f82019-07-24 19:37:55 -07002241 *datap = load;
Simon Glass782cfbb2013-05-16 13:53:21 +00002242 *lenp = len;
2243 if (fit_unamep)
2244 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002245 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002246 *fit_uname_configp = (char *)(fit_uname_config ? :
2247 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002248
2249 return noffset;
2250}
Simon Glass90268b82014-10-19 21:11:24 -06002251
2252int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2253 ulong *setup_start, ulong *setup_len)
2254{
2255 int noffset;
2256 ulong addr;
2257 ulong len;
2258 int ret;
2259
2260 addr = map_to_sysmem(images->fit_hdr_os);
2261 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2262 if (noffset < 0)
2263 return noffset;
2264
2265 ret = fit_image_load(images, addr, NULL, NULL, arch,
2266 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2267 FIT_LOAD_REQUIRED, setup_start, &len);
2268
2269 return ret;
2270}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002271
2272#ifndef USE_HOSTCC
2273int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2274 const char **fit_unamep, const char **fit_uname_configp,
2275 int arch, ulong *datap, ulong *lenp)
2276{
2277 int fdt_noffset, cfg_noffset, count;
2278 const void *fit;
2279 const char *fit_uname = NULL;
2280 const char *fit_uname_config = NULL;
2281 char *fit_uname_config_copy = NULL;
2282 char *next_config = NULL;
2283 ulong load, len;
2284#ifdef CONFIG_OF_LIBFDT_OVERLAY
2285 ulong image_start, image_end;
Marek Vasut4c531d92021-06-11 04:09:56 +02002286 ulong ovload, ovlen, ovcopylen;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002287 const char *uconfig;
2288 const char *uname;
Marek Vasut4c531d92021-06-11 04:09:56 +02002289 void *base, *ov, *ovcopy = NULL;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002290 int i, err, noffset, ov_noffset;
2291#endif
2292
2293 fit_uname = fit_unamep ? *fit_unamep : NULL;
2294
2295 if (fit_uname_configp && *fit_uname_configp) {
2296 fit_uname_config_copy = strdup(*fit_uname_configp);
2297 if (!fit_uname_config_copy)
2298 return -ENOMEM;
2299
2300 next_config = strchr(fit_uname_config_copy, '#');
2301 if (next_config)
2302 *next_config++ = '\0';
2303 if (next_config - 1 > fit_uname_config_copy)
2304 fit_uname_config = fit_uname_config_copy;
2305 }
2306
2307 fdt_noffset = fit_image_load(images,
2308 addr, &fit_uname, &fit_uname_config,
2309 arch, IH_TYPE_FLATDT,
2310 BOOTSTAGE_ID_FIT_FDT_START,
2311 FIT_LOAD_OPTIONAL, &load, &len);
2312
2313 if (fdt_noffset < 0)
2314 goto out;
2315
2316 debug("fit_uname=%s, fit_uname_config=%s\n",
2317 fit_uname ? fit_uname : "<NULL>",
2318 fit_uname_config ? fit_uname_config : "<NULL>");
2319
2320 fit = map_sysmem(addr, 0);
2321
2322 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2323
2324 /* single blob, or error just return as well */
2325 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2326 if (count <= 1 && !next_config)
2327 goto out;
2328
2329 /* we need to apply overlays */
2330
2331#ifdef CONFIG_OF_LIBFDT_OVERLAY
2332 image_start = addr;
2333 image_end = addr + fit_get_size(fit);
2334 /* verify that relocation took place by load address not being in fit */
2335 if (load >= image_start && load < image_end) {
2336 /* check is simplified; fit load checks for overlaps */
2337 printf("Overlayed FDT requires relocation\n");
2338 fdt_noffset = -EBADF;
2339 goto out;
2340 }
2341
2342 base = map_sysmem(load, len);
2343
2344 /* apply extra configs in FIT first, followed by args */
2345 for (i = 1; ; i++) {
2346 if (i < count) {
2347 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2348 FIT_FDT_PROP, i);
2349 uname = fit_get_name(fit, noffset, NULL);
2350 uconfig = NULL;
2351 } else {
2352 if (!next_config)
2353 break;
2354 uconfig = next_config;
2355 next_config = strchr(next_config, '#');
2356 if (next_config)
2357 *next_config++ = '\0';
2358 uname = NULL;
Peter Ujfalusi35c75272019-03-06 15:52:27 +02002359
2360 /*
2361 * fit_image_load() would load the first FDT from the
2362 * extra config only when uconfig is specified.
2363 * Check if the extra config contains multiple FDTs and
2364 * if so, load them.
2365 */
2366 cfg_noffset = fit_conf_get_node(fit, uconfig);
2367
2368 i = 0;
2369 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2370 FIT_FDT_PROP);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002371 }
2372
2373 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2374
2375 ov_noffset = fit_image_load(images,
2376 addr, &uname, &uconfig,
2377 arch, IH_TYPE_FLATDT,
2378 BOOTSTAGE_ID_FIT_FDT_START,
Marek Vasut4c531d92021-06-11 04:09:56 +02002379 FIT_LOAD_IGNORED, &ovload, &ovlen);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002380 if (ov_noffset < 0) {
2381 printf("load of %s failed\n", uname);
2382 continue;
2383 }
2384 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2385 uname, ovload, ovlen);
2386 ov = map_sysmem(ovload, ovlen);
2387
Marek Vasut4c531d92021-06-11 04:09:56 +02002388 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2389 ovcopy = malloc(ovcopylen);
2390 if (!ovcopy) {
2391 printf("failed to duplicate DTO before application\n");
2392 fdt_noffset = -ENOMEM;
2393 goto out;
2394 }
2395
2396 err = fdt_open_into(ov, ovcopy, ovcopylen);
2397 if (err < 0) {
2398 printf("failed on fdt_open_into for DTO\n");
2399 fdt_noffset = err;
2400 goto out;
2401 }
2402
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002403 base = map_sysmem(load, len + ovlen);
2404 err = fdt_open_into(base, base, len + ovlen);
2405 if (err < 0) {
2406 printf("failed on fdt_open_into\n");
2407 fdt_noffset = err;
2408 goto out;
2409 }
Marek Vasut4c531d92021-06-11 04:09:56 +02002410
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002411 /* the verbose method prints out messages on error */
Marek Vasut4c531d92021-06-11 04:09:56 +02002412 err = fdt_overlay_apply_verbose(base, ovcopy);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002413 if (err < 0) {
2414 fdt_noffset = err;
2415 goto out;
2416 }
2417 fdt_pack(base);
2418 len = fdt_totalsize(base);
Marek Vasut4c531d92021-06-11 04:09:56 +02002419
2420 free(ovcopy);
2421 ovcopy = NULL;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002422 }
2423#else
2424 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2425 fdt_noffset = -EBADF;
2426#endif
2427
2428out:
2429 if (datap)
2430 *datap = load;
2431 if (lenp)
2432 *lenp = len;
2433 if (fit_unamep)
2434 *fit_unamep = fit_uname;
2435 if (fit_uname_configp)
2436 *fit_uname_configp = fit_uname_config;
2437
Marek Vasut4c531d92021-06-11 04:09:56 +02002438#ifdef CONFIG_OF_LIBFDT_OVERLAY
2439 if (ovcopy)
2440 free(ovcopy);
2441#endif
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002442 if (fit_uname_config_copy)
2443 free(fit_uname_config_copy);
2444 return fdt_noffset;
2445}
2446#endif