blob: c3dc814115ff86d8d99f0b0c3a4c55c9cfeceff7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass53fbb7e2013-05-07 06:11:53 +00002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass53fbb7e2013-05-07 06:11:53 +00009 */
10
11#ifdef USE_HOSTCC
12#include "mkimage.h"
Simon Glass53fbb7e2013-05-07 06:11:53 +000013#include <time.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060014#include <linux/libfdt.h>
Simon Glass3db71102019-11-14 12:57:16 -070015#include <u-boot/crc.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000016#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050017#include <linux/compiler.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000018#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000019#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050021#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000022#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030023#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060024#include <asm/global_data.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000025DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000026#endif /* !USE_HOSTCC*/
27
Julius Wernerb1307f82019-07-24 19:37:55 -070028#include <bootm.h>
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050029#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000030#include <bootstage.h>
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +010031#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000032#include <u-boot/crc.h>
33#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020034#include <u-boot/sha1.h>
35#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120036#include <u-boot/sha512.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000037
38/*****************************************************************************/
39/* New uImage format routines */
40/*****************************************************************************/
41#ifndef USE_HOSTCC
42static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
43 ulong *addr, const char **name)
44{
45 const char *sep;
46
47 *addr = addr_curr;
48 *name = NULL;
49
50 sep = strchr(spec, sepc);
51 if (sep) {
52 if (sep - spec > 0)
53 *addr = simple_strtoul(spec, NULL, 16);
54
55 *name = sep + 1;
56 return 1;
57 }
58
59 return 0;
60}
61
62/**
63 * fit_parse_conf - parse FIT configuration spec
64 * @spec: input string, containing configuration spec
65 * @add_curr: current image address (to be used as a possible default)
66 * @addr: pointer to a ulong variable, will hold FIT image address of a given
67 * configuration
68 * @conf_name double pointer to a char, will hold pointer to a configuration
69 * unit name
70 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090071 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000072 * where <addr> is a FIT image address that contains configuration
73 * with a <conf> unit name.
74 *
75 * Address part is optional, and if omitted default add_curr will
76 * be used instead.
77 *
78 * returns:
79 * 1 if spec is a valid configuration string,
80 * addr and conf_name are set accordingly
81 * 0 otherwise
82 */
83int fit_parse_conf(const char *spec, ulong addr_curr,
84 ulong *addr, const char **conf_name)
85{
86 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
87}
88
89/**
90 * fit_parse_subimage - parse FIT subimage spec
91 * @spec: input string, containing subimage spec
92 * @add_curr: current image address (to be used as a possible default)
93 * @addr: pointer to a ulong variable, will hold FIT image address of a given
94 * subimage
95 * @image_name: double pointer to a char, will hold pointer to a subimage name
96 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090097 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000098 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
99 * subimage with a <subimg> unit name.
100 *
101 * Address part is optional, and if omitted default add_curr will
102 * be used instead.
103 *
104 * returns:
105 * 1 if spec is a valid subimage string,
106 * addr and image_name are set accordingly
107 * 0 otherwise
108 */
109int fit_parse_subimage(const char *spec, ulong addr_curr,
110 ulong *addr, const char **image_name)
111{
112 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
113}
114#endif /* !USE_HOSTCC */
115
Joel Stanley93af80f2020-12-08 14:42:14 +1030116#ifdef USE_HOSTCC
117/* Host tools use these implementations for Cipher and Signature support */
118static void *host_blob;
119
120void image_set_host_blob(void *blob)
121{
122 host_blob = blob;
123}
124
125void *image_get_host_blob(void)
126{
127 return host_blob;
128}
129#endif /* USE_HOSTCC */
130
Simon Glass53fbb7e2013-05-07 06:11:53 +0000131static void fit_get_debug(const void *fit, int noffset,
132 char *prop_name, int err)
133{
134 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
135 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
136 fdt_strerror(err));
137}
138
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200139/**
140 * fit_get_subimage_count - get component (sub-image) count
141 * @fit: pointer to the FIT format image header
142 * @images_noffset: offset of images node
143 *
144 * returns:
145 * number of image components
146 */
147int fit_get_subimage_count(const void *fit, int images_noffset)
148{
149 int noffset;
150 int ndepth;
151 int count = 0;
152
153 /* Process its subnodes, print out component images details */
154 for (ndepth = 0, count = 0,
155 noffset = fdt_next_node(fit, images_noffset, &ndepth);
156 (noffset >= 0) && (ndepth > 0);
157 noffset = fdt_next_node(fit, noffset, &ndepth)) {
158 if (ndepth == 1) {
159 count++;
160 }
161 }
162
163 return count;
164}
165
Ravik Hasija7a018822021-01-27 14:01:48 -0800166#if CONFIG_IS_ENABLED(FIT_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000167/**
Tom Rini16c4b162018-05-08 14:34:05 -0400168 * fit_image_print_data() - prints out the hash node details
169 * @fit: pointer to the FIT format image header
170 * @noffset: offset of the hash node
171 * @p: pointer to prefix string
172 * @type: Type of information to print ("hash" or "sign")
173 *
174 * fit_image_print_data() lists properties for the processed hash node
175 *
176 * This function avoid using puts() since it prints a newline on the host
177 * but does not in U-Boot.
178 *
179 * returns:
180 * no returned results
181 */
182static void fit_image_print_data(const void *fit, int noffset, const char *p,
183 const char *type)
184{
185 const char *keyname;
186 uint8_t *value;
187 int value_len;
188 char *algo;
Philippe Reynes20031562018-11-14 13:51:00 +0100189 const char *padding;
Simon Glass72188f52020-03-18 11:44:06 -0600190 bool required;
Tom Rini16c4b162018-05-08 14:34:05 -0400191 int ret, i;
192
193 debug("%s %s node: '%s'\n", p, type,
194 fit_get_name(fit, noffset, NULL));
195 printf("%s %s algo: ", p, type);
196 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
197 printf("invalid/unsupported\n");
198 return;
199 }
200 printf("%s", algo);
Simon Glass72188f52020-03-18 11:44:06 -0600201 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
202 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
Tom Rini16c4b162018-05-08 14:34:05 -0400203 if (keyname)
204 printf(":%s", keyname);
205 if (required)
206 printf(" (required)");
207 printf("\n");
208
Philippe Reynes20031562018-11-14 13:51:00 +0100209 padding = fdt_getprop(fit, noffset, "padding", NULL);
210 if (padding)
211 printf("%s %s padding: %s\n", p, type, padding);
212
Tom Rini16c4b162018-05-08 14:34:05 -0400213 ret = fit_image_hash_get_value(fit, noffset, &value,
214 &value_len);
215 printf("%s %s value: ", p, type);
216 if (ret) {
217 printf("unavailable\n");
218 } else {
219 for (i = 0; i < value_len; i++)
220 printf("%02x", value[i]);
221 printf("\n");
222 }
223
224 debug("%s %s len: %d\n", p, type, value_len);
225
226 /* Signatures have a time stamp */
227 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
228 time_t timestamp;
229
230 printf("%s Timestamp: ", p);
231 if (fit_get_timestamp(fit, noffset, &timestamp))
232 printf("unavailable\n");
233 else
234 genimg_print_time(timestamp);
235 }
236}
237
238/**
239 * fit_image_print_verification_data() - prints out the hash/signature details
240 * @fit: pointer to the FIT format image header
241 * @noffset: offset of the hash or signature node
242 * @p: pointer to prefix string
243 *
244 * This lists properties for the processed hash node
245 *
246 * returns:
247 * no returned results
248 */
249static void fit_image_print_verification_data(const void *fit, int noffset,
250 const char *p)
251{
252 const char *name;
253
254 /*
255 * Check subnode name, must be equal to "hash" or "signature".
256 * Multiple hash/signature nodes require unique unit node
257 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
258 */
259 name = fit_get_name(fit, noffset, NULL);
260 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
261 fit_image_print_data(fit, noffset, p, "Hash");
262 } else if (!strncmp(name, FIT_SIG_NODENAME,
263 strlen(FIT_SIG_NODENAME))) {
264 fit_image_print_data(fit, noffset, p, "Sign");
265 }
266}
267
268/**
269 * fit_conf_print - prints out the FIT configuration details
270 * @fit: pointer to the FIT format image header
271 * @noffset: offset of the configuration node
272 * @p: pointer to prefix string
273 *
274 * fit_conf_print() lists all mandatory properties for the processed
275 * configuration node.
276 *
277 * returns:
278 * no returned results
279 */
280static void fit_conf_print(const void *fit, int noffset, const char *p)
281{
282 char *desc;
283 const char *uname;
284 int ret;
285 int fdt_index, loadables_index;
286 int ndepth;
287
288 /* Mandatory properties */
289 ret = fit_get_desc(fit, noffset, &desc);
290 printf("%s Description: ", p);
291 if (ret)
292 printf("unavailable\n");
293 else
294 printf("%s\n", desc);
295
296 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
297 printf("%s Kernel: ", p);
298 if (!uname)
299 printf("unavailable\n");
300 else
301 printf("%s\n", uname);
302
303 /* Optional properties */
304 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
305 if (uname)
306 printf("%s Init Ramdisk: %s\n", p, uname);
307
308 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
309 if (uname)
310 printf("%s Firmware: %s\n", p, uname);
311
312 for (fdt_index = 0;
313 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
314 fdt_index, NULL), uname;
315 fdt_index++) {
316 if (fdt_index == 0)
317 printf("%s FDT: ", p);
318 else
319 printf("%s ", p);
320 printf("%s\n", uname);
321 }
322
323 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
324 if (uname)
325 printf("%s FPGA: %s\n", p, uname);
326
327 /* Print out all of the specified loadables */
328 for (loadables_index = 0;
329 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
330 loadables_index, NULL), uname;
331 loadables_index++) {
332 if (loadables_index == 0) {
333 printf("%s Loadables: ", p);
334 } else {
335 printf("%s ", p);
336 }
337 printf("%s\n", uname);
338 }
339
340 /* Process all hash subnodes of the component configuration node */
341 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
342 (noffset >= 0) && (ndepth > 0);
343 noffset = fdt_next_node(fit, noffset, &ndepth)) {
344 if (ndepth == 1) {
345 /* Direct child node of the component configuration node */
346 fit_image_print_verification_data(fit, noffset, p);
347 }
348 }
349}
350
351/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000352 * fit_print_contents - prints out the contents of the FIT format image
353 * @fit: pointer to the FIT format image header
354 * @p: pointer to prefix string
355 *
356 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500357 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000358 * the details of each component image.
359 *
360 * returns:
361 * no returned results
362 */
363void fit_print_contents(const void *fit)
364{
365 char *desc;
366 char *uname;
367 int images_noffset;
368 int confs_noffset;
369 int noffset;
370 int ndepth;
371 int count = 0;
372 int ret;
373 const char *p;
374 time_t timestamp;
375
Simon Glass1fe7d932013-05-08 08:05:58 +0000376 /* Indent string is defined in header image.h */
377 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000378
379 /* Root node properties */
380 ret = fit_get_desc(fit, 0, &desc);
381 printf("%sFIT description: ", p);
382 if (ret)
383 printf("unavailable\n");
384 else
385 printf("%s\n", desc);
386
387 if (IMAGE_ENABLE_TIMESTAMP) {
388 ret = fit_get_timestamp(fit, 0, &timestamp);
389 printf("%sCreated: ", p);
390 if (ret)
391 printf("unavailable\n");
392 else
393 genimg_print_time(timestamp);
394 }
395
396 /* Find images parent node offset */
397 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
398 if (images_noffset < 0) {
399 printf("Can't find images parent node '%s' (%s)\n",
400 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
401 return;
402 }
403
404 /* Process its subnodes, print out component images details */
405 for (ndepth = 0, count = 0,
406 noffset = fdt_next_node(fit, images_noffset, &ndepth);
407 (noffset >= 0) && (ndepth > 0);
408 noffset = fdt_next_node(fit, noffset, &ndepth)) {
409 if (ndepth == 1) {
410 /*
411 * Direct child node of the images parent node,
412 * i.e. component image node.
413 */
414 printf("%s Image %u (%s)\n", p, count++,
415 fit_get_name(fit, noffset, NULL));
416
417 fit_image_print(fit, noffset, p);
418 }
419 }
420
421 /* Find configurations parent node offset */
422 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
423 if (confs_noffset < 0) {
424 debug("Can't get configurations parent node '%s' (%s)\n",
425 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
426 return;
427 }
428
429 /* get default configuration unit name from default property */
430 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
431 if (uname)
432 printf("%s Default Configuration: '%s'\n", p, uname);
433
434 /* Process its subnodes, print out configurations details */
435 for (ndepth = 0, count = 0,
436 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
437 (noffset >= 0) && (ndepth > 0);
438 noffset = fdt_next_node(fit, noffset, &ndepth)) {
439 if (ndepth == 1) {
440 /*
441 * Direct child node of the configurations parent node,
442 * i.e. configuration node.
443 */
444 printf("%s Configuration %u (%s)\n", p, count++,
445 fit_get_name(fit, noffset, NULL));
446
447 fit_conf_print(fit, noffset, p);
448 }
449 }
450}
451
452/**
453 * fit_image_print - prints out the FIT component image details
454 * @fit: pointer to the FIT format image header
455 * @image_noffset: offset of the component image node
456 * @p: pointer to prefix string
457 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500458 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000459 * image. If present, hash nodes are printed out as well. Load
460 * address for images of type firmware is also printed out. Since the load
461 * address is not mandatory for firmware images, it will be output as
462 * "unavailable" when not present.
463 *
464 * returns:
465 * no returned results
466 */
467void fit_image_print(const void *fit, int image_noffset, const char *p)
468{
469 char *desc;
470 uint8_t type, arch, os, comp;
471 size_t size;
472 ulong load, entry;
473 const void *data;
474 int noffset;
475 int ndepth;
476 int ret;
477
478 /* Mandatory properties */
479 ret = fit_get_desc(fit, image_noffset, &desc);
480 printf("%s Description: ", p);
481 if (ret)
482 printf("unavailable\n");
483 else
484 printf("%s\n", desc);
485
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700486 if (IMAGE_ENABLE_TIMESTAMP) {
487 time_t timestamp;
488
489 ret = fit_get_timestamp(fit, 0, &timestamp);
490 printf("%s Created: ", p);
491 if (ret)
492 printf("unavailable\n");
493 else
494 genimg_print_time(timestamp);
495 }
496
Simon Glass53fbb7e2013-05-07 06:11:53 +0000497 fit_image_get_type(fit, image_noffset, &type);
498 printf("%s Type: %s\n", p, genimg_get_type_name(type));
499
500 fit_image_get_comp(fit, image_noffset, &comp);
501 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
502
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800503 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000504
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100505 if (!host_build()) {
506 printf("%s Data Start: ", p);
507 if (ret) {
508 printf("unavailable\n");
509 } else {
510 void *vdata = (void *)data;
Simon Glassc6ac13b2013-05-16 13:53:26 +0000511
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +0100512 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
513 }
Simon Glassc6ac13b2013-05-16 13:53:26 +0000514 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000515
516 printf("%s Data Size: ", p);
517 if (ret)
518 printf("unavailable\n");
519 else
520 genimg_print_size(size);
521
522 /* Remaining, type dependent properties */
523 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
524 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
525 (type == IH_TYPE_FLATDT)) {
526 fit_image_get_arch(fit, image_noffset, &arch);
527 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
528 }
529
Michal Simek6faf4622018-03-26 16:31:27 +0200530 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
531 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000532 fit_image_get_os(fit, image_noffset, &os);
533 printf("%s OS: %s\n", p, genimg_get_os_name(os));
534 }
535
536 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200537 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
538 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000539 ret = fit_image_get_load(fit, image_noffset, &load);
540 printf("%s Load Address: ", p);
541 if (ret)
542 printf("unavailable\n");
543 else
544 printf("0x%08lx\n", load);
545 }
546
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300547 /* optional load address for FDT */
548 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
549 printf("%s Load Address: 0x%08lx\n", p, load);
550
Simon Glass53fbb7e2013-05-07 06:11:53 +0000551 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
552 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800553 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000554 printf("%s Entry Point: ", p);
555 if (ret)
556 printf("unavailable\n");
557 else
558 printf("0x%08lx\n", entry);
559 }
560
561 /* Process all hash subnodes of the component image node */
562 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
563 (noffset >= 0) && (ndepth > 0);
564 noffset = fdt_next_node(fit, noffset, &ndepth)) {
565 if (ndepth == 1) {
566 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000567 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000568 }
569 }
570}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200571#else
572void fit_print_contents(const void *fit) { }
573void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Ravik Hasija7a018822021-01-27 14:01:48 -0800574#endif /* CONFIG_IS_ENABLED(FIR_PRINT) || CONFIG_IS_ENABLED(SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000575
576/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000577 * fit_get_desc - get node description property
578 * @fit: pointer to the FIT format image header
579 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500580 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000581 *
582 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500583 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000584 *
585 * returns:
586 * 0, on success
587 * -1, on failure
588 */
589int fit_get_desc(const void *fit, int noffset, char **desc)
590{
591 int len;
592
593 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
594 if (*desc == NULL) {
595 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
596 return -1;
597 }
598
599 return 0;
600}
601
602/**
603 * fit_get_timestamp - get node timestamp property
604 * @fit: pointer to the FIT format image header
605 * @noffset: node offset
606 * @timestamp: pointer to the time_t, will hold read timestamp
607 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500608 * fit_get_timestamp() reads timestamp property from given node, if timestamp
609 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000610 * argument.
611 *
612 * returns:
613 * 0, on success
614 * -1, on property read failure
615 * -2, on wrong timestamp size
616 */
617int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
618{
619 int len;
620 const void *data;
621
622 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
623 if (data == NULL) {
624 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
625 return -1;
626 }
627 if (len != sizeof(uint32_t)) {
628 debug("FIT timestamp with incorrect size of (%u)\n", len);
629 return -2;
630 }
631
632 *timestamp = uimage_to_cpu(*((uint32_t *)data));
633 return 0;
634}
635
636/**
637 * fit_image_get_node - get node offset for component image of a given unit name
638 * @fit: pointer to the FIT format image header
639 * @image_uname: component image node unit name
640 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500641 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000642 * node) of a provided unit name. If image is found its node offset is
643 * returned to the caller.
644 *
645 * returns:
646 * image node offset when found (>=0)
647 * negative number on failure (FDT_ERR_* code)
648 */
649int fit_image_get_node(const void *fit, const char *image_uname)
650{
651 int noffset, images_noffset;
652
653 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
654 if (images_noffset < 0) {
655 debug("Can't find images parent node '%s' (%s)\n",
656 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
657 return images_noffset;
658 }
659
660 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
661 if (noffset < 0) {
662 debug("Can't get node offset for image unit name: '%s' (%s)\n",
663 image_uname, fdt_strerror(noffset));
664 }
665
666 return noffset;
667}
668
669/**
670 * fit_image_get_os - get os id for a given component image node
671 * @fit: pointer to the FIT format image header
672 * @noffset: component image node offset
673 * @os: pointer to the uint8_t, will hold os numeric id
674 *
675 * fit_image_get_os() finds os property in a given component image node.
676 * If the property is found, its (string) value is translated to the numeric
677 * id which is returned to the caller.
678 *
679 * returns:
680 * 0, on success
681 * -1, on failure
682 */
683int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
684{
685 int len;
686 const void *data;
687
688 /* Get OS name from property data */
689 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
690 if (data == NULL) {
691 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
692 *os = -1;
693 return -1;
694 }
695
696 /* Translate OS name to id */
697 *os = genimg_get_os_id(data);
698 return 0;
699}
700
701/**
702 * fit_image_get_arch - get arch id for a given component image node
703 * @fit: pointer to the FIT format image header
704 * @noffset: component image node offset
705 * @arch: pointer to the uint8_t, will hold arch numeric id
706 *
707 * fit_image_get_arch() finds arch property in a given component image node.
708 * If the property is found, its (string) value is translated to the numeric
709 * id which is returned to the caller.
710 *
711 * returns:
712 * 0, on success
713 * -1, on failure
714 */
715int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
716{
717 int len;
718 const void *data;
719
720 /* Get architecture name from property data */
721 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
722 if (data == NULL) {
723 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
724 *arch = -1;
725 return -1;
726 }
727
728 /* Translate architecture name to id */
729 *arch = genimg_get_arch_id(data);
730 return 0;
731}
732
733/**
734 * fit_image_get_type - get type id for a given component image node
735 * @fit: pointer to the FIT format image header
736 * @noffset: component image node offset
737 * @type: pointer to the uint8_t, will hold type numeric id
738 *
739 * fit_image_get_type() finds type property in a given component image node.
740 * If the property is found, its (string) value is translated to the numeric
741 * id which is returned to the caller.
742 *
743 * returns:
744 * 0, on success
745 * -1, on failure
746 */
747int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
748{
749 int len;
750 const void *data;
751
752 /* Get image type name from property data */
753 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
754 if (data == NULL) {
755 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
756 *type = -1;
757 return -1;
758 }
759
760 /* Translate image type name to id */
761 *type = genimg_get_type_id(data);
762 return 0;
763}
764
765/**
766 * fit_image_get_comp - get comp id for a given component image node
767 * @fit: pointer to the FIT format image header
768 * @noffset: component image node offset
769 * @comp: pointer to the uint8_t, will hold comp numeric id
770 *
771 * fit_image_get_comp() finds comp property in a given component image node.
772 * If the property is found, its (string) value is translated to the numeric
773 * id which is returned to the caller.
774 *
775 * returns:
776 * 0, on success
777 * -1, on failure
778 */
779int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
780{
781 int len;
782 const void *data;
783
784 /* Get compression name from property data */
785 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
786 if (data == NULL) {
787 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
788 *comp = -1;
789 return -1;
790 }
791
792 /* Translate compression name to id */
793 *comp = genimg_get_comp_id(data);
794 return 0;
795}
796
York Sun60047652016-02-29 15:48:40 -0800797static int fit_image_get_address(const void *fit, int noffset, char *name,
798 ulong *load)
799{
York Sunc1913cb2016-02-29 15:48:41 -0800800 int len, cell_len;
801 const fdt32_t *cell;
802 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800803
York Sunc1913cb2016-02-29 15:48:41 -0800804 cell = fdt_getprop(fit, noffset, name, &len);
805 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800806 fit_get_debug(fit, noffset, name, len);
807 return -1;
808 }
809
York Sunc1913cb2016-02-29 15:48:41 -0800810 cell_len = len >> 2;
811 /* Use load64 to avoid compiling warning for 32-bit target */
812 while (cell_len--) {
813 load64 = (load64 << 32) | uimage_to_cpu(*cell);
814 cell++;
815 }
Michal Simek13d1ca82020-09-03 12:44:51 +0200816
817 if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) {
818 printf("Unsupported %s address size\n", name);
819 return -1;
820 }
821
York Sunc1913cb2016-02-29 15:48:41 -0800822 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800823
824 return 0;
825}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000826/**
827 * fit_image_get_load() - get load addr property for given component image node
828 * @fit: pointer to the FIT format image header
829 * @noffset: component image node offset
830 * @load: pointer to the uint32_t, will hold load address
831 *
832 * fit_image_get_load() finds load address property in a given component
833 * image node. If the property is found, its value is returned to the caller.
834 *
835 * returns:
836 * 0, on success
837 * -1, on failure
838 */
839int fit_image_get_load(const void *fit, int noffset, ulong *load)
840{
York Sun60047652016-02-29 15:48:40 -0800841 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000842}
843
844/**
845 * fit_image_get_entry() - get entry point address property
846 * @fit: pointer to the FIT format image header
847 * @noffset: component image node offset
848 * @entry: pointer to the uint32_t, will hold entry point address
849 *
850 * This gets the entry point address property for a given component image
851 * node.
852 *
853 * fit_image_get_entry() finds entry point address property in a given
854 * component image node. If the property is found, its value is returned
855 * to the caller.
856 *
857 * returns:
858 * 0, on success
859 * -1, on failure
860 */
861int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
862{
York Sun60047652016-02-29 15:48:40 -0800863 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000864}
865
866/**
867 * fit_image_get_data - get data property and its size for a given component image node
868 * @fit: pointer to the FIT format image header
869 * @noffset: component image node offset
870 * @data: double pointer to void, will hold data property's data address
871 * @size: pointer to size_t, will hold data property's data size
872 *
873 * fit_image_get_data() finds data property in a given component image node.
874 * If the property is found its data start address and size are returned to
875 * the caller.
876 *
877 * returns:
878 * 0, on success
879 * -1, on failure
880 */
881int fit_image_get_data(const void *fit, int noffset,
882 const void **data, size_t *size)
883{
884 int len;
885
886 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
887 if (*data == NULL) {
888 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
889 *size = 0;
890 return -1;
891 }
892
893 *size = len;
894 return 0;
895}
896
897/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200898 * Get 'data-offset' property from a given image node.
899 *
900 * @fit: pointer to the FIT image header
901 * @noffset: component image node offset
902 * @data_offset: holds the data-offset property
903 *
904 * returns:
905 * 0, on success
906 * -ENOENT if the property could not be found
907 */
908int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
909{
910 const fdt32_t *val;
911
912 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
913 if (!val)
914 return -ENOENT;
915
916 *data_offset = fdt32_to_cpu(*val);
917
918 return 0;
919}
920
921/**
Peng Fana1be94b2017-12-05 13:20:59 +0800922 * Get 'data-position' property from a given image node.
923 *
924 * @fit: pointer to the FIT image header
925 * @noffset: component image node offset
926 * @data_position: holds the data-position property
927 *
928 * returns:
929 * 0, on success
930 * -ENOENT if the property could not be found
931 */
932int fit_image_get_data_position(const void *fit, int noffset,
933 int *data_position)
934{
935 const fdt32_t *val;
936
937 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
938 if (!val)
939 return -ENOENT;
940
941 *data_position = fdt32_to_cpu(*val);
942
943 return 0;
944}
945
946/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200947 * Get 'data-size' property from a given image node.
948 *
949 * @fit: pointer to the FIT image header
950 * @noffset: component image node offset
951 * @data_size: holds the data-size property
952 *
953 * returns:
954 * 0, on success
955 * -ENOENT if the property could not be found
956 */
957int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
958{
959 const fdt32_t *val;
960
961 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
962 if (!val)
963 return -ENOENT;
964
965 *data_size = fdt32_to_cpu(*val);
966
967 return 0;
968}
969
970/**
Philippe Reynes4df35782019-12-18 18:25:42 +0100971 * Get 'data-size-unciphered' property from a given image node.
972 *
973 * @fit: pointer to the FIT image header
974 * @noffset: component image node offset
975 * @data_size: holds the data-size property
976 *
977 * returns:
978 * 0, on success
979 * -ENOENT if the property could not be found
980 */
981int fit_image_get_data_size_unciphered(const void *fit, int noffset,
982 size_t *data_size)
983{
984 const fdt32_t *val;
985
986 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
987 if (!val)
988 return -ENOENT;
989
990 *data_size = (size_t)fdt32_to_cpu(*val);
991
992 return 0;
993}
994
995/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800996 * fit_image_get_data_and_size - get data and its size including
997 * both embedded and external data
998 * @fit: pointer to the FIT format image header
999 * @noffset: component image node offset
1000 * @data: double pointer to void, will hold data property's data address
1001 * @size: pointer to size_t, will hold data property's data size
1002 *
1003 * fit_image_get_data_and_size() finds data and its size including
1004 * both embedded and external data. If the property is found
1005 * its data start address and size are returned to the caller.
1006 *
1007 * returns:
1008 * 0, on success
1009 * otherwise, on failure
1010 */
1011int fit_image_get_data_and_size(const void *fit, int noffset,
1012 const void **data, size_t *size)
1013{
1014 bool external_data = false;
1015 int offset;
1016 int len;
1017 int ret;
1018
1019 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1020 external_data = true;
1021 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1022 external_data = true;
1023 /*
1024 * For FIT with external data, figure out where
1025 * the external images start. This is the base
1026 * for the data-offset properties in each image.
1027 */
1028 offset += ((fdt_totalsize(fit) + 3) & ~3);
1029 }
1030
1031 if (external_data) {
1032 debug("External Data\n");
1033 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt18378042020-03-11 21:51:08 +01001034 if (!ret) {
1035 *data = fit + offset;
1036 *size = len;
1037 }
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001038 } else {
1039 ret = fit_image_get_data(fit, noffset, data, size);
1040 }
1041
1042 return ret;
1043}
1044
1045/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001046 * fit_image_hash_get_algo - get hash algorithm name
1047 * @fit: pointer to the FIT format image header
1048 * @noffset: hash node offset
1049 * @algo: double pointer to char, will hold pointer to the algorithm name
1050 *
1051 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1052 * If the property is found its data start address is returned to the caller.
1053 *
1054 * returns:
1055 * 0, on success
1056 * -1, on failure
1057 */
1058int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1059{
1060 int len;
1061
1062 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1063 if (*algo == NULL) {
1064 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1065 return -1;
1066 }
1067
1068 return 0;
1069}
1070
1071/**
1072 * fit_image_hash_get_value - get hash value and length
1073 * @fit: pointer to the FIT format image header
1074 * @noffset: hash node offset
1075 * @value: double pointer to uint8_t, will hold address of a hash value data
1076 * @value_len: pointer to an int, will hold hash data length
1077 *
1078 * fit_image_hash_get_value() finds hash value property in a given hash node.
1079 * If the property is found its data start address and size are returned to
1080 * the caller.
1081 *
1082 * returns:
1083 * 0, on success
1084 * -1, on failure
1085 */
1086int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1087 int *value_len)
1088{
1089 int len;
1090
1091 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1092 if (*value == NULL) {
1093 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1094 *value_len = 0;
1095 return -1;
1096 }
1097
1098 *value_len = len;
1099 return 0;
1100}
1101
Simon Glass53fbb7e2013-05-07 06:11:53 +00001102/**
1103 * fit_image_hash_get_ignore - get hash ignore flag
1104 * @fit: pointer to the FIT format image header
1105 * @noffset: hash node offset
1106 * @ignore: pointer to an int, will hold hash ignore flag
1107 *
1108 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1109 * If the property is found and non-zero, the hash algorithm is not verified by
1110 * u-boot automatically.
1111 *
1112 * returns:
1113 * 0, on ignore not found
1114 * value, on ignore found
1115 */
Simon Glassab9efc62013-05-07 06:11:58 +00001116static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001117{
1118 int len;
1119 int *value;
1120
1121 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1122 if (value == NULL || len != sizeof(int))
1123 *ignore = 0;
1124 else
1125 *ignore = *value;
1126
1127 return 0;
1128}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001129
Philippe Reynes7298e422019-12-18 18:25:41 +01001130/**
1131 * fit_image_cipher_get_algo - get cipher algorithm name
1132 * @fit: pointer to the FIT format image header
1133 * @noffset: cipher node offset
1134 * @algo: double pointer to char, will hold pointer to the algorithm name
1135 *
1136 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1137 * cipher node. If the property is found its data start address is returned
1138 * to the caller.
1139 *
1140 * returns:
1141 * 0, on success
1142 * -1, on failure
1143 */
1144int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1145{
1146 int len;
1147
1148 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1149 if (!*algo) {
1150 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1151 return -1;
1152 }
1153
1154 return 0;
1155}
1156
Simon Glass7a80de42016-02-24 09:14:42 -07001157ulong fit_get_end(const void *fit)
1158{
1159 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1160}
1161
Simon Glass53fbb7e2013-05-07 06:11:53 +00001162/**
1163 * fit_set_timestamp - set node timestamp property
1164 * @fit: pointer to the FIT format image header
1165 * @noffset: node offset
1166 * @timestamp: timestamp value to be set
1167 *
1168 * fit_set_timestamp() attempts to set timestamp property in the requested
1169 * node and returns operation status to the caller.
1170 *
1171 * returns:
1172 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001173 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001174 */
1175int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1176{
1177 uint32_t t;
1178 int ret;
1179
1180 t = cpu_to_uimage(timestamp);
1181 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1182 sizeof(uint32_t));
1183 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001184 debug("Can't set '%s' property for '%s' node (%s)\n",
1185 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1186 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001187 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001188 }
1189
1190 return 0;
1191}
1192
1193/**
1194 * calculate_hash - calculate and return hash for provided input data
1195 * @data: pointer to the input data
1196 * @data_len: data length
1197 * @algo: requested hash algorithm
1198 * @value: pointer to the char, will hold hash value data (caller must
1199 * allocate enough free space)
1200 * value_len: length of the calculated hash
1201 *
1202 * calculate_hash() computes input data hash according to the requested
1203 * algorithm.
1204 * Resulting hash value is placed in caller provided 'value' buffer, length
1205 * of the calculated hash is returned via value_len pointer argument.
1206 *
1207 * returns:
1208 * 0, on success
1209 * -1, when algo is unsupported
1210 */
Simon Glass604f23d2013-05-07 06:11:54 +00001211int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001212 uint8_t *value, int *value_len)
1213{
Simon Glass87ebee32013-05-08 08:05:59 +00001214 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001215 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1216 CHUNKSZ_CRC32);
1217 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1218 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001219 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001220 sha1_csum_wd((unsigned char *)data, data_len,
1221 (unsigned char *)value, CHUNKSZ_SHA1);
1222 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001223 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1224 sha256_csum_wd((unsigned char *)data, data_len,
1225 (unsigned char *)value, CHUNKSZ_SHA256);
1226 *value_len = SHA256_SUM_LEN;
Reuben Dowled16b38f2020-04-16 17:36:52 +12001227 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1228 sha384_csum_wd((unsigned char *)data, data_len,
1229 (unsigned char *)value, CHUNKSZ_SHA384);
1230 *value_len = SHA384_SUM_LEN;
1231 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1232 sha512_csum_wd((unsigned char *)data, data_len,
1233 (unsigned char *)value, CHUNKSZ_SHA512);
1234 *value_len = SHA512_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001235 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001236 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1237 *value_len = 16;
1238 } else {
1239 debug("Unsupported hash alogrithm\n");
1240 return -1;
1241 }
1242 return 0;
1243}
1244
Simon Glassab9efc62013-05-07 06:11:58 +00001245static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1246 size_t size, char **err_msgp)
1247{
1248 uint8_t value[FIT_MAX_HASH_LEN];
1249 int value_len;
1250 char *algo;
1251 uint8_t *fit_value;
1252 int fit_value_len;
1253 int ignore;
1254
1255 *err_msgp = NULL;
1256
1257 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001258 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001259 return -1;
1260 }
1261 printf("%s", algo);
1262
1263 if (IMAGE_ENABLE_IGNORE) {
1264 fit_image_hash_get_ignore(fit, noffset, &ignore);
1265 if (ignore) {
1266 printf("-skipped ");
1267 return 0;
1268 }
1269 }
1270
1271 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1272 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001273 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001274 return -1;
1275 }
1276
1277 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001278 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001279 return -1;
1280 }
1281
1282 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001283 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001284 return -1;
1285 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001286 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001287 return -1;
1288 }
1289
1290 return 0;
1291}
1292
Jun Nie5c643db2018-02-27 16:55:58 +08001293int fit_image_verify_with_data(const void *fit, int image_noffset,
1294 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001295{
Simon Glass56518e72013-06-13 15:10:01 -07001296 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001297 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001298 int verify_all = 1;
1299 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001300
Simon Glass56518e72013-06-13 15:10:01 -07001301 /* Verify all required signatures */
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001302 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glass56518e72013-06-13 15:10:01 -07001303 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1304 gd_fdt_blob(), &verify_all)) {
1305 err_msg = "Unable to verify required signature";
1306 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001307 }
1308
1309 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001310 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001311 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001312
Simon Glassab9efc62013-05-07 06:11:58 +00001313 /*
1314 * Check subnode name, must be equal to "hash".
1315 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001316 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001317 */
1318 if (!strncmp(name, FIT_HASH_NODENAME,
1319 strlen(FIT_HASH_NODENAME))) {
1320 if (fit_image_check_hash(fit, noffset, data, size,
1321 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001322 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001323 puts("+ ");
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001324 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glass56518e72013-06-13 15:10:01 -07001325 !strncmp(name, FIT_SIG_NODENAME,
1326 strlen(FIT_SIG_NODENAME))) {
1327 ret = fit_image_check_sig(fit, noffset, data,
1328 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001329
1330 /*
1331 * Show an indication on failure, but do not return
1332 * an error. Only keys marked 'required' can cause
1333 * an image validation failure. See the call to
1334 * fit_image_verify_required_sigs() above.
1335 */
1336 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001337 puts("- ");
1338 else
1339 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001340 }
1341 }
1342
1343 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001344 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001345 goto error;
1346 }
1347
1348 return 1;
1349
1350error:
Simon Glasse754da22013-05-07 06:11:59 +00001351 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001352 err_msg, fit_get_name(fit, noffset, NULL),
1353 fit_get_name(fit, image_noffset, NULL));
1354 return 0;
1355}
1356
1357/**
Jun Nie5c643db2018-02-27 16:55:58 +08001358 * fit_image_verify - verify data integrity
1359 * @fit: pointer to the FIT format image header
1360 * @image_noffset: component image node offset
1361 *
1362 * fit_image_verify() goes over component image hash nodes,
1363 * re-calculates each data hash and compares with the value stored in hash
1364 * node.
1365 *
1366 * returns:
1367 * 1, if all hashes are valid
1368 * 0, otherwise (or on error)
1369 */
1370int fit_image_verify(const void *fit, int image_noffset)
1371{
Simon Glass79af75f2021-02-15 17:08:06 -07001372 const char *name = fit_get_name(fit, image_noffset, NULL);
Jun Nie5c643db2018-02-27 16:55:58 +08001373 const void *data;
1374 size_t size;
Jun Nie5c643db2018-02-27 16:55:58 +08001375 char *err_msg = "";
1376
Simon Glass79af75f2021-02-15 17:08:06 -07001377 if (strchr(name, '@')) {
1378 /*
1379 * We don't support this since libfdt considers names with the
1380 * name root but different @ suffix to be equal
1381 */
1382 err_msg = "Node name contains @";
1383 goto err;
1384 }
Jun Nie5c643db2018-02-27 16:55:58 +08001385 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001386 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001387 err_msg = "Can't get image data/size";
Simon Glass79af75f2021-02-15 17:08:06 -07001388 goto err;
Jun Nie5c643db2018-02-27 16:55:58 +08001389 }
1390
1391 return fit_image_verify_with_data(fit, image_noffset, data, size);
Simon Glass79af75f2021-02-15 17:08:06 -07001392
1393err:
1394 printf("error!\n%s in '%s' image node\n", err_msg,
1395 fit_get_name(fit, image_noffset, NULL));
1396 return 0;
Jun Nie5c643db2018-02-27 16:55:58 +08001397}
1398
1399/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001400 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001401 * @fit: pointer to the FIT format image header
1402 *
Simon Glassb8da8362013-05-07 06:11:57 +00001403 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001404 * for every images checks if all it's hashes are valid.
1405 *
1406 * returns:
1407 * 1, if all hashes of all images are valid
1408 * 0, otherwise (or on error)
1409 */
Simon Glassb8da8362013-05-07 06:11:57 +00001410int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001411{
1412 int images_noffset;
1413 int noffset;
1414 int ndepth;
1415 int count;
1416
1417 /* Find images parent node offset */
1418 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1419 if (images_noffset < 0) {
1420 printf("Can't find images parent node '%s' (%s)\n",
1421 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1422 return 0;
1423 }
1424
1425 /* Process all image subnodes, check hashes for each */
1426 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1427 (ulong)fit);
1428 for (ndepth = 0, count = 0,
1429 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1430 (noffset >= 0) && (ndepth > 0);
1431 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1432 if (ndepth == 1) {
1433 /*
1434 * Direct child node of the images parent node,
1435 * i.e. component image node.
1436 */
Simon Glass73223f02016-02-22 22:55:43 -07001437 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001438 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001439 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001440
Simon Glassb8da8362013-05-07 06:11:57 +00001441 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001442 return 0;
1443 printf("\n");
1444 }
1445 }
1446 return 1;
1447}
1448
Philippe Reynes4df35782019-12-18 18:25:42 +01001449static int fit_image_uncipher(const void *fit, int image_noffset,
1450 void **data, size_t *size)
1451{
1452 int cipher_noffset, ret;
1453 void *dst;
1454 size_t size_dst;
1455
1456 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1457 FIT_CIPHER_NODENAME);
1458 if (cipher_noffset < 0)
1459 return 0;
1460
1461 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1462 *data, *size, &dst, &size_dst);
1463 if (ret)
1464 goto out;
1465
1466 *data = dst;
1467 *size = size_dst;
1468
1469 out:
1470 return ret;
1471}
Philippe Reynes4df35782019-12-18 18:25:42 +01001472
Simon Glass53fbb7e2013-05-07 06:11:53 +00001473/**
1474 * fit_image_check_os - check whether image node is of a given os type
1475 * @fit: pointer to the FIT format image header
1476 * @noffset: component image node offset
1477 * @os: requested image os
1478 *
1479 * fit_image_check_os() reads image os property and compares its numeric
1480 * id with the requested os. Comparison result is returned to the caller.
1481 *
1482 * returns:
1483 * 1 if image is of given os type
1484 * 0 otherwise (or on error)
1485 */
1486int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1487{
1488 uint8_t image_os;
1489
1490 if (fit_image_get_os(fit, noffset, &image_os))
1491 return 0;
1492 return (os == image_os);
1493}
1494
1495/**
1496 * fit_image_check_arch - check whether image node is of a given arch
1497 * @fit: pointer to the FIT format image header
1498 * @noffset: component image node offset
1499 * @arch: requested imagearch
1500 *
1501 * fit_image_check_arch() reads image arch property and compares its numeric
1502 * id with the requested arch. Comparison result is returned to the caller.
1503 *
1504 * returns:
1505 * 1 if image is of given arch
1506 * 0 otherwise (or on error)
1507 */
1508int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1509{
1510 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001511 int aarch32_support = 0;
1512
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01001513 if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
1514 aarch32_support = 1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001515
1516 if (fit_image_get_arch(fit, noffset, &image_arch))
1517 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001518 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001519 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1520 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1521 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001522}
1523
1524/**
1525 * fit_image_check_type - check whether image node is of a given type
1526 * @fit: pointer to the FIT format image header
1527 * @noffset: component image node offset
1528 * @type: requested image type
1529 *
1530 * fit_image_check_type() reads image type property and compares its numeric
1531 * id with the requested type. Comparison result is returned to the caller.
1532 *
1533 * returns:
1534 * 1 if image is of given type
1535 * 0 otherwise (or on error)
1536 */
1537int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1538{
1539 uint8_t image_type;
1540
1541 if (fit_image_get_type(fit, noffset, &image_type))
1542 return 0;
1543 return (type == image_type);
1544}
1545
1546/**
1547 * fit_image_check_comp - check whether image node uses given compression
1548 * @fit: pointer to the FIT format image header
1549 * @noffset: component image node offset
1550 * @comp: requested image compression type
1551 *
1552 * fit_image_check_comp() reads image compression property and compares its
1553 * numeric id with the requested compression type. Comparison result is
1554 * returned to the caller.
1555 *
1556 * returns:
1557 * 1 if image uses requested compression
1558 * 0 otherwise (or on error)
1559 */
1560int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1561{
1562 uint8_t image_comp;
1563
1564 if (fit_image_get_comp(fit, noffset, &image_comp))
1565 return 0;
1566 return (comp == image_comp);
1567}
1568
1569/**
1570 * fit_check_format - sanity check FIT image format
1571 * @fit: pointer to the FIT format image header
1572 *
1573 * fit_check_format() runs a basic sanity FIT image verification.
1574 * Routine checks for mandatory properties, nodes, etc.
1575 *
1576 * returns:
1577 * 1, on success
1578 * 0, on failure
1579 */
1580int fit_check_format(const void *fit)
1581{
Heinrich Schuchardtea1a9ec2021-01-13 02:09:12 +01001582 /* A FIT image must be a valid FDT */
1583 if (fdt_check_header(fit)) {
1584 debug("Wrong FIT format: not a flattened device tree\n");
1585 return 0;
1586 }
1587
Simon Glass53fbb7e2013-05-07 06:11:53 +00001588 /* mandatory / node 'description' property */
1589 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1590 debug("Wrong FIT format: no description\n");
1591 return 0;
1592 }
1593
1594 if (IMAGE_ENABLE_TIMESTAMP) {
1595 /* mandatory / node 'timestamp' property */
1596 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1597 debug("Wrong FIT format: no timestamp\n");
1598 return 0;
1599 }
1600 }
1601
1602 /* mandatory subimages parent '/images' node */
1603 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1604 debug("Wrong FIT format: no images parent node\n");
1605 return 0;
1606 }
1607
1608 return 1;
1609}
1610
1611
1612/**
1613 * fit_conf_find_compat
1614 * @fit: pointer to the FIT format image header
1615 * @fdt: pointer to the device tree to compare against
1616 *
1617 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1618 * most compatible with the passed in device tree.
1619 *
1620 * Example:
1621 *
1622 * / o image-tree
1623 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001624 * | |-o fdt-1
1625 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001626 * |
1627 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001628 * |-o config-1
1629 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001630 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001631 * |-o config-2
1632 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001633 *
1634 * / o U-Boot fdt
1635 * |-compatible = "foo,bar", "bim,bam"
1636 *
1637 * / o kernel fdt1
1638 * |-compatible = "foo,bar",
1639 *
1640 * / o kernel fdt2
1641 * |-compatible = "bim,bam", "baz,biz"
1642 *
1643 * Configuration 1 would be picked because the first string in U-Boot's
1644 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1645 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1646 *
Julius Werner18cfa612019-07-24 19:37:56 -07001647 * As an optimization, the compatible property from the FDT's root node can be
1648 * copied into the configuration node in the FIT image. This is required to
1649 * match configurations with compressed FDTs.
1650 *
Simon Glass53fbb7e2013-05-07 06:11:53 +00001651 * returns:
1652 * offset to the configuration to use if one was found
1653 * -1 otherwise
1654 */
1655int fit_conf_find_compat(const void *fit, const void *fdt)
1656{
1657 int ndepth = 0;
1658 int noffset, confs_noffset, images_noffset;
1659 const void *fdt_compat;
1660 int fdt_compat_len;
1661 int best_match_offset = 0;
1662 int best_match_pos = 0;
1663
1664 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1665 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1666 if (confs_noffset < 0 || images_noffset < 0) {
1667 debug("Can't find configurations or images nodes.\n");
1668 return -1;
1669 }
1670
1671 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1672 if (!fdt_compat) {
1673 debug("Fdt for comparison has no \"compatible\" property.\n");
1674 return -1;
1675 }
1676
1677 /*
1678 * Loop over the configurations in the FIT image.
1679 */
1680 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1681 (noffset >= 0) && (ndepth > 0);
1682 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001683 const void *fdt;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001684 const char *kfdt_name;
Julius Werner18cfa612019-07-24 19:37:56 -07001685 int kfdt_noffset, compat_noffset;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001686 const char *cur_fdt_compat;
1687 int len;
Julius Werner18cfa612019-07-24 19:37:56 -07001688 size_t sz;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001689 int i;
1690
1691 if (ndepth > 1)
1692 continue;
1693
Julius Werner18cfa612019-07-24 19:37:56 -07001694 /* If there's a compat property in the config node, use that. */
1695 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1696 fdt = fit; /* search in FIT image */
1697 compat_noffset = noffset; /* search under config node */
1698 } else { /* Otherwise extract it from the kernel FDT. */
1699 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1700 if (!kfdt_name) {
1701 debug("No fdt property found.\n");
1702 continue;
1703 }
1704 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1705 kfdt_name);
1706 if (kfdt_noffset < 0) {
1707 debug("No image node named \"%s\" found.\n",
1708 kfdt_name);
1709 continue;
1710 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001711
Julius Werner18cfa612019-07-24 19:37:56 -07001712 if (!fit_image_check_comp(fit, kfdt_noffset,
1713 IH_COMP_NONE)) {
1714 debug("Can't extract compat from \"%s\" "
1715 "(compressed)\n", kfdt_name);
1716 continue;
1717 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001718
Julius Werner18cfa612019-07-24 19:37:56 -07001719 /* search in this config's kernel FDT */
1720 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1721 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1722 continue;
1723 }
1724
1725 compat_noffset = 0; /* search kFDT under root node */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001726 }
1727
1728 len = fdt_compat_len;
1729 cur_fdt_compat = fdt_compat;
1730 /*
1731 * Look for a match for each U-Boot compatibility string in
Julius Werner18cfa612019-07-24 19:37:56 -07001732 * turn in the compat string property.
Simon Glass53fbb7e2013-05-07 06:11:53 +00001733 */
1734 for (i = 0; len > 0 &&
1735 (!best_match_offset || best_match_pos > i); i++) {
1736 int cur_len = strlen(cur_fdt_compat) + 1;
1737
Julius Werner18cfa612019-07-24 19:37:56 -07001738 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001739 cur_fdt_compat)) {
1740 best_match_offset = noffset;
1741 best_match_pos = i;
1742 break;
1743 }
1744 len -= cur_len;
1745 cur_fdt_compat += cur_len;
1746 }
1747 }
1748 if (!best_match_offset) {
1749 debug("No match found.\n");
1750 return -1;
1751 }
1752
1753 return best_match_offset;
1754}
1755
Simon Glass53fbb7e2013-05-07 06:11:53 +00001756int fit_conf_get_node(const void *fit, const char *conf_uname)
1757{
1758 int noffset, confs_noffset;
1759 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001760 const char *s;
1761 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001762
1763 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1764 if (confs_noffset < 0) {
1765 debug("Can't find configurations parent node '%s' (%s)\n",
1766 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1767 return confs_noffset;
1768 }
1769
1770 if (conf_uname == NULL) {
1771 /* get configuration unit name from the default property */
1772 debug("No configuration specified, trying default...\n");
Sebastian Reicheld8ab0fe2021-01-04 20:48:04 +01001773 if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
1774 noffset = fit_find_config_node(fit);
1775 if (noffset < 0)
1776 return noffset;
1777 conf_uname = fdt_get_name(fit, noffset, NULL);
1778 } else {
1779 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1780 FIT_DEFAULT_PROP, &len);
1781 if (conf_uname == NULL) {
1782 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1783 len);
1784 return len;
1785 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001786 }
1787 debug("Found default configuration: '%s'\n", conf_uname);
1788 }
1789
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001790 s = strchr(conf_uname, '#');
1791 if (s) {
1792 len = s - conf_uname;
1793 conf_uname_copy = malloc(len + 1);
1794 if (!conf_uname_copy) {
1795 debug("Can't allocate uname copy: '%s'\n",
1796 conf_uname);
1797 return -ENOMEM;
1798 }
1799 memcpy(conf_uname_copy, conf_uname, len);
1800 conf_uname_copy[len] = '\0';
1801 conf_uname = conf_uname_copy;
1802 }
1803
Simon Glass53fbb7e2013-05-07 06:11:53 +00001804 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1805 if (noffset < 0) {
1806 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1807 conf_uname, fdt_strerror(noffset));
1808 }
1809
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001810 if (conf_uname_copy)
1811 free(conf_uname_copy);
1812
Simon Glass53fbb7e2013-05-07 06:11:53 +00001813 return noffset;
1814}
1815
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001816int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001817 const char *prop_name)
1818{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001819 return fdt_stringlist_count(fit, noffset, prop_name);
1820}
1821
1822int fit_conf_get_prop_node_index(const void *fit, int noffset,
1823 const char *prop_name, int index)
1824{
1825 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001826 int len;
1827
1828 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001829 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001830 if (uname == NULL)
1831 return len;
1832
1833 return fit_image_get_node(fit, uname);
1834}
1835
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001836int fit_conf_get_prop_node(const void *fit, int noffset,
1837 const char *prop_name)
1838{
1839 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1840}
1841
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001842static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001843{
1844 fit_image_print(fit, rd_noffset, " ");
1845
1846 if (verify) {
1847 puts(" Verifying Hash Integrity ... ");
1848 if (!fit_image_verify(fit, rd_noffset)) {
1849 puts("Bad Data Hash\n");
1850 return -EACCES;
1851 }
1852 puts("OK\n");
1853 }
1854
1855 return 0;
1856}
1857
Simon Glass782cfbb2013-05-16 13:53:21 +00001858int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1859 ulong addr)
1860{
1861 int cfg_noffset;
1862 void *fit_hdr;
1863 int noffset;
1864
1865 debug("* %s: using config '%s' from image at 0x%08lx\n",
1866 prop_name, images->fit_uname_cfg, addr);
1867
1868 /* Check whether configuration has this property defined */
1869 fit_hdr = map_sysmem(addr, 0);
1870 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1871 if (cfg_noffset < 0) {
1872 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001873 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001874 }
1875
1876 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1877 if (noffset < 0) {
1878 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001879 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001880 }
1881
1882 return noffset;
1883}
1884
Simon Glass126cc862014-06-12 07:24:47 -06001885/**
1886 * fit_get_image_type_property() - get property name for IH_TYPE_...
1887 *
1888 * @return the properly name where we expect to find the image in the
1889 * config node
1890 */
1891static const char *fit_get_image_type_property(int type)
1892{
1893 /*
1894 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001895 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001896 * anyway. So let's just keep it here.
1897 */
1898 switch (type) {
1899 case IH_TYPE_FLATDT:
1900 return FIT_FDT_PROP;
1901 case IH_TYPE_KERNEL:
1902 return FIT_KERNEL_PROP;
1903 case IH_TYPE_RAMDISK:
1904 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001905 case IH_TYPE_X86_SETUP:
1906 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001907 case IH_TYPE_LOADABLE:
1908 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001909 case IH_TYPE_FPGA:
1910 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001911 case IH_TYPE_STANDALONE:
1912 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001913 }
1914
1915 return "unknown";
1916}
1917
1918int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001919 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001920 int arch, int image_type, int bootstage_id,
1921 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1922{
1923 int cfg_noffset, noffset;
1924 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001925 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001926 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001927 const void *fit;
Julius Wernerb1307f82019-07-24 19:37:55 -07001928 void *buf;
1929 void *loadbuf;
Simon Glass782cfbb2013-05-16 13:53:21 +00001930 size_t size;
1931 int type_ok, os_ok;
Julius Wernerb1307f82019-07-24 19:37:55 -07001932 ulong load, load_end, data, len;
1933 uint8_t os, comp;
Alison Wangec6617c2016-11-10 10:49:03 +08001934#ifndef USE_HOSTCC
1935 uint8_t os_arch;
1936#endif
Simon Glass126cc862014-06-12 07:24:47 -06001937 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001938 int ret;
1939
1940 fit = map_sysmem(addr, 0);
1941 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001942 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001943 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001944 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001945 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1946
1947 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1948 if (!fit_check_format(fit)) {
1949 printf("Bad FIT %s image format!\n", prop_name);
1950 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1951 return -ENOEXEC;
1952 }
1953 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1954 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001955 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001956 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1957 noffset = fit_image_get_node(fit, fit_uname);
1958 } else {
1959 /*
1960 * no image node unit name, try to get config
1961 * node first. If config unit node name is NULL
1962 * fit_conf_get_node() will try to find default config node
1963 */
1964 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1965 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1966 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1967 } else {
1968 cfg_noffset = fit_conf_get_node(fit,
1969 fit_uname_config);
1970 }
1971 if (cfg_noffset < 0) {
1972 puts("Could not find configuration node\n");
1973 bootstage_error(bootstage_id +
1974 BOOTSTAGE_SUB_NO_UNIT_NAME);
1975 return -ENOENT;
1976 }
Marek Vasut078e5582018-05-31 17:59:07 +02001977
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001978 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1979 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02001980 /* Remember this config */
1981 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001982 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02001983
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001984 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut078e5582018-05-31 17:59:07 +02001985 puts(" Verifying Hash Integrity ... ");
1986 if (fit_config_verify(fit, cfg_noffset)) {
1987 puts("Bad Data Hash\n");
1988 bootstage_error(bootstage_id +
1989 BOOTSTAGE_SUB_HASH);
1990 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00001991 }
Marek Vasut078e5582018-05-31 17:59:07 +02001992 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00001993 }
1994
Marek Vasut078e5582018-05-31 17:59:07 +02001995 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1996
Simon Glass782cfbb2013-05-16 13:53:21 +00001997 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1998 prop_name);
1999 fit_uname = fit_get_name(fit, noffset, NULL);
2000 }
2001 if (noffset < 0) {
Simon Glass382cf622020-03-18 11:43:56 -06002002 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass782cfbb2013-05-16 13:53:21 +00002003 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2004 return -ENOENT;
2005 }
2006
2007 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2008
2009 ret = fit_image_select(fit, noffset, images->verify);
2010 if (ret) {
2011 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2012 return ret;
2013 }
2014
2015 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002016 if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
2017 if (!fit_image_check_target_arch(fit, noffset)) {
2018 puts("Unsupported Architecture\n");
2019 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2020 return -ENOEXEC;
2021 }
Simon Glass782cfbb2013-05-16 13:53:21 +00002022 }
Alison Wangec6617c2016-11-10 10:49:03 +08002023
2024#ifndef USE_HOSTCC
2025 fit_image_get_arch(fit, noffset, &os_arch);
2026 images->os.arch = os_arch;
2027#endif
2028
Simon Glass782cfbb2013-05-16 13:53:21 +00002029 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2030 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002031 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2032 (image_type == IH_TYPE_KERNEL &&
2033 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01002034
Andreas Bießmann950fe262016-08-14 20:31:24 +02002035 os_ok = image_type == IH_TYPE_FLATDT ||
2036 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01002037 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002038 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Cristian Ciocalteaa031b032019-12-24 18:05:38 +02002039 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao0df27d62020-03-18 07:32:07 -07002040 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2041 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
Karl Apsite84a07db2015-05-21 09:52:48 -04002042
2043 /*
2044 * If either of the checks fail, we should report an error, but
2045 * if the image type is coming from the "loadables" field, we
2046 * don't care what it is
2047 */
2048 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01002049 fit_image_get_os(fit, noffset, &os);
2050 printf("No %s %s %s Image\n",
2051 genimg_get_os_name(os),
2052 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00002053 genimg_get_type_name(image_type));
2054 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2055 return -EIO;
2056 }
2057
2058 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2059
2060 /* get image data address and length */
Julius Wernerb1307f82019-07-24 19:37:55 -07002061 if (fit_image_get_data_and_size(fit, noffset,
2062 (const void **)&buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002063 printf("Could not find %s subimage data!\n", prop_name);
2064 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07002065 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00002066 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002067
Philippe Reynes4df35782019-12-18 18:25:42 +01002068 /* Decrypt data before uncompress/move */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002069 if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
Philippe Reynes4df35782019-12-18 18:25:42 +01002070 puts(" Decrypting Data ... ");
2071 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2072 puts("Error\n");
2073 return -EACCES;
2074 }
2075 puts("OK\n");
2076 }
Philippe Reynes4df35782019-12-18 18:25:42 +01002077
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002078 /* perform any post-processing on the image data */
Sebastian Reichelf14e6ee2021-01-04 20:48:03 +01002079 if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
2080 board_fit_image_post_process(&buf, &size);
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002081
Simon Glass782cfbb2013-05-16 13:53:21 +00002082 len = (ulong)size;
2083
Simon Glass782cfbb2013-05-16 13:53:21 +00002084 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2085
Julius Wernerb1307f82019-07-24 19:37:55 -07002086 data = map_to_sysmem(buf);
2087 load = data;
Simon Glass782cfbb2013-05-16 13:53:21 +00002088 if (load_op == FIT_LOAD_IGNORED) {
2089 /* Don't load */
2090 } else if (fit_image_get_load(fit, noffset, &load)) {
2091 if (load_op == FIT_LOAD_REQUIRED) {
2092 printf("Can't get %s subimage load address!\n",
2093 prop_name);
2094 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2095 return -EBADF;
2096 }
Simon Glassfe20a812014-08-22 14:26:43 -06002097 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002098 ulong image_start, image_end;
Simon Glass782cfbb2013-05-16 13:53:21 +00002099
2100 /*
2101 * move image data to the load address,
2102 * make sure we don't overwrite initial image
2103 */
2104 image_start = addr;
2105 image_end = addr + fit_get_size(fit);
2106
2107 load_end = load + len;
2108 if (image_type != IH_TYPE_KERNEL &&
2109 load < image_end && load_end > image_start) {
2110 printf("Error: %s overwritten\n", prop_name);
2111 return -EXDEV;
2112 }
2113
2114 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2115 prop_name, data, load);
Julius Wernerb1307f82019-07-24 19:37:55 -07002116 } else {
2117 load = data; /* No load address specified */
Simon Glass782cfbb2013-05-16 13:53:21 +00002118 }
Julius Wernerb1307f82019-07-24 19:37:55 -07002119
2120 comp = IH_COMP_NONE;
2121 loadbuf = buf;
2122 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Wernerbddd9852019-08-02 15:52:28 -07002123 if (!fit_image_get_comp(fit, noffset, &comp) &&
2124 comp != IH_COMP_NONE &&
2125 !(image_type == IH_TYPE_KERNEL ||
2126 image_type == IH_TYPE_KERNEL_NOLOAD ||
2127 image_type == IH_TYPE_RAMDISK)) {
Julius Wernerb1307f82019-07-24 19:37:55 -07002128 ulong max_decomp_len = len * 20;
2129 if (load == data) {
2130 loadbuf = malloc(max_decomp_len);
2131 load = map_to_sysmem(loadbuf);
2132 } else {
2133 loadbuf = map_sysmem(load, max_decomp_len);
2134 }
2135 if (image_decomp(comp, load, data, image_type,
2136 loadbuf, buf, len, max_decomp_len, &load_end)) {
2137 printf("Error decompressing %s\n", prop_name);
2138
2139 return -ENOEXEC;
2140 }
2141 len = load_end - load;
2142 } else if (load != data) {
2143 loadbuf = map_sysmem(load, len);
2144 memcpy(loadbuf, buf, len);
2145 }
2146
Julius Wernerbddd9852019-08-02 15:52:28 -07002147 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2148 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2149 " please fix your .its file!\n");
2150
Julius Wernerb1307f82019-07-24 19:37:55 -07002151 /* verify that image data is a proper FDT blob */
2152 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2153 puts("Subimage data is not a FDT");
2154 return -ENOEXEC;
2155 }
2156
Simon Glass782cfbb2013-05-16 13:53:21 +00002157 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2158
Julius Wernerb1307f82019-07-24 19:37:55 -07002159 *datap = load;
Simon Glass782cfbb2013-05-16 13:53:21 +00002160 *lenp = len;
2161 if (fit_unamep)
2162 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002163 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002164 *fit_uname_configp = (char *)(fit_uname_config ? :
2165 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002166
2167 return noffset;
2168}
Simon Glass90268b82014-10-19 21:11:24 -06002169
2170int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2171 ulong *setup_start, ulong *setup_len)
2172{
2173 int noffset;
2174 ulong addr;
2175 ulong len;
2176 int ret;
2177
2178 addr = map_to_sysmem(images->fit_hdr_os);
2179 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2180 if (noffset < 0)
2181 return noffset;
2182
2183 ret = fit_image_load(images, addr, NULL, NULL, arch,
2184 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2185 FIT_LOAD_REQUIRED, setup_start, &len);
2186
2187 return ret;
2188}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002189
2190#ifndef USE_HOSTCC
2191int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2192 const char **fit_unamep, const char **fit_uname_configp,
2193 int arch, ulong *datap, ulong *lenp)
2194{
2195 int fdt_noffset, cfg_noffset, count;
2196 const void *fit;
2197 const char *fit_uname = NULL;
2198 const char *fit_uname_config = NULL;
2199 char *fit_uname_config_copy = NULL;
2200 char *next_config = NULL;
2201 ulong load, len;
2202#ifdef CONFIG_OF_LIBFDT_OVERLAY
2203 ulong image_start, image_end;
2204 ulong ovload, ovlen;
2205 const char *uconfig;
2206 const char *uname;
2207 void *base, *ov;
2208 int i, err, noffset, ov_noffset;
2209#endif
2210
2211 fit_uname = fit_unamep ? *fit_unamep : NULL;
2212
2213 if (fit_uname_configp && *fit_uname_configp) {
2214 fit_uname_config_copy = strdup(*fit_uname_configp);
2215 if (!fit_uname_config_copy)
2216 return -ENOMEM;
2217
2218 next_config = strchr(fit_uname_config_copy, '#');
2219 if (next_config)
2220 *next_config++ = '\0';
2221 if (next_config - 1 > fit_uname_config_copy)
2222 fit_uname_config = fit_uname_config_copy;
2223 }
2224
2225 fdt_noffset = fit_image_load(images,
2226 addr, &fit_uname, &fit_uname_config,
2227 arch, IH_TYPE_FLATDT,
2228 BOOTSTAGE_ID_FIT_FDT_START,
2229 FIT_LOAD_OPTIONAL, &load, &len);
2230
2231 if (fdt_noffset < 0)
2232 goto out;
2233
2234 debug("fit_uname=%s, fit_uname_config=%s\n",
2235 fit_uname ? fit_uname : "<NULL>",
2236 fit_uname_config ? fit_uname_config : "<NULL>");
2237
2238 fit = map_sysmem(addr, 0);
2239
2240 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2241
2242 /* single blob, or error just return as well */
2243 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2244 if (count <= 1 && !next_config)
2245 goto out;
2246
2247 /* we need to apply overlays */
2248
2249#ifdef CONFIG_OF_LIBFDT_OVERLAY
2250 image_start = addr;
2251 image_end = addr + fit_get_size(fit);
2252 /* verify that relocation took place by load address not being in fit */
2253 if (load >= image_start && load < image_end) {
2254 /* check is simplified; fit load checks for overlaps */
2255 printf("Overlayed FDT requires relocation\n");
2256 fdt_noffset = -EBADF;
2257 goto out;
2258 }
2259
2260 base = map_sysmem(load, len);
2261
2262 /* apply extra configs in FIT first, followed by args */
2263 for (i = 1; ; i++) {
2264 if (i < count) {
2265 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2266 FIT_FDT_PROP, i);
2267 uname = fit_get_name(fit, noffset, NULL);
2268 uconfig = NULL;
2269 } else {
2270 if (!next_config)
2271 break;
2272 uconfig = next_config;
2273 next_config = strchr(next_config, '#');
2274 if (next_config)
2275 *next_config++ = '\0';
2276 uname = NULL;
Peter Ujfalusi35c75272019-03-06 15:52:27 +02002277
2278 /*
2279 * fit_image_load() would load the first FDT from the
2280 * extra config only when uconfig is specified.
2281 * Check if the extra config contains multiple FDTs and
2282 * if so, load them.
2283 */
2284 cfg_noffset = fit_conf_get_node(fit, uconfig);
2285
2286 i = 0;
2287 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2288 FIT_FDT_PROP);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002289 }
2290
2291 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2292
2293 ov_noffset = fit_image_load(images,
2294 addr, &uname, &uconfig,
2295 arch, IH_TYPE_FLATDT,
2296 BOOTSTAGE_ID_FIT_FDT_START,
2297 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2298 if (ov_noffset < 0) {
2299 printf("load of %s failed\n", uname);
2300 continue;
2301 }
2302 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2303 uname, ovload, ovlen);
2304 ov = map_sysmem(ovload, ovlen);
2305
2306 base = map_sysmem(load, len + ovlen);
2307 err = fdt_open_into(base, base, len + ovlen);
2308 if (err < 0) {
2309 printf("failed on fdt_open_into\n");
2310 fdt_noffset = err;
2311 goto out;
2312 }
2313 /* the verbose method prints out messages on error */
2314 err = fdt_overlay_apply_verbose(base, ov);
2315 if (err < 0) {
2316 fdt_noffset = err;
2317 goto out;
2318 }
2319 fdt_pack(base);
2320 len = fdt_totalsize(base);
2321 }
2322#else
2323 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2324 fdt_noffset = -EBADF;
2325#endif
2326
2327out:
2328 if (datap)
2329 *datap = load;
2330 if (lenp)
2331 *lenp = len;
2332 if (fit_unamep)
2333 *fit_unamep = fit_uname;
2334 if (fit_uname_configp)
2335 *fit_uname_configp = fit_uname_config;
2336
2337 if (fit_uname_config_copy)
2338 free(fit_uname_config_copy);
2339 return fdt_noffset;
2340}
2341#endif