blob: d54eff9033cc25ebf07bfa72d182645c9aa60fca [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>
York Sun020198b2016-11-23 09:25:09 -080018#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000019#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000020#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060021#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050022#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000023#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030024#include <malloc.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>
Simon Glass53fbb7e2013-05-07 06:11:53 +000031#include <u-boot/crc.h>
32#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020033#include <u-boot/sha1.h>
34#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120035#include <u-boot/sha512.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000036
37/*****************************************************************************/
38/* New uImage format routines */
39/*****************************************************************************/
40#ifndef USE_HOSTCC
41static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
42 ulong *addr, const char **name)
43{
44 const char *sep;
45
46 *addr = addr_curr;
47 *name = NULL;
48
49 sep = strchr(spec, sepc);
50 if (sep) {
51 if (sep - spec > 0)
52 *addr = simple_strtoul(spec, NULL, 16);
53
54 *name = sep + 1;
55 return 1;
56 }
57
58 return 0;
59}
60
61/**
62 * fit_parse_conf - parse FIT configuration spec
63 * @spec: input string, containing configuration spec
64 * @add_curr: current image address (to be used as a possible default)
65 * @addr: pointer to a ulong variable, will hold FIT image address of a given
66 * configuration
67 * @conf_name double pointer to a char, will hold pointer to a configuration
68 * unit name
69 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090070 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000071 * where <addr> is a FIT image address that contains configuration
72 * with a <conf> unit name.
73 *
74 * Address part is optional, and if omitted default add_curr will
75 * be used instead.
76 *
77 * returns:
78 * 1 if spec is a valid configuration string,
79 * addr and conf_name are set accordingly
80 * 0 otherwise
81 */
82int fit_parse_conf(const char *spec, ulong addr_curr,
83 ulong *addr, const char **conf_name)
84{
85 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
86}
87
88/**
89 * fit_parse_subimage - parse FIT subimage spec
90 * @spec: input string, containing subimage spec
91 * @add_curr: current image address (to be used as a possible default)
92 * @addr: pointer to a ulong variable, will hold FIT image address of a given
93 * subimage
94 * @image_name: double pointer to a char, will hold pointer to a subimage name
95 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090096 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000097 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
98 * subimage with a <subimg> unit name.
99 *
100 * Address part is optional, and if omitted default add_curr will
101 * be used instead.
102 *
103 * returns:
104 * 1 if spec is a valid subimage string,
105 * addr and image_name are set accordingly
106 * 0 otherwise
107 */
108int fit_parse_subimage(const char *spec, ulong addr_curr,
109 ulong *addr, const char **image_name)
110{
111 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
112}
113#endif /* !USE_HOSTCC */
114
115static void fit_get_debug(const void *fit, int noffset,
116 char *prop_name, int err)
117{
118 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
119 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
120 fdt_strerror(err));
121}
122
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200123/**
124 * fit_get_subimage_count - get component (sub-image) count
125 * @fit: pointer to the FIT format image header
126 * @images_noffset: offset of images node
127 *
128 * returns:
129 * number of image components
130 */
131int fit_get_subimage_count(const void *fit, int images_noffset)
132{
133 int noffset;
134 int ndepth;
135 int count = 0;
136
137 /* Process its subnodes, print out component images details */
138 for (ndepth = 0, count = 0,
139 noffset = fdt_next_node(fit, images_noffset, &ndepth);
140 (noffset >= 0) && (ndepth > 0);
141 noffset = fdt_next_node(fit, noffset, &ndepth)) {
142 if (ndepth == 1) {
143 count++;
144 }
145 }
146
147 return count;
148}
149
Marek Vasutb527b9c2018-05-13 00:22:52 +0200150#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000151/**
Tom Rini16c4b162018-05-08 14:34:05 -0400152 * fit_image_print_data() - prints out the hash node details
153 * @fit: pointer to the FIT format image header
154 * @noffset: offset of the hash node
155 * @p: pointer to prefix string
156 * @type: Type of information to print ("hash" or "sign")
157 *
158 * fit_image_print_data() lists properties for the processed hash node
159 *
160 * This function avoid using puts() since it prints a newline on the host
161 * but does not in U-Boot.
162 *
163 * returns:
164 * no returned results
165 */
166static void fit_image_print_data(const void *fit, int noffset, const char *p,
167 const char *type)
168{
169 const char *keyname;
170 uint8_t *value;
171 int value_len;
172 char *algo;
Philippe Reynes20031562018-11-14 13:51:00 +0100173 const char *padding;
Simon Glass72188f52020-03-18 11:44:06 -0600174 bool required;
Tom Rini16c4b162018-05-08 14:34:05 -0400175 int ret, i;
176
177 debug("%s %s node: '%s'\n", p, type,
178 fit_get_name(fit, noffset, NULL));
179 printf("%s %s algo: ", p, type);
180 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
181 printf("invalid/unsupported\n");
182 return;
183 }
184 printf("%s", algo);
Simon Glass72188f52020-03-18 11:44:06 -0600185 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
186 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
Tom Rini16c4b162018-05-08 14:34:05 -0400187 if (keyname)
188 printf(":%s", keyname);
189 if (required)
190 printf(" (required)");
191 printf("\n");
192
Philippe Reynes20031562018-11-14 13:51:00 +0100193 padding = fdt_getprop(fit, noffset, "padding", NULL);
194 if (padding)
195 printf("%s %s padding: %s\n", p, type, padding);
196
Tom Rini16c4b162018-05-08 14:34:05 -0400197 ret = fit_image_hash_get_value(fit, noffset, &value,
198 &value_len);
199 printf("%s %s value: ", p, type);
200 if (ret) {
201 printf("unavailable\n");
202 } else {
203 for (i = 0; i < value_len; i++)
204 printf("%02x", value[i]);
205 printf("\n");
206 }
207
208 debug("%s %s len: %d\n", p, type, value_len);
209
210 /* Signatures have a time stamp */
211 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
212 time_t timestamp;
213
214 printf("%s Timestamp: ", p);
215 if (fit_get_timestamp(fit, noffset, &timestamp))
216 printf("unavailable\n");
217 else
218 genimg_print_time(timestamp);
219 }
220}
221
222/**
223 * fit_image_print_verification_data() - prints out the hash/signature details
224 * @fit: pointer to the FIT format image header
225 * @noffset: offset of the hash or signature node
226 * @p: pointer to prefix string
227 *
228 * This lists properties for the processed hash node
229 *
230 * returns:
231 * no returned results
232 */
233static void fit_image_print_verification_data(const void *fit, int noffset,
234 const char *p)
235{
236 const char *name;
237
238 /*
239 * Check subnode name, must be equal to "hash" or "signature".
240 * Multiple hash/signature nodes require unique unit node
241 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
242 */
243 name = fit_get_name(fit, noffset, NULL);
244 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
245 fit_image_print_data(fit, noffset, p, "Hash");
246 } else if (!strncmp(name, FIT_SIG_NODENAME,
247 strlen(FIT_SIG_NODENAME))) {
248 fit_image_print_data(fit, noffset, p, "Sign");
249 }
250}
251
252/**
253 * fit_conf_print - prints out the FIT configuration details
254 * @fit: pointer to the FIT format image header
255 * @noffset: offset of the configuration node
256 * @p: pointer to prefix string
257 *
258 * fit_conf_print() lists all mandatory properties for the processed
259 * configuration node.
260 *
261 * returns:
262 * no returned results
263 */
264static void fit_conf_print(const void *fit, int noffset, const char *p)
265{
266 char *desc;
267 const char *uname;
268 int ret;
269 int fdt_index, loadables_index;
270 int ndepth;
271
272 /* Mandatory properties */
273 ret = fit_get_desc(fit, noffset, &desc);
274 printf("%s Description: ", p);
275 if (ret)
276 printf("unavailable\n");
277 else
278 printf("%s\n", desc);
279
280 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
281 printf("%s Kernel: ", p);
282 if (!uname)
283 printf("unavailable\n");
284 else
285 printf("%s\n", uname);
286
287 /* Optional properties */
288 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
289 if (uname)
290 printf("%s Init Ramdisk: %s\n", p, uname);
291
292 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
293 if (uname)
294 printf("%s Firmware: %s\n", p, uname);
295
296 for (fdt_index = 0;
297 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
298 fdt_index, NULL), uname;
299 fdt_index++) {
300 if (fdt_index == 0)
301 printf("%s FDT: ", p);
302 else
303 printf("%s ", p);
304 printf("%s\n", uname);
305 }
306
307 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
308 if (uname)
309 printf("%s FPGA: %s\n", p, uname);
310
311 /* Print out all of the specified loadables */
312 for (loadables_index = 0;
313 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
314 loadables_index, NULL), uname;
315 loadables_index++) {
316 if (loadables_index == 0) {
317 printf("%s Loadables: ", p);
318 } else {
319 printf("%s ", p);
320 }
321 printf("%s\n", uname);
322 }
323
324 /* Process all hash subnodes of the component configuration node */
325 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
326 (noffset >= 0) && (ndepth > 0);
327 noffset = fdt_next_node(fit, noffset, &ndepth)) {
328 if (ndepth == 1) {
329 /* Direct child node of the component configuration node */
330 fit_image_print_verification_data(fit, noffset, p);
331 }
332 }
333}
334
335/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000336 * fit_print_contents - prints out the contents of the FIT format image
337 * @fit: pointer to the FIT format image header
338 * @p: pointer to prefix string
339 *
340 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500341 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000342 * the details of each component image.
343 *
344 * returns:
345 * no returned results
346 */
347void fit_print_contents(const void *fit)
348{
349 char *desc;
350 char *uname;
351 int images_noffset;
352 int confs_noffset;
353 int noffset;
354 int ndepth;
355 int count = 0;
356 int ret;
357 const char *p;
358 time_t timestamp;
359
Simon Glass1fe7d932013-05-08 08:05:58 +0000360 /* Indent string is defined in header image.h */
361 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000362
363 /* Root node properties */
364 ret = fit_get_desc(fit, 0, &desc);
365 printf("%sFIT description: ", p);
366 if (ret)
367 printf("unavailable\n");
368 else
369 printf("%s\n", desc);
370
371 if (IMAGE_ENABLE_TIMESTAMP) {
372 ret = fit_get_timestamp(fit, 0, &timestamp);
373 printf("%sCreated: ", p);
374 if (ret)
375 printf("unavailable\n");
376 else
377 genimg_print_time(timestamp);
378 }
379
380 /* Find images parent node offset */
381 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
382 if (images_noffset < 0) {
383 printf("Can't find images parent node '%s' (%s)\n",
384 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
385 return;
386 }
387
388 /* Process its subnodes, print out component images details */
389 for (ndepth = 0, count = 0,
390 noffset = fdt_next_node(fit, images_noffset, &ndepth);
391 (noffset >= 0) && (ndepth > 0);
392 noffset = fdt_next_node(fit, noffset, &ndepth)) {
393 if (ndepth == 1) {
394 /*
395 * Direct child node of the images parent node,
396 * i.e. component image node.
397 */
398 printf("%s Image %u (%s)\n", p, count++,
399 fit_get_name(fit, noffset, NULL));
400
401 fit_image_print(fit, noffset, p);
402 }
403 }
404
405 /* Find configurations parent node offset */
406 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
407 if (confs_noffset < 0) {
408 debug("Can't get configurations parent node '%s' (%s)\n",
409 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
410 return;
411 }
412
413 /* get default configuration unit name from default property */
414 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
415 if (uname)
416 printf("%s Default Configuration: '%s'\n", p, uname);
417
418 /* Process its subnodes, print out configurations details */
419 for (ndepth = 0, count = 0,
420 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
421 (noffset >= 0) && (ndepth > 0);
422 noffset = fdt_next_node(fit, noffset, &ndepth)) {
423 if (ndepth == 1) {
424 /*
425 * Direct child node of the configurations parent node,
426 * i.e. configuration node.
427 */
428 printf("%s Configuration %u (%s)\n", p, count++,
429 fit_get_name(fit, noffset, NULL));
430
431 fit_conf_print(fit, noffset, p);
432 }
433 }
434}
435
436/**
437 * fit_image_print - prints out the FIT component image details
438 * @fit: pointer to the FIT format image header
439 * @image_noffset: offset of the component image node
440 * @p: pointer to prefix string
441 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500442 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000443 * image. If present, hash nodes are printed out as well. Load
444 * address for images of type firmware is also printed out. Since the load
445 * address is not mandatory for firmware images, it will be output as
446 * "unavailable" when not present.
447 *
448 * returns:
449 * no returned results
450 */
451void fit_image_print(const void *fit, int image_noffset, const char *p)
452{
453 char *desc;
454 uint8_t type, arch, os, comp;
455 size_t size;
456 ulong load, entry;
457 const void *data;
458 int noffset;
459 int ndepth;
460 int ret;
461
462 /* Mandatory properties */
463 ret = fit_get_desc(fit, image_noffset, &desc);
464 printf("%s Description: ", p);
465 if (ret)
466 printf("unavailable\n");
467 else
468 printf("%s\n", desc);
469
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700470 if (IMAGE_ENABLE_TIMESTAMP) {
471 time_t timestamp;
472
473 ret = fit_get_timestamp(fit, 0, &timestamp);
474 printf("%s Created: ", p);
475 if (ret)
476 printf("unavailable\n");
477 else
478 genimg_print_time(timestamp);
479 }
480
Simon Glass53fbb7e2013-05-07 06:11:53 +0000481 fit_image_get_type(fit, image_noffset, &type);
482 printf("%s Type: %s\n", p, genimg_get_type_name(type));
483
484 fit_image_get_comp(fit, image_noffset, &comp);
485 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
486
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800487 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000488
489#ifndef USE_HOSTCC
490 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000491 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000492 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000493 } else {
494 void *vdata = (void *)data;
495
496 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
497 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000498#endif
499
500 printf("%s Data Size: ", p);
501 if (ret)
502 printf("unavailable\n");
503 else
504 genimg_print_size(size);
505
506 /* Remaining, type dependent properties */
507 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
508 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
509 (type == IH_TYPE_FLATDT)) {
510 fit_image_get_arch(fit, image_noffset, &arch);
511 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
512 }
513
Michal Simek6faf4622018-03-26 16:31:27 +0200514 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
515 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000516 fit_image_get_os(fit, image_noffset, &os);
517 printf("%s OS: %s\n", p, genimg_get_os_name(os));
518 }
519
520 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200521 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
522 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000523 ret = fit_image_get_load(fit, image_noffset, &load);
524 printf("%s Load Address: ", p);
525 if (ret)
526 printf("unavailable\n");
527 else
528 printf("0x%08lx\n", load);
529 }
530
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300531 /* optional load address for FDT */
532 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
533 printf("%s Load Address: 0x%08lx\n", p, load);
534
Simon Glass53fbb7e2013-05-07 06:11:53 +0000535 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
536 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800537 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000538 printf("%s Entry Point: ", p);
539 if (ret)
540 printf("unavailable\n");
541 else
542 printf("0x%08lx\n", entry);
543 }
544
545 /* Process all hash subnodes of the component image node */
546 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
547 (noffset >= 0) && (ndepth > 0);
548 noffset = fdt_next_node(fit, noffset, &ndepth)) {
549 if (ndepth == 1) {
550 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000551 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000552 }
553 }
554}
Marek Vasuta3c43b12018-05-13 00:22:53 +0200555#else
556void fit_print_contents(const void *fit) { }
557void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Marek Vasutb527b9c2018-05-13 00:22:52 +0200558#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000559
560/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000561 * fit_get_desc - get node description property
562 * @fit: pointer to the FIT format image header
563 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500564 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000565 *
566 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500567 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000568 *
569 * returns:
570 * 0, on success
571 * -1, on failure
572 */
573int fit_get_desc(const void *fit, int noffset, char **desc)
574{
575 int len;
576
577 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
578 if (*desc == NULL) {
579 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
580 return -1;
581 }
582
583 return 0;
584}
585
586/**
587 * fit_get_timestamp - get node timestamp property
588 * @fit: pointer to the FIT format image header
589 * @noffset: node offset
590 * @timestamp: pointer to the time_t, will hold read timestamp
591 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500592 * fit_get_timestamp() reads timestamp property from given node, if timestamp
593 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000594 * argument.
595 *
596 * returns:
597 * 0, on success
598 * -1, on property read failure
599 * -2, on wrong timestamp size
600 */
601int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
602{
603 int len;
604 const void *data;
605
606 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
607 if (data == NULL) {
608 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
609 return -1;
610 }
611 if (len != sizeof(uint32_t)) {
612 debug("FIT timestamp with incorrect size of (%u)\n", len);
613 return -2;
614 }
615
616 *timestamp = uimage_to_cpu(*((uint32_t *)data));
617 return 0;
618}
619
620/**
621 * fit_image_get_node - get node offset for component image of a given unit name
622 * @fit: pointer to the FIT format image header
623 * @image_uname: component image node unit name
624 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500625 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000626 * node) of a provided unit name. If image is found its node offset is
627 * returned to the caller.
628 *
629 * returns:
630 * image node offset when found (>=0)
631 * negative number on failure (FDT_ERR_* code)
632 */
633int fit_image_get_node(const void *fit, const char *image_uname)
634{
635 int noffset, images_noffset;
636
637 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
638 if (images_noffset < 0) {
639 debug("Can't find images parent node '%s' (%s)\n",
640 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
641 return images_noffset;
642 }
643
644 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
645 if (noffset < 0) {
646 debug("Can't get node offset for image unit name: '%s' (%s)\n",
647 image_uname, fdt_strerror(noffset));
648 }
649
650 return noffset;
651}
652
653/**
654 * fit_image_get_os - get os id for a given component image node
655 * @fit: pointer to the FIT format image header
656 * @noffset: component image node offset
657 * @os: pointer to the uint8_t, will hold os numeric id
658 *
659 * fit_image_get_os() finds os property in a given component image node.
660 * If the property is found, its (string) value is translated to the numeric
661 * id which is returned to the caller.
662 *
663 * returns:
664 * 0, on success
665 * -1, on failure
666 */
667int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
668{
669 int len;
670 const void *data;
671
672 /* Get OS name from property data */
673 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
674 if (data == NULL) {
675 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
676 *os = -1;
677 return -1;
678 }
679
680 /* Translate OS name to id */
681 *os = genimg_get_os_id(data);
682 return 0;
683}
684
685/**
686 * fit_image_get_arch - get arch id for a given component image node
687 * @fit: pointer to the FIT format image header
688 * @noffset: component image node offset
689 * @arch: pointer to the uint8_t, will hold arch numeric id
690 *
691 * fit_image_get_arch() finds arch property in a given component image node.
692 * If the property is found, its (string) value is translated to the numeric
693 * id which is returned to the caller.
694 *
695 * returns:
696 * 0, on success
697 * -1, on failure
698 */
699int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
700{
701 int len;
702 const void *data;
703
704 /* Get architecture name from property data */
705 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
706 if (data == NULL) {
707 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
708 *arch = -1;
709 return -1;
710 }
711
712 /* Translate architecture name to id */
713 *arch = genimg_get_arch_id(data);
714 return 0;
715}
716
717/**
718 * fit_image_get_type - get type id for a given component image node
719 * @fit: pointer to the FIT format image header
720 * @noffset: component image node offset
721 * @type: pointer to the uint8_t, will hold type numeric id
722 *
723 * fit_image_get_type() finds type property in a given component image node.
724 * If the property is found, its (string) value is translated to the numeric
725 * id which is returned to the caller.
726 *
727 * returns:
728 * 0, on success
729 * -1, on failure
730 */
731int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
732{
733 int len;
734 const void *data;
735
736 /* Get image type name from property data */
737 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
738 if (data == NULL) {
739 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
740 *type = -1;
741 return -1;
742 }
743
744 /* Translate image type name to id */
745 *type = genimg_get_type_id(data);
746 return 0;
747}
748
749/**
750 * fit_image_get_comp - get comp id for a given component image node
751 * @fit: pointer to the FIT format image header
752 * @noffset: component image node offset
753 * @comp: pointer to the uint8_t, will hold comp numeric id
754 *
755 * fit_image_get_comp() finds comp property in a given component image node.
756 * If the property is found, its (string) value is translated to the numeric
757 * id which is returned to the caller.
758 *
759 * returns:
760 * 0, on success
761 * -1, on failure
762 */
763int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
764{
765 int len;
766 const void *data;
767
768 /* Get compression name from property data */
769 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
770 if (data == NULL) {
771 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
772 *comp = -1;
773 return -1;
774 }
775
776 /* Translate compression name to id */
777 *comp = genimg_get_comp_id(data);
778 return 0;
779}
780
York Sun60047652016-02-29 15:48:40 -0800781static int fit_image_get_address(const void *fit, int noffset, char *name,
782 ulong *load)
783{
York Sunc1913cb2016-02-29 15:48:41 -0800784 int len, cell_len;
785 const fdt32_t *cell;
786 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800787
York Sunc1913cb2016-02-29 15:48:41 -0800788 cell = fdt_getprop(fit, noffset, name, &len);
789 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800790 fit_get_debug(fit, noffset, name, len);
791 return -1;
792 }
793
York Sunc1913cb2016-02-29 15:48:41 -0800794 if (len > sizeof(ulong)) {
795 printf("Unsupported %s address size\n", name);
796 return -1;
797 }
798
799 cell_len = len >> 2;
800 /* Use load64 to avoid compiling warning for 32-bit target */
801 while (cell_len--) {
802 load64 = (load64 << 32) | uimage_to_cpu(*cell);
803 cell++;
804 }
805 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800806
807 return 0;
808}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000809/**
810 * fit_image_get_load() - get load addr property for given component image node
811 * @fit: pointer to the FIT format image header
812 * @noffset: component image node offset
813 * @load: pointer to the uint32_t, will hold load address
814 *
815 * fit_image_get_load() finds load address property in a given component
816 * image node. If the property is found, its value is returned to the caller.
817 *
818 * returns:
819 * 0, on success
820 * -1, on failure
821 */
822int fit_image_get_load(const void *fit, int noffset, ulong *load)
823{
York Sun60047652016-02-29 15:48:40 -0800824 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000825}
826
827/**
828 * fit_image_get_entry() - get entry point address property
829 * @fit: pointer to the FIT format image header
830 * @noffset: component image node offset
831 * @entry: pointer to the uint32_t, will hold entry point address
832 *
833 * This gets the entry point address property for a given component image
834 * node.
835 *
836 * fit_image_get_entry() finds entry point address property in a given
837 * component image node. If the property is found, its value is returned
838 * to the caller.
839 *
840 * returns:
841 * 0, on success
842 * -1, on failure
843 */
844int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
845{
York Sun60047652016-02-29 15:48:40 -0800846 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000847}
848
849/**
850 * fit_image_get_data - get data property and its size for a given component image node
851 * @fit: pointer to the FIT format image header
852 * @noffset: component image node offset
853 * @data: double pointer to void, will hold data property's data address
854 * @size: pointer to size_t, will hold data property's data size
855 *
856 * fit_image_get_data() finds data property in a given component image node.
857 * If the property is found its data start address and size are returned to
858 * the caller.
859 *
860 * returns:
861 * 0, on success
862 * -1, on failure
863 */
864int fit_image_get_data(const void *fit, int noffset,
865 const void **data, size_t *size)
866{
867 int len;
868
869 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
870 if (*data == NULL) {
871 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
872 *size = 0;
873 return -1;
874 }
875
876 *size = len;
877 return 0;
878}
879
880/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200881 * Get 'data-offset' property from a given image node.
882 *
883 * @fit: pointer to the FIT image header
884 * @noffset: component image node offset
885 * @data_offset: holds the data-offset property
886 *
887 * returns:
888 * 0, on success
889 * -ENOENT if the property could not be found
890 */
891int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
892{
893 const fdt32_t *val;
894
895 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
896 if (!val)
897 return -ENOENT;
898
899 *data_offset = fdt32_to_cpu(*val);
900
901 return 0;
902}
903
904/**
Peng Fana1be94b2017-12-05 13:20:59 +0800905 * Get 'data-position' property from a given image node.
906 *
907 * @fit: pointer to the FIT image header
908 * @noffset: component image node offset
909 * @data_position: holds the data-position property
910 *
911 * returns:
912 * 0, on success
913 * -ENOENT if the property could not be found
914 */
915int fit_image_get_data_position(const void *fit, int noffset,
916 int *data_position)
917{
918 const fdt32_t *val;
919
920 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
921 if (!val)
922 return -ENOENT;
923
924 *data_position = fdt32_to_cpu(*val);
925
926 return 0;
927}
928
929/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200930 * Get 'data-size' property from a given image node.
931 *
932 * @fit: pointer to the FIT image header
933 * @noffset: component image node offset
934 * @data_size: holds the data-size property
935 *
936 * returns:
937 * 0, on success
938 * -ENOENT if the property could not be found
939 */
940int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
941{
942 const fdt32_t *val;
943
944 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
945 if (!val)
946 return -ENOENT;
947
948 *data_size = fdt32_to_cpu(*val);
949
950 return 0;
951}
952
953/**
Philippe Reynes4df35782019-12-18 18:25:42 +0100954 * Get 'data-size-unciphered' property from a given image node.
955 *
956 * @fit: pointer to the FIT image header
957 * @noffset: component image node offset
958 * @data_size: holds the data-size property
959 *
960 * returns:
961 * 0, on success
962 * -ENOENT if the property could not be found
963 */
964int fit_image_get_data_size_unciphered(const void *fit, int noffset,
965 size_t *data_size)
966{
967 const fdt32_t *val;
968
969 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
970 if (!val)
971 return -ENOENT;
972
973 *data_size = (size_t)fdt32_to_cpu(*val);
974
975 return 0;
976}
977
978/**
Kelvin Cheungc3c86382018-05-19 18:21:37 +0800979 * fit_image_get_data_and_size - get data and its size including
980 * both embedded and external data
981 * @fit: pointer to the FIT format image header
982 * @noffset: component image node offset
983 * @data: double pointer to void, will hold data property's data address
984 * @size: pointer to size_t, will hold data property's data size
985 *
986 * fit_image_get_data_and_size() finds data and its size including
987 * both embedded and external data. If the property is found
988 * its data start address and size are returned to the caller.
989 *
990 * returns:
991 * 0, on success
992 * otherwise, on failure
993 */
994int fit_image_get_data_and_size(const void *fit, int noffset,
995 const void **data, size_t *size)
996{
997 bool external_data = false;
998 int offset;
999 int len;
1000 int ret;
1001
1002 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1003 external_data = true;
1004 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1005 external_data = true;
1006 /*
1007 * For FIT with external data, figure out where
1008 * the external images start. This is the base
1009 * for the data-offset properties in each image.
1010 */
1011 offset += ((fdt_totalsize(fit) + 3) & ~3);
1012 }
1013
1014 if (external_data) {
1015 debug("External Data\n");
1016 ret = fit_image_get_data_size(fit, noffset, &len);
Heinrich Schuchardt18378042020-03-11 21:51:08 +01001017 if (!ret) {
1018 *data = fit + offset;
1019 *size = len;
1020 }
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001021 } else {
1022 ret = fit_image_get_data(fit, noffset, data, size);
1023 }
1024
1025 return ret;
1026}
1027
1028/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001029 * fit_image_hash_get_algo - get hash algorithm name
1030 * @fit: pointer to the FIT format image header
1031 * @noffset: hash node offset
1032 * @algo: double pointer to char, will hold pointer to the algorithm name
1033 *
1034 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1035 * If the property is found its data start address is returned to the caller.
1036 *
1037 * returns:
1038 * 0, on success
1039 * -1, on failure
1040 */
1041int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1042{
1043 int len;
1044
1045 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1046 if (*algo == NULL) {
1047 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1048 return -1;
1049 }
1050
1051 return 0;
1052}
1053
1054/**
1055 * fit_image_hash_get_value - get hash value and length
1056 * @fit: pointer to the FIT format image header
1057 * @noffset: hash node offset
1058 * @value: double pointer to uint8_t, will hold address of a hash value data
1059 * @value_len: pointer to an int, will hold hash data length
1060 *
1061 * fit_image_hash_get_value() finds hash value property in a given hash node.
1062 * If the property is found its data start address and size are returned to
1063 * the caller.
1064 *
1065 * returns:
1066 * 0, on success
1067 * -1, on failure
1068 */
1069int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1070 int *value_len)
1071{
1072 int len;
1073
1074 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1075 if (*value == NULL) {
1076 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1077 *value_len = 0;
1078 return -1;
1079 }
1080
1081 *value_len = len;
1082 return 0;
1083}
1084
Simon Glass53fbb7e2013-05-07 06:11:53 +00001085/**
1086 * fit_image_hash_get_ignore - get hash ignore flag
1087 * @fit: pointer to the FIT format image header
1088 * @noffset: hash node offset
1089 * @ignore: pointer to an int, will hold hash ignore flag
1090 *
1091 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1092 * If the property is found and non-zero, the hash algorithm is not verified by
1093 * u-boot automatically.
1094 *
1095 * returns:
1096 * 0, on ignore not found
1097 * value, on ignore found
1098 */
Simon Glassab9efc62013-05-07 06:11:58 +00001099static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001100{
1101 int len;
1102 int *value;
1103
1104 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1105 if (value == NULL || len != sizeof(int))
1106 *ignore = 0;
1107 else
1108 *ignore = *value;
1109
1110 return 0;
1111}
Simon Glass53fbb7e2013-05-07 06:11:53 +00001112
Philippe Reynes7298e422019-12-18 18:25:41 +01001113/**
1114 * fit_image_cipher_get_algo - get cipher algorithm name
1115 * @fit: pointer to the FIT format image header
1116 * @noffset: cipher node offset
1117 * @algo: double pointer to char, will hold pointer to the algorithm name
1118 *
1119 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1120 * cipher node. If the property is found its data start address is returned
1121 * to the caller.
1122 *
1123 * returns:
1124 * 0, on success
1125 * -1, on failure
1126 */
1127int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1128{
1129 int len;
1130
1131 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1132 if (!*algo) {
1133 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1134 return -1;
1135 }
1136
1137 return 0;
1138}
1139
Simon Glass7a80de42016-02-24 09:14:42 -07001140ulong fit_get_end(const void *fit)
1141{
1142 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1143}
1144
Simon Glass53fbb7e2013-05-07 06:11:53 +00001145/**
1146 * fit_set_timestamp - set node timestamp property
1147 * @fit: pointer to the FIT format image header
1148 * @noffset: node offset
1149 * @timestamp: timestamp value to be set
1150 *
1151 * fit_set_timestamp() attempts to set timestamp property in the requested
1152 * node and returns operation status to the caller.
1153 *
1154 * returns:
1155 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -06001156 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +00001157 */
1158int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1159{
1160 uint32_t t;
1161 int ret;
1162
1163 t = cpu_to_uimage(timestamp);
1164 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1165 sizeof(uint32_t));
1166 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -06001167 debug("Can't set '%s' property for '%s' node (%s)\n",
1168 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1169 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -06001170 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001171 }
1172
1173 return 0;
1174}
1175
1176/**
1177 * calculate_hash - calculate and return hash for provided input data
1178 * @data: pointer to the input data
1179 * @data_len: data length
1180 * @algo: requested hash algorithm
1181 * @value: pointer to the char, will hold hash value data (caller must
1182 * allocate enough free space)
1183 * value_len: length of the calculated hash
1184 *
1185 * calculate_hash() computes input data hash according to the requested
1186 * algorithm.
1187 * Resulting hash value is placed in caller provided 'value' buffer, length
1188 * of the calculated hash is returned via value_len pointer argument.
1189 *
1190 * returns:
1191 * 0, on success
1192 * -1, when algo is unsupported
1193 */
Simon Glass604f23d2013-05-07 06:11:54 +00001194int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001195 uint8_t *value, int *value_len)
1196{
Simon Glass87ebee32013-05-08 08:05:59 +00001197 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001198 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1199 CHUNKSZ_CRC32);
1200 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1201 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001202 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001203 sha1_csum_wd((unsigned char *)data, data_len,
1204 (unsigned char *)value, CHUNKSZ_SHA1);
1205 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001206 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1207 sha256_csum_wd((unsigned char *)data, data_len,
1208 (unsigned char *)value, CHUNKSZ_SHA256);
1209 *value_len = SHA256_SUM_LEN;
Reuben Dowled16b38f2020-04-16 17:36:52 +12001210 } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) {
1211 sha384_csum_wd((unsigned char *)data, data_len,
1212 (unsigned char *)value, CHUNKSZ_SHA384);
1213 *value_len = SHA384_SUM_LEN;
1214 } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) {
1215 sha512_csum_wd((unsigned char *)data, data_len,
1216 (unsigned char *)value, CHUNKSZ_SHA512);
1217 *value_len = SHA512_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001218 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001219 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1220 *value_len = 16;
1221 } else {
1222 debug("Unsupported hash alogrithm\n");
1223 return -1;
1224 }
1225 return 0;
1226}
1227
Simon Glassab9efc62013-05-07 06:11:58 +00001228static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1229 size_t size, char **err_msgp)
1230{
1231 uint8_t value[FIT_MAX_HASH_LEN];
1232 int value_len;
1233 char *algo;
1234 uint8_t *fit_value;
1235 int fit_value_len;
1236 int ignore;
1237
1238 *err_msgp = NULL;
1239
1240 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001241 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001242 return -1;
1243 }
1244 printf("%s", algo);
1245
1246 if (IMAGE_ENABLE_IGNORE) {
1247 fit_image_hash_get_ignore(fit, noffset, &ignore);
1248 if (ignore) {
1249 printf("-skipped ");
1250 return 0;
1251 }
1252 }
1253
1254 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1255 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001256 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001257 return -1;
1258 }
1259
1260 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001261 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001262 return -1;
1263 }
1264
1265 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001266 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001267 return -1;
1268 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001269 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001270 return -1;
1271 }
1272
1273 return 0;
1274}
1275
Jun Nie5c643db2018-02-27 16:55:58 +08001276int fit_image_verify_with_data(const void *fit, int image_noffset,
1277 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001278{
Simon Glass56518e72013-06-13 15:10:01 -07001279 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001280 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001281 int verify_all = 1;
1282 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001283
Simon Glass56518e72013-06-13 15:10:01 -07001284 /* Verify all required signatures */
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001285 if (FIT_IMAGE_ENABLE_VERIFY &&
Simon Glass56518e72013-06-13 15:10:01 -07001286 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1287 gd_fdt_blob(), &verify_all)) {
1288 err_msg = "Unable to verify required signature";
1289 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001290 }
1291
1292 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001293 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001294 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001295
Simon Glassab9efc62013-05-07 06:11:58 +00001296 /*
1297 * Check subnode name, must be equal to "hash".
1298 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001299 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001300 */
1301 if (!strncmp(name, FIT_HASH_NODENAME,
1302 strlen(FIT_HASH_NODENAME))) {
1303 if (fit_image_check_hash(fit, noffset, data, size,
1304 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001305 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001306 puts("+ ");
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001307 } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
Simon Glass56518e72013-06-13 15:10:01 -07001308 !strncmp(name, FIT_SIG_NODENAME,
1309 strlen(FIT_SIG_NODENAME))) {
1310 ret = fit_image_check_sig(fit, noffset, data,
1311 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001312
1313 /*
1314 * Show an indication on failure, but do not return
1315 * an error. Only keys marked 'required' can cause
1316 * an image validation failure. See the call to
1317 * fit_image_verify_required_sigs() above.
1318 */
1319 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001320 puts("- ");
1321 else
1322 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001323 }
1324 }
1325
1326 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001327 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001328 goto error;
1329 }
1330
1331 return 1;
1332
1333error:
Simon Glasse754da22013-05-07 06:11:59 +00001334 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001335 err_msg, fit_get_name(fit, noffset, NULL),
1336 fit_get_name(fit, image_noffset, NULL));
1337 return 0;
1338}
1339
1340/**
Jun Nie5c643db2018-02-27 16:55:58 +08001341 * fit_image_verify - verify data integrity
1342 * @fit: pointer to the FIT format image header
1343 * @image_noffset: component image node offset
1344 *
1345 * fit_image_verify() goes over component image hash nodes,
1346 * re-calculates each data hash and compares with the value stored in hash
1347 * node.
1348 *
1349 * returns:
1350 * 1, if all hashes are valid
1351 * 0, otherwise (or on error)
1352 */
1353int fit_image_verify(const void *fit, int image_noffset)
1354{
1355 const void *data;
1356 size_t size;
1357 int noffset = 0;
1358 char *err_msg = "";
1359
1360 /* Get image data and data length */
Kelvin Cheungc3c86382018-05-19 18:21:37 +08001361 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nie5c643db2018-02-27 16:55:58 +08001362 err_msg = "Can't get image data/size";
1363 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1364 err_msg, fit_get_name(fit, noffset, NULL),
1365 fit_get_name(fit, image_noffset, NULL));
1366 return 0;
1367 }
1368
1369 return fit_image_verify_with_data(fit, image_noffset, data, size);
1370}
1371
1372/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001373 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001374 * @fit: pointer to the FIT format image header
1375 *
Simon Glassb8da8362013-05-07 06:11:57 +00001376 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001377 * for every images checks if all it's hashes are valid.
1378 *
1379 * returns:
1380 * 1, if all hashes of all images are valid
1381 * 0, otherwise (or on error)
1382 */
Simon Glassb8da8362013-05-07 06:11:57 +00001383int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001384{
1385 int images_noffset;
1386 int noffset;
1387 int ndepth;
1388 int count;
1389
1390 /* Find images parent node offset */
1391 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1392 if (images_noffset < 0) {
1393 printf("Can't find images parent node '%s' (%s)\n",
1394 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1395 return 0;
1396 }
1397
1398 /* Process all image subnodes, check hashes for each */
1399 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1400 (ulong)fit);
1401 for (ndepth = 0, count = 0,
1402 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1403 (noffset >= 0) && (ndepth > 0);
1404 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1405 if (ndepth == 1) {
1406 /*
1407 * Direct child node of the images parent node,
1408 * i.e. component image node.
1409 */
Simon Glass73223f02016-02-22 22:55:43 -07001410 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001411 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001412 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001413
Simon Glassb8da8362013-05-07 06:11:57 +00001414 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001415 return 0;
1416 printf("\n");
1417 }
1418 }
1419 return 1;
1420}
1421
Philippe Reynes4df35782019-12-18 18:25:42 +01001422#ifdef CONFIG_FIT_CIPHER
1423static int fit_image_uncipher(const void *fit, int image_noffset,
1424 void **data, size_t *size)
1425{
1426 int cipher_noffset, ret;
1427 void *dst;
1428 size_t size_dst;
1429
1430 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1431 FIT_CIPHER_NODENAME);
1432 if (cipher_noffset < 0)
1433 return 0;
1434
1435 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1436 *data, *size, &dst, &size_dst);
1437 if (ret)
1438 goto out;
1439
1440 *data = dst;
1441 *size = size_dst;
1442
1443 out:
1444 return ret;
1445}
1446#endif /* CONFIG_FIT_CIPHER */
1447
Simon Glass53fbb7e2013-05-07 06:11:53 +00001448/**
1449 * fit_image_check_os - check whether image node is of a given os type
1450 * @fit: pointer to the FIT format image header
1451 * @noffset: component image node offset
1452 * @os: requested image os
1453 *
1454 * fit_image_check_os() reads image os property and compares its numeric
1455 * id with the requested os. Comparison result is returned to the caller.
1456 *
1457 * returns:
1458 * 1 if image is of given os type
1459 * 0 otherwise (or on error)
1460 */
1461int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1462{
1463 uint8_t image_os;
1464
1465 if (fit_image_get_os(fit, noffset, &image_os))
1466 return 0;
1467 return (os == image_os);
1468}
1469
1470/**
1471 * fit_image_check_arch - check whether image node is of a given arch
1472 * @fit: pointer to the FIT format image header
1473 * @noffset: component image node offset
1474 * @arch: requested imagearch
1475 *
1476 * fit_image_check_arch() reads image arch property and compares its numeric
1477 * id with the requested arch. Comparison result is returned to the caller.
1478 *
1479 * returns:
1480 * 1 if image is of given arch
1481 * 0 otherwise (or on error)
1482 */
1483int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1484{
1485 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001486 int aarch32_support = 0;
1487
1488#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1489 aarch32_support = 1;
1490#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001491
1492 if (fit_image_get_arch(fit, noffset, &image_arch))
1493 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001494 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001495 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1496 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1497 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001498}
1499
1500/**
1501 * fit_image_check_type - check whether image node is of a given type
1502 * @fit: pointer to the FIT format image header
1503 * @noffset: component image node offset
1504 * @type: requested image type
1505 *
1506 * fit_image_check_type() reads image type property and compares its numeric
1507 * id with the requested type. Comparison result is returned to the caller.
1508 *
1509 * returns:
1510 * 1 if image is of given type
1511 * 0 otherwise (or on error)
1512 */
1513int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1514{
1515 uint8_t image_type;
1516
1517 if (fit_image_get_type(fit, noffset, &image_type))
1518 return 0;
1519 return (type == image_type);
1520}
1521
1522/**
1523 * fit_image_check_comp - check whether image node uses given compression
1524 * @fit: pointer to the FIT format image header
1525 * @noffset: component image node offset
1526 * @comp: requested image compression type
1527 *
1528 * fit_image_check_comp() reads image compression property and compares its
1529 * numeric id with the requested compression type. Comparison result is
1530 * returned to the caller.
1531 *
1532 * returns:
1533 * 1 if image uses requested compression
1534 * 0 otherwise (or on error)
1535 */
1536int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1537{
1538 uint8_t image_comp;
1539
1540 if (fit_image_get_comp(fit, noffset, &image_comp))
1541 return 0;
1542 return (comp == image_comp);
1543}
1544
1545/**
1546 * fit_check_format - sanity check FIT image format
1547 * @fit: pointer to the FIT format image header
1548 *
1549 * fit_check_format() runs a basic sanity FIT image verification.
1550 * Routine checks for mandatory properties, nodes, etc.
1551 *
1552 * returns:
1553 * 1, on success
1554 * 0, on failure
1555 */
1556int fit_check_format(const void *fit)
1557{
1558 /* mandatory / node 'description' property */
1559 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1560 debug("Wrong FIT format: no description\n");
1561 return 0;
1562 }
1563
1564 if (IMAGE_ENABLE_TIMESTAMP) {
1565 /* mandatory / node 'timestamp' property */
1566 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1567 debug("Wrong FIT format: no timestamp\n");
1568 return 0;
1569 }
1570 }
1571
1572 /* mandatory subimages parent '/images' node */
1573 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1574 debug("Wrong FIT format: no images parent node\n");
1575 return 0;
1576 }
1577
1578 return 1;
1579}
1580
1581
1582/**
1583 * fit_conf_find_compat
1584 * @fit: pointer to the FIT format image header
1585 * @fdt: pointer to the device tree to compare against
1586 *
1587 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1588 * most compatible with the passed in device tree.
1589 *
1590 * Example:
1591 *
1592 * / o image-tree
1593 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001594 * | |-o fdt-1
1595 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001596 * |
1597 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001598 * |-o config-1
1599 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001600 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001601 * |-o config-2
1602 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001603 *
1604 * / o U-Boot fdt
1605 * |-compatible = "foo,bar", "bim,bam"
1606 *
1607 * / o kernel fdt1
1608 * |-compatible = "foo,bar",
1609 *
1610 * / o kernel fdt2
1611 * |-compatible = "bim,bam", "baz,biz"
1612 *
1613 * Configuration 1 would be picked because the first string in U-Boot's
1614 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1615 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1616 *
Julius Werner18cfa612019-07-24 19:37:56 -07001617 * As an optimization, the compatible property from the FDT's root node can be
1618 * copied into the configuration node in the FIT image. This is required to
1619 * match configurations with compressed FDTs.
1620 *
Simon Glass53fbb7e2013-05-07 06:11:53 +00001621 * returns:
1622 * offset to the configuration to use if one was found
1623 * -1 otherwise
1624 */
1625int fit_conf_find_compat(const void *fit, const void *fdt)
1626{
1627 int ndepth = 0;
1628 int noffset, confs_noffset, images_noffset;
1629 const void *fdt_compat;
1630 int fdt_compat_len;
1631 int best_match_offset = 0;
1632 int best_match_pos = 0;
1633
1634 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1635 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1636 if (confs_noffset < 0 || images_noffset < 0) {
1637 debug("Can't find configurations or images nodes.\n");
1638 return -1;
1639 }
1640
1641 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1642 if (!fdt_compat) {
1643 debug("Fdt for comparison has no \"compatible\" property.\n");
1644 return -1;
1645 }
1646
1647 /*
1648 * Loop over the configurations in the FIT image.
1649 */
1650 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1651 (noffset >= 0) && (ndepth > 0);
1652 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner18cfa612019-07-24 19:37:56 -07001653 const void *fdt;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001654 const char *kfdt_name;
Julius Werner18cfa612019-07-24 19:37:56 -07001655 int kfdt_noffset, compat_noffset;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001656 const char *cur_fdt_compat;
1657 int len;
Julius Werner18cfa612019-07-24 19:37:56 -07001658 size_t sz;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001659 int i;
1660
1661 if (ndepth > 1)
1662 continue;
1663
Julius Werner18cfa612019-07-24 19:37:56 -07001664 /* If there's a compat property in the config node, use that. */
1665 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1666 fdt = fit; /* search in FIT image */
1667 compat_noffset = noffset; /* search under config node */
1668 } else { /* Otherwise extract it from the kernel FDT. */
1669 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1670 if (!kfdt_name) {
1671 debug("No fdt property found.\n");
1672 continue;
1673 }
1674 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1675 kfdt_name);
1676 if (kfdt_noffset < 0) {
1677 debug("No image node named \"%s\" found.\n",
1678 kfdt_name);
1679 continue;
1680 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001681
Julius Werner18cfa612019-07-24 19:37:56 -07001682 if (!fit_image_check_comp(fit, kfdt_noffset,
1683 IH_COMP_NONE)) {
1684 debug("Can't extract compat from \"%s\" "
1685 "(compressed)\n", kfdt_name);
1686 continue;
1687 }
Julius Wernerb1307f82019-07-24 19:37:55 -07001688
Julius Werner18cfa612019-07-24 19:37:56 -07001689 /* search in this config's kernel FDT */
1690 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1691 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1692 continue;
1693 }
1694
1695 compat_noffset = 0; /* search kFDT under root node */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001696 }
1697
1698 len = fdt_compat_len;
1699 cur_fdt_compat = fdt_compat;
1700 /*
1701 * Look for a match for each U-Boot compatibility string in
Julius Werner18cfa612019-07-24 19:37:56 -07001702 * turn in the compat string property.
Simon Glass53fbb7e2013-05-07 06:11:53 +00001703 */
1704 for (i = 0; len > 0 &&
1705 (!best_match_offset || best_match_pos > i); i++) {
1706 int cur_len = strlen(cur_fdt_compat) + 1;
1707
Julius Werner18cfa612019-07-24 19:37:56 -07001708 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001709 cur_fdt_compat)) {
1710 best_match_offset = noffset;
1711 best_match_pos = i;
1712 break;
1713 }
1714 len -= cur_len;
1715 cur_fdt_compat += cur_len;
1716 }
1717 }
1718 if (!best_match_offset) {
1719 debug("No match found.\n");
1720 return -1;
1721 }
1722
1723 return best_match_offset;
1724}
1725
Simon Glass53fbb7e2013-05-07 06:11:53 +00001726int fit_conf_get_node(const void *fit, const char *conf_uname)
1727{
1728 int noffset, confs_noffset;
1729 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001730 const char *s;
1731 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001732
1733 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1734 if (confs_noffset < 0) {
1735 debug("Can't find configurations parent node '%s' (%s)\n",
1736 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1737 return confs_noffset;
1738 }
1739
1740 if (conf_uname == NULL) {
1741 /* get configuration unit name from the default property */
1742 debug("No configuration specified, trying default...\n");
1743 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1744 FIT_DEFAULT_PROP, &len);
1745 if (conf_uname == NULL) {
1746 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1747 len);
1748 return len;
1749 }
1750 debug("Found default configuration: '%s'\n", conf_uname);
1751 }
1752
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001753 s = strchr(conf_uname, '#');
1754 if (s) {
1755 len = s - conf_uname;
1756 conf_uname_copy = malloc(len + 1);
1757 if (!conf_uname_copy) {
1758 debug("Can't allocate uname copy: '%s'\n",
1759 conf_uname);
1760 return -ENOMEM;
1761 }
1762 memcpy(conf_uname_copy, conf_uname, len);
1763 conf_uname_copy[len] = '\0';
1764 conf_uname = conf_uname_copy;
1765 }
1766
Simon Glass53fbb7e2013-05-07 06:11:53 +00001767 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1768 if (noffset < 0) {
1769 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1770 conf_uname, fdt_strerror(noffset));
1771 }
1772
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001773 if (conf_uname_copy)
1774 free(conf_uname_copy);
1775
Simon Glass53fbb7e2013-05-07 06:11:53 +00001776 return noffset;
1777}
1778
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001779int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001780 const char *prop_name)
1781{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001782 return fdt_stringlist_count(fit, noffset, prop_name);
1783}
1784
1785int fit_conf_get_prop_node_index(const void *fit, int noffset,
1786 const char *prop_name, int index)
1787{
1788 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001789 int len;
1790
1791 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001792 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001793 if (uname == NULL)
1794 return len;
1795
1796 return fit_image_get_node(fit, uname);
1797}
1798
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001799int fit_conf_get_prop_node(const void *fit, int noffset,
1800 const char *prop_name)
1801{
1802 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1803}
1804
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001805static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001806{
1807 fit_image_print(fit, rd_noffset, " ");
1808
1809 if (verify) {
1810 puts(" Verifying Hash Integrity ... ");
1811 if (!fit_image_verify(fit, rd_noffset)) {
1812 puts("Bad Data Hash\n");
1813 return -EACCES;
1814 }
1815 puts("OK\n");
1816 }
1817
1818 return 0;
1819}
1820
Simon Glass782cfbb2013-05-16 13:53:21 +00001821int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1822 ulong addr)
1823{
1824 int cfg_noffset;
1825 void *fit_hdr;
1826 int noffset;
1827
1828 debug("* %s: using config '%s' from image at 0x%08lx\n",
1829 prop_name, images->fit_uname_cfg, addr);
1830
1831 /* Check whether configuration has this property defined */
1832 fit_hdr = map_sysmem(addr, 0);
1833 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1834 if (cfg_noffset < 0) {
1835 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001836 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001837 }
1838
1839 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1840 if (noffset < 0) {
1841 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001842 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001843 }
1844
1845 return noffset;
1846}
1847
Simon Glass126cc862014-06-12 07:24:47 -06001848/**
1849 * fit_get_image_type_property() - get property name for IH_TYPE_...
1850 *
1851 * @return the properly name where we expect to find the image in the
1852 * config node
1853 */
1854static const char *fit_get_image_type_property(int type)
1855{
1856 /*
1857 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001858 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001859 * anyway. So let's just keep it here.
1860 */
1861 switch (type) {
1862 case IH_TYPE_FLATDT:
1863 return FIT_FDT_PROP;
1864 case IH_TYPE_KERNEL:
1865 return FIT_KERNEL_PROP;
1866 case IH_TYPE_RAMDISK:
1867 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001868 case IH_TYPE_X86_SETUP:
1869 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001870 case IH_TYPE_LOADABLE:
1871 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001872 case IH_TYPE_FPGA:
1873 return FIT_FPGA_PROP;
Marek Vasut0298d202018-05-13 00:22:54 +02001874 case IH_TYPE_STANDALONE:
1875 return FIT_STANDALONE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001876 }
1877
1878 return "unknown";
1879}
1880
1881int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001882 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001883 int arch, int image_type, int bootstage_id,
1884 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1885{
1886 int cfg_noffset, noffset;
1887 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001888 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001889 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001890 const void *fit;
Julius Wernerb1307f82019-07-24 19:37:55 -07001891 void *buf;
1892 void *loadbuf;
Simon Glass782cfbb2013-05-16 13:53:21 +00001893 size_t size;
1894 int type_ok, os_ok;
Julius Wernerb1307f82019-07-24 19:37:55 -07001895 ulong load, load_end, data, len;
1896 uint8_t os, comp;
Alison Wangec6617c2016-11-10 10:49:03 +08001897#ifndef USE_HOSTCC
1898 uint8_t os_arch;
1899#endif
Simon Glass126cc862014-06-12 07:24:47 -06001900 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001901 int ret;
1902
1903 fit = map_sysmem(addr, 0);
1904 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001905 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001906 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001907 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001908 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1909
1910 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1911 if (!fit_check_format(fit)) {
1912 printf("Bad FIT %s image format!\n", prop_name);
1913 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1914 return -ENOEXEC;
1915 }
1916 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1917 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001918 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001919 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1920 noffset = fit_image_get_node(fit, fit_uname);
1921 } else {
1922 /*
1923 * no image node unit name, try to get config
1924 * node first. If config unit node name is NULL
1925 * fit_conf_get_node() will try to find default config node
1926 */
1927 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1928 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1929 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1930 } else {
1931 cfg_noffset = fit_conf_get_node(fit,
1932 fit_uname_config);
1933 }
1934 if (cfg_noffset < 0) {
1935 puts("Could not find configuration node\n");
1936 bootstage_error(bootstage_id +
1937 BOOTSTAGE_SUB_NO_UNIT_NAME);
1938 return -ENOENT;
1939 }
Marek Vasut078e5582018-05-31 17:59:07 +02001940
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001941 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1942 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut078e5582018-05-31 17:59:07 +02001943 /* Remember this config */
1944 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001945 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut078e5582018-05-31 17:59:07 +02001946
AKASHI Takahirob983cc22020-02-21 15:12:55 +09001947 if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
Marek Vasut078e5582018-05-31 17:59:07 +02001948 puts(" Verifying Hash Integrity ... ");
1949 if (fit_config_verify(fit, cfg_noffset)) {
1950 puts("Bad Data Hash\n");
1951 bootstage_error(bootstage_id +
1952 BOOTSTAGE_SUB_HASH);
1953 return -EACCES;
Simon Glass782cfbb2013-05-16 13:53:21 +00001954 }
Marek Vasut078e5582018-05-31 17:59:07 +02001955 puts("OK\n");
Simon Glass782cfbb2013-05-16 13:53:21 +00001956 }
1957
Marek Vasut078e5582018-05-31 17:59:07 +02001958 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1959
Simon Glass782cfbb2013-05-16 13:53:21 +00001960 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1961 prop_name);
1962 fit_uname = fit_get_name(fit, noffset, NULL);
1963 }
1964 if (noffset < 0) {
Simon Glass382cf622020-03-18 11:43:56 -06001965 printf("Could not find subimage node type '%s'\n", prop_name);
Simon Glass782cfbb2013-05-16 13:53:21 +00001966 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1967 return -ENOENT;
1968 }
1969
1970 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1971
1972 ret = fit_image_select(fit, noffset, images->verify);
1973 if (ret) {
1974 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1975 return ret;
1976 }
1977
1978 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001979#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001980 if (!fit_image_check_target_arch(fit, noffset)) {
1981 puts("Unsupported Architecture\n");
1982 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1983 return -ENOEXEC;
1984 }
Simon Glassce1400f2014-06-12 07:24:53 -06001985#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001986
1987#ifndef USE_HOSTCC
1988 fit_image_get_arch(fit, noffset, &os_arch);
1989 images->os.arch = os_arch;
1990#endif
1991
Simon Glass782cfbb2013-05-16 13:53:21 +00001992 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1993 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001994 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1995 (image_type == IH_TYPE_KERNEL &&
1996 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001997
Andreas Bießmann950fe262016-08-14 20:31:24 +02001998 os_ok = image_type == IH_TYPE_FLATDT ||
1999 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01002000 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02002001 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Cristian Ciocalteaa031b032019-12-24 18:05:38 +02002002 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
Lihua Zhao0df27d62020-03-18 07:32:07 -07002003 fit_image_check_os(fit, noffset, IH_OS_EFI) ||
2004 fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
Karl Apsite84a07db2015-05-21 09:52:48 -04002005
2006 /*
2007 * If either of the checks fail, we should report an error, but
2008 * if the image type is coming from the "loadables" field, we
2009 * don't care what it is
2010 */
2011 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01002012 fit_image_get_os(fit, noffset, &os);
2013 printf("No %s %s %s Image\n",
2014 genimg_get_os_name(os),
2015 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00002016 genimg_get_type_name(image_type));
2017 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2018 return -EIO;
2019 }
2020
2021 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2022
2023 /* get image data address and length */
Julius Wernerb1307f82019-07-24 19:37:55 -07002024 if (fit_image_get_data_and_size(fit, noffset,
2025 (const void **)&buf, &size)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002026 printf("Could not find %s subimage data!\n", prop_name);
2027 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07002028 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00002029 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002030
Philippe Reynes4df35782019-12-18 18:25:42 +01002031#ifdef CONFIG_FIT_CIPHER
2032 /* Decrypt data before uncompress/move */
2033 if (IMAGE_ENABLE_DECRYPT) {
2034 puts(" Decrypting Data ... ");
2035 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2036 puts("Error\n");
2037 return -EACCES;
2038 }
2039 puts("OK\n");
2040 }
2041#endif
2042
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002043#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2044 /* perform any post-processing on the image data */
Julius Wernerb1307f82019-07-24 19:37:55 -07002045 board_fit_image_post_process(&buf, &size);
Andrew F. Davis44402fe2016-11-21 14:37:09 -06002046#endif
2047
Simon Glass782cfbb2013-05-16 13:53:21 +00002048 len = (ulong)size;
2049
Simon Glass782cfbb2013-05-16 13:53:21 +00002050 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2051
Julius Wernerb1307f82019-07-24 19:37:55 -07002052 data = map_to_sysmem(buf);
2053 load = data;
Simon Glass782cfbb2013-05-16 13:53:21 +00002054 if (load_op == FIT_LOAD_IGNORED) {
2055 /* Don't load */
2056 } else if (fit_image_get_load(fit, noffset, &load)) {
2057 if (load_op == FIT_LOAD_REQUIRED) {
2058 printf("Can't get %s subimage load address!\n",
2059 prop_name);
2060 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2061 return -EBADF;
2062 }
Simon Glassfe20a812014-08-22 14:26:43 -06002063 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00002064 ulong image_start, image_end;
Simon Glass782cfbb2013-05-16 13:53:21 +00002065
2066 /*
2067 * move image data to the load address,
2068 * make sure we don't overwrite initial image
2069 */
2070 image_start = addr;
2071 image_end = addr + fit_get_size(fit);
2072
2073 load_end = load + len;
2074 if (image_type != IH_TYPE_KERNEL &&
2075 load < image_end && load_end > image_start) {
2076 printf("Error: %s overwritten\n", prop_name);
2077 return -EXDEV;
2078 }
2079
2080 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2081 prop_name, data, load);
Julius Wernerb1307f82019-07-24 19:37:55 -07002082 } else {
2083 load = data; /* No load address specified */
Simon Glass782cfbb2013-05-16 13:53:21 +00002084 }
Julius Wernerb1307f82019-07-24 19:37:55 -07002085
2086 comp = IH_COMP_NONE;
2087 loadbuf = buf;
2088 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Wernerbddd9852019-08-02 15:52:28 -07002089 if (!fit_image_get_comp(fit, noffset, &comp) &&
2090 comp != IH_COMP_NONE &&
2091 !(image_type == IH_TYPE_KERNEL ||
2092 image_type == IH_TYPE_KERNEL_NOLOAD ||
2093 image_type == IH_TYPE_RAMDISK)) {
Julius Wernerb1307f82019-07-24 19:37:55 -07002094 ulong max_decomp_len = len * 20;
2095 if (load == data) {
2096 loadbuf = malloc(max_decomp_len);
2097 load = map_to_sysmem(loadbuf);
2098 } else {
2099 loadbuf = map_sysmem(load, max_decomp_len);
2100 }
2101 if (image_decomp(comp, load, data, image_type,
2102 loadbuf, buf, len, max_decomp_len, &load_end)) {
2103 printf("Error decompressing %s\n", prop_name);
2104
2105 return -ENOEXEC;
2106 }
2107 len = load_end - load;
2108 } else if (load != data) {
2109 loadbuf = map_sysmem(load, len);
2110 memcpy(loadbuf, buf, len);
2111 }
2112
Julius Wernerbddd9852019-08-02 15:52:28 -07002113 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2114 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2115 " please fix your .its file!\n");
2116
Julius Wernerb1307f82019-07-24 19:37:55 -07002117 /* verify that image data is a proper FDT blob */
2118 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2119 puts("Subimage data is not a FDT");
2120 return -ENOEXEC;
2121 }
2122
Simon Glass782cfbb2013-05-16 13:53:21 +00002123 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2124
Julius Wernerb1307f82019-07-24 19:37:55 -07002125 *datap = load;
Simon Glass782cfbb2013-05-16 13:53:21 +00002126 *lenp = len;
2127 if (fit_unamep)
2128 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07002129 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03002130 *fit_uname_configp = (char *)(fit_uname_config ? :
2131 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00002132
2133 return noffset;
2134}
Simon Glass90268b82014-10-19 21:11:24 -06002135
2136int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2137 ulong *setup_start, ulong *setup_len)
2138{
2139 int noffset;
2140 ulong addr;
2141 ulong len;
2142 int ret;
2143
2144 addr = map_to_sysmem(images->fit_hdr_os);
2145 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2146 if (noffset < 0)
2147 return noffset;
2148
2149 ret = fit_image_load(images, addr, NULL, NULL, arch,
2150 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2151 FIT_LOAD_REQUIRED, setup_start, &len);
2152
2153 return ret;
2154}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002155
2156#ifndef USE_HOSTCC
2157int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2158 const char **fit_unamep, const char **fit_uname_configp,
2159 int arch, ulong *datap, ulong *lenp)
2160{
2161 int fdt_noffset, cfg_noffset, count;
2162 const void *fit;
2163 const char *fit_uname = NULL;
2164 const char *fit_uname_config = NULL;
2165 char *fit_uname_config_copy = NULL;
2166 char *next_config = NULL;
2167 ulong load, len;
2168#ifdef CONFIG_OF_LIBFDT_OVERLAY
2169 ulong image_start, image_end;
2170 ulong ovload, ovlen;
2171 const char *uconfig;
2172 const char *uname;
2173 void *base, *ov;
2174 int i, err, noffset, ov_noffset;
2175#endif
2176
2177 fit_uname = fit_unamep ? *fit_unamep : NULL;
2178
2179 if (fit_uname_configp && *fit_uname_configp) {
2180 fit_uname_config_copy = strdup(*fit_uname_configp);
2181 if (!fit_uname_config_copy)
2182 return -ENOMEM;
2183
2184 next_config = strchr(fit_uname_config_copy, '#');
2185 if (next_config)
2186 *next_config++ = '\0';
2187 if (next_config - 1 > fit_uname_config_copy)
2188 fit_uname_config = fit_uname_config_copy;
2189 }
2190
2191 fdt_noffset = fit_image_load(images,
2192 addr, &fit_uname, &fit_uname_config,
2193 arch, IH_TYPE_FLATDT,
2194 BOOTSTAGE_ID_FIT_FDT_START,
2195 FIT_LOAD_OPTIONAL, &load, &len);
2196
2197 if (fdt_noffset < 0)
2198 goto out;
2199
2200 debug("fit_uname=%s, fit_uname_config=%s\n",
2201 fit_uname ? fit_uname : "<NULL>",
2202 fit_uname_config ? fit_uname_config : "<NULL>");
2203
2204 fit = map_sysmem(addr, 0);
2205
2206 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2207
2208 /* single blob, or error just return as well */
2209 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2210 if (count <= 1 && !next_config)
2211 goto out;
2212
2213 /* we need to apply overlays */
2214
2215#ifdef CONFIG_OF_LIBFDT_OVERLAY
2216 image_start = addr;
2217 image_end = addr + fit_get_size(fit);
2218 /* verify that relocation took place by load address not being in fit */
2219 if (load >= image_start && load < image_end) {
2220 /* check is simplified; fit load checks for overlaps */
2221 printf("Overlayed FDT requires relocation\n");
2222 fdt_noffset = -EBADF;
2223 goto out;
2224 }
2225
2226 base = map_sysmem(load, len);
2227
2228 /* apply extra configs in FIT first, followed by args */
2229 for (i = 1; ; i++) {
2230 if (i < count) {
2231 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2232 FIT_FDT_PROP, i);
2233 uname = fit_get_name(fit, noffset, NULL);
2234 uconfig = NULL;
2235 } else {
2236 if (!next_config)
2237 break;
2238 uconfig = next_config;
2239 next_config = strchr(next_config, '#');
2240 if (next_config)
2241 *next_config++ = '\0';
2242 uname = NULL;
Peter Ujfalusi35c75272019-03-06 15:52:27 +02002243
2244 /*
2245 * fit_image_load() would load the first FDT from the
2246 * extra config only when uconfig is specified.
2247 * Check if the extra config contains multiple FDTs and
2248 * if so, load them.
2249 */
2250 cfg_noffset = fit_conf_get_node(fit, uconfig);
2251
2252 i = 0;
2253 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2254 FIT_FDT_PROP);
Pantelis Antoniou169043d2017-09-04 23:12:16 +03002255 }
2256
2257 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2258
2259 ov_noffset = fit_image_load(images,
2260 addr, &uname, &uconfig,
2261 arch, IH_TYPE_FLATDT,
2262 BOOTSTAGE_ID_FIT_FDT_START,
2263 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2264 if (ov_noffset < 0) {
2265 printf("load of %s failed\n", uname);
2266 continue;
2267 }
2268 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2269 uname, ovload, ovlen);
2270 ov = map_sysmem(ovload, ovlen);
2271
2272 base = map_sysmem(load, len + ovlen);
2273 err = fdt_open_into(base, base, len + ovlen);
2274 if (err < 0) {
2275 printf("failed on fdt_open_into\n");
2276 fdt_noffset = err;
2277 goto out;
2278 }
2279 /* the verbose method prints out messages on error */
2280 err = fdt_overlay_apply_verbose(base, ov);
2281 if (err < 0) {
2282 fdt_noffset = err;
2283 goto out;
2284 }
2285 fdt_pack(base);
2286 len = fdt_totalsize(base);
2287 }
2288#else
2289 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2290 fdt_noffset = -EBADF;
2291#endif
2292
2293out:
2294 if (datap)
2295 *datap = load;
2296 if (lenp)
2297 *lenp = len;
2298 if (fit_unamep)
2299 *fit_unamep = fit_uname;
2300 if (fit_uname_configp)
2301 *fit_uname_configp = fit_uname_config;
2302
2303 if (fit_uname_config_copy)
2304 free(fit_uname_config_copy);
2305 return fdt_noffset;
2306}
2307#endif