blob: 7d56f0b5e6ed39a75e029b09dc95d605835c0f91 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassda0af362013-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 Glassda0af362013-05-07 06:11:53 +00009 */
10
Simon Glassd563c252021-02-15 17:08:09 -070011#define LOG_CATEGORY LOGC_BOOT
12
Simon Glassda0af362013-05-07 06:11:53 +000013#ifdef USE_HOSTCC
14#include "mkimage.h"
Simon Glassda0af362013-05-07 06:11:53 +000015#include <time.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060016#include <linux/libfdt.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Tom Rini8fac3d42023-12-14 07:16:54 -050018#include <linux/kconfig.h>
Simon Glassda0af362013-05-07 06:11:53 +000019#else
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050020#include <linux/compiler.h>
Marek Vasutf7d24612021-06-11 04:09:56 +020021#include <linux/sizes.h>
Simon Glass384d86d2013-05-16 13:53:21 +000022#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050024#include <mapmem.h>
Simon Glass384d86d2013-05-16 13:53:21 +000025#include <asm/io.h>
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +030026#include <malloc.h>
Sean Anderson615e31c2022-03-24 11:26:11 -040027#include <memalign.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060028#include <asm/global_data.h>
Chia-Wei Wang4a733b32021-07-30 09:08:05 +080029#ifdef CONFIG_DM_HASH
30#include <dm.h>
31#include <u-boot/hash.h>
32#endif
Simon Glass384d86d2013-05-16 13:53:21 +000033DECLARE_GLOBAL_DATA_PTR;
Simon Glassda0af362013-05-07 06:11:53 +000034#endif /* !USE_HOSTCC*/
35
Julius Werner97b09cd2019-07-24 19:37:55 -070036#include <bootm.h>
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050037#include <image.h>
Simon Glassda0af362013-05-07 06:11:53 +000038#include <bootstage.h>
Simon Glass9b5bfba2024-08-07 16:47:33 -060039#include <upl.h>
Simon Glassda0af362013-05-07 06:11:53 +000040#include <u-boot/crc.h>
Simon Glassda0af362013-05-07 06:11:53 +000041
42/*****************************************************************************/
43/* New uImage format routines */
44/*****************************************************************************/
45#ifndef USE_HOSTCC
46static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
47 ulong *addr, const char **name)
48{
49 const char *sep;
50
51 *addr = addr_curr;
52 *name = NULL;
53
54 sep = strchr(spec, sepc);
55 if (sep) {
56 if (sep - spec > 0)
Simon Glass3ff49ec2021-07-24 09:03:29 -060057 *addr = hextoul(spec, NULL);
Simon Glassda0af362013-05-07 06:11:53 +000058
59 *name = sep + 1;
60 return 1;
61 }
62
63 return 0;
64}
65
66/**
67 * fit_parse_conf - parse FIT configuration spec
68 * @spec: input string, containing configuration spec
69 * @add_curr: current image address (to be used as a possible default)
70 * @addr: pointer to a ulong variable, will hold FIT image address of a given
71 * configuration
72 * @conf_name double pointer to a char, will hold pointer to a configuration
73 * unit name
74 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090075 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glassda0af362013-05-07 06:11:53 +000076 * where <addr> is a FIT image address that contains configuration
77 * with a <conf> unit name.
78 *
79 * Address part is optional, and if omitted default add_curr will
80 * be used instead.
81 *
82 * returns:
83 * 1 if spec is a valid configuration string,
84 * addr and conf_name are set accordingly
85 * 0 otherwise
86 */
87int fit_parse_conf(const char *spec, ulong addr_curr,
88 ulong *addr, const char **conf_name)
89{
90 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
91}
92
93/**
94 * fit_parse_subimage - parse FIT subimage spec
95 * @spec: input string, containing subimage spec
96 * @add_curr: current image address (to be used as a possible default)
97 * @addr: pointer to a ulong variable, will hold FIT image address of a given
98 * subimage
99 * @image_name: double pointer to a char, will hold pointer to a subimage name
100 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +0900101 * fit_parse_subimage() expects subimage spec in the form of
Simon Glassda0af362013-05-07 06:11:53 +0000102 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
103 * subimage with a <subimg> unit name.
104 *
105 * Address part is optional, and if omitted default add_curr will
106 * be used instead.
107 *
108 * returns:
109 * 1 if spec is a valid subimage string,
110 * addr and image_name are set accordingly
111 * 0 otherwise
112 */
113int fit_parse_subimage(const char *spec, ulong addr_curr,
114 ulong *addr, const char **image_name)
115{
116 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
117}
118#endif /* !USE_HOSTCC */
119
Joel Stanleyc3baacd2020-12-08 14:42:14 +1030120#ifdef USE_HOSTCC
121/* Host tools use these implementations for Cipher and Signature support */
122static void *host_blob;
123
124void image_set_host_blob(void *blob)
125{
126 host_blob = blob;
127}
128
129void *image_get_host_blob(void)
130{
131 return host_blob;
132}
133#endif /* USE_HOSTCC */
134
Simon Glassda0af362013-05-07 06:11:53 +0000135static void fit_get_debug(const void *fit, int noffset,
136 char *prop_name, int err)
137{
138 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
139 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
140 fdt_strerror(err));
141}
142
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200143/**
144 * fit_get_subimage_count - get component (sub-image) count
145 * @fit: pointer to the FIT format image header
146 * @images_noffset: offset of images node
147 *
148 * returns:
149 * number of image components
150 */
151int fit_get_subimage_count(const void *fit, int images_noffset)
152{
153 int noffset;
154 int ndepth;
155 int count = 0;
156
157 /* Process its subnodes, print out component images details */
158 for (ndepth = 0, count = 0,
159 noffset = fdt_next_node(fit, images_noffset, &ndepth);
160 (noffset >= 0) && (ndepth > 0);
161 noffset = fdt_next_node(fit, noffset, &ndepth)) {
162 if (ndepth == 1) {
163 count++;
164 }
165 }
166
167 return count;
168}
169
Simon Glassda0af362013-05-07 06:11:53 +0000170/**
Tom Rini7d77b662018-05-08 14:34:05 -0400171 * fit_image_print_data() - prints out the hash node details
172 * @fit: pointer to the FIT format image header
173 * @noffset: offset of the hash node
174 * @p: pointer to prefix string
175 * @type: Type of information to print ("hash" or "sign")
176 *
177 * fit_image_print_data() lists properties for the processed hash node
178 *
179 * This function avoid using puts() since it prints a newline on the host
180 * but does not in U-Boot.
181 *
182 * returns:
183 * no returned results
184 */
185static void fit_image_print_data(const void *fit, int noffset, const char *p,
186 const char *type)
187{
188 const char *keyname;
189 uint8_t *value;
190 int value_len;
Jan Kiszka27beec22022-01-14 10:21:17 +0100191 const char *algo;
Philippe Reynes12468352018-11-14 13:51:00 +0100192 const char *padding;
Simon Glassd7aabcc2020-03-18 11:44:06 -0600193 bool required;
Tom Rini7d77b662018-05-08 14:34:05 -0400194 int ret, i;
195
196 debug("%s %s node: '%s'\n", p, type,
197 fit_get_name(fit, noffset, NULL));
198 printf("%s %s algo: ", p, type);
199 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
200 printf("invalid/unsupported\n");
201 return;
202 }
203 printf("%s", algo);
Simon Glassd7aabcc2020-03-18 11:44:06 -0600204 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
205 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
Tom Rini7d77b662018-05-08 14:34:05 -0400206 if (keyname)
207 printf(":%s", keyname);
208 if (required)
209 printf(" (required)");
210 printf("\n");
211
Philippe Reynes12468352018-11-14 13:51:00 +0100212 padding = fdt_getprop(fit, noffset, "padding", NULL);
213 if (padding)
214 printf("%s %s padding: %s\n", p, type, padding);
215
Tom Rini7d77b662018-05-08 14:34:05 -0400216 ret = fit_image_hash_get_value(fit, noffset, &value,
217 &value_len);
218 printf("%s %s value: ", p, type);
219 if (ret) {
220 printf("unavailable\n");
221 } else {
222 for (i = 0; i < value_len; i++)
223 printf("%02x", value[i]);
224 printf("\n");
225 }
226
227 debug("%s %s len: %d\n", p, type, value_len);
228
229 /* Signatures have a time stamp */
230 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
231 time_t timestamp;
232
233 printf("%s Timestamp: ", p);
234 if (fit_get_timestamp(fit, noffset, &timestamp))
235 printf("unavailable\n");
236 else
237 genimg_print_time(timestamp);
238 }
239}
240
241/**
242 * fit_image_print_verification_data() - prints out the hash/signature details
243 * @fit: pointer to the FIT format image header
244 * @noffset: offset of the hash or signature node
245 * @p: pointer to prefix string
246 *
247 * This lists properties for the processed hash node
248 *
249 * returns:
250 * no returned results
251 */
252static void fit_image_print_verification_data(const void *fit, int noffset,
253 const char *p)
254{
255 const char *name;
256
257 /*
258 * Check subnode name, must be equal to "hash" or "signature".
259 * Multiple hash/signature nodes require unique unit node
260 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
261 */
262 name = fit_get_name(fit, noffset, NULL);
263 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
264 fit_image_print_data(fit, noffset, p, "Hash");
265 } else if (!strncmp(name, FIT_SIG_NODENAME,
266 strlen(FIT_SIG_NODENAME))) {
267 fit_image_print_data(fit, noffset, p, "Sign");
268 }
269}
270
271/**
272 * fit_conf_print - prints out the FIT configuration details
273 * @fit: pointer to the FIT format image header
274 * @noffset: offset of the configuration node
275 * @p: pointer to prefix string
276 *
277 * fit_conf_print() lists all mandatory properties for the processed
278 * configuration node.
279 *
280 * returns:
281 * no returned results
282 */
283static void fit_conf_print(const void *fit, int noffset, const char *p)
284{
285 char *desc;
286 const char *uname;
287 int ret;
288 int fdt_index, loadables_index;
289 int ndepth;
290
291 /* Mandatory properties */
292 ret = fit_get_desc(fit, noffset, &desc);
293 printf("%s Description: ", p);
294 if (ret)
295 printf("unavailable\n");
296 else
297 printf("%s\n", desc);
298
299 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
300 printf("%s Kernel: ", p);
301 if (!uname)
302 printf("unavailable\n");
303 else
304 printf("%s\n", uname);
305
306 /* Optional properties */
307 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
308 if (uname)
309 printf("%s Init Ramdisk: %s\n", p, uname);
310
311 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
312 if (uname)
313 printf("%s Firmware: %s\n", p, uname);
314
315 for (fdt_index = 0;
316 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
317 fdt_index, NULL), uname;
318 fdt_index++) {
319 if (fdt_index == 0)
320 printf("%s FDT: ", p);
321 else
322 printf("%s ", p);
323 printf("%s\n", uname);
324 }
325
326 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
327 if (uname)
328 printf("%s FPGA: %s\n", p, uname);
329
330 /* Print out all of the specified loadables */
331 for (loadables_index = 0;
332 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
333 loadables_index, NULL), uname;
334 loadables_index++) {
335 if (loadables_index == 0) {
336 printf("%s Loadables: ", p);
337 } else {
338 printf("%s ", p);
339 }
340 printf("%s\n", uname);
341 }
342
343 /* Process all hash subnodes of the component configuration node */
344 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
345 (noffset >= 0) && (ndepth > 0);
346 noffset = fdt_next_node(fit, noffset, &ndepth)) {
347 if (ndepth == 1) {
348 /* Direct child node of the component configuration node */
349 fit_image_print_verification_data(fit, noffset, p);
350 }
351 }
352}
353
354/**
Simon Glassda0af362013-05-07 06:11:53 +0000355 * fit_print_contents - prints out the contents of the FIT format image
356 * @fit: pointer to the FIT format image header
357 * @p: pointer to prefix string
358 *
359 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500360 * The routine prints out FIT image properties (root node level) followed by
Simon Glassda0af362013-05-07 06:11:53 +0000361 * the details of each component image.
362 *
363 * returns:
364 * no returned results
365 */
366void fit_print_contents(const void *fit)
367{
368 char *desc;
369 char *uname;
370 int images_noffset;
371 int confs_noffset;
372 int noffset;
373 int ndepth;
374 int count = 0;
375 int ret;
376 const char *p;
377 time_t timestamp;
378
Simon Glass800b6fb2021-09-25 19:43:34 -0600379 if (!CONFIG_IS_ENABLED(FIT_PRINT))
380 return;
381
Simon Glass1030f162013-05-08 08:05:58 +0000382 /* Indent string is defined in header image.h */
383 p = IMAGE_INDENT_STRING;
Simon Glassda0af362013-05-07 06:11:53 +0000384
385 /* Root node properties */
386 ret = fit_get_desc(fit, 0, &desc);
387 printf("%sFIT description: ", p);
388 if (ret)
389 printf("unavailable\n");
390 else
391 printf("%s\n", desc);
392
393 if (IMAGE_ENABLE_TIMESTAMP) {
394 ret = fit_get_timestamp(fit, 0, &timestamp);
395 printf("%sCreated: ", p);
396 if (ret)
397 printf("unavailable\n");
398 else
399 genimg_print_time(timestamp);
400 }
401
402 /* Find images parent node offset */
403 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
404 if (images_noffset < 0) {
405 printf("Can't find images parent node '%s' (%s)\n",
406 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
407 return;
408 }
409
410 /* Process its subnodes, print out component images details */
411 for (ndepth = 0, count = 0,
412 noffset = fdt_next_node(fit, images_noffset, &ndepth);
413 (noffset >= 0) && (ndepth > 0);
414 noffset = fdt_next_node(fit, noffset, &ndepth)) {
415 if (ndepth == 1) {
416 /*
417 * Direct child node of the images parent node,
418 * i.e. component image node.
419 */
420 printf("%s Image %u (%s)\n", p, count++,
421 fit_get_name(fit, noffset, NULL));
422
423 fit_image_print(fit, noffset, p);
424 }
425 }
426
427 /* Find configurations parent node offset */
428 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
429 if (confs_noffset < 0) {
430 debug("Can't get configurations parent node '%s' (%s)\n",
431 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
432 return;
433 }
434
435 /* get default configuration unit name from default property */
436 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
437 if (uname)
438 printf("%s Default Configuration: '%s'\n", p, uname);
439
440 /* Process its subnodes, print out configurations details */
441 for (ndepth = 0, count = 0,
442 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
443 (noffset >= 0) && (ndepth > 0);
444 noffset = fdt_next_node(fit, noffset, &ndepth)) {
445 if (ndepth == 1) {
446 /*
447 * Direct child node of the configurations parent node,
448 * i.e. configuration node.
449 */
450 printf("%s Configuration %u (%s)\n", p, count++,
451 fit_get_name(fit, noffset, NULL));
452
453 fit_conf_print(fit, noffset, p);
454 }
455 }
456}
457
458/**
459 * fit_image_print - prints out the FIT component image details
460 * @fit: pointer to the FIT format image header
461 * @image_noffset: offset of the component image node
462 * @p: pointer to prefix string
463 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500464 * fit_image_print() lists all mandatory properties for the processed component
Simon Glassda0af362013-05-07 06:11:53 +0000465 * image. If present, hash nodes are printed out as well. Load
466 * address for images of type firmware is also printed out. Since the load
467 * address is not mandatory for firmware images, it will be output as
468 * "unavailable" when not present.
469 *
470 * returns:
471 * no returned results
472 */
473void fit_image_print(const void *fit, int image_noffset, const char *p)
474{
475 char *desc;
Daniel Golle2ebfd222022-08-27 04:17:28 +0100476 uint8_t type, arch, os, comp = IH_COMP_NONE;
Simon Glassda0af362013-05-07 06:11:53 +0000477 size_t size;
478 ulong load, entry;
479 const void *data;
480 int noffset;
481 int ndepth;
482 int ret;
483
Simon Glass800b6fb2021-09-25 19:43:34 -0600484 if (!CONFIG_IS_ENABLED(FIT_PRINT))
485 return;
486
Simon Glassda0af362013-05-07 06:11:53 +0000487 /* Mandatory properties */
488 ret = fit_get_desc(fit, image_noffset, &desc);
489 printf("%s Description: ", p);
490 if (ret)
491 printf("unavailable\n");
492 else
493 printf("%s\n", desc);
494
Simon Glass749a6e72013-07-16 20:10:01 -0700495 if (IMAGE_ENABLE_TIMESTAMP) {
496 time_t timestamp;
497
498 ret = fit_get_timestamp(fit, 0, &timestamp);
499 printf("%s Created: ", p);
500 if (ret)
501 printf("unavailable\n");
502 else
503 genimg_print_time(timestamp);
504 }
505
Simon Glassda0af362013-05-07 06:11:53 +0000506 fit_image_get_type(fit, image_noffset, &type);
507 printf("%s Type: %s\n", p, genimg_get_type_name(type));
508
509 fit_image_get_comp(fit, image_noffset, &comp);
510 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
511
Kelvin Cheung186cc992018-05-19 18:21:37 +0800512 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glassda0af362013-05-07 06:11:53 +0000513
Simon Glassdb503fb2021-09-25 19:43:14 -0600514 if (!tools_build()) {
Sebastian Reichelbfe8ddf2021-01-04 20:48:03 +0100515 printf("%s Data Start: ", p);
516 if (ret) {
517 printf("unavailable\n");
518 } else {
519 void *vdata = (void *)data;
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000520
Sebastian Reichelbfe8ddf2021-01-04 20:48:03 +0100521 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
522 }
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000523 }
Simon Glassda0af362013-05-07 06:11:53 +0000524
525 printf("%s Data Size: ", p);
526 if (ret)
527 printf("unavailable\n");
528 else
529 genimg_print_size(size);
530
531 /* Remaining, type dependent properties */
532 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
533 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
534 (type == IH_TYPE_FLATDT)) {
535 fit_image_get_arch(fit, image_noffset, &arch);
536 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
537 }
538
Michal Simek55f698f2018-03-26 16:31:27 +0200539 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
540 (type == IH_TYPE_FIRMWARE)) {
Simon Glassda0af362013-05-07 06:11:53 +0000541 fit_image_get_os(fit, image_noffset, &os);
542 printf("%s OS: %s\n", p, genimg_get_os_name(os));
543 }
544
545 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simekebae78b2016-05-17 14:03:50 +0200546 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
547 (type == IH_TYPE_FPGA)) {
Simon Glassda0af362013-05-07 06:11:53 +0000548 ret = fit_image_get_load(fit, image_noffset, &load);
549 printf("%s Load Address: ", p);
550 if (ret)
551 printf("unavailable\n");
552 else
553 printf("0x%08lx\n", load);
554 }
555
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +0300556 /* optional load address for FDT */
557 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
558 printf("%s Load Address: 0x%08lx\n", p, load);
559
Simon Glassda0af362013-05-07 06:11:53 +0000560 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
561 (type == IH_TYPE_RAMDISK)) {
York Sun2b0464e2016-02-29 15:48:40 -0800562 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glassda0af362013-05-07 06:11:53 +0000563 printf("%s Entry Point: ", p);
564 if (ret)
565 printf("unavailable\n");
566 else
567 printf("0x%08lx\n", entry);
568 }
569
570 /* Process all hash subnodes of the component image node */
571 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
572 (noffset >= 0) && (ndepth > 0);
573 noffset = fdt_next_node(fit, noffset, &ndepth)) {
574 if (ndepth == 1) {
575 /* Direct child node of the component image node */
Simon Glasse61aa442013-05-07 06:12:02 +0000576 fit_image_print_verification_data(fit, noffset, p);
Simon Glassda0af362013-05-07 06:11:53 +0000577 }
578 }
Simon Glassda0af362013-05-07 06:11:53 +0000579}
580
581/**
582 * fit_get_desc - get node description property
583 * @fit: pointer to the FIT format image header
584 * @noffset: node offset
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500585 * @desc: double pointer to the char, will hold pointer to the description
Simon Glassda0af362013-05-07 06:11:53 +0000586 *
587 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500588 * description is found pointer to it is returned in third call argument.
Simon Glassda0af362013-05-07 06:11:53 +0000589 *
590 * returns:
591 * 0, on success
592 * -1, on failure
593 */
594int fit_get_desc(const void *fit, int noffset, char **desc)
595{
596 int len;
597
598 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
599 if (*desc == NULL) {
600 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
601 return -1;
602 }
603
604 return 0;
605}
606
607/**
608 * fit_get_timestamp - get node timestamp property
609 * @fit: pointer to the FIT format image header
610 * @noffset: node offset
611 * @timestamp: pointer to the time_t, will hold read timestamp
612 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500613 * fit_get_timestamp() reads timestamp property from given node, if timestamp
614 * is found and has a correct size its value is returned in third call
Simon Glassda0af362013-05-07 06:11:53 +0000615 * argument.
616 *
617 * returns:
618 * 0, on success
619 * -1, on property read failure
620 * -2, on wrong timestamp size
621 */
622int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
623{
624 int len;
625 const void *data;
626
627 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
628 if (data == NULL) {
629 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
630 return -1;
631 }
632 if (len != sizeof(uint32_t)) {
633 debug("FIT timestamp with incorrect size of (%u)\n", len);
634 return -2;
635 }
636
637 *timestamp = uimage_to_cpu(*((uint32_t *)data));
638 return 0;
639}
640
641/**
642 * fit_image_get_node - get node offset for component image of a given unit name
643 * @fit: pointer to the FIT format image header
644 * @image_uname: component image node unit name
645 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500646 * fit_image_get_node() finds a component image (within the '/images'
Simon Glassda0af362013-05-07 06:11:53 +0000647 * node) of a provided unit name. If image is found its node offset is
648 * returned to the caller.
649 *
650 * returns:
651 * image node offset when found (>=0)
652 * negative number on failure (FDT_ERR_* code)
653 */
654int fit_image_get_node(const void *fit, const char *image_uname)
655{
656 int noffset, images_noffset;
657
658 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
659 if (images_noffset < 0) {
660 debug("Can't find images parent node '%s' (%s)\n",
661 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
662 return images_noffset;
663 }
664
665 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
666 if (noffset < 0) {
667 debug("Can't get node offset for image unit name: '%s' (%s)\n",
668 image_uname, fdt_strerror(noffset));
669 }
670
671 return noffset;
672}
673
674/**
675 * fit_image_get_os - get os id for a given component image node
676 * @fit: pointer to the FIT format image header
677 * @noffset: component image node offset
678 * @os: pointer to the uint8_t, will hold os numeric id
679 *
680 * fit_image_get_os() finds os property in a given component image node.
681 * If the property is found, its (string) value is translated to the numeric
682 * id which is returned to the caller.
683 *
684 * returns:
685 * 0, on success
686 * -1, on failure
687 */
688int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
689{
690 int len;
691 const void *data;
692
693 /* Get OS name from property data */
694 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
695 if (data == NULL) {
696 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
697 *os = -1;
698 return -1;
699 }
700
701 /* Translate OS name to id */
702 *os = genimg_get_os_id(data);
703 return 0;
704}
705
706/**
707 * fit_image_get_arch - get arch id for a given component image node
708 * @fit: pointer to the FIT format image header
709 * @noffset: component image node offset
710 * @arch: pointer to the uint8_t, will hold arch numeric id
711 *
712 * fit_image_get_arch() finds arch property in a given component image node.
713 * If the property is found, its (string) value is translated to the numeric
714 * id which is returned to the caller.
715 *
716 * returns:
717 * 0, on success
718 * -1, on failure
719 */
720int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
721{
722 int len;
723 const void *data;
724
725 /* Get architecture name from property data */
726 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
727 if (data == NULL) {
728 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
729 *arch = -1;
730 return -1;
731 }
732
733 /* Translate architecture name to id */
734 *arch = genimg_get_arch_id(data);
735 return 0;
736}
737
738/**
739 * fit_image_get_type - get type id for a given component image node
740 * @fit: pointer to the FIT format image header
741 * @noffset: component image node offset
742 * @type: pointer to the uint8_t, will hold type numeric id
743 *
744 * fit_image_get_type() finds type property in a given component image node.
745 * If the property is found, its (string) value is translated to the numeric
746 * id which is returned to the caller.
747 *
748 * returns:
749 * 0, on success
750 * -1, on failure
751 */
752int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
753{
754 int len;
755 const void *data;
756
757 /* Get image type name from property data */
758 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
759 if (data == NULL) {
760 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
761 *type = -1;
762 return -1;
763 }
764
765 /* Translate image type name to id */
766 *type = genimg_get_type_id(data);
767 return 0;
768}
769
770/**
771 * fit_image_get_comp - get comp id for a given component image node
772 * @fit: pointer to the FIT format image header
773 * @noffset: component image node offset
774 * @comp: pointer to the uint8_t, will hold comp numeric id
775 *
776 * fit_image_get_comp() finds comp property in a given component image node.
777 * If the property is found, its (string) value is translated to the numeric
778 * id which is returned to the caller.
779 *
780 * returns:
781 * 0, on success
782 * -1, on failure
783 */
784int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
785{
786 int len;
787 const void *data;
788
789 /* Get compression name from property data */
790 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
791 if (data == NULL) {
792 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Simon Glassda0af362013-05-07 06:11:53 +0000793 return -1;
794 }
795
796 /* Translate compression name to id */
797 *comp = genimg_get_comp_id(data);
798 return 0;
799}
800
Simon Glassa42f11f2022-10-20 18:23:04 -0600801/**
802 * fit_image_get_phase() - get the phase for a configuration node
803 * @fit: pointer to the FIT format image header
804 * @offset: configuration-node offset
805 * @phasep: returns the phase
806 *
807 * Finds the phase property in a given configuration node. If the property is
808 * found, its (string) value is translated to the numeric id which is returned
809 * to the caller.
810 *
811 * Returns: 0 on success, -ENOENT if missing, -EINVAL for invalid value
812 */
813int fit_image_get_phase(const void *fit, int offset, enum image_phase_t *phasep)
814{
815 const void *data;
816 int len, ret;
817
818 /* Get phase name from property data */
819 data = fdt_getprop(fit, offset, FIT_PHASE_PROP, &len);
820 if (!data) {
821 fit_get_debug(fit, offset, FIT_PHASE_PROP, len);
822 *phasep = 0;
823 return -ENOENT;
824 }
825
826 /* Translate phase name to id */
827 ret = genimg_get_phase_id(data);
828 if (ret < 0)
829 return ret;
830 *phasep = ret;
831
832 return 0;
833}
834
York Sun2b0464e2016-02-29 15:48:40 -0800835static int fit_image_get_address(const void *fit, int noffset, char *name,
836 ulong *load)
837{
York Sun31e5add2016-02-29 15:48:41 -0800838 int len, cell_len;
839 const fdt32_t *cell;
840 uint64_t load64 = 0;
York Sun2b0464e2016-02-29 15:48:40 -0800841
York Sun31e5add2016-02-29 15:48:41 -0800842 cell = fdt_getprop(fit, noffset, name, &len);
843 if (cell == NULL) {
York Sun2b0464e2016-02-29 15:48:40 -0800844 fit_get_debug(fit, noffset, name, len);
845 return -1;
846 }
847
York Sun31e5add2016-02-29 15:48:41 -0800848 cell_len = len >> 2;
849 /* Use load64 to avoid compiling warning for 32-bit target */
850 while (cell_len--) {
851 load64 = (load64 << 32) | uimage_to_cpu(*cell);
852 cell++;
853 }
Michal Simekd31b40f2020-09-03 12:44:51 +0200854
855 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
856 printf("Unsupported %s address size\n", name);
857 return -1;
858 }
859
York Sun31e5add2016-02-29 15:48:41 -0800860 *load = (ulong)load64;
York Sun2b0464e2016-02-29 15:48:40 -0800861
862 return 0;
863}
Simon Glassda0af362013-05-07 06:11:53 +0000864/**
865 * fit_image_get_load() - get load addr property for given component image node
866 * @fit: pointer to the FIT format image header
867 * @noffset: component image node offset
868 * @load: pointer to the uint32_t, will hold load address
869 *
870 * fit_image_get_load() finds load address property in a given component
871 * image node. If the property is found, its value is returned to the caller.
872 *
873 * returns:
874 * 0, on success
875 * -1, on failure
876 */
877int fit_image_get_load(const void *fit, int noffset, ulong *load)
878{
York Sun2b0464e2016-02-29 15:48:40 -0800879 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glassda0af362013-05-07 06:11:53 +0000880}
881
882/**
883 * fit_image_get_entry() - get entry point address property
884 * @fit: pointer to the FIT format image header
885 * @noffset: component image node offset
886 * @entry: pointer to the uint32_t, will hold entry point address
887 *
888 * This gets the entry point address property for a given component image
889 * node.
890 *
891 * fit_image_get_entry() finds entry point address property in a given
892 * component image node. If the property is found, its value is returned
893 * to the caller.
894 *
895 * returns:
896 * 0, on success
897 * -1, on failure
898 */
899int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
900{
York Sun2b0464e2016-02-29 15:48:40 -0800901 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glassda0af362013-05-07 06:11:53 +0000902}
903
904/**
905 * fit_image_get_data - get data property and its size for a given component image node
906 * @fit: pointer to the FIT format image header
907 * @noffset: component image node offset
908 * @data: double pointer to void, will hold data property's data address
909 * @size: pointer to size_t, will hold data property's data size
910 *
911 * fit_image_get_data() finds data property in a given component image node.
912 * If the property is found its data start address and size are returned to
913 * the caller.
914 *
915 * returns:
916 * 0, on success
917 * -1, on failure
918 */
919int fit_image_get_data(const void *fit, int noffset,
920 const void **data, size_t *size)
921{
922 int len;
923
924 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
925 if (*data == NULL) {
926 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
927 *size = 0;
928 return -1;
929 }
930
931 *size = len;
932 return 0;
933}
934
935/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200936 * Get 'data-offset' property from a given image node.
937 *
938 * @fit: pointer to the FIT image header
939 * @noffset: component image node offset
940 * @data_offset: holds the data-offset property
941 *
942 * returns:
943 * 0, on success
944 * -ENOENT if the property could not be found
945 */
946int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
947{
948 const fdt32_t *val;
949
950 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
951 if (!val)
952 return -ENOENT;
953
954 *data_offset = fdt32_to_cpu(*val);
955
956 return 0;
957}
958
959/**
Peng Fan8876c7e2017-12-05 13:20:59 +0800960 * Get 'data-position' property from a given image node.
961 *
962 * @fit: pointer to the FIT image header
963 * @noffset: component image node offset
964 * @data_position: holds the data-position property
965 *
966 * returns:
967 * 0, on success
968 * -ENOENT if the property could not be found
969 */
970int fit_image_get_data_position(const void *fit, int noffset,
971 int *data_position)
972{
973 const fdt32_t *val;
974
975 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
976 if (!val)
977 return -ENOENT;
978
979 *data_position = fdt32_to_cpu(*val);
980
981 return 0;
982}
983
984/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200985 * Get 'data-size' property from a given image node.
986 *
987 * @fit: pointer to the FIT image header
988 * @noffset: component image node offset
989 * @data_size: holds the data-size property
990 *
991 * returns:
992 * 0, on success
993 * -ENOENT if the property could not be found
994 */
995int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
996{
997 const fdt32_t *val;
998
999 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
1000 if (!val)
1001 return -ENOENT;
1002
1003 *data_size = fdt32_to_cpu(*val);
1004
1005 return 0;
1006}
1007
1008/**
Philippe Reynes3d964702019-12-18 18:25:42 +01001009 * Get 'data-size-unciphered' property from a given image node.
1010 *
1011 * @fit: pointer to the FIT image header
1012 * @noffset: component image node offset
1013 * @data_size: holds the data-size property
1014 *
1015 * returns:
1016 * 0, on success
1017 * -ENOENT if the property could not be found
1018 */
1019int fit_image_get_data_size_unciphered(const void *fit, int noffset,
1020 size_t *data_size)
1021{
1022 const fdt32_t *val;
1023
1024 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
1025 if (!val)
1026 return -ENOENT;
1027
1028 *data_size = (size_t)fdt32_to_cpu(*val);
1029
1030 return 0;
1031}
1032
1033/**
Kelvin Cheung186cc992018-05-19 18:21:37 +08001034 * fit_image_get_data_and_size - get data and its size including
1035 * both embedded and external data
1036 * @fit: pointer to the FIT format image header
1037 * @noffset: component image node offset
1038 * @data: double pointer to void, will hold data property's data address
1039 * @size: pointer to size_t, will hold data property's data size
1040 *
1041 * fit_image_get_data_and_size() finds data and its size including
1042 * both embedded and external data. If the property is found
1043 * its data start address and size are returned to the caller.
1044 *
1045 * returns:
1046 * 0, on success
1047 * otherwise, on failure
1048 */
1049int fit_image_get_data_and_size(const void *fit, int noffset,
1050 const void **data, size_t *size)
1051{
1052 bool external_data = false;
1053 int offset;
1054 int len;
1055 int ret;
1056
1057 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1058 external_data = true;
1059 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1060 external_data = true;
1061 /*
1062 * For FIT with external data, figure out where
1063 * the external images start. This is the base
1064 * for the data-offset properties in each image.
1065 */
1066 offset += ((fdt_totalsize(fit) + 3) & ~3);
1067 }
1068
1069 if (external_data) {
1070 debug("External Data\n");
1071 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt0be986f2020-03-11 21:51:08 +01001072 if (!ret) {
1073 *data = fit + offset;
1074 *size = len;
1075 }
Kelvin Cheung186cc992018-05-19 18:21:37 +08001076 } else {
1077 ret = fit_image_get_data(fit, noffset, data, size);
1078 }
1079
1080 return ret;
1081}
1082
1083/**
Simon Glassda0af362013-05-07 06:11:53 +00001084 * fit_image_hash_get_algo - get hash algorithm name
1085 * @fit: pointer to the FIT format image header
1086 * @noffset: hash node offset
1087 * @algo: double pointer to char, will hold pointer to the algorithm name
1088 *
1089 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1090 * If the property is found its data start address is returned to the caller.
1091 *
1092 * returns:
1093 * 0, on success
1094 * -1, on failure
1095 */
Jan Kiszka27beec22022-01-14 10:21:17 +01001096int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo)
Simon Glassda0af362013-05-07 06:11:53 +00001097{
1098 int len;
1099
Jan Kiszka27beec22022-01-14 10:21:17 +01001100 *algo = (const char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Simon Glassda0af362013-05-07 06:11:53 +00001101 if (*algo == NULL) {
1102 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1103 return -1;
1104 }
1105
1106 return 0;
1107}
1108
1109/**
1110 * fit_image_hash_get_value - get hash value and length
1111 * @fit: pointer to the FIT format image header
1112 * @noffset: hash node offset
1113 * @value: double pointer to uint8_t, will hold address of a hash value data
1114 * @value_len: pointer to an int, will hold hash data length
1115 *
1116 * fit_image_hash_get_value() finds hash value property in a given hash node.
1117 * If the property is found its data start address and size are returned to
1118 * the caller.
1119 *
1120 * returns:
1121 * 0, on success
1122 * -1, on failure
1123 */
1124int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1125 int *value_len)
1126{
1127 int len;
1128
1129 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1130 if (*value == NULL) {
1131 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1132 *value_len = 0;
1133 return -1;
1134 }
1135
1136 *value_len = len;
1137 return 0;
1138}
1139
Simon Glassda0af362013-05-07 06:11:53 +00001140/**
1141 * fit_image_hash_get_ignore - get hash ignore flag
1142 * @fit: pointer to the FIT format image header
1143 * @noffset: hash node offset
1144 * @ignore: pointer to an int, will hold hash ignore flag
1145 *
1146 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1147 * If the property is found and non-zero, the hash algorithm is not verified by
1148 * u-boot automatically.
1149 *
1150 * returns:
1151 * 0, on ignore not found
1152 * value, on ignore found
1153 */
Simon Glassf0fd5112013-05-07 06:11:58 +00001154static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glassda0af362013-05-07 06:11:53 +00001155{
1156 int len;
1157 int *value;
1158
1159 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1160 if (value == NULL || len != sizeof(int))
1161 *ignore = 0;
1162 else
1163 *ignore = *value;
1164
1165 return 0;
1166}
Simon Glassda0af362013-05-07 06:11:53 +00001167
Philippe Reynes3148e422019-12-18 18:25:41 +01001168/**
1169 * fit_image_cipher_get_algo - get cipher algorithm name
1170 * @fit: pointer to the FIT format image header
1171 * @noffset: cipher node offset
1172 * @algo: double pointer to char, will hold pointer to the algorithm name
1173 *
1174 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1175 * cipher node. If the property is found its data start address is returned
1176 * to the caller.
1177 *
1178 * returns:
1179 * 0, on success
1180 * -1, on failure
1181 */
1182int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1183{
1184 int len;
1185
1186 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1187 if (!*algo) {
1188 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1189 return -1;
1190 }
1191
1192 return 0;
1193}
1194
Simon Glass5b539a02016-02-24 09:14:42 -07001195ulong fit_get_end(const void *fit)
1196{
1197 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1198}
1199
Simon Glassda0af362013-05-07 06:11:53 +00001200/**
1201 * fit_set_timestamp - set node timestamp property
1202 * @fit: pointer to the FIT format image header
1203 * @noffset: node offset
1204 * @timestamp: timestamp value to be set
1205 *
1206 * fit_set_timestamp() attempts to set timestamp property in the requested
1207 * node and returns operation status to the caller.
1208 *
1209 * returns:
1210 * 0, on success
Simon Glassaf2f9d52014-06-02 22:04:51 -06001211 * -ENOSPC if no space in device tree, -1 for other error
Simon Glassda0af362013-05-07 06:11:53 +00001212 */
1213int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1214{
1215 uint32_t t;
1216 int ret;
1217
1218 t = cpu_to_uimage(timestamp);
1219 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1220 sizeof(uint32_t));
1221 if (ret) {
Simon Glasse4016fa2016-05-01 13:55:37 -06001222 debug("Can't set '%s' property for '%s' node (%s)\n",
1223 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1224 fdt_strerror(ret));
Simon Glassaf2f9d52014-06-02 22:04:51 -06001225 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glassda0af362013-05-07 06:11:53 +00001226 }
1227
1228 return 0;
1229}
1230
1231/**
1232 * calculate_hash - calculate and return hash for provided input data
1233 * @data: pointer to the input data
1234 * @data_len: data length
Chia-Wei Wang311c1f22021-10-27 14:17:24 +08001235 * @name: requested hash algorithm name
Simon Glassda0af362013-05-07 06:11:53 +00001236 * @value: pointer to the char, will hold hash value data (caller must
1237 * allocate enough free space)
1238 * value_len: length of the calculated hash
1239 *
1240 * calculate_hash() computes input data hash according to the requested
1241 * algorithm.
1242 * Resulting hash value is placed in caller provided 'value' buffer, length
1243 * of the calculated hash is returned via value_len pointer argument.
1244 *
1245 * returns:
1246 * 0, on success
1247 * -1, when algo is unsupported
1248 */
Alexandru Gagniuc650b7862021-09-02 19:54:21 -05001249int calculate_hash(const void *data, int data_len, const char *name,
Simon Glassda0af362013-05-07 06:11:53 +00001250 uint8_t *value, int *value_len)
1251{
Chia-Wei Wang4a733b32021-07-30 09:08:05 +08001252#if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
1253 int rc;
1254 enum HASH_ALGO hash_algo;
1255 struct udevice *dev;
1256
1257 rc = uclass_get_device(UCLASS_HASH, 0, &dev);
1258 if (rc) {
1259 debug("failed to get hash device, rc=%d\n", rc);
1260 return -1;
1261 }
1262
Chia-Wei Wang311c1f22021-10-27 14:17:24 +08001263 hash_algo = hash_algo_lookup_by_name(name);
Chia-Wei Wang4a733b32021-07-30 09:08:05 +08001264 if (hash_algo == HASH_ALGO_INVALID) {
1265 debug("Unsupported hash algorithm\n");
1266 return -1;
1267 };
1268
1269 rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ);
1270 if (rc) {
1271 debug("failed to get hash value, rc=%d\n", rc);
1272 return -1;
1273 }
1274
1275 *value_len = hash_algo_digest_size(hash_algo);
1276#else
Alexandru Gagniuc650b7862021-09-02 19:54:21 -05001277 struct hash_algo *algo;
1278 int ret;
1279
1280 ret = hash_lookup_algo(name, &algo);
1281 if (ret < 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001282 debug("Unsupported hash alogrithm\n");
1283 return -1;
1284 }
Alexandru Gagniuc650b7862021-09-02 19:54:21 -05001285
1286 algo->hash_func_ws(data, data_len, value, algo->chunk_size);
1287 *value_len = algo->digest_size;
Chia-Wei Wang4a733b32021-07-30 09:08:05 +08001288#endif
Alexandru Gagniuc650b7862021-09-02 19:54:21 -05001289
Simon Glassda0af362013-05-07 06:11:53 +00001290 return 0;
1291}
1292
Simon Glassf0fd5112013-05-07 06:11:58 +00001293static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1294 size_t size, char **err_msgp)
1295{
Joel Stanley447cc7a2022-06-20 16:31:17 +09301296 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, value, FIT_MAX_HASH_LEN);
Simon Glassf0fd5112013-05-07 06:11:58 +00001297 int value_len;
Jan Kiszka27beec22022-01-14 10:21:17 +01001298 const char *algo;
Simon Glassf0fd5112013-05-07 06:11:58 +00001299 uint8_t *fit_value;
1300 int fit_value_len;
1301 int ignore;
1302
1303 *err_msgp = NULL;
1304
1305 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001306 *err_msgp = "Can't get hash algo property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001307 return -1;
1308 }
1309 printf("%s", algo);
1310
Simon Glassa0cfb842021-09-25 19:43:28 -06001311 if (!tools_build()) {
Simon Glassf0fd5112013-05-07 06:11:58 +00001312 fit_image_hash_get_ignore(fit, noffset, &ignore);
1313 if (ignore) {
1314 printf("-skipped ");
1315 return 0;
1316 }
1317 }
1318
1319 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1320 &fit_value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001321 *err_msgp = "Can't get hash value property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001322 return -1;
1323 }
1324
1325 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001326 *err_msgp = "Unsupported hash algorithm";
Simon Glassf0fd5112013-05-07 06:11:58 +00001327 return -1;
1328 }
1329
1330 if (value_len != fit_value_len) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001331 *err_msgp = "Bad hash value len";
Simon Glassf0fd5112013-05-07 06:11:58 +00001332 return -1;
1333 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001334 *err_msgp = "Bad hash value";
Simon Glassf0fd5112013-05-07 06:11:58 +00001335 return -1;
1336 }
1337
1338 return 0;
1339}
1340
Jun Nieadf5d1c2018-02-27 16:55:58 +08001341int fit_image_verify_with_data(const void *fit, int image_noffset,
Simon Glasse10e2ee2021-11-12 12:28:10 -07001342 const void *key_blob, const void *data,
1343 size_t size)
Simon Glassda0af362013-05-07 06:11:53 +00001344{
Simon Glassfbabc0f2013-06-13 15:10:01 -07001345 int noffset = 0;
Simon Glassda0af362013-05-07 06:11:53 +00001346 char *err_msg = "";
Simon Glassfbabc0f2013-06-13 15:10:01 -07001347 int verify_all = 1;
1348 int ret;
Simon Glassda0af362013-05-07 06:11:53 +00001349
Simon Glassfbabc0f2013-06-13 15:10:01 -07001350 /* Verify all required signatures */
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +09001351 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glassfbabc0f2013-06-13 15:10:01 -07001352 fit_image_verify_required_sigs(fit, image_noffset, data, size,
Simon Glasse10e2ee2021-11-12 12:28:10 -07001353 key_blob, &verify_all)) {
Simon Glassfbabc0f2013-06-13 15:10:01 -07001354 err_msg = "Unable to verify required signature";
1355 goto error;
1356 }
1357
Simon Glassda0af362013-05-07 06:11:53 +00001358 /* Process all hash subnodes of the component image node */
Simon Glass499c29e2016-10-02 17:59:29 -06001359 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassf0fd5112013-05-07 06:11:58 +00001360 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001361
Simon Glassf0fd5112013-05-07 06:11:58 +00001362 /*
1363 * Check subnode name, must be equal to "hash".
1364 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +00001365 * names, e.g. hash-1, hash-2, etc.
Simon Glassf0fd5112013-05-07 06:11:58 +00001366 */
1367 if (!strncmp(name, FIT_HASH_NODENAME,
1368 strlen(FIT_HASH_NODENAME))) {
1369 if (fit_image_check_hash(fit, noffset, data, size,
1370 &err_msg))
Simon Glassda0af362013-05-07 06:11:53 +00001371 goto error;
Simon Glassf0fd5112013-05-07 06:11:58 +00001372 puts("+ ");
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +09001373 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glassfbabc0f2013-06-13 15:10:01 -07001374 !strncmp(name, FIT_SIG_NODENAME,
1375 strlen(FIT_SIG_NODENAME))) {
Simon Glasse10e2ee2021-11-12 12:28:10 -07001376 ret = fit_image_check_sig(fit, noffset, data, size,
1377 gd_fdt_blob(), -1, &err_msg);
Simon Glass51026aa2016-02-24 09:14:43 -07001378
1379 /*
1380 * Show an indication on failure, but do not return
1381 * an error. Only keys marked 'required' can cause
1382 * an image validation failure. See the call to
1383 * fit_image_verify_required_sigs() above.
1384 */
1385 if (ret)
Simon Glassfbabc0f2013-06-13 15:10:01 -07001386 puts("- ");
1387 else
1388 puts("+ ");
Simon Glassda0af362013-05-07 06:11:53 +00001389 }
1390 }
1391
1392 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001393 err_msg = "Corrupted or truncated tree";
Simon Glassda0af362013-05-07 06:11:53 +00001394 goto error;
1395 }
1396
1397 return 1;
1398
1399error:
Simon Glass8f3aa462013-05-07 06:11:59 +00001400 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glassda0af362013-05-07 06:11:53 +00001401 err_msg, fit_get_name(fit, noffset, NULL),
1402 fit_get_name(fit, image_noffset, NULL));
1403 return 0;
1404}
1405
1406/**
Jun Nieadf5d1c2018-02-27 16:55:58 +08001407 * fit_image_verify - verify data integrity
1408 * @fit: pointer to the FIT format image header
1409 * @image_noffset: component image node offset
1410 *
1411 * fit_image_verify() goes over component image hash nodes,
1412 * re-calculates each data hash and compares with the value stored in hash
1413 * node.
1414 *
1415 * returns:
1416 * 1, if all hashes are valid
1417 * 0, otherwise (or on error)
1418 */
1419int fit_image_verify(const void *fit, int image_noffset)
1420{
Simon Glass44338902021-02-15 17:08:06 -07001421 const char *name = fit_get_name(fit, image_noffset, NULL);
Jun Nieadf5d1c2018-02-27 16:55:58 +08001422 const void *data;
1423 size_t size;
Jun Nieadf5d1c2018-02-27 16:55:58 +08001424 char *err_msg = "";
1425
Simon Glass3d702182021-07-05 16:32:56 -06001426 if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) {
Simon Glass44338902021-02-15 17:08:06 -07001427 /*
1428 * We don't support this since libfdt considers names with the
1429 * name root but different @ suffix to be equal
1430 */
1431 err_msg = "Node name contains @";
1432 goto err;
1433 }
Jun Nieadf5d1c2018-02-27 16:55:58 +08001434 /* Get image data and data length */
Kelvin Cheung186cc992018-05-19 18:21:37 +08001435 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nieadf5d1c2018-02-27 16:55:58 +08001436 err_msg = "Can't get image data/size";
Simon Glass44338902021-02-15 17:08:06 -07001437 goto err;
Jun Nieadf5d1c2018-02-27 16:55:58 +08001438 }
1439
Simon Glasse10e2ee2021-11-12 12:28:10 -07001440 return fit_image_verify_with_data(fit, image_noffset, gd_fdt_blob(),
1441 data, size);
Simon Glass44338902021-02-15 17:08:06 -07001442
1443err:
1444 printf("error!\n%s in '%s' image node\n", err_msg,
1445 fit_get_name(fit, image_noffset, NULL));
1446 return 0;
Jun Nieadf5d1c2018-02-27 16:55:58 +08001447}
1448
1449/**
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001450 * fit_all_image_verify - verify data integrity for all images
Simon Glassda0af362013-05-07 06:11:53 +00001451 * @fit: pointer to the FIT format image header
1452 *
Simon Glass7428ad12013-05-07 06:11:57 +00001453 * fit_all_image_verify() goes over all images in the FIT and
Simon Glassda0af362013-05-07 06:11:53 +00001454 * for every images checks if all it's hashes are valid.
1455 *
1456 * returns:
1457 * 1, if all hashes of all images are valid
1458 * 0, otherwise (or on error)
1459 */
Simon Glass7428ad12013-05-07 06:11:57 +00001460int fit_all_image_verify(const void *fit)
Simon Glassda0af362013-05-07 06:11:53 +00001461{
1462 int images_noffset;
1463 int noffset;
1464 int ndepth;
1465 int count;
1466
1467 /* Find images parent node offset */
1468 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1469 if (images_noffset < 0) {
1470 printf("Can't find images parent node '%s' (%s)\n",
1471 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1472 return 0;
1473 }
1474
1475 /* Process all image subnodes, check hashes for each */
1476 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1477 (ulong)fit);
1478 for (ndepth = 0, count = 0,
1479 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1480 (noffset >= 0) && (ndepth > 0);
1481 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1482 if (ndepth == 1) {
1483 /*
1484 * Direct child node of the images parent node,
1485 * i.e. component image node.
1486 */
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001487 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glassda0af362013-05-07 06:11:53 +00001488 fit_get_name(fit, noffset, NULL));
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001489 count++;
Simon Glassda0af362013-05-07 06:11:53 +00001490
Simon Glass7428ad12013-05-07 06:11:57 +00001491 if (!fit_image_verify(fit, noffset))
Simon Glassda0af362013-05-07 06:11:53 +00001492 return 0;
1493 printf("\n");
1494 }
1495 }
1496 return 1;
1497}
1498
Philippe Reynes3d964702019-12-18 18:25:42 +01001499static int fit_image_uncipher(const void *fit, int image_noffset,
1500 void **data, size_t *size)
1501{
1502 int cipher_noffset, ret;
1503 void *dst;
1504 size_t size_dst;
1505
1506 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1507 FIT_CIPHER_NODENAME);
1508 if (cipher_noffset < 0)
1509 return 0;
1510
1511 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1512 *data, *size, &dst, &size_dst);
1513 if (ret)
1514 goto out;
1515
1516 *data = dst;
1517 *size = size_dst;
1518
1519 out:
1520 return ret;
1521}
Philippe Reynes3d964702019-12-18 18:25:42 +01001522
Simon Glassda0af362013-05-07 06:11:53 +00001523/**
1524 * fit_image_check_os - check whether image node is of a given os type
1525 * @fit: pointer to the FIT format image header
1526 * @noffset: component image node offset
1527 * @os: requested image os
1528 *
1529 * fit_image_check_os() reads image os property and compares its numeric
1530 * id with the requested os. Comparison result is returned to the caller.
1531 *
1532 * returns:
1533 * 1 if image is of given os type
1534 * 0 otherwise (or on error)
1535 */
1536int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1537{
1538 uint8_t image_os;
1539
1540 if (fit_image_get_os(fit, noffset, &image_os))
1541 return 0;
1542 return (os == image_os);
1543}
1544
1545/**
1546 * fit_image_check_arch - check whether image node is of a given arch
1547 * @fit: pointer to the FIT format image header
1548 * @noffset: component image node offset
1549 * @arch: requested imagearch
1550 *
1551 * fit_image_check_arch() reads image arch property and compares its numeric
1552 * id with the requested arch. Comparison result is returned to the caller.
1553 *
1554 * returns:
1555 * 1 if image is of given arch
1556 * 0 otherwise (or on error)
1557 */
1558int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1559{
1560 uint8_t image_arch;
Alison Wang73818d52016-11-10 10:49:03 +08001561 int aarch32_support = 0;
1562
Simon Glass062e79f2021-03-15 18:11:12 +13001563 /* Let's assume that sandbox can load any architecture */
1564 if (IS_ENABLED(CONFIG_SANDBOX))
1565 return true;
1566
Sebastian Reichelbfe8ddf2021-01-04 20:48:03 +01001567 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1568 aarch32_support = 1;
Simon Glassda0af362013-05-07 06:11:53 +00001569
1570 if (fit_image_get_arch(fit, noffset, &image_arch))
1571 return 0;
Simon Glass9d428302014-10-10 08:21:57 -06001572 return (arch == image_arch) ||
Alison Wang73818d52016-11-10 10:49:03 +08001573 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1574 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1575 aarch32_support);
Simon Glassda0af362013-05-07 06:11:53 +00001576}
1577
1578/**
1579 * fit_image_check_type - check whether image node is of a given type
1580 * @fit: pointer to the FIT format image header
1581 * @noffset: component image node offset
1582 * @type: requested image type
1583 *
1584 * fit_image_check_type() reads image type property and compares its numeric
1585 * id with the requested type. Comparison result is returned to the caller.
1586 *
1587 * returns:
1588 * 1 if image is of given type
1589 * 0 otherwise (or on error)
1590 */
1591int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1592{
1593 uint8_t image_type;
1594
1595 if (fit_image_get_type(fit, noffset, &image_type))
1596 return 0;
1597 return (type == image_type);
1598}
1599
1600/**
1601 * fit_image_check_comp - check whether image node uses given compression
1602 * @fit: pointer to the FIT format image header
1603 * @noffset: component image node offset
1604 * @comp: requested image compression type
1605 *
1606 * fit_image_check_comp() reads image compression property and compares its
1607 * numeric id with the requested compression type. Comparison result is
1608 * returned to the caller.
1609 *
1610 * returns:
1611 * 1 if image uses requested compression
1612 * 0 otherwise (or on error)
1613 */
1614int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1615{
1616 uint8_t image_comp;
1617
1618 if (fit_image_get_comp(fit, noffset, &image_comp))
1619 return 0;
1620 return (comp == image_comp);
1621}
1622
Simon Glassb823daa2021-02-15 17:08:12 -07001623/**
1624 * fdt_check_no_at() - Check for nodes whose names contain '@'
1625 *
1626 * This checks the parent node and all subnodes recursively
1627 *
1628 * @fit: FIT to check
1629 * @parent: Parent node to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001630 * Return: 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
Simon Glassb823daa2021-02-15 17:08:12 -07001631 */
1632static int fdt_check_no_at(const void *fit, int parent)
1633{
1634 const char *name;
1635 int node;
1636 int ret;
1637
1638 name = fdt_get_name(fit, parent, NULL);
1639 if (!name || strchr(name, '@'))
1640 return -EADDRNOTAVAIL;
1641
1642 fdt_for_each_subnode(node, fit, parent) {
1643 ret = fdt_check_no_at(fit, node);
1644 if (ret)
1645 return ret;
1646 }
1647
1648 return 0;
1649}
1650
Simon Glassd563c252021-02-15 17:08:09 -07001651int fit_check_format(const void *fit, ulong size)
Simon Glassda0af362013-05-07 06:11:53 +00001652{
Simon Glassd563c252021-02-15 17:08:09 -07001653 int ret;
1654
Heinrich Schuchardtef98b212021-01-13 02:09:12 +01001655 /* A FIT image must be a valid FDT */
Simon Glassd563c252021-02-15 17:08:09 -07001656 ret = fdt_check_header(fit);
1657 if (ret) {
1658 log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1659 ret);
1660 return -ENOEXEC;
Heinrich Schuchardtef98b212021-01-13 02:09:12 +01001661 }
1662
Simon Glass244705b2021-02-15 17:08:10 -07001663 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1664 /*
1665 * If we are not given the size, make do wtih calculating it.
1666 * This is not as secure, so we should consider a flag to
1667 * control this.
1668 */
1669 if (size == IMAGE_SIZE_INVAL)
1670 size = fdt_totalsize(fit);
1671 ret = fdt_check_full(fit, size);
Simon Glassb823daa2021-02-15 17:08:12 -07001672 if (ret)
1673 ret = -EINVAL;
1674
1675 /*
1676 * U-Boot stopped using unit addressed in 2017. Since libfdt
1677 * can match nodes ignoring any unit address, signature
1678 * verification can see the wrong node if one is inserted with
1679 * the same name as a valid node but with a unit address
1680 * attached. Protect against this by disallowing unit addresses.
1681 */
1682 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1683 ret = fdt_check_no_at(fit, 0);
Simon Glass244705b2021-02-15 17:08:10 -07001684
Simon Glassb823daa2021-02-15 17:08:12 -07001685 if (ret) {
1686 log_debug("FIT check error %d\n", ret);
1687 return ret;
1688 }
1689 }
Simon Glass244705b2021-02-15 17:08:10 -07001690 if (ret) {
1691 log_debug("FIT check error %d\n", ret);
Simon Glassb823daa2021-02-15 17:08:12 -07001692 return ret;
Simon Glass244705b2021-02-15 17:08:10 -07001693 }
1694 }
1695
Simon Glassda0af362013-05-07 06:11:53 +00001696 /* mandatory / node 'description' property */
Simon Glassd563c252021-02-15 17:08:09 -07001697 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1698 log_debug("Wrong FIT format: no description\n");
1699 return -ENOMSG;
Simon Glassda0af362013-05-07 06:11:53 +00001700 }
1701
1702 if (IMAGE_ENABLE_TIMESTAMP) {
1703 /* mandatory / node 'timestamp' property */
Simon Glassd563c252021-02-15 17:08:09 -07001704 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1705 log_debug("Wrong FIT format: no timestamp\n");
Simon Glassb641de82021-02-24 08:50:32 -05001706 return -EBADMSG;
Simon Glassda0af362013-05-07 06:11:53 +00001707 }
1708 }
1709
1710 /* mandatory subimages parent '/images' node */
1711 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
Simon Glassd563c252021-02-15 17:08:09 -07001712 log_debug("Wrong FIT format: no images parent node\n");
1713 return -ENOENT;
Simon Glassda0af362013-05-07 06:11:53 +00001714 }
1715
Simon Glassd563c252021-02-15 17:08:09 -07001716 return 0;
Simon Glassda0af362013-05-07 06:11:53 +00001717}
1718
Simon Glassda0af362013-05-07 06:11:53 +00001719int fit_conf_find_compat(const void *fit, const void *fdt)
1720{
1721 int ndepth = 0;
1722 int noffset, confs_noffset, images_noffset;
1723 const void *fdt_compat;
1724 int fdt_compat_len;
1725 int best_match_offset = 0;
1726 int best_match_pos = 0;
1727
1728 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1729 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1730 if (confs_noffset < 0 || images_noffset < 0) {
1731 debug("Can't find configurations or images nodes.\n");
1732 return -1;
1733 }
1734
1735 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1736 if (!fdt_compat) {
1737 debug("Fdt for comparison has no \"compatible\" property.\n");
1738 return -1;
1739 }
1740
1741 /*
1742 * Loop over the configurations in the FIT image.
1743 */
1744 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1745 (noffset >= 0) && (ndepth > 0);
1746 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner4e823522019-07-24 19:37:56 -07001747 const void *fdt;
Simon Glassda0af362013-05-07 06:11:53 +00001748 const char *kfdt_name;
Julius Werner4e823522019-07-24 19:37:56 -07001749 int kfdt_noffset, compat_noffset;
Simon Glassda0af362013-05-07 06:11:53 +00001750 const char *cur_fdt_compat;
1751 int len;
Julius Werner4e823522019-07-24 19:37:56 -07001752 size_t sz;
Simon Glassda0af362013-05-07 06:11:53 +00001753 int i;
1754
1755 if (ndepth > 1)
1756 continue;
1757
Julius Werner4e823522019-07-24 19:37:56 -07001758 /* If there's a compat property in the config node, use that. */
1759 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1760 fdt = fit; /* search in FIT image */
1761 compat_noffset = noffset; /* search under config node */
1762 } else { /* Otherwise extract it from the kernel FDT. */
1763 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1764 if (!kfdt_name) {
1765 debug("No fdt property found.\n");
1766 continue;
1767 }
1768 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1769 kfdt_name);
1770 if (kfdt_noffset < 0) {
1771 debug("No image node named \"%s\" found.\n",
1772 kfdt_name);
1773 continue;
1774 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001775
Julius Werner4e823522019-07-24 19:37:56 -07001776 if (!fit_image_check_comp(fit, kfdt_noffset,
1777 IH_COMP_NONE)) {
1778 debug("Can't extract compat from \"%s\" "
1779 "(compressed)\n", kfdt_name);
1780 continue;
1781 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001782
Julius Werner4e823522019-07-24 19:37:56 -07001783 /* search in this config's kernel FDT */
John Keeping645a3122021-06-25 17:58:04 +01001784 if (fit_image_get_data_and_size(fit, kfdt_noffset,
1785 &fdt, &sz)) {
Julius Werner4e823522019-07-24 19:37:56 -07001786 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1787 continue;
1788 }
1789
1790 compat_noffset = 0; /* search kFDT under root node */
Simon Glassda0af362013-05-07 06:11:53 +00001791 }
1792
1793 len = fdt_compat_len;
1794 cur_fdt_compat = fdt_compat;
1795 /*
1796 * Look for a match for each U-Boot compatibility string in
Julius Werner4e823522019-07-24 19:37:56 -07001797 * turn in the compat string property.
Simon Glassda0af362013-05-07 06:11:53 +00001798 */
1799 for (i = 0; len > 0 &&
1800 (!best_match_offset || best_match_pos > i); i++) {
1801 int cur_len = strlen(cur_fdt_compat) + 1;
1802
Julius Werner4e823522019-07-24 19:37:56 -07001803 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001804 cur_fdt_compat)) {
1805 best_match_offset = noffset;
1806 best_match_pos = i;
1807 break;
1808 }
1809 len -= cur_len;
1810 cur_fdt_compat += cur_len;
1811 }
1812 }
1813 if (!best_match_offset) {
1814 debug("No match found.\n");
1815 return -1;
1816 }
1817
1818 return best_match_offset;
1819}
1820
Simon Glassda0af362013-05-07 06:11:53 +00001821int fit_conf_get_node(const void *fit, const char *conf_uname)
1822{
1823 int noffset, confs_noffset;
1824 int len;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001825 const char *s;
1826 char *conf_uname_copy = NULL;
Simon Glassda0af362013-05-07 06:11:53 +00001827
1828 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1829 if (confs_noffset < 0) {
1830 debug("Can't find configurations parent node '%s' (%s)\n",
1831 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1832 return confs_noffset;
1833 }
1834
1835 if (conf_uname == NULL) {
1836 /* get configuration unit name from the default property */
1837 debug("No configuration specified, trying default...\n");
Simon Glassdb503fb2021-09-25 19:43:14 -06001838 if (!tools_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
Sebastian Reichel0890f942021-01-04 20:48:04 +01001839 noffset = fit_find_config_node(fit);
1840 if (noffset < 0)
1841 return noffset;
1842 conf_uname = fdt_get_name(fit, noffset, NULL);
1843 } else {
1844 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1845 FIT_DEFAULT_PROP, &len);
1846 if (conf_uname == NULL) {
1847 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1848 len);
1849 return len;
1850 }
Simon Glassda0af362013-05-07 06:11:53 +00001851 }
1852 debug("Found default configuration: '%s'\n", conf_uname);
1853 }
1854
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001855 s = strchr(conf_uname, '#');
1856 if (s) {
1857 len = s - conf_uname;
1858 conf_uname_copy = malloc(len + 1);
1859 if (!conf_uname_copy) {
1860 debug("Can't allocate uname copy: '%s'\n",
1861 conf_uname);
1862 return -ENOMEM;
1863 }
1864 memcpy(conf_uname_copy, conf_uname, len);
1865 conf_uname_copy[len] = '\0';
1866 conf_uname = conf_uname_copy;
1867 }
1868
Simon Glassda0af362013-05-07 06:11:53 +00001869 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1870 if (noffset < 0) {
1871 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1872 conf_uname, fdt_strerror(noffset));
1873 }
1874
Heinrich Schuchardtf5686462022-04-11 20:08:03 +02001875 free(conf_uname_copy);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001876
Simon Glassda0af362013-05-07 06:11:53 +00001877 return noffset;
1878}
1879
Pantelis Antonioua4880742017-09-04 23:12:14 +03001880int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001881 const char *prop_name)
1882{
Pantelis Antonioua4880742017-09-04 23:12:14 +03001883 return fdt_stringlist_count(fit, noffset, prop_name);
1884}
1885
1886int fit_conf_get_prop_node_index(const void *fit, int noffset,
1887 const char *prop_name, int index)
1888{
1889 const char *uname;
Simon Glassda0af362013-05-07 06:11:53 +00001890 int len;
1891
1892 /* get kernel image unit name from configuration kernel property */
Pantelis Antonioua4880742017-09-04 23:12:14 +03001893 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glassda0af362013-05-07 06:11:53 +00001894 if (uname == NULL)
1895 return len;
1896
1897 return fit_image_get_node(fit, uname);
1898}
1899
Simon Glassa42f11f2022-10-20 18:23:04 -06001900int fit_conf_get_prop_node(const void *fit, int noffset, const char *prop_name,
1901 enum image_phase_t sel_phase)
Pantelis Antonioua4880742017-09-04 23:12:14 +03001902{
Simon Glassa42f11f2022-10-20 18:23:04 -06001903 int i, count;
1904
1905 if (sel_phase == IH_PHASE_NONE)
1906 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1907
1908 count = fit_conf_get_prop_node_count(fit, noffset, prop_name);
1909 if (count < 0)
1910 return count;
1911
1912 /* check each image in the list */
1913 for (i = 0; i < count; i++) {
1914 enum image_phase_t phase;
1915 int ret, node;
1916
1917 node = fit_conf_get_prop_node_index(fit, noffset, prop_name, i);
1918 ret = fit_image_get_phase(fit, node, &phase);
1919
1920 /* if the image is for any phase, let's use it */
1921 if (ret == -ENOENT)
1922 return node;
1923 else if (ret < 0)
1924 return ret;
1925
1926 if (phase == sel_phase)
1927 return node;
1928 }
1929
1930 return -ENOENT;
Pantelis Antonioua4880742017-09-04 23:12:14 +03001931}
Simon Glassda0af362013-05-07 06:11:53 +00001932
Sean Anderson080d3a02022-08-16 11:16:03 -04001933static int fit_get_data_tail(const void *fit, int noffset,
1934 const void **data, size_t *size)
1935{
1936 char *desc;
1937
1938 if (noffset < 0)
1939 return noffset;
1940
1941 if (!fit_image_verify(fit, noffset))
1942 return -EINVAL;
1943
1944 if (fit_image_get_data_and_size(fit, noffset, data, size))
1945 return -ENOENT;
1946
1947 if (!fit_get_desc(fit, noffset, &desc))
1948 printf("%s\n", desc);
1949
1950 return 0;
1951}
1952
1953int fit_get_data_node(const void *fit, const char *image_uname,
1954 const void **data, size_t *size)
1955{
1956 int noffset = fit_image_get_node(fit, image_uname);
1957
1958 return fit_get_data_tail(fit, noffset, data, size);
1959}
1960
1961int fit_get_data_conf_prop(const void *fit, const char *prop_name,
1962 const void **data, size_t *size)
1963{
1964 int noffset = fit_conf_get_node(fit, NULL);
1965
Simon Glassa42f11f2022-10-20 18:23:04 -06001966 noffset = fit_conf_get_prop_node(fit, noffset, prop_name,
1967 IH_PHASE_NONE);
Sean Anderson080d3a02022-08-16 11:16:03 -04001968 return fit_get_data_tail(fit, noffset, data, size);
1969}
1970
Jeroen Hofsteeffa60da2014-10-08 22:57:38 +02001971static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass384d86d2013-05-16 13:53:21 +00001972{
1973 fit_image_print(fit, rd_noffset, " ");
1974
1975 if (verify) {
1976 puts(" Verifying Hash Integrity ... ");
1977 if (!fit_image_verify(fit, rd_noffset)) {
1978 puts("Bad Data Hash\n");
1979 return -EACCES;
1980 }
1981 puts("OK\n");
1982 }
1983
1984 return 0;
1985}
1986
Simon Glassdf00afa2022-09-06 20:26:50 -06001987int fit_get_node_from_config(struct bootm_headers *images,
1988 const char *prop_name, ulong addr)
Simon Glass384d86d2013-05-16 13:53:21 +00001989{
1990 int cfg_noffset;
1991 void *fit_hdr;
1992 int noffset;
1993
1994 debug("* %s: using config '%s' from image at 0x%08lx\n",
1995 prop_name, images->fit_uname_cfg, addr);
1996
1997 /* Check whether configuration has this property defined */
1998 fit_hdr = map_sysmem(addr, 0);
1999 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
2000 if (cfg_noffset < 0) {
2001 debug("* %s: no such config\n", prop_name);
Paul Burton14171b12016-09-20 18:17:12 +01002002 return -EINVAL;
Simon Glass384d86d2013-05-16 13:53:21 +00002003 }
2004
Simon Glassa42f11f2022-10-20 18:23:04 -06002005 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name,
2006 IH_PHASE_NONE);
Simon Glass384d86d2013-05-16 13:53:21 +00002007 if (noffset < 0) {
2008 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Grayae6a02f2016-09-03 08:30:14 +10002009 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00002010 }
2011
2012 return noffset;
2013}
2014
Simon Glassa0c0b632014-06-12 07:24:47 -06002015/**
2016 * fit_get_image_type_property() - get property name for IH_TYPE_...
2017 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002018 * Return: the properly name where we expect to find the image in the
Simon Glassa0c0b632014-06-12 07:24:47 -06002019 * config node
2020 */
2021static const char *fit_get_image_type_property(int type)
2022{
2023 /*
2024 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05002025 * but we don't have access to the short name, and "fdt" is different
Simon Glassa0c0b632014-06-12 07:24:47 -06002026 * anyway. So let's just keep it here.
2027 */
2028 switch (type) {
2029 case IH_TYPE_FLATDT:
2030 return FIT_FDT_PROP;
2031 case IH_TYPE_KERNEL:
2032 return FIT_KERNEL_PROP;
Alexandru Gagniucd1f78262021-04-01 13:25:30 -05002033 case IH_TYPE_FIRMWARE:
2034 return FIT_FIRMWARE_PROP;
Simon Glassa0c0b632014-06-12 07:24:47 -06002035 case IH_TYPE_RAMDISK:
2036 return FIT_RAMDISK_PROP;
Simon Glass0129b522014-10-19 21:11:24 -06002037 case IH_TYPE_X86_SETUP:
2038 return FIT_SETUP_PROP;
Karl Apsite1b21c282015-05-21 09:52:48 -04002039 case IH_TYPE_LOADABLE:
2040 return FIT_LOADABLE_PROP;
Michal Simekebae78b2016-05-17 14:03:50 +02002041 case IH_TYPE_FPGA:
2042 return FIT_FPGA_PROP;
Marek Vasut93e95752018-05-13 00:22:54 +02002043 case IH_TYPE_STANDALONE:
2044 return FIT_STANDALONE_PROP;
Simon Glassa0c0b632014-06-12 07:24:47 -06002045 }
2046
2047 return "unknown";
2048}
2049
Simon Glassdf00afa2022-09-06 20:26:50 -06002050int fit_image_load(struct bootm_headers *images, ulong addr,
Simon Glassad68fc32013-07-10 23:08:10 -07002051 const char **fit_unamep, const char **fit_uname_configp,
Simon Glassa42f11f2022-10-20 18:23:04 -06002052 int arch, int ph_type, int bootstage_id,
Simon Glass384d86d2013-05-16 13:53:21 +00002053 enum fit_load_op load_op, ulong *datap, ulong *lenp)
2054{
Simon Glassa42f11f2022-10-20 18:23:04 -06002055 int image_type = image_ph_type(ph_type);
Simon Glass384d86d2013-05-16 13:53:21 +00002056 int cfg_noffset, noffset;
2057 const char *fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07002058 const char *fit_uname_config;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002059 const char *fit_base_uname_config;
Simon Glass384d86d2013-05-16 13:53:21 +00002060 const void *fit;
Julius Werner97b09cd2019-07-24 19:37:55 -07002061 void *buf;
2062 void *loadbuf;
Simon Glass384d86d2013-05-16 13:53:21 +00002063 size_t size;
2064 int type_ok, os_ok;
Julius Werner97b09cd2019-07-24 19:37:55 -07002065 ulong load, load_end, data, len;
2066 uint8_t os, comp;
Simon Glassa0c0b632014-06-12 07:24:47 -06002067 const char *prop_name;
Simon Glass384d86d2013-05-16 13:53:21 +00002068 int ret;
2069
2070 fit = map_sysmem(addr, 0);
2071 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassad68fc32013-07-10 23:08:10 -07002072 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002073 fit_base_uname_config = NULL;
Simon Glassa0c0b632014-06-12 07:24:47 -06002074 prop_name = fit_get_image_type_property(image_type);
Simon Glass384d86d2013-05-16 13:53:21 +00002075 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
2076
2077 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glassb823daa2021-02-15 17:08:12 -07002078 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
2079 if (ret) {
2080 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
2081 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
2082 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
Simon Glass384d86d2013-05-16 13:53:21 +00002083 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
Simon Glassb823daa2021-02-15 17:08:12 -07002084 return ret;
Simon Glass384d86d2013-05-16 13:53:21 +00002085 }
2086 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
2087 if (fit_uname) {
Masahiro Yamadaaa58eec2014-02-18 15:39:21 +09002088 /* get FIT component image node offset */
Simon Glass384d86d2013-05-16 13:53:21 +00002089 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
2090 noffset = fit_image_get_node(fit, fit_uname);
2091 } else {
2092 /*
2093 * no image node unit name, try to get config
2094 * node first. If config unit node name is NULL
2095 * fit_conf_get_node() will try to find default config node
2096 */
2097 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
Simon Glass0434ea12021-07-14 17:05:36 -05002098 if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) {
Simon Glass384d86d2013-05-16 13:53:21 +00002099 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2100 } else {
Simon Glassa42f11f2022-10-20 18:23:04 -06002101 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00002102 }
2103 if (cfg_noffset < 0) {
2104 puts("Could not find configuration node\n");
2105 bootstage_error(bootstage_id +
2106 BOOTSTAGE_SUB_NO_UNIT_NAME);
2107 return -ENOENT;
2108 }
Marek Vasut5dee1342018-05-31 17:59:07 +02002109
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002110 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2111 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut5dee1342018-05-31 17:59:07 +02002112 /* Remember this config */
2113 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002114 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut5dee1342018-05-31 17:59:07 +02002115
AKASHI Takahiro2223c7d2020-02-21 15:12:55 +09002116 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut5dee1342018-05-31 17:59:07 +02002117 puts(" Verifying Hash Integrity ... ");
2118 if (fit_config_verify(fit, cfg_noffset)) {
2119 puts("Bad Data Hash\n");
2120 bootstage_error(bootstage_id +
2121 BOOTSTAGE_SUB_HASH);
2122 return -EACCES;
Simon Glass384d86d2013-05-16 13:53:21 +00002123 }
Marek Vasut5dee1342018-05-31 17:59:07 +02002124 puts("OK\n");
Simon Glass384d86d2013-05-16 13:53:21 +00002125 }
2126
Marek Vasut5dee1342018-05-31 17:59:07 +02002127 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2128
Simon Glassa42f11f2022-10-20 18:23:04 -06002129 noffset = fit_conf_get_prop_node(fit, cfg_noffset, prop_name,
2130 image_ph_phase(ph_type));
Simon Glass384d86d2013-05-16 13:53:21 +00002131 fit_uname = fit_get_name(fit, noffset, NULL);
2132 }
2133 if (noffset < 0) {
Simon Glassa559bb22020-03-18 11:43:56 -06002134 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass384d86d2013-05-16 13:53:21 +00002135 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2136 return -ENOENT;
2137 }
2138
2139 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2140
2141 ret = fit_image_select(fit, noffset, images->verify);
2142 if (ret) {
2143 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2144 return ret;
2145 }
2146
2147 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glassdb503fb2021-09-25 19:43:14 -06002148 if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX)) {
Sebastian Reichelbfe8ddf2021-01-04 20:48:03 +01002149 if (!fit_image_check_target_arch(fit, noffset)) {
2150 puts("Unsupported Architecture\n");
2151 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2152 return -ENOEXEC;
2153 }
Simon Glass384d86d2013-05-16 13:53:21 +00002154 }
Alison Wang73818d52016-11-10 10:49:03 +08002155
2156#ifndef USE_HOSTCC
Simon Glassc0cabbc2021-09-25 19:43:39 -06002157 {
2158 uint8_t os_arch;
2159
Alison Wang73818d52016-11-10 10:49:03 +08002160 fit_image_get_arch(fit, noffset, &os_arch);
2161 images->os.arch = os_arch;
Simon Glassc0cabbc2021-09-25 19:43:39 -06002162 }
Alison Wang73818d52016-11-10 10:49:03 +08002163#endif
2164
Simon Glass384d86d2013-05-16 13:53:21 +00002165 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2166 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02002167 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
Alexandru Gagniuce14c9b42021-04-01 13:25:31 -05002168 fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02002169 (image_type == IH_TYPE_KERNEL &&
2170 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasutcaee41f2014-12-16 14:07:22 +01002171
Andreas Bießmannb4cc60d2016-08-14 20:31:24 +02002172 os_ok = image_type == IH_TYPE_FLATDT ||
2173 image_type == IH_TYPE_FPGA ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01002174 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02002175 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Alexandru Gagniuce14c9b42021-04-01 13:25:31 -05002176 fit_image_check_os(fit, noffset, IH_OS_TEE) ||
Cristian Ciocaltea217f9642019-12-24 18:05:38 +02002177 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao03b0d1e2020-03-18 07:32:07 -07002178 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
Maxim Moskaletse2b258c2024-06-21 14:42:10 +03002179 fit_image_check_os(fit, noffset, IH_OS_VXWORKS) ||
2180 fit_image_check_os(fit, noffset, IH_OS_ELF);
Karl Apsite1b21c282015-05-21 09:52:48 -04002181
2182 /*
2183 * If either of the checks fail, we should report an error, but
2184 * if the image type is coming from the "loadables" field, we
2185 * don't care what it is
2186 */
2187 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasutcaee41f2014-12-16 14:07:22 +01002188 fit_image_get_os(fit, noffset, &os);
2189 printf("No %s %s %s Image\n",
2190 genimg_get_os_name(os),
2191 genimg_get_arch_name(arch),
Simon Glass384d86d2013-05-16 13:53:21 +00002192 genimg_get_type_name(image_type));
2193 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2194 return -EIO;
2195 }
2196
2197 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2198
2199 /* get image data address and length */
Julius Werner97b09cd2019-07-24 19:37:55 -07002200 if (fit_image_get_data_and_size(fit, noffset,
2201 (const void **)&buf, &size)) {
Simon Glass384d86d2013-05-16 13:53:21 +00002202 printf("Could not find %s subimage data!\n", prop_name);
2203 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass9d0da5b2013-06-16 07:46:49 -07002204 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00002205 }
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002206
Philippe Reynes3d964702019-12-18 18:25:42 +01002207 /* Decrypt data before uncompress/move */
Sebastian Reichelbfe8ddf2021-01-04 20:48:03 +01002208 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
Philippe Reynes3d964702019-12-18 18:25:42 +01002209 puts(" Decrypting Data ... ");
2210 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2211 puts("Error\n");
2212 return -EACCES;
2213 }
2214 puts("OK\n");
2215 }
Philippe Reynes3d964702019-12-18 18:25:42 +01002216
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002217 /* perform any post-processing on the image data */
Simon Glassdb503fb2021-09-25 19:43:14 -06002218 if (!tools_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
Lokesh Vutlab36dd3e2021-06-11 11:45:05 +03002219 board_fit_image_post_process(fit, noffset, &buf, &size);
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002220
Simon Glass384d86d2013-05-16 13:53:21 +00002221 len = (ulong)size;
2222
Simon Glass384d86d2013-05-16 13:53:21 +00002223 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2224
Julius Werner97b09cd2019-07-24 19:37:55 -07002225 data = map_to_sysmem(buf);
2226 load = data;
Simon Glass384d86d2013-05-16 13:53:21 +00002227 if (load_op == FIT_LOAD_IGNORED) {
2228 /* Don't load */
2229 } else if (fit_image_get_load(fit, noffset, &load)) {
2230 if (load_op == FIT_LOAD_REQUIRED) {
2231 printf("Can't get %s subimage load address!\n",
2232 prop_name);
2233 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2234 return -EBADF;
2235 }
Simon Glass05a9ad72014-08-22 14:26:43 -06002236 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass384d86d2013-05-16 13:53:21 +00002237 ulong image_start, image_end;
Simon Glass384d86d2013-05-16 13:53:21 +00002238
2239 /*
2240 * move image data to the load address,
2241 * make sure we don't overwrite initial image
2242 */
2243 image_start = addr;
2244 image_end = addr + fit_get_size(fit);
2245
2246 load_end = load + len;
2247 if (image_type != IH_TYPE_KERNEL &&
2248 load < image_end && load_end > image_start) {
2249 printf("Error: %s overwritten\n", prop_name);
2250 return -EXDEV;
2251 }
2252
2253 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2254 prop_name, data, load);
Julius Werner97b09cd2019-07-24 19:37:55 -07002255 } else {
2256 load = data; /* No load address specified */
2257 }
2258
2259 comp = IH_COMP_NONE;
2260 loadbuf = buf;
2261 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Werner88040022019-08-02 15:52:28 -07002262 if (!fit_image_get_comp(fit, noffset, &comp) &&
2263 comp != IH_COMP_NONE &&
2264 !(image_type == IH_TYPE_KERNEL ||
2265 image_type == IH_TYPE_KERNEL_NOLOAD ||
2266 image_type == IH_TYPE_RAMDISK)) {
Julius Werner97b09cd2019-07-24 19:37:55 -07002267 ulong max_decomp_len = len * 20;
2268 if (load == data) {
2269 loadbuf = malloc(max_decomp_len);
2270 load = map_to_sysmem(loadbuf);
2271 } else {
2272 loadbuf = map_sysmem(load, max_decomp_len);
2273 }
2274 if (image_decomp(comp, load, data, image_type,
2275 loadbuf, buf, len, max_decomp_len, &load_end)) {
2276 printf("Error decompressing %s\n", prop_name);
Simon Glass384d86d2013-05-16 13:53:21 +00002277
Julius Werner97b09cd2019-07-24 19:37:55 -07002278 return -ENOEXEC;
2279 }
2280 len = load_end - load;
2281 } else if (load != data) {
2282 loadbuf = map_sysmem(load, len);
2283 memcpy(loadbuf, buf, len);
Simon Glass384d86d2013-05-16 13:53:21 +00002284 }
Julius Werner97b09cd2019-07-24 19:37:55 -07002285
Julius Werner88040022019-08-02 15:52:28 -07002286 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2287 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2288 " please fix your .its file!\n");
2289
Julius Werner97b09cd2019-07-24 19:37:55 -07002290 /* verify that image data is a proper FDT blob */
2291 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2292 puts("Subimage data is not a FDT");
2293 return -ENOEXEC;
2294 }
2295
Simon Glass384d86d2013-05-16 13:53:21 +00002296 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2297
Simon Glass9b5bfba2024-08-07 16:47:33 -06002298 upl_add_image(fit, noffset, load, len);
2299
Julius Werner97b09cd2019-07-24 19:37:55 -07002300 *datap = load;
Simon Glass384d86d2013-05-16 13:53:21 +00002301 *lenp = len;
2302 if (fit_unamep)
2303 *fit_unamep = (char *)fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07002304 if (fit_uname_configp)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002305 *fit_uname_configp = (char *)(fit_uname_config ? :
2306 fit_base_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00002307
2308 return noffset;
2309}
Simon Glass0129b522014-10-19 21:11:24 -06002310
Simon Glassdf00afa2022-09-06 20:26:50 -06002311int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,
2312 ulong *setup_start, ulong *setup_len)
Simon Glass0129b522014-10-19 21:11:24 -06002313{
2314 int noffset;
2315 ulong addr;
2316 ulong len;
2317 int ret;
2318
2319 addr = map_to_sysmem(images->fit_hdr_os);
2320 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2321 if (noffset < 0)
2322 return noffset;
2323
2324 ret = fit_image_load(images, addr, NULL, NULL, arch,
2325 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2326 FIT_LOAD_REQUIRED, setup_start, &len);
2327
2328 return ret;
2329}
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002330
2331#ifndef USE_HOSTCC
Simon Glassdf00afa2022-09-06 20:26:50 -06002332int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,
2333 const char **fit_unamep, const char **fit_uname_configp,
2334 int arch, ulong *datap, ulong *lenp)
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002335{
2336 int fdt_noffset, cfg_noffset, count;
2337 const void *fit;
2338 const char *fit_uname = NULL;
2339 const char *fit_uname_config = NULL;
2340 char *fit_uname_config_copy = NULL;
2341 char *next_config = NULL;
2342 ulong load, len;
2343#ifdef CONFIG_OF_LIBFDT_OVERLAY
2344 ulong image_start, image_end;
Marek Vasutf7d24612021-06-11 04:09:56 +02002345 ulong ovload, ovlen, ovcopylen;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002346 const char *uconfig;
2347 const char *uname;
Marek Vasutf7d24612021-06-11 04:09:56 +02002348 void *base, *ov, *ovcopy = NULL;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002349 int i, err, noffset, ov_noffset;
2350#endif
2351
2352 fit_uname = fit_unamep ? *fit_unamep : NULL;
2353
2354 if (fit_uname_configp && *fit_uname_configp) {
2355 fit_uname_config_copy = strdup(*fit_uname_configp);
2356 if (!fit_uname_config_copy)
2357 return -ENOMEM;
2358
2359 next_config = strchr(fit_uname_config_copy, '#');
2360 if (next_config)
2361 *next_config++ = '\0';
2362 if (next_config - 1 > fit_uname_config_copy)
2363 fit_uname_config = fit_uname_config_copy;
2364 }
2365
2366 fdt_noffset = fit_image_load(images,
2367 addr, &fit_uname, &fit_uname_config,
2368 arch, IH_TYPE_FLATDT,
2369 BOOTSTAGE_ID_FIT_FDT_START,
2370 FIT_LOAD_OPTIONAL, &load, &len);
2371
2372 if (fdt_noffset < 0)
2373 goto out;
2374
2375 debug("fit_uname=%s, fit_uname_config=%s\n",
2376 fit_uname ? fit_uname : "<NULL>",
2377 fit_uname_config ? fit_uname_config : "<NULL>");
2378
2379 fit = map_sysmem(addr, 0);
2380
2381 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2382
2383 /* single blob, or error just return as well */
2384 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2385 if (count <= 1 && !next_config)
2386 goto out;
2387
2388 /* we need to apply overlays */
2389
2390#ifdef CONFIG_OF_LIBFDT_OVERLAY
2391 image_start = addr;
2392 image_end = addr + fit_get_size(fit);
2393 /* verify that relocation took place by load address not being in fit */
2394 if (load >= image_start && load < image_end) {
2395 /* check is simplified; fit load checks for overlaps */
2396 printf("Overlayed FDT requires relocation\n");
2397 fdt_noffset = -EBADF;
2398 goto out;
2399 }
2400
2401 base = map_sysmem(load, len);
2402
2403 /* apply extra configs in FIT first, followed by args */
2404 for (i = 1; ; i++) {
2405 if (i < count) {
2406 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2407 FIT_FDT_PROP, i);
2408 uname = fit_get_name(fit, noffset, NULL);
2409 uconfig = NULL;
2410 } else {
2411 if (!next_config)
2412 break;
2413 uconfig = next_config;
2414 next_config = strchr(next_config, '#');
2415 if (next_config)
2416 *next_config++ = '\0';
2417 uname = NULL;
Peter Ujfalusiaf15dc42019-03-06 15:52:27 +02002418
2419 /*
2420 * fit_image_load() would load the first FDT from the
2421 * extra config only when uconfig is specified.
2422 * Check if the extra config contains multiple FDTs and
2423 * if so, load them.
2424 */
2425 cfg_noffset = fit_conf_get_node(fit, uconfig);
2426
2427 i = 0;
2428 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2429 FIT_FDT_PROP);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002430 }
2431
2432 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2433
2434 ov_noffset = fit_image_load(images,
2435 addr, &uname, &uconfig,
2436 arch, IH_TYPE_FLATDT,
2437 BOOTSTAGE_ID_FIT_FDT_START,
Marek Vasutf7d24612021-06-11 04:09:56 +02002438 FIT_LOAD_IGNORED, &ovload, &ovlen);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002439 if (ov_noffset < 0) {
2440 printf("load of %s failed\n", uname);
2441 continue;
2442 }
2443 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2444 uname, ovload, ovlen);
2445 ov = map_sysmem(ovload, ovlen);
2446
Marek Vasutf7d24612021-06-11 04:09:56 +02002447 ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K);
2448 ovcopy = malloc(ovcopylen);
2449 if (!ovcopy) {
2450 printf("failed to duplicate DTO before application\n");
2451 fdt_noffset = -ENOMEM;
2452 goto out;
2453 }
2454
2455 err = fdt_open_into(ov, ovcopy, ovcopylen);
2456 if (err < 0) {
2457 printf("failed on fdt_open_into for DTO\n");
2458 fdt_noffset = err;
2459 goto out;
2460 }
2461
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002462 base = map_sysmem(load, len + ovlen);
2463 err = fdt_open_into(base, base, len + ovlen);
2464 if (err < 0) {
2465 printf("failed on fdt_open_into\n");
2466 fdt_noffset = err;
2467 goto out;
2468 }
Marek Vasutf7d24612021-06-11 04:09:56 +02002469
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002470 /* the verbose method prints out messages on error */
Marek Vasutf7d24612021-06-11 04:09:56 +02002471 err = fdt_overlay_apply_verbose(base, ovcopy);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002472 if (err < 0) {
2473 fdt_noffset = err;
2474 goto out;
2475 }
2476 fdt_pack(base);
2477 len = fdt_totalsize(base);
2478 }
2479#else
2480 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2481 fdt_noffset = -EBADF;
2482#endif
2483
2484out:
2485 if (datap)
2486 *datap = load;
2487 if (lenp)
2488 *lenp = len;
2489 if (fit_unamep)
2490 *fit_unamep = fit_uname;
2491 if (fit_uname_configp)
2492 *fit_uname_configp = fit_uname_config;
2493
Marek Vasutf7d24612021-06-11 04:09:56 +02002494#ifdef CONFIG_OF_LIBFDT_OVERLAY
Heinrich Schuchardtf5686462022-04-11 20:08:03 +02002495 free(ovcopy);
Marek Vasutf7d24612021-06-11 04:09:56 +02002496#endif
Heinrich Schuchardtf5686462022-04-11 20:08:03 +02002497 free(fit_uname_config_copy);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002498 return fdt_noffset;
2499}
2500#endif