blob: f57d97f552295587f088e21fa42c63687653c58c [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>
Sean Andersonb5833482022-03-24 11:26:11 -040027#include <memalign.h>
Simon Glass401d1c42020-10-30 21:38:53 -060028#include <asm/global_data.h>
Chia-Wei Wangca479552021-07-30 09:08:05 +080029#ifdef CONFIG_DM_HASH
30#include <dm.h>
31#include <u-boot/hash.h>
32#endif
Simon Glass782cfbb2013-05-16 13:53:21 +000033DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000034#endif /* !USE_HOSTCC*/
35
Julius Wernerb1307f82019-07-24 19:37:55 -070036#include <bootm.h>
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050037#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000038#include <bootstage.h>
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +010039#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000040#include <u-boot/crc.h>
41#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020042#include <u-boot/sha1.h>
43#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120044#include <u-boot/sha512.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000045
46/*****************************************************************************/
47/* New uImage format routines */
48/*****************************************************************************/
49#ifndef USE_HOSTCC
50static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
51 ulong *addr, const char **name)
52{
53 const char *sep;
54
55 *addr = addr_curr;
56 *name = NULL;
57
58 sep = strchr(spec, sepc);
59 if (sep) {
60 if (sep - spec > 0)
Simon Glass7e5f4602021-07-24 09:03:29 -060061 *addr = hextoul(spec, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +000062
63 *name = sep + 1;
64 return 1;
65 }
66
67 return 0;
68}
69
70/**
71 * fit_parse_conf - parse FIT configuration spec
72 * @spec: input string, containing configuration spec
73 * @add_curr: current image address (to be used as a possible default)
74 * @addr: pointer to a ulong variable, will hold FIT image address of a given
75 * configuration
76 * @conf_name double pointer to a char, will hold pointer to a configuration
77 * unit name
78 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090079 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000080 * where <addr> is a FIT image address that contains configuration
81 * with a <conf> unit name.
82 *
83 * Address part is optional, and if omitted default add_curr will
84 * be used instead.
85 *
86 * returns:
87 * 1 if spec is a valid configuration string,
88 * addr and conf_name are set accordingly
89 * 0 otherwise
90 */
91int fit_parse_conf(const char *spec, ulong addr_curr,
92 ulong *addr, const char **conf_name)
93{
94 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
95}
96
97/**
98 * fit_parse_subimage - parse FIT subimage spec
99 * @spec: input string, containing subimage spec
100 * @add_curr: current image address (to be used as a possible default)
101 * @addr: pointer to a ulong variable, will hold FIT image address of a given
102 * subimage
103 * @image_name: double pointer to a char, will hold pointer to a subimage name
104 *
Masahiro Yamada069d5942013-09-18 09:36:38 +0900105 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +0000106 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
107 * subimage with a <subimg> unit name.
108 *
109 * Address part is optional, and if omitted default add_curr will
110 * be used instead.
111 *
112 * returns:
113 * 1 if spec is a valid subimage string,
114 * addr and image_name are set accordingly
115 * 0 otherwise
116 */
117int fit_parse_subimage(const char *spec, ulong addr_curr,
118 ulong *addr, const char **image_name)
119{
120 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
121}
122#endif /* !USE_HOSTCC */
123
Joel Stanley93af80f2020-12-08 14:42:14 +1030124#ifdef USE_HOSTCC
125/* Host tools use these implementations for Cipher and Signature support */
126static void *host_blob;
127
128void image_set_host_blob(void *blob)
129{
130 host_blob = blob;
131}
132
133void *image_get_host_blob(void)
134{
135 return host_blob;
136}
137#endif /* USE_HOSTCC */
138
Simon Glass53fbb7e2013-05-07 06:11:53 +0000139static void fit_get_debug(const void *fit, int noffset,
140 char *prop_name, int err)
141{
142 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
143 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
144 fdt_strerror(err));
145}
146
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200147/**
148 * fit_get_subimage_count - get component (sub-image) count
149 * @fit: pointer to the FIT format image header
150 * @images_noffset: offset of images node
151 *
152 * returns:
153 * number of image components
154 */
155int fit_get_subimage_count(const void *fit, int images_noffset)
156{
157 int noffset;
158 int ndepth;
159 int count = 0;
160
161 /* Process its subnodes, print out component images details */
162 for (ndepth = 0, count = 0,
163 noffset = fdt_next_node(fit, images_noffset, &ndepth);
164 (noffset >= 0) && (ndepth > 0);
165 noffset = fdt_next_node(fit, noffset, &ndepth)) {
166 if (ndepth == 1) {
167 count++;
168 }
169 }
170
171 return count;
172}
173
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;
Jan Kiszka4550ce92022-01-14 10:21:17 +0100195 const 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 Glass78740bc2021-09-25 19:43:34 -0600383 if (!CONFIG_IS_ENABLED(FIT_PRINT))
384 return;
385
Simon Glass1fe7d932013-05-08 08:05:58 +0000386 /* Indent string is defined in header image.h */
387 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000388
389 /* Root node properties */
390 ret = fit_get_desc(fit, 0, &desc);
391 printf("%sFIT description: ", p);
392 if (ret)
393 printf("unavailable\n");
394 else
395 printf("%s\n", desc);
396
397 if (IMAGE_ENABLE_TIMESTAMP) {
398 ret = fit_get_timestamp(fit, 0, &timestamp);
399 printf("%sCreated: ", p);
400 if (ret)
401 printf("unavailable\n");
402 else
403 genimg_print_time(timestamp);
404 }
405
406 /* Find images parent node offset */
407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
408 if (images_noffset < 0) {
409 printf("Can't find images parent node '%s' (%s)\n",
410 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
411 return;
412 }
413
414 /* Process its subnodes, print out component images details */
415 for (ndepth = 0, count = 0,
416 noffset = fdt_next_node(fit, images_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
419 if (ndepth == 1) {
420 /*
421 * Direct child node of the images parent node,
422 * i.e. component image node.
423 */
424 printf("%s Image %u (%s)\n", p, count++,
425 fit_get_name(fit, noffset, NULL));
426
427 fit_image_print(fit, noffset, p);
428 }
429 }
430
431 /* Find configurations parent node offset */
432 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
433 if (confs_noffset < 0) {
434 debug("Can't get configurations parent node '%s' (%s)\n",
435 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
436 return;
437 }
438
439 /* get default configuration unit name from default property */
440 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
441 if (uname)
442 printf("%s Default Configuration: '%s'\n", p, uname);
443
444 /* Process its subnodes, print out configurations details */
445 for (ndepth = 0, count = 0,
446 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
447 (noffset >= 0) && (ndepth > 0);
448 noffset = fdt_next_node(fit, noffset, &ndepth)) {
449 if (ndepth == 1) {
450 /*
451 * Direct child node of the configurations parent node,
452 * i.e. configuration node.
453 */
454 printf("%s Configuration %u (%s)\n", p, count++,
455 fit_get_name(fit, noffset, NULL));
456
457 fit_conf_print(fit, noffset, p);
458 }
459 }
460}
461
462/**
463 * fit_image_print - prints out the FIT component image details
464 * @fit: pointer to the FIT format image header
465 * @image_noffset: offset of the component image node
466 * @p: pointer to prefix string
467 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500468 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000469 * image. If present, hash nodes are printed out as well. Load
470 * address for images of type firmware is also printed out. Since the load
471 * address is not mandatory for firmware images, it will be output as
472 * "unavailable" when not present.
473 *
474 * returns:
475 * no returned results
476 */
477void fit_image_print(const void *fit, int image_noffset, const char *p)
478{
479 char *desc;
480 uint8_t type, arch, os, comp;
481 size_t size;
482 ulong load, entry;
483 const void *data;
484 int noffset;
485 int ndepth;
486 int ret;
487
Simon Glass78740bc2021-09-25 19:43:34 -0600488 if (!CONFIG_IS_ENABLED(FIT_PRINT))
489 return;
490
Simon Glass53fbb7e2013-05-07 06:11:53 +0000491 /* Mandatory properties */
492 ret = fit_get_desc(fit, image_noffset, &desc);
493 printf("%s Description: ", p);
494 if (ret)
495 printf("unavailable\n");
496 else
497 printf("%s\n", desc);
498
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700499 if (IMAGE_ENABLE_TIMESTAMP) {
500 time_t timestamp;
501
502 ret = fit_get_timestamp(fit, 0, &timestamp);
503 printf("%s Created: ", p);
504 if (ret)
505 printf("unavailable\n");
506 else
507 genimg_print_time(timestamp);
508 }
509
Simon Glass53fbb7e2013-05-07 06:11:53 +0000510 fit_image_get_type(fit, image_noffset, &type);
511 printf("%s Type: %s\n", p, genimg_get_type_name(type));
512
513 fit_image_get_comp(fit, image_noffset, &comp);
514 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
515
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800516 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000517
Simon Glassc9d6b5b2021-09-25 19:43:14 -0600518 if (!tools_build()) {
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100519 printf("%s Data Start: ", p);
520 if (ret) {
521 printf("unavailable\n");
522 } else {
523 void *vdata = (void *)data;
Simon Glassc6ac13b2013-05-16 13:53:26 +0000524
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100525 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
526 }
Simon Glassc6ac13b2013-05-16 13:53:26 +0000527 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000528
529 printf("%s Data Size: ", p);
530 if (ret)
531 printf("unavailable\n");
532 else
533 genimg_print_size(size);
534
535 /* Remaining, type dependent properties */
536 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
537 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
538 (type == IH_TYPE_FLATDT)) {
539 fit_image_get_arch(fit, image_noffset, &arch);
540 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
541 }
542
Michal Simek6faf4622018-03-26 16:31:27 +0200543 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
544 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000545 fit_image_get_os(fit, image_noffset, &os);
546 printf("%s OS: %s\n", p, genimg_get_os_name(os));
547 }
548
549 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200550 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
551 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000552 ret = fit_image_get_load(fit, image_noffset, &load);
553 printf("%s Load Address: ", p);
554 if (ret)
555 printf("unavailable\n");
556 else
557 printf("0x%08lx\n", load);
558 }
559
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300560 /* optional load address for FDT */
561 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
562 printf("%s Load Address: 0x%08lx\n", p, load);
563
Simon Glass53fbb7e2013-05-07 06:11:53 +0000564 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
565 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800566 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000567 printf("%s Entry Point: ", p);
568 if (ret)
569 printf("unavailable\n");
570 else
571 printf("0x%08lx\n", entry);
572 }
573
574 /* Process all hash subnodes of the component image node */
575 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
576 (noffset >= 0) && (ndepth > 0);
577 noffset = fdt_next_node(fit, noffset, &ndepth)) {
578 if (ndepth == 1) {
579 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000580 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000581 }
582 }
583}
584
585/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000586 * fit_get_desc - get node description property
587 * @fit: pointer to the FIT format image header
588 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500589 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000590 *
591 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500592 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000593 *
594 * returns:
595 * 0, on success
596 * -1, on failure
597 */
598int fit_get_desc(const void *fit, int noffset, char **desc)
599{
600 int len;
601
602 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
603 if (*desc == NULL) {
604 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
605 return -1;
606 }
607
608 return 0;
609}
610
611/**
612 * fit_get_timestamp - get node timestamp property
613 * @fit: pointer to the FIT format image header
614 * @noffset: node offset
615 * @timestamp: pointer to the time_t, will hold read timestamp
616 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500617 * fit_get_timestamp() reads timestamp property from given node, if timestamp
618 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000619 * argument.
620 *
621 * returns:
622 * 0, on success
623 * -1, on property read failure
624 * -2, on wrong timestamp size
625 */
626int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
627{
628 int len;
629 const void *data;
630
631 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
632 if (data == NULL) {
633 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
634 return -1;
635 }
636 if (len != sizeof(uint32_t)) {
637 debug("FIT timestamp with incorrect size of (%u)\n", len);
638 return -2;
639 }
640
641 *timestamp = uimage_to_cpu(*((uint32_t *)data));
642 return 0;
643}
644
645/**
646 * fit_image_get_node - get node offset for component image of a given unit name
647 * @fit: pointer to the FIT format image header
648 * @image_uname: component image node unit name
649 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500650 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000651 * node) of a provided unit name. If image is found its node offset is
652 * returned to the caller.
653 *
654 * returns:
655 * image node offset when found (>=0)
656 * negative number on failure (FDT_ERR_* code)
657 */
658int fit_image_get_node(const void *fit, const char *image_uname)
659{
660 int noffset, images_noffset;
661
662 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
663 if (images_noffset < 0) {
664 debug("Can't find images parent node '%s' (%s)\n",
665 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
666 return images_noffset;
667 }
668
669 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
670 if (noffset < 0) {
671 debug("Can't get node offset for image unit name: '%s' (%s)\n",
672 image_uname, fdt_strerror(noffset));
673 }
674
675 return noffset;
676}
677
678/**
679 * fit_image_get_os - get os id for a given component image node
680 * @fit: pointer to the FIT format image header
681 * @noffset: component image node offset
682 * @os: pointer to the uint8_t, will hold os numeric id
683 *
684 * fit_image_get_os() finds os property in a given component image node.
685 * If the property is found, its (string) value is translated to the numeric
686 * id which is returned to the caller.
687 *
688 * returns:
689 * 0, on success
690 * -1, on failure
691 */
692int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
693{
694 int len;
695 const void *data;
696
697 /* Get OS name from property data */
698 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
699 if (data == NULL) {
700 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
701 *os = -1;
702 return -1;
703 }
704
705 /* Translate OS name to id */
706 *os = genimg_get_os_id(data);
707 return 0;
708}
709
710/**
711 * fit_image_get_arch - get arch id for a given component image node
712 * @fit: pointer to the FIT format image header
713 * @noffset: component image node offset
714 * @arch: pointer to the uint8_t, will hold arch numeric id
715 *
716 * fit_image_get_arch() finds arch property in a given component image node.
717 * If the property is found, its (string) value is translated to the numeric
718 * id which is returned to the caller.
719 *
720 * returns:
721 * 0, on success
722 * -1, on failure
723 */
724int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
725{
726 int len;
727 const void *data;
728
729 /* Get architecture name from property data */
730 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
731 if (data == NULL) {
732 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
733 *arch = -1;
734 return -1;
735 }
736
737 /* Translate architecture name to id */
738 *arch = genimg_get_arch_id(data);
739 return 0;
740}
741
742/**
743 * fit_image_get_type - get type id for a given component image node
744 * @fit: pointer to the FIT format image header
745 * @noffset: component image node offset
746 * @type: pointer to the uint8_t, will hold type numeric id
747 *
748 * fit_image_get_type() finds type property in a given component image node.
749 * If the property is found, its (string) value is translated to the numeric
750 * id which is returned to the caller.
751 *
752 * returns:
753 * 0, on success
754 * -1, on failure
755 */
756int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
757{
758 int len;
759 const void *data;
760
761 /* Get image type name from property data */
762 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
763 if (data == NULL) {
764 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
765 *type = -1;
766 return -1;
767 }
768
769 /* Translate image type name to id */
770 *type = genimg_get_type_id(data);
771 return 0;
772}
773
774/**
775 * fit_image_get_comp - get comp id for a given component image node
776 * @fit: pointer to the FIT format image header
777 * @noffset: component image node offset
778 * @comp: pointer to the uint8_t, will hold comp numeric id
779 *
780 * fit_image_get_comp() finds comp property in a given component image node.
781 * If the property is found, its (string) value is translated to the numeric
782 * id which is returned to the caller.
783 *
784 * returns:
785 * 0, on success
786 * -1, on failure
787 */
788int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
789{
790 int len;
791 const void *data;
792
793 /* Get compression name from property data */
794 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
795 if (data == NULL) {
796 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
797 *comp = -1;
798 return -1;
799 }
800
801 /* Translate compression name to id */
802 *comp = genimg_get_comp_id(data);
803 return 0;
804}
805
York Sun60047652016-02-29 15:48:40 -0800806static int fit_image_get_address(const void *fit, int noffset, char *name,
807 ulong *load)
808{
York Sunc1913cb2016-02-29 15:48:41 -0800809 int len, cell_len;
810 const fdt32_t *cell;
811 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800812
York Sunc1913cb2016-02-29 15:48:41 -0800813 cell = fdt_getprop(fit, noffset, name, &len);
814 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800815 fit_get_debug(fit, noffset, name, len);
816 return -1;
817 }
818
York Sunc1913cb2016-02-29 15:48:41 -0800819 cell_len = len >> 2;
820 /* Use load64 to avoid compiling warning for 32-bit target */
821 while (cell_len--) {
822 load64 = (load64 << 32) | uimage_to_cpu(*cell);
823 cell++;
824 }
Michal Simek13d1ca82020-09-03 12:44:51 +0200825
826 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
827 printf("Unsupported %s address size\n", name);
828 return -1;
829 }
830
York Sunc1913cb2016-02-29 15:48:41 -0800831 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800832
833 return 0;
834}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000835/**
836 * fit_image_get_load() - get load addr property for given component image node
837 * @fit: pointer to the FIT format image header
838 * @noffset: component image node offset
839 * @load: pointer to the uint32_t, will hold load address
840 *
841 * fit_image_get_load() finds load address property in a given component
842 * image node. If the property is found, its value is returned to the caller.
843 *
844 * returns:
845 * 0, on success
846 * -1, on failure
847 */
848int fit_image_get_load(const void *fit, int noffset, ulong *load)
849{
York Sun60047652016-02-29 15:48:40 -0800850 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000851}
852
853/**
854 * fit_image_get_entry() - get entry point address property
855 * @fit: pointer to the FIT format image header
856 * @noffset: component image node offset
857 * @entry: pointer to the uint32_t, will hold entry point address
858 *
859 * This gets the entry point address property for a given component image
860 * node.
861 *
862 * fit_image_get_entry() finds entry point address property in a given
863 * component image node. If the property is found, its value is returned
864 * to the caller.
865 *
866 * returns:
867 * 0, on success
868 * -1, on failure
869 */
870int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
871{
York Sun60047652016-02-29 15:48:40 -0800872 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000873}
874
875/**
876 * fit_image_get_data - get data property and its size for a given component image node
877 * @fit: pointer to the FIT format image header
878 * @noffset: component image node offset
879 * @data: double pointer to void, will hold data property's data address
880 * @size: pointer to size_t, will hold data property's data size
881 *
882 * fit_image_get_data() finds data property in a given component image node.
883 * If the property is found its data start address and size are returned to
884 * the caller.
885 *
886 * returns:
887 * 0, on success
888 * -1, on failure
889 */
890int fit_image_get_data(const void *fit, int noffset,
891 const void **data, size_t *size)
892{
893 int len;
894
895 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
896 if (*data == NULL) {
897 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
898 *size = 0;
899 return -1;
900 }
901
902 *size = len;
903 return 0;
904}
905
906/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200907 * Get 'data-offset' property from a given image node.
908 *
909 * @fit: pointer to the FIT image header
910 * @noffset: component image node offset
911 * @data_offset: holds the data-offset property
912 *
913 * returns:
914 * 0, on success
915 * -ENOENT if the property could not be found
916 */
917int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
918{
919 const fdt32_t *val;
920
921 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
922 if (!val)
923 return -ENOENT;
924
925 *data_offset = fdt32_to_cpu(*val);
926
927 return 0;
928}
929
930/**
Peng Fana1be94b2017-12-05 13:20:59 +0800931 * Get 'data-position' property from a given image node.
932 *
933 * @fit: pointer to the FIT image header
934 * @noffset: component image node offset
935 * @data_position: holds the data-position property
936 *
937 * returns:
938 * 0, on success
939 * -ENOENT if the property could not be found
940 */
941int fit_image_get_data_position(const void *fit, int noffset,
942 int *data_position)
943{
944 const fdt32_t *val;
945
946 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
947 if (!val)
948 return -ENOENT;
949
950 *data_position = fdt32_to_cpu(*val);
951
952 return 0;
953}
954
955/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200956 * Get 'data-size' property from a given image node.
957 *
958 * @fit: pointer to the FIT image header
959 * @noffset: component image node offset
960 * @data_size: holds the data-size property
961 *
962 * returns:
963 * 0, on success
964 * -ENOENT if the property could not be found
965 */
966int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
967{
968 const fdt32_t *val;
969
970 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
971 if (!val)
972 return -ENOENT;
973
974 *data_size = fdt32_to_cpu(*val);
975
976 return 0;
977}
978
979/**
Philippe Reynes4df35782019-12-18 18:25:42 +0100980 * Get 'data-size-unciphered' property from a given image node.
981 *
982 * @fit: pointer to the FIT image header
983 * @noffset: component image node offset
984 * @data_size: holds the data-size property
985 *
986 * returns:
987 * 0, on success
988 * -ENOENT if the property could not be found
989 */
990int fit_image_get_data_size_unciphered(const void *fit, int noffset,
991 size_t *data_size)
992{
993 const fdt32_t *val;
994
995 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
996 if (!val)
997 return -ENOENT;
998
999 *data_size = (size_t)fdt32_to_cpu(*val);
1000
1001 return 0;
1002}
1003
1004/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001005 * fit_image_get_data_and_size - get data and its size including
1006 * both embedded and external data
1007 * @fit: pointer to the FIT format image header
1008 * @noffset: component image node offset
1009 * @data: double pointer to void, will hold data property's data address
1010 * @size: pointer to size_t, will hold data property's data size
1011 *
1012 * fit_image_get_data_and_size() finds data and its size including
1013 * both embedded and external data. If the property is found
1014 * its data start address and size are returned to the caller.
1015 *
1016 * returns:
1017 * 0, on success
1018 * otherwise, on failure
1019 */
1020int fit_image_get_data_and_size(const void *fit, int noffset,
1021 const void **data, size_t *size)
1022{
1023 bool external_data = false;
1024 int offset;
1025 int len;
1026 int ret;
1027
1028 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1029 external_data = true;
1030 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1031 external_data = true;
1032 /*
1033 * For FIT with external data, figure out where
1034 * the external images start. This is the base
1035 * for the data-offset properties in each image.
1036 */
1037 offset += ((fdt_totalsize(fit) + 3) & ~3);
1038 }
1039
1040 if (external_data) {
1041 debug("External Data\n");
1042 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt18378042020-03-11 21:51:08 +01001043 if (!ret) {
1044 *data = fit + offset;
1045 *size = len;
1046 }
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001047 } else {
1048 ret = fit_image_get_data(fit, noffset, data, size);
1049 }
1050
1051 return ret;
1052}
1053
1054/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001055 * fit_image_hash_get_algo - get hash algorithm name
1056 * @fit: pointer to the FIT format image header
1057 * @noffset: hash node offset
1058 * @algo: double pointer to char, will hold pointer to the algorithm name
1059 *
1060 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1061 * If the property is found its data start address is returned to the caller.
1062 *
1063 * returns:
1064 * 0, on success
1065 * -1, on failure
1066 */
Jan Kiszka4550ce92022-01-14 10:21:17 +01001067int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001068{
1069 int len;
1070
Jan Kiszka4550ce92022-01-14 10:21:17 +01001071 *algo = (const char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001072 if (*algo == NULL) {
1073 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1074 return -1;
1075 }
1076
1077 return 0;
1078}
1079
1080/**
1081 * fit_image_hash_get_value - get hash value and length
1082 * @fit: pointer to the FIT format image header
1083 * @noffset: hash node offset
1084 * @value: double pointer to uint8_t, will hold address of a hash value data
1085 * @value_len: pointer to an int, will hold hash data length
1086 *
1087 * fit_image_hash_get_value() finds hash value property in a given hash node.
1088 * If the property is found its data start address and size are returned to
1089 * the caller.
1090 *
1091 * returns:
1092 * 0, on success
1093 * -1, on failure
1094 */
1095int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1096 int *value_len)
1097{
1098 int len;
1099
1100 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1101 if (*value == NULL) {
1102 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1103 *value_len = 0;
1104 return -1;
1105 }
1106
1107 *value_len = len;
1108 return 0;
1109}
1110
Simon Glass53fbb7e2013-05-07 06:11:53 +00001111/**
1112 * fit_image_hash_get_ignore - get hash ignore flag
1113 * @fit: pointer to the FIT format image header
1114 * @noffset: hash node offset
1115 * @ignore: pointer to an int, will hold hash ignore flag
1116 *
1117 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1118 * If the property is found and non-zero, the hash algorithm is not verified by
1119 * u-boot automatically.
1120 *
1121 * returns:
1122 * 0, on ignore not found
1123 * value, on ignore found
1124 */
Simon Glassab9efc62013-05-07 06:11:58 +00001125static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001126{
1127 int len;
1128 int *value;
1129
1130 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1131 if (value == NULL || len != sizeof(int))
1132 *ignore = 0;
1133 else
1134 *ignore = *value;
1135
1136 return 0;
1137}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001138
Philippe Reynes7298e422019-12-18 18:25:41 +01001139/**
1140 * fit_image_cipher_get_algo - get cipher algorithm name
1141 * @fit: pointer to the FIT format image header
1142 * @noffset: cipher node offset
1143 * @algo: double pointer to char, will hold pointer to the algorithm name
1144 *
1145 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1146 * cipher node. If the property is found its data start address is returned
1147 * to the caller.
1148 *
1149 * returns:
1150 * 0, on success
1151 * -1, on failure
1152 */
1153int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1154{
1155 int len;
1156
1157 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1158 if (!*algo) {
1159 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1160 return -1;
1161 }
1162
1163 return 0;
1164}
1165
Simon Glass7a80de42016-02-24 09:14:42 -07001166ulong fit_get_end(const void *fit)
1167{
1168 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1169}
1170
Simon Glass53fbb7e2013-05-07 06:11:53 +00001171/**
1172 * fit_set_timestamp - set node timestamp property
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: node offset
1175 * @timestamp: timestamp value to be set
1176 *
1177 * fit_set_timestamp() attempts to set timestamp property in the requested
1178 * node and returns operation status to the caller.
1179 *
1180 * returns:
1181 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001182 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001183 */
1184int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1185{
1186 uint32_t t;
1187 int ret;
1188
1189 t = cpu_to_uimage(timestamp);
1190 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1191 sizeof(uint32_t));
1192 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001193 debug("Can't set '%s' property for '%s' node (%s)\n",
1194 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1195 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001196 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001197 }
1198
1199 return 0;
1200}
1201
1202/**
1203 * calculate_hash - calculate and return hash for provided input data
1204 * @data: pointer to the input data
1205 * @data_len: data length
Chia-Wei Wangdeea0892021-10-27 14:17:24 +08001206 * @name: requested hash algorithm name
Simon Glass53fbb7e2013-05-07 06:11:53 +00001207 * @value: pointer to the char, will hold hash value data (caller must
1208 * allocate enough free space)
1209 * value_len: length of the calculated hash
1210 *
1211 * calculate_hash() computes input data hash according to the requested
1212 * algorithm.
1213 * Resulting hash value is placed in caller provided 'value' buffer, length
1214 * of the calculated hash is returned via value_len pointer argument.
1215 *
1216 * returns:
1217 * 0, on success
1218 * -1, when algo is unsupported
1219 */
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001220int calculate_hash(const void *data, int data_len, const char *name,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001221 uint8_t *value, int *value_len)
1222{
Chia-Wei Wangca479552021-07-30 09:08:05 +08001223#if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
1224 int rc;
1225 enum HASH_ALGO hash_algo;
1226 struct udevice *dev;
1227
1228 rc = uclass_get_device(UCLASS_HASH, 0, &dev);
1229 if (rc) {
1230 debug("failed to get hash device, rc=%d\n", rc);
1231 return -1;
1232 }
1233
Chia-Wei Wangdeea0892021-10-27 14:17:24 +08001234 hash_algo = hash_algo_lookup_by_name(name);
Chia-Wei Wangca479552021-07-30 09:08:05 +08001235 if (hash_algo == HASH_ALGO_INVALID) {
1236 debug("Unsupported hash algorithm\n");
1237 return -1;
1238 };
1239
1240 rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
1241 if (rc) {
1242 debug("failed to get hash value, rc=%d\n", rc);
1243 return -1;
1244 }
1245
1246 *value_len = hash_algo_digest_size(hash_algo);
1247#else
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001248 struct hash_algo *algo;
1249 int ret;
1250
1251 ret = hash_lookup_algo(name, &algo);
1252 if (ret < 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001253 debug("Unsupported hash alogrithm\n");
1254 return -1;
1255 }
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001256
1257 algo->hash_func_ws(data, data_len, value, algo->chunk_size);
1258 *value_len = algo->digest_size;
Chia-Wei Wangca479552021-07-30 09:08:05 +08001259#endif
Alexandru Gagniuc92055e12021-09-02 19:54:21 -05001260
Simon Glass53fbb7e2013-05-07 06:11:53 +00001261 return 0;
1262}
1263
Simon Glassab9efc62013-05-07 06:11:58 +00001264static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1265 size_t size, char **err_msgp)
1266{
Sean Andersonb5833482022-03-24 11:26:11 -04001267 DEFINE_ALIGN_BUFFER(uint8_t, value, FIT_MAX_HASH_LEN,
1268 ARCH_DMA_MINALIGN);
Simon Glassab9efc62013-05-07 06:11:58 +00001269 int value_len;
Jan Kiszka4550ce92022-01-14 10:21:17 +01001270 const char *algo;
Simon Glassab9efc62013-05-07 06:11:58 +00001271 uint8_t *fit_value;
1272 int fit_value_len;
1273 int ignore;
1274
1275 *err_msgp = NULL;
1276
1277 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001278 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001279 return -1;
1280 }
1281 printf("%s", algo);
1282
Simon Glassfa139402021-09-25 19:43:28 -06001283 if (!tools_build()) {
Simon Glassab9efc62013-05-07 06:11:58 +00001284 fit_image_hash_get_ignore(fit, noffset, &ignore);
1285 if (ignore) {
1286 printf("-skipped ");
1287 return 0;
1288 }
1289 }
1290
1291 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1292 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001293 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001294 return -1;
1295 }
1296
1297 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001298 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001299 return -1;
1300 }
1301
1302 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001303 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001304 return -1;
1305 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001306 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001307 return -1;
1308 }
1309
1310 return 0;
1311}
1312
Jun Nie5c643db2018-02-27 16:55:58 +08001313int fit_image_verify_with_data(const void *fit, int image_noffset,
Simon Glass99f844b2021-11-12 12:28:10 -07001314 const void *key_blob, const void *data,
1315 size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001316{
Simon Glass56518e72013-06-13 15:10:01 -07001317 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001318 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001319 int verify_all = 1;
1320 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001321
Simon Glass56518e72013-06-13 15:10:01 -07001322 /* Verify all required signatures */
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001323 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glass56518e72013-06-13 15:10:01 -07001324 fit_image_verify_required_sigs(fit, image_noffset, data, size,
Simon Glass99f844b2021-11-12 12:28:10 -07001325 key_blob, &verify_all)) {
Simon Glass56518e72013-06-13 15:10:01 -07001326 err_msg = "Unable to verify required signature";
1327 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001328 }
1329
1330 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001331 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001332 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001333
Simon Glassab9efc62013-05-07 06:11:58 +00001334 /*
1335 * Check subnode name, must be equal to "hash".
1336 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001337 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001338 */
1339 if (!strncmp(name, FIT_HASH_NODENAME,
1340 strlen(FIT_HASH_NODENAME))) {
1341 if (fit_image_check_hash(fit, noffset, data, size,
1342 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001343 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001344 puts("+ ");
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001345 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glass56518e72013-06-13 15:10:01 -07001346 !strncmp(name, FIT_SIG_NODENAME,
1347 strlen(FIT_SIG_NODENAME))) {
Simon Glass99f844b2021-11-12 12:28:10 -07001348 ret = fit_image_check_sig(fit, noffset, data, size,
1349 gd_fdt_blob(), -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001350
1351 /*
1352 * Show an indication on failure, but do not return
1353 * an error. Only keys marked 'required' can cause
1354 * an image validation failure. See the call to
1355 * fit_image_verify_required_sigs() above.
1356 */
1357 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001358 puts("- ");
1359 else
1360 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001361 }
1362 }
1363
1364 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001365 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001366 goto error;
1367 }
1368
1369 return 1;
1370
1371error:
Simon Glasse754da22013-05-07 06:11:59 +00001372 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001373 err_msg, fit_get_name(fit, noffset, NULL),
1374 fit_get_name(fit, image_noffset, NULL));
1375 return 0;
1376}
1377
1378/**
Jun Nie5c643db2018-02-27 16:55:58 +08001379 * fit_image_verify - verify data integrity
1380 * @fit: pointer to the FIT format image header
1381 * @image_noffset: component image node offset
1382 *
1383 * fit_image_verify() goes over component image hash nodes,
1384 * re-calculates each data hash and compares with the value stored in hash
1385 * node.
1386 *
1387 * returns:
1388 * 1, if all hashes are valid
1389 * 0, otherwise (or on error)
1390 */
1391int fit_image_verify(const void *fit, int image_noffset)
1392{
Simon Glass79af75f2021-02-15 17:08:06 -07001393 const char *name = fit_get_name(fit, image_noffset, NULL);
Jun Nie5c643db2018-02-27 16:55:58 +08001394 const void *data;
1395 size_t size;
Jun Nie5c643db2018-02-27 16:55:58 +08001396 char *err_msg = "";
1397
Simon Glass1ac9c4c2021-07-05 16:32:56 -06001398 if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) {
Simon Glass79af75f2021-02-15 17:08:06 -07001399 /*
1400 * We don't support this since libfdt considers names with the
1401 * name root but different @ suffix to be equal
1402 */
1403 err_msg = "Node name contains @";
1404 goto err;
1405 }
Jun Nie5c643db2018-02-27 16:55:58 +08001406 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001407 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001408 err_msg = "Can't get image data/size";
Simon Glass79af75f2021-02-15 17:08:06 -07001409 goto err;
Jun Nie5c643db2018-02-27 16:55:58 +08001410 }
1411
Simon Glass99f844b2021-11-12 12:28:10 -07001412 return fit_image_verify_with_data(fit, image_noffset, gd_fdt_blob(),
1413 data, size);
Simon Glass79af75f2021-02-15 17:08:06 -07001414
1415err:
1416 printf("error!\n%s in '%s' image node\n", err_msg,
1417 fit_get_name(fit, image_noffset, NULL));
1418 return 0;
Jun Nie5c643db2018-02-27 16:55:58 +08001419}
1420
1421/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001422 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001423 * @fit: pointer to the FIT format image header
1424 *
Simon Glassb8da8362013-05-07 06:11:57 +00001425 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001426 * for every images checks if all it's hashes are valid.
1427 *
1428 * returns:
1429 * 1, if all hashes of all images are valid
1430 * 0, otherwise (or on error)
1431 */
Simon Glassb8da8362013-05-07 06:11:57 +00001432int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001433{
1434 int images_noffset;
1435 int noffset;
1436 int ndepth;
1437 int count;
1438
1439 /* Find images parent node offset */
1440 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1441 if (images_noffset < 0) {
1442 printf("Can't find images parent node '%s' (%s)\n",
1443 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1444 return 0;
1445 }
1446
1447 /* Process all image subnodes, check hashes for each */
1448 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1449 (ulong)fit);
1450 for (ndepth = 0, count = 0,
1451 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1452 (noffset >= 0) && (ndepth > 0);
1453 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1454 if (ndepth == 1) {
1455 /*
1456 * Direct child node of the images parent node,
1457 * i.e. component image node.
1458 */
Simon Glass73223f02016-02-22 22:55:43 -07001459 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001460 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001461 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001462
Simon Glassb8da8362013-05-07 06:11:57 +00001463 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001464 return 0;
1465 printf("\n");
1466 }
1467 }
1468 return 1;
1469}
1470
Philippe Reynes4df35782019-12-18 18:25:42 +01001471static int fit_image_uncipher(const void *fit, int image_noffset,
1472 void **data, size_t *size)
1473{
1474 int cipher_noffset, ret;
1475 void *dst;
1476 size_t size_dst;
1477
1478 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1479 FIT_CIPHER_NODENAME);
1480 if (cipher_noffset < 0)
1481 return 0;
1482
1483 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1484 *data, *size, &dst, &size_dst);
1485 if (ret)
1486 goto out;
1487
1488 *data = dst;
1489 *size = size_dst;
1490
1491 out:
1492 return ret;
1493}
Philippe Reynes4df35782019-12-18 18:25:42 +01001494
Simon Glass53fbb7e2013-05-07 06:11:53 +00001495/**
1496 * fit_image_check_os - check whether image node is of a given os type
1497 * @fit: pointer to the FIT format image header
1498 * @noffset: component image node offset
1499 * @os: requested image os
1500 *
1501 * fit_image_check_os() reads image os property and compares its numeric
1502 * id with the requested os. Comparison result is returned to the caller.
1503 *
1504 * returns:
1505 * 1 if image is of given os type
1506 * 0 otherwise (or on error)
1507 */
1508int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1509{
1510 uint8_t image_os;
1511
1512 if (fit_image_get_os(fit, noffset, &image_os))
1513 return 0;
1514 return (os == image_os);
1515}
1516
1517/**
1518 * fit_image_check_arch - check whether image node is of a given arch
1519 * @fit: pointer to the FIT format image header
1520 * @noffset: component image node offset
1521 * @arch: requested imagearch
1522 *
1523 * fit_image_check_arch() reads image arch property and compares its numeric
1524 * id with the requested arch. Comparison result is returned to the caller.
1525 *
1526 * returns:
1527 * 1 if image is of given arch
1528 * 0 otherwise (or on error)
1529 */
1530int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1531{
1532 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001533 int aarch32_support = 0;
1534
Simon Glasse2734d62021-03-15 18:11:12 +13001535 /* Let's assume that sandbox can load any architecture */
1536 if (IS_ENABLED(CONFIG_SANDBOX))
1537 return true;
1538
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01001539 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1540 aarch32_support = 1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001541
1542 if (fit_image_get_arch(fit, noffset, &image_arch))
1543 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001544 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001545 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1546 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1547 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001548}
1549
1550/**
1551 * fit_image_check_type - check whether image node is of a given type
1552 * @fit: pointer to the FIT format image header
1553 * @noffset: component image node offset
1554 * @type: requested image type
1555 *
1556 * fit_image_check_type() reads image type property and compares its numeric
1557 * id with the requested type. Comparison result is returned to the caller.
1558 *
1559 * returns:
1560 * 1 if image is of given type
1561 * 0 otherwise (or on error)
1562 */
1563int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1564{
1565 uint8_t image_type;
1566
1567 if (fit_image_get_type(fit, noffset, &image_type))
1568 return 0;
1569 return (type == image_type);
1570}
1571
1572/**
1573 * fit_image_check_comp - check whether image node uses given compression
1574 * @fit: pointer to the FIT format image header
1575 * @noffset: component image node offset
1576 * @comp: requested image compression type
1577 *
1578 * fit_image_check_comp() reads image compression property and compares its
1579 * numeric id with the requested compression type. Comparison result is
1580 * returned to the caller.
1581 *
1582 * returns:
1583 * 1 if image uses requested compression
1584 * 0 otherwise (or on error)
1585 */
1586int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1587{
1588 uint8_t image_comp;
1589
1590 if (fit_image_get_comp(fit, noffset, &image_comp))
1591 return 0;
1592 return (comp == image_comp);
1593}
1594
Simon Glass3f04db82021-02-15 17:08:12 -07001595/**
1596 * fdt_check_no_at() - Check for nodes whose names contain '@'
1597 *
1598 * This checks the parent node and all subnodes recursively
1599 *
1600 * @fit: FIT to check
1601 * @parent: Parent node to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001602 * Return: 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
Simon Glass3f04db82021-02-15 17:08:12 -07001603 */
1604static int fdt_check_no_at(const void *fit, int parent)
1605{
1606 const char *name;
1607 int node;
1608 int ret;
1609
1610 name = fdt_get_name(fit, parent, NULL);
1611 if (!name || strchr(name, '@'))
1612 return -EADDRNOTAVAIL;
1613
1614 fdt_for_each_subnode(node, fit, parent) {
1615 ret = fdt_check_no_at(fit, node);
1616 if (ret)
1617 return ret;
1618 }
1619
1620 return 0;
1621}
1622
Simon Glassc5819702021-02-15 17:08:09 -07001623int fit_check_format(const void *fit, ulong size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001624{
Simon Glassc5819702021-02-15 17:08:09 -07001625 int ret;
1626
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001627 /* A FIT image must be a valid FDT */
Simon Glassc5819702021-02-15 17:08:09 -07001628 ret = fdt_check_header(fit);
1629 if (ret) {
1630 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1631 ret);
1632 return -ENOEXEC;
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001633 }
1634
Simon Glass6f3c2d82021-02-15 17:08:10 -07001635 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1636 /*
1637 * If we are not given the size, make do wtih calculating it.
1638 * This is not as secure, so we should consider a flag to
1639 * control this.
1640 */
1641 if (size == IMAGE_SIZE_INVAL)
1642 size = fdt_totalsize(fit);
1643 ret = fdt_check_full(fit, size);
Simon Glass3f04db82021-02-15 17:08:12 -07001644 if (ret)
1645 ret = -EINVAL;
Simon Glass6f3c2d82021-02-15 17:08:10 -07001646
Simon Glass3f04db82021-02-15 17:08:12 -07001647 /*
1648 * U-Boot stopped using unit addressed in 2017. Since libfdt
1649 * can match nodes ignoring any unit address, signature
1650 * verification can see the wrong node if one is inserted with
1651 * the same name as a valid node but with a unit address
1652 * attached. Protect against this by disallowing unit addresses.
1653 */
1654 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1655 ret = fdt_check_no_at(fit, 0);
1656
1657 if (ret) {
1658 log_debug("FIT check error %d\n", ret);
1659 return ret;
1660 }
1661 }
Simon Glass6f3c2d82021-02-15 17:08:10 -07001662 if (ret) {
1663 log_debug("FIT check error %d\n", ret);
Simon Glass3f04db82021-02-15 17:08:12 -07001664 return ret;
Simon Glass6f3c2d82021-02-15 17:08:10 -07001665 }
1666 }
1667
Simon Glass53fbb7e2013-05-07 06:11:53 +00001668 /* mandatory / node 'description' property */
Simon Glassc5819702021-02-15 17:08:09 -07001669 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1670 log_debug("Wrong FIT format: no description\n");
1671 return -ENOMSG;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001672 }
1673
1674 if (IMAGE_ENABLE_TIMESTAMP) {
1675 /* mandatory / node 'timestamp' property */
Simon Glassc5819702021-02-15 17:08:09 -07001676 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1677 log_debug("Wrong FIT format: no timestamp\n");
Simon Glass29cbc4b2021-02-24 08:50:32 -05001678 return -EBADMSG;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001679 }
1680 }
1681
1682 /* mandatory subimages parent '/images' node */
1683 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
Simon Glassc5819702021-02-15 17:08:09 -07001684 log_debug("Wrong FIT format: no images parent node\n");
1685 return -ENOENT;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001686 }
1687
Simon Glassc5819702021-02-15 17:08:09 -07001688 return 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001689}
1690
Simon Glass53fbb7e2013-05-07 06:11:53 +00001691/**
1692 * fit_conf_find_compat
1693 * @fit: pointer to the FIT format image header
1694 * @fdt: pointer to the device tree to compare against
1695 *
1696 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1697 * most compatible with the passed in device tree.
1698 *
1699 * Example:
1700 *
1701 * / o image-tree
1702 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001703 * | |-o fdt-1
1704 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001705 * |
1706 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001707 * |-o config-1
1708 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001709 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001710 * |-o config-2
1711 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001712 *
1713 * / o U-Boot fdt
1714 * |-compatible = "foo,bar", "bim,bam"
1715 *
1716 * / o kernel fdt1
1717 * |-compatible = "foo,bar",
1718 *
1719 * / o kernel fdt2
1720 * |-compatible = "bim,bam", "baz,biz"
1721 *
1722 * Configuration 1 would be picked because the first string in U-Boot's
1723 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1724 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1725 *
Julius Werner18cfa612019-07-24 19:37:56 -07001726 * As an optimization, the compatible property from the FDT's root node can be
1727 * copied into the configuration node in the FIT image. This is required to
1728 * match configurations with compressed FDTs.
1729 *
Simon Glass53fbb7e2013-05-07 06:11:53 +00001730 * returns:
1731 * offset to the configuration to use if one was found
1732 * -1 otherwise
1733 */
1734int fit_conf_find_compat(const void *fit, const void *fdt)
1735{
1736 int ndepth = 0;
1737 int noffset, confs_noffset, images_noffset;
1738 const void *fdt_compat;
1739 int fdt_compat_len;
1740 int best_match_offset = 0;
1741 int best_match_pos = 0;
1742
1743 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1744 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1745 if (confs_noffset < 0 || images_noffset < 0) {
1746 debug("Can't find configurations or images nodes.\n");
1747 return -1;
1748 }
1749
1750 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1751 if (!fdt_compat) {
1752 debug("Fdt for comparison has no \"compatible\" property.\n");
1753 return -1;
1754 }
1755
1756 /*
1757 * Loop over the configurations in the FIT image.
1758 */
1759 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1760 (noffset >= 0) && (ndepth > 0);
1761 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001762 const void *fdt;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001763 const char *kfdt_name;
Julius Werner18cfa612019-07-24 19:37:56 -07001764 int kfdt_noffset, compat_noffset;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001765 const char *cur_fdt_compat;
1766 int len;
Julius Werner18cfa612019-07-24 19:37:56 -07001767 size_t sz;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001768 int i;
1769
1770 if (ndepth > 1)
1771 continue;
1772
Julius Werner18cfa612019-07-24 19:37:56 -07001773 /* If there's a compat property in the config node, use that. */
1774 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1775 fdt = fit; /* search in FIT image */
1776 compat_noffset = noffset; /* search under config node */
1777 } else { /* Otherwise extract it from the kernel FDT. */
1778 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1779 if (!kfdt_name) {
1780 debug("No fdt property found.\n");
1781 continue;
1782 }
1783 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1784 kfdt_name);
1785 if (kfdt_noffset < 0) {
1786 debug("No image node named \"%s\" found.\n",
1787 kfdt_name);
1788 continue;
1789 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001790
Julius Werner18cfa612019-07-24 19:37:56 -07001791 if (!fit_image_check_comp(fit, kfdt_noffset,
1792 IH_COMP_NONE)) {
1793 debug("Can't extract compat from \"%s\" "
1794 "(compressed)\n", kfdt_name);
1795 continue;
1796 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001797
Julius Werner18cfa612019-07-24 19:37:56 -07001798 /* search in this config's kernel FDT */
John Keeping650bf002021-06-25 17:58:04 +01001799 if (fit_image_get_data_and_size(fit, kfdt_noffset,
1800 &fdt, &sz)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001801 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1802 continue;
1803 }
1804
1805 compat_noffset = 0; /* search kFDT under root node */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001806 }
1807
1808 len = fdt_compat_len;
1809 cur_fdt_compat = fdt_compat;
1810 /*
1811 * Look for a match for each U-Boot compatibility string in
Julius Werner18cfa612019-07-24 19:37:56 -07001812 * turn in the compat string property.
Simon Glass53fbb7e2013-05-07 06:11:53 +00001813 */
1814 for (i = 0; len > 0 &&
1815 (!best_match_offset || best_match_pos > i); i++) {
1816 int cur_len = strlen(cur_fdt_compat) + 1;
1817
Julius Werner18cfa612019-07-24 19:37:56 -07001818 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001819 cur_fdt_compat)) {
1820 best_match_offset = noffset;
1821 best_match_pos = i;
1822 break;
1823 }
1824 len -= cur_len;
1825 cur_fdt_compat += cur_len;
1826 }
1827 }
1828 if (!best_match_offset) {
1829 debug("No match found.\n");
1830 return -1;
1831 }
1832
1833 return best_match_offset;
1834}
1835
Simon Glass53fbb7e2013-05-07 06:11:53 +00001836int fit_conf_get_node(const void *fit, const char *conf_uname)
1837{
1838 int noffset, confs_noffset;
1839 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001840 const char *s;
1841 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001842
1843 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1844 if (confs_noffset < 0) {
1845 debug("Can't find configurations parent node '%s' (%s)\n",
1846 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1847 return confs_noffset;
1848 }
1849
1850 if (conf_uname == NULL) {
1851 /* get configuration unit name from the default property */
1852 debug("No configuration specified, trying default...\n");
Simon Glassc9d6b5b2021-09-25 19:43:14 -06001853 if (!tools_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
Sebastian Reicheld8ab0fe2021-01-04 20:48:04 +01001854 noffset = fit_find_config_node(fit);
1855 if (noffset < 0)
1856 return noffset;
1857 conf_uname = fdt_get_name(fit, noffset, NULL);
1858 } else {
1859 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1860 FIT_DEFAULT_PROP, &len);
1861 if (conf_uname == NULL) {
1862 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1863 len);
1864 return len;
1865 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001866 }
1867 debug("Found default configuration: '%s'\n", conf_uname);
1868 }
1869
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001870 s = strchr(conf_uname, '#');
1871 if (s) {
1872 len = s - conf_uname;
1873 conf_uname_copy = malloc(len + 1);
1874 if (!conf_uname_copy) {
1875 debug("Can't allocate uname copy: '%s'\n",
1876 conf_uname);
1877 return -ENOMEM;
1878 }
1879 memcpy(conf_uname_copy, conf_uname, len);
1880 conf_uname_copy[len] = '\0';
1881 conf_uname = conf_uname_copy;
1882 }
1883
Simon Glass53fbb7e2013-05-07 06:11:53 +00001884 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1885 if (noffset < 0) {
1886 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1887 conf_uname, fdt_strerror(noffset));
1888 }
1889
Heinrich Schuchardt7ffc66e2022-04-11 20:08:03 +02001890 free(conf_uname_copy);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001891
Simon Glass53fbb7e2013-05-07 06:11:53 +00001892 return noffset;
1893}
1894
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001895int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001896 const char *prop_name)
1897{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001898 return fdt_stringlist_count(fit, noffset, prop_name);
1899}
1900
1901int fit_conf_get_prop_node_index(const void *fit, int noffset,
1902 const char *prop_name, int index)
1903{
1904 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001905 int len;
1906
1907 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001908 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001909 if (uname == NULL)
1910 return len;
1911
1912 return fit_image_get_node(fit, uname);
1913}
1914
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001915int fit_conf_get_prop_node(const void *fit, int noffset,
1916 const char *prop_name)
1917{
1918 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1919}
1920
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001921static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001922{
1923 fit_image_print(fit, rd_noffset, " ");
1924
1925 if (verify) {
1926 puts(" Verifying Hash Integrity ... ");
1927 if (!fit_image_verify(fit, rd_noffset)) {
1928 puts("Bad Data Hash\n");
1929 return -EACCES;
1930 }
1931 puts("OK\n");
1932 }
1933
1934 return 0;
1935}
1936
Simon Glass782cfbb2013-05-16 13:53:21 +00001937int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1938 ulong addr)
1939{
1940 int cfg_noffset;
1941 void *fit_hdr;
1942 int noffset;
1943
1944 debug("* %s: using config '%s' from image at 0x%08lx\n",
1945 prop_name, images->fit_uname_cfg, addr);
1946
1947 /* Check whether configuration has this property defined */
1948 fit_hdr = map_sysmem(addr, 0);
1949 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1950 if (cfg_noffset < 0) {
1951 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001952 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001953 }
1954
1955 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1956 if (noffset < 0) {
1957 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001958 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001959 }
1960
1961 return noffset;
1962}
1963
Simon Glass126cc862014-06-12 07:24:47 -06001964/**
1965 * fit_get_image_type_property() - get property name for IH_TYPE_...
1966 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001967 * Return: the properly name where we expect to find the image in the
Simon Glass126cc862014-06-12 07:24:47 -06001968 * config node
1969 */
1970static const char *fit_get_image_type_property(int type)
1971{
1972 /*
1973 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001974 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001975 * anyway. So let's just keep it here.
1976 */
1977 switch (type) {
1978 case IH_TYPE_FLATDT:
1979 return FIT_FDT_PROP;
1980 case IH_TYPE_KERNEL:
1981 return FIT_KERNEL_PROP;
Alexandru Gagniuc47b6f7f2021-04-01 13:25:30 -05001982 case IH_TYPE_FIRMWARE:
1983 return FIT_FIRMWARE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001984 case IH_TYPE_RAMDISK:
1985 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001986 case IH_TYPE_X86_SETUP:
1987 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001988 case IH_TYPE_LOADABLE:
1989 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001990 case IH_TYPE_FPGA:
1991 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001992 case IH_TYPE_STANDALONE:
1993 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001994 }
1995
1996 return "unknown";
1997}
1998
1999int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07002000 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00002001 int arch, int image_type, int bootstage_id,
2002 enum fit_load_op load_op, ulong *datap, ulong *lenp)
2003{
2004 int cfg_noffset, noffset;
2005 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002006 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002007 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00002008 const void *fit;
Julius Wernerb1307f82019-07-24 19:37:55 -07002009 void *buf;
2010 void *loadbuf;
Simon Glass782cfbb2013-05-16 13:53:21 +00002011 size_t size;
2012 int type_ok, os_ok;
Julius Wernerb1307f82019-07-24 19:37:55 -07002013 ulong load, load_end, data, len;
2014 uint8_t os, comp;
Simon Glass126cc862014-06-12 07:24:47 -06002015 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00002016 int ret;
2017
2018 fit = map_sysmem(addr, 0);
2019 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07002020 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002021 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06002022 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00002023 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2024
2025 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glass3f04db82021-02-15 17:08:12 -07002026 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2027 if (ret) {
2028 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2029 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2030 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00002031 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glass3f04db82021-02-15 17:08:12 -07002032 return ret;
Simon Glass782cfbb2013-05-16 13:53:21 +00002033 }
2034 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2035 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09002036 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00002037 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2038 noffset = fit_image_get_node(fit, fit_uname);
2039 } else {
2040 /*
2041 * no image node unit name, try to get config
2042 * node first. If config unit node name is NULL
2043 * fit_conf_get_node() will try to find default config node
2044 */
2045 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
Simon Glass70c1c892021-07-14 17:05:36 -05002046 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002047 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2048 } else {
2049 cfg_noffset = fit_conf_get_node(fit,
2050 fit_uname_config);
2051 }
2052 if (cfg_noffset < 0) {
2053 puts("Could not find configuration node\n");
2054 bootstage_error(bootstage_id +
2055 BOOTSTAGE_SUB_NO_UNIT_NAME);
2056 return -ENOENT;
2057 }
Marek Vasut078e5582018-05-31 17:59:07 +02002058
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002059 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2060 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02002061 /* Remember this config */
2062 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002063 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02002064
AKASHI Takahirob983cc22020-02-21 15:12:55 +09002065 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut078e5582018-05-31 17:59:07 +02002066 puts(" Verifying Hash Integrity ... ");
2067 if (fit_config_verify(fit, cfg_noffset)) {
2068 puts("Bad Data Hash\n");
2069 bootstage_error(bootstage_id +
2070 BOOTSTAGE_SUB_HASH);
2071 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00002072 }
Marek Vasut078e5582018-05-31 17:59:07 +02002073 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00002074 }
2075
Marek Vasut078e5582018-05-31 17:59:07 +02002076 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2077
Simon Glass782cfbb2013-05-16 13:53:21 +00002078 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2079 prop_name);
2080 fit_uname = fit_get_name(fit, noffset, NULL);
2081 }
2082 if (noffset < 0) {
Simon Glass382cf622020-03-18 11:43:56 -06002083 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass782cfbb2013-05-16 13:53:21 +00002084 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2085 return -ENOENT;
2086 }
2087
2088 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2089
2090 ret = fit_image_select(fit, noffset, images->verify);
2091 if (ret) {
2092 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2093 return ret;
2094 }
2095
2096 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glassc9d6b5b2021-09-25 19:43:14 -06002097 if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX)) {
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002098 if (!fit_image_check_target_arch(fit, noffset)) {
2099 puts("Unsupported Architecture\n");
2100 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2101 return -ENOEXEC;
2102 }
Simon Glass782cfbb2013-05-16 13:53:21 +00002103 }
Alison Wangec6617c2016-11-10 10:49:03 +08002104
2105#ifndef USE_HOSTCC
Simon Glassb53541f2021-09-25 19:43:39 -06002106 {
2107 uint8_t os_arch;
2108
Alison Wangec6617c2016-11-10 10:49:03 +08002109 fit_image_get_arch(fit, noffset, &os_arch);
2110 images->os.arch = os_arch;
Simon Glassb53541f2021-09-25 19:43:39 -06002111 }
Alison Wangec6617c2016-11-10 10:49:03 +08002112#endif
2113
Simon Glass782cfbb2013-05-16 13:53:21 +00002114 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2115 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002116 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
Alexandru Gagniuc033ac4e2021-04-01 13:25:31 -05002117 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002118 (image_type == IH_TYPE_KERNEL &&
2119 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01002120
Andreas Bießmann950fe262016-08-14 20:31:24 +02002121 os_ok = image_type == IH_TYPE_FLATDT ||
2122 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01002123 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002124 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Alexandru Gagniuc033ac4e2021-04-01 13:25:31 -05002125 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
Cristian Ciocalteaa031b032019-12-24 18:05:38 +02002126 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao0df27d62020-03-18 07:32:07 -07002127 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2128 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
Karl Apsite84a07db2015-05-21 09:52:48 -04002129
2130 /*
2131 * If either of the checks fail, we should report an error, but
2132 * if the image type is coming from the "loadables" field, we
2133 * don't care what it is
2134 */
2135 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01002136 fit_image_get_os(fit, noffset, &os);
2137 printf("No %s %s %s Image\n",
2138 genimg_get_os_name(os),
2139 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00002140 genimg_get_type_name(image_type));
2141 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2142 return -EIO;
2143 }
2144
2145 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2146
2147 /* get image data address and length */
Julius Wernerb1307f82019-07-24 19:37:55 -07002148 if (fit_image_get_data_and_size(fit, noffset,
2149 (const void **)&buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002150 printf("Could not find %s subimage data!\n", prop_name);
2151 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07002152 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00002153 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002154
Philippe Reynes4df35782019-12-18 18:25:42 +01002155 /* Decrypt data before uncompress/move */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002156 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
Philippe Reynes4df35782019-12-18 18:25:42 +01002157 puts(" Decrypting Data ... ");
2158 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2159 puts("Error\n");
2160 return -EACCES;
2161 }
2162 puts("OK\n");
2163 }
Philippe Reynes4df35782019-12-18 18:25:42 +01002164
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002165 /* perform any post-processing on the image data */
Simon Glassc9d6b5b2021-09-25 19:43:14 -06002166 if (!tools_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
Lokesh Vutla481d3942021-06-11 11:45:05 +03002167 board_fit_image_post_process(fit, noffset, &buf, &size);
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002168
Simon Glass782cfbb2013-05-16 13:53:21 +00002169 len = (ulong)size;
2170
Simon Glass782cfbb2013-05-16 13:53:21 +00002171 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2172
Julius Wernerb1307f82019-07-24 19:37:55 -07002173 data = map_to_sysmem(buf);
2174 load = data;
Simon Glass782cfbb2013-05-16 13:53:21 +00002175 if (load_op == FIT_LOAD_IGNORED) {
2176 /* Don't load */
2177 } else if (fit_image_get_load(fit, noffset, &load)) {
2178 if (load_op == FIT_LOAD_REQUIRED) {
2179 printf("Can't get %s subimage load address!\n",
2180 prop_name);
2181 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2182 return -EBADF;
2183 }
Simon Glassfe20a812014-08-22 14:26:43 -06002184 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002185 ulong image_start, image_end;
Simon Glass782cfbb2013-05-16 13:53:21 +00002186
2187 /*
2188 * move image data to the load address,
2189 * make sure we don't overwrite initial image
2190 */
2191 image_start = addr;
2192 image_end = addr + fit_get_size(fit);
2193
2194 load_end = load + len;
2195 if (image_type != IH_TYPE_KERNEL &&
2196 load < image_end && load_end > image_start) {
2197 printf("Error: %s overwritten\n", prop_name);
2198 return -EXDEV;
2199 }
2200
2201 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2202 prop_name, data, load);
Julius Wernerb1307f82019-07-24 19:37:55 -07002203 } else {
2204 load = data; /* No load address specified */
Simon Glass782cfbb2013-05-16 13:53:21 +00002205 }
Julius Wernerb1307f82019-07-24 19:37:55 -07002206
2207 comp = IH_COMP_NONE;
2208 loadbuf = buf;
2209 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Wernerbddd9852019-08-02 15:52:28 -07002210 if (!fit_image_get_comp(fit, noffset, &comp) &&
2211 comp != IH_COMP_NONE &&
2212 !(image_type == IH_TYPE_KERNEL ||
2213 image_type == IH_TYPE_KERNEL_NOLOAD ||
2214 image_type == IH_TYPE_RAMDISK)) {
Julius Wernerb1307f82019-07-24 19:37:55 -07002215 ulong max_decomp_len = len * 20;
2216 if (load == data) {
2217 loadbuf = malloc(max_decomp_len);
2218 load = map_to_sysmem(loadbuf);
2219 } else {
2220 loadbuf = map_sysmem(load, max_decomp_len);
2221 }
2222 if (image_decomp(comp, load, data, image_type,
2223 loadbuf, buf, len, max_decomp_len, &load_end)) {
2224 printf("Error decompressing %s\n", prop_name);
2225
2226 return -ENOEXEC;
2227 }
2228 len = load_end - load;
2229 } else if (load != data) {
2230 loadbuf = map_sysmem(load, len);
2231 memcpy(loadbuf, buf, len);
2232 }
2233
Julius Wernerbddd9852019-08-02 15:52:28 -07002234 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2235 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2236 " please fix your .its file!\n");
2237
Julius Wernerb1307f82019-07-24 19:37:55 -07002238 /* verify that image data is a proper FDT blob */
2239 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2240 puts("Subimage data is not a FDT");
2241 return -ENOEXEC;
2242 }
2243
Simon Glass782cfbb2013-05-16 13:53:21 +00002244 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2245
Julius Wernerb1307f82019-07-24 19:37:55 -07002246 *datap = load;
Simon Glass782cfbb2013-05-16 13:53:21 +00002247 *lenp = len;
2248 if (fit_unamep)
2249 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002250 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002251 *fit_uname_configp = (char *)(fit_uname_config ? :
2252 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002253
2254 return noffset;
2255}
Simon Glass90268b82014-10-19 21:11:24 -06002256
2257int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2258 ulong *setup_start, ulong *setup_len)
2259{
2260 int noffset;
2261 ulong addr;
2262 ulong len;
2263 int ret;
2264
2265 addr = map_to_sysmem(images->fit_hdr_os);
2266 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2267 if (noffset < 0)
2268 return noffset;
2269
2270 ret = fit_image_load(images, addr, NULL, NULL, arch,
2271 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2272 FIT_LOAD_REQUIRED, setup_start, &len);
2273
2274 return ret;
2275}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002276
2277#ifndef USE_HOSTCC
2278int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2279 const char **fit_unamep, const char **fit_uname_configp,
2280 int arch, ulong *datap, ulong *lenp)
2281{
2282 int fdt_noffset, cfg_noffset, count;
2283 const void *fit;
2284 const char *fit_uname = NULL;
2285 const char *fit_uname_config = NULL;
2286 char *fit_uname_config_copy = NULL;
2287 char *next_config = NULL;
2288 ulong load, len;
2289#ifdef CONFIG_OF_LIBFDT_OVERLAY
2290 ulong image_start, image_end;
Marek Vasut4c531d92021-06-11 04:09:56 +02002291 ulong ovload, ovlen, ovcopylen;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002292 const char *uconfig;
2293 const char *uname;
Marek Vasut4c531d92021-06-11 04:09:56 +02002294 void *base, *ov, *ovcopy = NULL;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002295 int i, err, noffset, ov_noffset;
2296#endif
2297
2298 fit_uname = fit_unamep ? *fit_unamep : NULL;
2299
2300 if (fit_uname_configp && *fit_uname_configp) {
2301 fit_uname_config_copy = strdup(*fit_uname_configp);
2302 if (!fit_uname_config_copy)
2303 return -ENOMEM;
2304
2305 next_config = strchr(fit_uname_config_copy, '#');
2306 if (next_config)
2307 *next_config++ = '\0';
2308 if (next_config - 1 > fit_uname_config_copy)
2309 fit_uname_config = fit_uname_config_copy;
2310 }
2311
2312 fdt_noffset = fit_image_load(images,
2313 addr, &fit_uname, &fit_uname_config,
2314 arch, IH_TYPE_FLATDT,
2315 BOOTSTAGE_ID_FIT_FDT_START,
2316 FIT_LOAD_OPTIONAL, &load, &len);
2317
2318 if (fdt_noffset < 0)
2319 goto out;
2320
2321 debug("fit_uname=%s, fit_uname_config=%s\n",
2322 fit_uname ? fit_uname : "<NULL>",
2323 fit_uname_config ? fit_uname_config : "<NULL>");
2324
2325 fit = map_sysmem(addr, 0);
2326
2327 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2328
2329 /* single blob, or error just return as well */
2330 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2331 if (count <= 1 && !next_config)
2332 goto out;
2333
2334 /* we need to apply overlays */
2335
2336#ifdef CONFIG_OF_LIBFDT_OVERLAY
2337 image_start = addr;
2338 image_end = addr + fit_get_size(fit);
2339 /* verify that relocation took place by load address not being in fit */
2340 if (load >= image_start && load < image_end) {
2341 /* check is simplified; fit load checks for overlaps */
2342 printf("Overlayed FDT requires relocation\n");
2343 fdt_noffset = -EBADF;
2344 goto out;
2345 }
2346
2347 base = map_sysmem(load, len);
2348
2349 /* apply extra configs in FIT first, followed by args */
2350 for (i = 1; ; i++) {
2351 if (i < count) {
2352 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2353 FIT_FDT_PROP, i);
2354 uname = fit_get_name(fit, noffset, NULL);
2355 uconfig = NULL;
2356 } else {
2357 if (!next_config)
2358 break;
2359 uconfig = next_config;
2360 next_config = strchr(next_config, '#');
2361 if (next_config)
2362 *next_config++ = '\0';
2363 uname = NULL;
Peter Ujfalusi35c75272019-03-06 15:52:27 +02002364
2365 /*
2366 * fit_image_load() would load the first FDT from the
2367 * extra config only when uconfig is specified.
2368 * Check if the extra config contains multiple FDTs and
2369 * if so, load them.
2370 */
2371 cfg_noffset = fit_conf_get_node(fit, uconfig);
2372
2373 i = 0;
2374 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2375 FIT_FDT_PROP);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002376 }
2377
2378 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2379
2380 ov_noffset = fit_image_load(images,
2381 addr, &uname, &uconfig,
2382 arch, IH_TYPE_FLATDT,
2383 BOOTSTAGE_ID_FIT_FDT_START,
Marek Vasut4c531d92021-06-11 04:09:56 +02002384 FIT_LOAD_IGNORED, &ovload, &ovlen);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002385 if (ov_noffset < 0) {
2386 printf("load of %s failed\n", uname);
2387 continue;
2388 }
2389 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2390 uname, ovload, ovlen);
2391 ov = map_sysmem(ovload, ovlen);
2392
Marek Vasut4c531d92021-06-11 04:09:56 +02002393 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2394 ovcopy = malloc(ovcopylen);
2395 if (!ovcopy) {
2396 printf("failed to duplicate DTO before application\n");
2397 fdt_noffset = -ENOMEM;
2398 goto out;
2399 }
2400
2401 err = fdt_open_into(ov, ovcopy, ovcopylen);
2402 if (err < 0) {
2403 printf("failed on fdt_open_into for DTO\n");
2404 fdt_noffset = err;
2405 goto out;
2406 }
2407
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002408 base = map_sysmem(load, len + ovlen);
2409 err = fdt_open_into(base, base, len + ovlen);
2410 if (err < 0) {
2411 printf("failed on fdt_open_into\n");
2412 fdt_noffset = err;
2413 goto out;
2414 }
Marek Vasut4c531d92021-06-11 04:09:56 +02002415
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002416 /* the verbose method prints out messages on error */
Marek Vasut4c531d92021-06-11 04:09:56 +02002417 err = fdt_overlay_apply_verbose(base, ovcopy);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002418 if (err < 0) {
2419 fdt_noffset = err;
2420 goto out;
2421 }
2422 fdt_pack(base);
2423 len = fdt_totalsize(base);
2424 }
2425#else
2426 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2427 fdt_noffset = -EBADF;
2428#endif
2429
2430out:
2431 if (datap)
2432 *datap = load;
2433 if (lenp)
2434 *lenp = len;
2435 if (fit_unamep)
2436 *fit_unamep = fit_uname;
2437 if (fit_uname_configp)
2438 *fit_uname_configp = fit_uname_config;
2439
Marek Vasut4c531d92021-06-11 04:09:56 +02002440#ifdef CONFIG_OF_LIBFDT_OVERLAY
Heinrich Schuchardt7ffc66e2022-04-11 20:08:03 +02002441 free(ovcopy);
Marek Vasut4c531d92021-06-11 04:09:56 +02002442#endif
Heinrich Schuchardt7ffc66e2022-04-11 20:08:03 +02002443 free(fit_uname_config_copy);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002444 return fdt_noffset;
2445}
2446#endif