blob: f3bb00c98a55d966c3e68869bd180db8a6fefcc9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassda0af362013-05-07 06:11:53 +00002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassda0af362013-05-07 06:11:53 +00009 */
10
11#ifdef USE_HOSTCC
12#include "mkimage.h"
Simon Glassda0af362013-05-07 06:11:53 +000013#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Simon Glassda0af362013-05-07 06:11:53 +000015#else
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050016#include <linux/compiler.h>
York Sun48cd6ab2016-11-23 09:25:09 -080017#include <linux/kconfig.h>
Simon Glassda0af362013-05-07 06:11:53 +000018#include <common.h>
Simon Glass384d86d2013-05-16 13:53:21 +000019#include <errno.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass384d86d2013-05-16 13:53:21 +000021#include <asm/io.h>
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +030022#include <malloc.h>
Simon Glass384d86d2013-05-16 13:53:21 +000023DECLARE_GLOBAL_DATA_PTR;
Simon Glassda0af362013-05-07 06:11:53 +000024#endif /* !USE_HOSTCC*/
25
Julius Werner97b09cd2019-07-24 19:37:55 -070026#include <bootm.h>
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050027#include <image.h>
Simon Glassda0af362013-05-07 06:11:53 +000028#include <bootstage.h>
Simon Glassda0af362013-05-07 06:11:53 +000029#include <u-boot/crc.h>
30#include <u-boot/md5.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020031#include <u-boot/sha1.h>
32#include <u-boot/sha256.h>
Simon Glassda0af362013-05-07 06:11:53 +000033
34/*****************************************************************************/
35/* New uImage format routines */
36/*****************************************************************************/
37#ifndef USE_HOSTCC
38static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
39 ulong *addr, const char **name)
40{
41 const char *sep;
42
43 *addr = addr_curr;
44 *name = NULL;
45
46 sep = strchr(spec, sepc);
47 if (sep) {
48 if (sep - spec > 0)
49 *addr = simple_strtoul(spec, NULL, 16);
50
51 *name = sep + 1;
52 return 1;
53 }
54
55 return 0;
56}
57
58/**
59 * fit_parse_conf - parse FIT configuration spec
60 * @spec: input string, containing configuration spec
61 * @add_curr: current image address (to be used as a possible default)
62 * @addr: pointer to a ulong variable, will hold FIT image address of a given
63 * configuration
64 * @conf_name double pointer to a char, will hold pointer to a configuration
65 * unit name
66 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090067 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glassda0af362013-05-07 06:11:53 +000068 * where <addr> is a FIT image address that contains configuration
69 * with a <conf> unit name.
70 *
71 * Address part is optional, and if omitted default add_curr will
72 * be used instead.
73 *
74 * returns:
75 * 1 if spec is a valid configuration string,
76 * addr and conf_name are set accordingly
77 * 0 otherwise
78 */
79int fit_parse_conf(const char *spec, ulong addr_curr,
80 ulong *addr, const char **conf_name)
81{
82 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
83}
84
85/**
86 * fit_parse_subimage - parse FIT subimage spec
87 * @spec: input string, containing subimage spec
88 * @add_curr: current image address (to be used as a possible default)
89 * @addr: pointer to a ulong variable, will hold FIT image address of a given
90 * subimage
91 * @image_name: double pointer to a char, will hold pointer to a subimage name
92 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090093 * fit_parse_subimage() expects subimage spec in the form of
Simon Glassda0af362013-05-07 06:11:53 +000094 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
95 * subimage with a <subimg> unit name.
96 *
97 * Address part is optional, and if omitted default add_curr will
98 * be used instead.
99 *
100 * returns:
101 * 1 if spec is a valid subimage string,
102 * addr and image_name are set accordingly
103 * 0 otherwise
104 */
105int fit_parse_subimage(const char *spec, ulong addr_curr,
106 ulong *addr, const char **image_name)
107{
108 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
109}
110#endif /* !USE_HOSTCC */
111
112static void fit_get_debug(const void *fit, int noffset,
113 char *prop_name, int err)
114{
115 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
116 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
117 fdt_strerror(err));
118}
119
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200120/**
121 * fit_get_subimage_count - get component (sub-image) count
122 * @fit: pointer to the FIT format image header
123 * @images_noffset: offset of images node
124 *
125 * returns:
126 * number of image components
127 */
128int fit_get_subimage_count(const void *fit, int images_noffset)
129{
130 int noffset;
131 int ndepth;
132 int count = 0;
133
134 /* Process its subnodes, print out component images details */
135 for (ndepth = 0, count = 0,
136 noffset = fdt_next_node(fit, images_noffset, &ndepth);
137 (noffset >= 0) && (ndepth > 0);
138 noffset = fdt_next_node(fit, noffset, &ndepth)) {
139 if (ndepth == 1) {
140 count++;
141 }
142 }
143
144 return count;
145}
146
Marek Vasut9f2e0eb2018-05-13 00:22:52 +0200147#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
Simon Glassda0af362013-05-07 06:11:53 +0000148/**
Tom Rini7d77b662018-05-08 14:34:05 -0400149 * fit_image_print_data() - prints out the hash node details
150 * @fit: pointer to the FIT format image header
151 * @noffset: offset of the hash node
152 * @p: pointer to prefix string
153 * @type: Type of information to print ("hash" or "sign")
154 *
155 * fit_image_print_data() lists properties for the processed hash node
156 *
157 * This function avoid using puts() since it prints a newline on the host
158 * but does not in U-Boot.
159 *
160 * returns:
161 * no returned results
162 */
163static void fit_image_print_data(const void *fit, int noffset, const char *p,
164 const char *type)
165{
166 const char *keyname;
167 uint8_t *value;
168 int value_len;
169 char *algo;
Philippe Reynes12468352018-11-14 13:51:00 +0100170 const char *padding;
Tom Rini7d77b662018-05-08 14:34:05 -0400171 int required;
172 int ret, i;
173
174 debug("%s %s node: '%s'\n", p, type,
175 fit_get_name(fit, noffset, NULL));
176 printf("%s %s algo: ", p, type);
177 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
178 printf("invalid/unsupported\n");
179 return;
180 }
181 printf("%s", algo);
182 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
183 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
184 if (keyname)
185 printf(":%s", keyname);
186 if (required)
187 printf(" (required)");
188 printf("\n");
189
Philippe Reynes12468352018-11-14 13:51:00 +0100190 padding = fdt_getprop(fit, noffset, "padding", NULL);
191 if (padding)
192 printf("%s %s padding: %s\n", p, type, padding);
193
Tom Rini7d77b662018-05-08 14:34:05 -0400194 ret = fit_image_hash_get_value(fit, noffset, &value,
195 &value_len);
196 printf("%s %s value: ", p, type);
197 if (ret) {
198 printf("unavailable\n");
199 } else {
200 for (i = 0; i < value_len; i++)
201 printf("%02x", value[i]);
202 printf("\n");
203 }
204
205 debug("%s %s len: %d\n", p, type, value_len);
206
207 /* Signatures have a time stamp */
208 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
209 time_t timestamp;
210
211 printf("%s Timestamp: ", p);
212 if (fit_get_timestamp(fit, noffset, &timestamp))
213 printf("unavailable\n");
214 else
215 genimg_print_time(timestamp);
216 }
217}
218
219/**
220 * fit_image_print_verification_data() - prints out the hash/signature details
221 * @fit: pointer to the FIT format image header
222 * @noffset: offset of the hash or signature node
223 * @p: pointer to prefix string
224 *
225 * This lists properties for the processed hash node
226 *
227 * returns:
228 * no returned results
229 */
230static void fit_image_print_verification_data(const void *fit, int noffset,
231 const char *p)
232{
233 const char *name;
234
235 /*
236 * Check subnode name, must be equal to "hash" or "signature".
237 * Multiple hash/signature nodes require unique unit node
238 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
239 */
240 name = fit_get_name(fit, noffset, NULL);
241 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
242 fit_image_print_data(fit, noffset, p, "Hash");
243 } else if (!strncmp(name, FIT_SIG_NODENAME,
244 strlen(FIT_SIG_NODENAME))) {
245 fit_image_print_data(fit, noffset, p, "Sign");
246 }
247}
248
249/**
250 * fit_conf_print - prints out the FIT configuration details
251 * @fit: pointer to the FIT format image header
252 * @noffset: offset of the configuration node
253 * @p: pointer to prefix string
254 *
255 * fit_conf_print() lists all mandatory properties for the processed
256 * configuration node.
257 *
258 * returns:
259 * no returned results
260 */
261static void fit_conf_print(const void *fit, int noffset, const char *p)
262{
263 char *desc;
264 const char *uname;
265 int ret;
266 int fdt_index, loadables_index;
267 int ndepth;
268
269 /* Mandatory properties */
270 ret = fit_get_desc(fit, noffset, &desc);
271 printf("%s Description: ", p);
272 if (ret)
273 printf("unavailable\n");
274 else
275 printf("%s\n", desc);
276
277 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
278 printf("%s Kernel: ", p);
279 if (!uname)
280 printf("unavailable\n");
281 else
282 printf("%s\n", uname);
283
284 /* Optional properties */
285 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
286 if (uname)
287 printf("%s Init Ramdisk: %s\n", p, uname);
288
289 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
290 if (uname)
291 printf("%s Firmware: %s\n", p, uname);
292
293 for (fdt_index = 0;
294 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
295 fdt_index, NULL), uname;
296 fdt_index++) {
297 if (fdt_index == 0)
298 printf("%s FDT: ", p);
299 else
300 printf("%s ", p);
301 printf("%s\n", uname);
302 }
303
304 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
305 if (uname)
306 printf("%s FPGA: %s\n", p, uname);
307
308 /* Print out all of the specified loadables */
309 for (loadables_index = 0;
310 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
311 loadables_index, NULL), uname;
312 loadables_index++) {
313 if (loadables_index == 0) {
314 printf("%s Loadables: ", p);
315 } else {
316 printf("%s ", p);
317 }
318 printf("%s\n", uname);
319 }
320
321 /* Process all hash subnodes of the component configuration node */
322 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
323 (noffset >= 0) && (ndepth > 0);
324 noffset = fdt_next_node(fit, noffset, &ndepth)) {
325 if (ndepth == 1) {
326 /* Direct child node of the component configuration node */
327 fit_image_print_verification_data(fit, noffset, p);
328 }
329 }
330}
331
332/**
Simon Glassda0af362013-05-07 06:11:53 +0000333 * fit_print_contents - prints out the contents of the FIT format image
334 * @fit: pointer to the FIT format image header
335 * @p: pointer to prefix string
336 *
337 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500338 * The routine prints out FIT image properties (root node level) followed by
Simon Glassda0af362013-05-07 06:11:53 +0000339 * the details of each component image.
340 *
341 * returns:
342 * no returned results
343 */
344void fit_print_contents(const void *fit)
345{
346 char *desc;
347 char *uname;
348 int images_noffset;
349 int confs_noffset;
350 int noffset;
351 int ndepth;
352 int count = 0;
353 int ret;
354 const char *p;
355 time_t timestamp;
356
Simon Glass1030f162013-05-08 08:05:58 +0000357 /* Indent string is defined in header image.h */
358 p = IMAGE_INDENT_STRING;
Simon Glassda0af362013-05-07 06:11:53 +0000359
360 /* Root node properties */
361 ret = fit_get_desc(fit, 0, &desc);
362 printf("%sFIT description: ", p);
363 if (ret)
364 printf("unavailable\n");
365 else
366 printf("%s\n", desc);
367
368 if (IMAGE_ENABLE_TIMESTAMP) {
369 ret = fit_get_timestamp(fit, 0, &timestamp);
370 printf("%sCreated: ", p);
371 if (ret)
372 printf("unavailable\n");
373 else
374 genimg_print_time(timestamp);
375 }
376
377 /* Find images parent node offset */
378 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
379 if (images_noffset < 0) {
380 printf("Can't find images parent node '%s' (%s)\n",
381 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
382 return;
383 }
384
385 /* Process its subnodes, print out component images details */
386 for (ndepth = 0, count = 0,
387 noffset = fdt_next_node(fit, images_noffset, &ndepth);
388 (noffset >= 0) && (ndepth > 0);
389 noffset = fdt_next_node(fit, noffset, &ndepth)) {
390 if (ndepth == 1) {
391 /*
392 * Direct child node of the images parent node,
393 * i.e. component image node.
394 */
395 printf("%s Image %u (%s)\n", p, count++,
396 fit_get_name(fit, noffset, NULL));
397
398 fit_image_print(fit, noffset, p);
399 }
400 }
401
402 /* Find configurations parent node offset */
403 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
404 if (confs_noffset < 0) {
405 debug("Can't get configurations parent node '%s' (%s)\n",
406 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
407 return;
408 }
409
410 /* get default configuration unit name from default property */
411 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
412 if (uname)
413 printf("%s Default Configuration: '%s'\n", p, uname);
414
415 /* Process its subnodes, print out configurations details */
416 for (ndepth = 0, count = 0,
417 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
418 (noffset >= 0) && (ndepth > 0);
419 noffset = fdt_next_node(fit, noffset, &ndepth)) {
420 if (ndepth == 1) {
421 /*
422 * Direct child node of the configurations parent node,
423 * i.e. configuration node.
424 */
425 printf("%s Configuration %u (%s)\n", p, count++,
426 fit_get_name(fit, noffset, NULL));
427
428 fit_conf_print(fit, noffset, p);
429 }
430 }
431}
432
433/**
434 * fit_image_print - prints out the FIT component image details
435 * @fit: pointer to the FIT format image header
436 * @image_noffset: offset of the component image node
437 * @p: pointer to prefix string
438 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500439 * fit_image_print() lists all mandatory properties for the processed component
Simon Glassda0af362013-05-07 06:11:53 +0000440 * image. If present, hash nodes are printed out as well. Load
441 * address for images of type firmware is also printed out. Since the load
442 * address is not mandatory for firmware images, it will be output as
443 * "unavailable" when not present.
444 *
445 * returns:
446 * no returned results
447 */
448void fit_image_print(const void *fit, int image_noffset, const char *p)
449{
450 char *desc;
451 uint8_t type, arch, os, comp;
452 size_t size;
453 ulong load, entry;
454 const void *data;
455 int noffset;
456 int ndepth;
457 int ret;
458
459 /* Mandatory properties */
460 ret = fit_get_desc(fit, image_noffset, &desc);
461 printf("%s Description: ", p);
462 if (ret)
463 printf("unavailable\n");
464 else
465 printf("%s\n", desc);
466
Simon Glass749a6e72013-07-16 20:10:01 -0700467 if (IMAGE_ENABLE_TIMESTAMP) {
468 time_t timestamp;
469
470 ret = fit_get_timestamp(fit, 0, &timestamp);
471 printf("%s Created: ", p);
472 if (ret)
473 printf("unavailable\n");
474 else
475 genimg_print_time(timestamp);
476 }
477
Simon Glassda0af362013-05-07 06:11:53 +0000478 fit_image_get_type(fit, image_noffset, &type);
479 printf("%s Type: %s\n", p, genimg_get_type_name(type));
480
481 fit_image_get_comp(fit, image_noffset, &comp);
482 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
483
Kelvin Cheung186cc992018-05-19 18:21:37 +0800484 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glassda0af362013-05-07 06:11:53 +0000485
486#ifndef USE_HOSTCC
487 printf("%s Data Start: ", p);
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000488 if (ret) {
Simon Glassda0af362013-05-07 06:11:53 +0000489 printf("unavailable\n");
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000490 } else {
491 void *vdata = (void *)data;
492
493 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
494 }
Simon Glassda0af362013-05-07 06:11:53 +0000495#endif
496
497 printf("%s Data Size: ", p);
498 if (ret)
499 printf("unavailable\n");
500 else
501 genimg_print_size(size);
502
503 /* Remaining, type dependent properties */
504 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
505 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
506 (type == IH_TYPE_FLATDT)) {
507 fit_image_get_arch(fit, image_noffset, &arch);
508 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
509 }
510
Michal Simek55f698f2018-03-26 16:31:27 +0200511 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
512 (type == IH_TYPE_FIRMWARE)) {
Simon Glassda0af362013-05-07 06:11:53 +0000513 fit_image_get_os(fit, image_noffset, &os);
514 printf("%s OS: %s\n", p, genimg_get_os_name(os));
515 }
516
517 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simekebae78b2016-05-17 14:03:50 +0200518 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
519 (type == IH_TYPE_FPGA)) {
Simon Glassda0af362013-05-07 06:11:53 +0000520 ret = fit_image_get_load(fit, image_noffset, &load);
521 printf("%s Load Address: ", p);
522 if (ret)
523 printf("unavailable\n");
524 else
525 printf("0x%08lx\n", load);
526 }
527
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +0300528 /* optional load address for FDT */
529 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
530 printf("%s Load Address: 0x%08lx\n", p, load);
531
Simon Glassda0af362013-05-07 06:11:53 +0000532 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
533 (type == IH_TYPE_RAMDISK)) {
York Sun2b0464e2016-02-29 15:48:40 -0800534 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glassda0af362013-05-07 06:11:53 +0000535 printf("%s Entry Point: ", p);
536 if (ret)
537 printf("unavailable\n");
538 else
539 printf("0x%08lx\n", entry);
540 }
541
542 /* Process all hash subnodes of the component image node */
543 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
544 (noffset >= 0) && (ndepth > 0);
545 noffset = fdt_next_node(fit, noffset, &ndepth)) {
546 if (ndepth == 1) {
547 /* Direct child node of the component image node */
Simon Glasse61aa442013-05-07 06:12:02 +0000548 fit_image_print_verification_data(fit, noffset, p);
Simon Glassda0af362013-05-07 06:11:53 +0000549 }
550 }
Simon Glassda0af362013-05-07 06:11:53 +0000551}
Marek Vasut25114a62018-05-13 00:22:53 +0200552#else
553void fit_print_contents(const void *fit) { }
554void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Marek Vasut9f2e0eb2018-05-13 00:22:52 +0200555#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
Simon Glassda0af362013-05-07 06:11:53 +0000556
557/**
558 * fit_get_desc - get node description property
559 * @fit: pointer to the FIT format image header
560 * @noffset: node offset
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500561 * @desc: double pointer to the char, will hold pointer to the description
Simon Glassda0af362013-05-07 06:11:53 +0000562 *
563 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500564 * description is found pointer to it is returned in third call argument.
Simon Glassda0af362013-05-07 06:11:53 +0000565 *
566 * returns:
567 * 0, on success
568 * -1, on failure
569 */
570int fit_get_desc(const void *fit, int noffset, char **desc)
571{
572 int len;
573
574 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
575 if (*desc == NULL) {
576 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
577 return -1;
578 }
579
580 return 0;
581}
582
583/**
584 * fit_get_timestamp - get node timestamp property
585 * @fit: pointer to the FIT format image header
586 * @noffset: node offset
587 * @timestamp: pointer to the time_t, will hold read timestamp
588 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500589 * fit_get_timestamp() reads timestamp property from given node, if timestamp
590 * is found and has a correct size its value is returned in third call
Simon Glassda0af362013-05-07 06:11:53 +0000591 * argument.
592 *
593 * returns:
594 * 0, on success
595 * -1, on property read failure
596 * -2, on wrong timestamp size
597 */
598int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
599{
600 int len;
601 const void *data;
602
603 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
604 if (data == NULL) {
605 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
606 return -1;
607 }
608 if (len != sizeof(uint32_t)) {
609 debug("FIT timestamp with incorrect size of (%u)\n", len);
610 return -2;
611 }
612
613 *timestamp = uimage_to_cpu(*((uint32_t *)data));
614 return 0;
615}
616
617/**
618 * fit_image_get_node - get node offset for component image of a given unit name
619 * @fit: pointer to the FIT format image header
620 * @image_uname: component image node unit name
621 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500622 * fit_image_get_node() finds a component image (within the '/images'
Simon Glassda0af362013-05-07 06:11:53 +0000623 * node) of a provided unit name. If image is found its node offset is
624 * returned to the caller.
625 *
626 * returns:
627 * image node offset when found (>=0)
628 * negative number on failure (FDT_ERR_* code)
629 */
630int fit_image_get_node(const void *fit, const char *image_uname)
631{
632 int noffset, images_noffset;
633
634 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
635 if (images_noffset < 0) {
636 debug("Can't find images parent node '%s' (%s)\n",
637 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
638 return images_noffset;
639 }
640
641 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
642 if (noffset < 0) {
643 debug("Can't get node offset for image unit name: '%s' (%s)\n",
644 image_uname, fdt_strerror(noffset));
645 }
646
647 return noffset;
648}
649
650/**
651 * fit_image_get_os - get os id for a given component image node
652 * @fit: pointer to the FIT format image header
653 * @noffset: component image node offset
654 * @os: pointer to the uint8_t, will hold os numeric id
655 *
656 * fit_image_get_os() finds os property in a given component image node.
657 * If the property is found, its (string) value is translated to the numeric
658 * id which is returned to the caller.
659 *
660 * returns:
661 * 0, on success
662 * -1, on failure
663 */
664int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
665{
666 int len;
667 const void *data;
668
669 /* Get OS name from property data */
670 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
671 if (data == NULL) {
672 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
673 *os = -1;
674 return -1;
675 }
676
677 /* Translate OS name to id */
678 *os = genimg_get_os_id(data);
679 return 0;
680}
681
682/**
683 * fit_image_get_arch - get arch id for a given component image node
684 * @fit: pointer to the FIT format image header
685 * @noffset: component image node offset
686 * @arch: pointer to the uint8_t, will hold arch numeric id
687 *
688 * fit_image_get_arch() finds arch property in a given component image node.
689 * If the property is found, its (string) value is translated to the numeric
690 * id which is returned to the caller.
691 *
692 * returns:
693 * 0, on success
694 * -1, on failure
695 */
696int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
697{
698 int len;
699 const void *data;
700
701 /* Get architecture name from property data */
702 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
703 if (data == NULL) {
704 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
705 *arch = -1;
706 return -1;
707 }
708
709 /* Translate architecture name to id */
710 *arch = genimg_get_arch_id(data);
711 return 0;
712}
713
714/**
715 * fit_image_get_type - get type id for a given component image node
716 * @fit: pointer to the FIT format image header
717 * @noffset: component image node offset
718 * @type: pointer to the uint8_t, will hold type numeric id
719 *
720 * fit_image_get_type() finds type property in a given component image node.
721 * If the property is found, its (string) value is translated to the numeric
722 * id which is returned to the caller.
723 *
724 * returns:
725 * 0, on success
726 * -1, on failure
727 */
728int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
729{
730 int len;
731 const void *data;
732
733 /* Get image type name from property data */
734 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
735 if (data == NULL) {
736 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
737 *type = -1;
738 return -1;
739 }
740
741 /* Translate image type name to id */
742 *type = genimg_get_type_id(data);
743 return 0;
744}
745
746/**
747 * fit_image_get_comp - get comp id for a given component image node
748 * @fit: pointer to the FIT format image header
749 * @noffset: component image node offset
750 * @comp: pointer to the uint8_t, will hold comp numeric id
751 *
752 * fit_image_get_comp() finds comp property in a given component image node.
753 * If the property is found, its (string) value is translated to the numeric
754 * id which is returned to the caller.
755 *
756 * returns:
757 * 0, on success
758 * -1, on failure
759 */
760int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
761{
762 int len;
763 const void *data;
764
765 /* Get compression name from property data */
766 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
767 if (data == NULL) {
768 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
769 *comp = -1;
770 return -1;
771 }
772
773 /* Translate compression name to id */
774 *comp = genimg_get_comp_id(data);
775 return 0;
776}
777
York Sun2b0464e2016-02-29 15:48:40 -0800778static int fit_image_get_address(const void *fit, int noffset, char *name,
779 ulong *load)
780{
York Sun31e5add2016-02-29 15:48:41 -0800781 int len, cell_len;
782 const fdt32_t *cell;
783 uint64_t load64 = 0;
York Sun2b0464e2016-02-29 15:48:40 -0800784
York Sun31e5add2016-02-29 15:48:41 -0800785 cell = fdt_getprop(fit, noffset, name, &len);
786 if (cell == NULL) {
York Sun2b0464e2016-02-29 15:48:40 -0800787 fit_get_debug(fit, noffset, name, len);
788 return -1;
789 }
790
York Sun31e5add2016-02-29 15:48:41 -0800791 if (len > sizeof(ulong)) {
792 printf("Unsupported %s address size\n", name);
793 return -1;
794 }
795
796 cell_len = len >> 2;
797 /* Use load64 to avoid compiling warning for 32-bit target */
798 while (cell_len--) {
799 load64 = (load64 << 32) | uimage_to_cpu(*cell);
800 cell++;
801 }
802 *load = (ulong)load64;
York Sun2b0464e2016-02-29 15:48:40 -0800803
804 return 0;
805}
Simon Glassda0af362013-05-07 06:11:53 +0000806/**
807 * fit_image_get_load() - get load addr property for given component image node
808 * @fit: pointer to the FIT format image header
809 * @noffset: component image node offset
810 * @load: pointer to the uint32_t, will hold load address
811 *
812 * fit_image_get_load() finds load address property in a given component
813 * image node. If the property is found, its value is returned to the caller.
814 *
815 * returns:
816 * 0, on success
817 * -1, on failure
818 */
819int fit_image_get_load(const void *fit, int noffset, ulong *load)
820{
York Sun2b0464e2016-02-29 15:48:40 -0800821 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glassda0af362013-05-07 06:11:53 +0000822}
823
824/**
825 * fit_image_get_entry() - get entry point address property
826 * @fit: pointer to the FIT format image header
827 * @noffset: component image node offset
828 * @entry: pointer to the uint32_t, will hold entry point address
829 *
830 * This gets the entry point address property for a given component image
831 * node.
832 *
833 * fit_image_get_entry() finds entry point address property in a given
834 * component image node. If the property is found, its value is returned
835 * to the caller.
836 *
837 * returns:
838 * 0, on success
839 * -1, on failure
840 */
841int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
842{
York Sun2b0464e2016-02-29 15:48:40 -0800843 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glassda0af362013-05-07 06:11:53 +0000844}
845
846/**
847 * fit_image_get_data - get data property and its size for a given component image node
848 * @fit: pointer to the FIT format image header
849 * @noffset: component image node offset
850 * @data: double pointer to void, will hold data property's data address
851 * @size: pointer to size_t, will hold data property's data size
852 *
853 * fit_image_get_data() finds data property in a given component image node.
854 * If the property is found its data start address and size are returned to
855 * the caller.
856 *
857 * returns:
858 * 0, on success
859 * -1, on failure
860 */
861int fit_image_get_data(const void *fit, int noffset,
862 const void **data, size_t *size)
863{
864 int len;
865
866 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
867 if (*data == NULL) {
868 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
869 *size = 0;
870 return -1;
871 }
872
873 *size = len;
874 return 0;
875}
876
877/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200878 * Get 'data-offset' property from a given image node.
879 *
880 * @fit: pointer to the FIT image header
881 * @noffset: component image node offset
882 * @data_offset: holds the data-offset property
883 *
884 * returns:
885 * 0, on success
886 * -ENOENT if the property could not be found
887 */
888int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
889{
890 const fdt32_t *val;
891
892 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
893 if (!val)
894 return -ENOENT;
895
896 *data_offset = fdt32_to_cpu(*val);
897
898 return 0;
899}
900
901/**
Peng Fan8876c7e2017-12-05 13:20:59 +0800902 * Get 'data-position' property from a given image node.
903 *
904 * @fit: pointer to the FIT image header
905 * @noffset: component image node offset
906 * @data_position: holds the data-position property
907 *
908 * returns:
909 * 0, on success
910 * -ENOENT if the property could not be found
911 */
912int fit_image_get_data_position(const void *fit, int noffset,
913 int *data_position)
914{
915 const fdt32_t *val;
916
917 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
918 if (!val)
919 return -ENOENT;
920
921 *data_position = fdt32_to_cpu(*val);
922
923 return 0;
924}
925
926/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200927 * Get 'data-size' property from a given image node.
928 *
929 * @fit: pointer to the FIT image header
930 * @noffset: component image node offset
931 * @data_size: holds the data-size property
932 *
933 * returns:
934 * 0, on success
935 * -ENOENT if the property could not be found
936 */
937int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
938{
939 const fdt32_t *val;
940
941 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
942 if (!val)
943 return -ENOENT;
944
945 *data_size = fdt32_to_cpu(*val);
946
947 return 0;
948}
949
950/**
Philippe Reynes3d964702019-12-18 18:25:42 +0100951 * Get 'data-size-unciphered' property from a given image node.
952 *
953 * @fit: pointer to the FIT image header
954 * @noffset: component image node offset
955 * @data_size: holds the data-size property
956 *
957 * returns:
958 * 0, on success
959 * -ENOENT if the property could not be found
960 */
961int fit_image_get_data_size_unciphered(const void *fit, int noffset,
962 size_t *data_size)
963{
964 const fdt32_t *val;
965
966 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
967 if (!val)
968 return -ENOENT;
969
970 *data_size = (size_t)fdt32_to_cpu(*val);
971
972 return 0;
973}
974
975/**
Kelvin Cheung186cc992018-05-19 18:21:37 +0800976 * fit_image_get_data_and_size - get data and its size including
977 * both embedded and external data
978 * @fit: pointer to the FIT format image header
979 * @noffset: component image node offset
980 * @data: double pointer to void, will hold data property's data address
981 * @size: pointer to size_t, will hold data property's data size
982 *
983 * fit_image_get_data_and_size() finds data and its size including
984 * both embedded and external data. If the property is found
985 * its data start address and size are returned to the caller.
986 *
987 * returns:
988 * 0, on success
989 * otherwise, on failure
990 */
991int fit_image_get_data_and_size(const void *fit, int noffset,
992 const void **data, size_t *size)
993{
994 bool external_data = false;
995 int offset;
996 int len;
997 int ret;
998
999 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1000 external_data = true;
1001 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1002 external_data = true;
1003 /*
1004 * For FIT with external data, figure out where
1005 * the external images start. This is the base
1006 * for the data-offset properties in each image.
1007 */
1008 offset += ((fdt_totalsize(fit) + 3) & ~3);
1009 }
1010
1011 if (external_data) {
1012 debug("External Data\n");
1013 ret = fit_image_get_data_size(fit, noffset, &len);
1014 *data = fit + offset;
1015 *size = len;
1016 } else {
1017 ret = fit_image_get_data(fit, noffset, data, size);
1018 }
1019
1020 return ret;
1021}
1022
1023/**
Simon Glassda0af362013-05-07 06:11:53 +00001024 * fit_image_hash_get_algo - get hash algorithm name
1025 * @fit: pointer to the FIT format image header
1026 * @noffset: hash node offset
1027 * @algo: double pointer to char, will hold pointer to the algorithm name
1028 *
1029 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1030 * If the property is found its data start address is returned to the caller.
1031 *
1032 * returns:
1033 * 0, on success
1034 * -1, on failure
1035 */
1036int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1037{
1038 int len;
1039
1040 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1041 if (*algo == NULL) {
1042 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1043 return -1;
1044 }
1045
1046 return 0;
1047}
1048
1049/**
1050 * fit_image_hash_get_value - get hash value and length
1051 * @fit: pointer to the FIT format image header
1052 * @noffset: hash node offset
1053 * @value: double pointer to uint8_t, will hold address of a hash value data
1054 * @value_len: pointer to an int, will hold hash data length
1055 *
1056 * fit_image_hash_get_value() finds hash value property in a given hash node.
1057 * If the property is found its data start address and size are returned to
1058 * the caller.
1059 *
1060 * returns:
1061 * 0, on success
1062 * -1, on failure
1063 */
1064int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1065 int *value_len)
1066{
1067 int len;
1068
1069 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1070 if (*value == NULL) {
1071 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1072 *value_len = 0;
1073 return -1;
1074 }
1075
1076 *value_len = len;
1077 return 0;
1078}
1079
Simon Glassda0af362013-05-07 06:11:53 +00001080/**
1081 * fit_image_hash_get_ignore - get hash ignore flag
1082 * @fit: pointer to the FIT format image header
1083 * @noffset: hash node offset
1084 * @ignore: pointer to an int, will hold hash ignore flag
1085 *
1086 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1087 * If the property is found and non-zero, the hash algorithm is not verified by
1088 * u-boot automatically.
1089 *
1090 * returns:
1091 * 0, on ignore not found
1092 * value, on ignore found
1093 */
Simon Glassf0fd5112013-05-07 06:11:58 +00001094static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glassda0af362013-05-07 06:11:53 +00001095{
1096 int len;
1097 int *value;
1098
1099 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1100 if (value == NULL || len != sizeof(int))
1101 *ignore = 0;
1102 else
1103 *ignore = *value;
1104
1105 return 0;
1106}
Simon Glassda0af362013-05-07 06:11:53 +00001107
Philippe Reynes3148e422019-12-18 18:25:41 +01001108/**
1109 * fit_image_cipher_get_algo - get cipher algorithm name
1110 * @fit: pointer to the FIT format image header
1111 * @noffset: cipher node offset
1112 * @algo: double pointer to char, will hold pointer to the algorithm name
1113 *
1114 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1115 * cipher node. If the property is found its data start address is returned
1116 * to the caller.
1117 *
1118 * returns:
1119 * 0, on success
1120 * -1, on failure
1121 */
1122int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1123{
1124 int len;
1125
1126 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1127 if (!*algo) {
1128 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1129 return -1;
1130 }
1131
1132 return 0;
1133}
1134
Simon Glass5b539a02016-02-24 09:14:42 -07001135ulong fit_get_end(const void *fit)
1136{
1137 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1138}
1139
Simon Glassda0af362013-05-07 06:11:53 +00001140/**
1141 * fit_set_timestamp - set node timestamp property
1142 * @fit: pointer to the FIT format image header
1143 * @noffset: node offset
1144 * @timestamp: timestamp value to be set
1145 *
1146 * fit_set_timestamp() attempts to set timestamp property in the requested
1147 * node and returns operation status to the caller.
1148 *
1149 * returns:
1150 * 0, on success
Simon Glassaf2f9d52014-06-02 22:04:51 -06001151 * -ENOSPC if no space in device tree, -1 for other error
Simon Glassda0af362013-05-07 06:11:53 +00001152 */
1153int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1154{
1155 uint32_t t;
1156 int ret;
1157
1158 t = cpu_to_uimage(timestamp);
1159 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1160 sizeof(uint32_t));
1161 if (ret) {
Simon Glasse4016fa2016-05-01 13:55:37 -06001162 debug("Can't set '%s' property for '%s' node (%s)\n",
1163 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1164 fdt_strerror(ret));
Simon Glassaf2f9d52014-06-02 22:04:51 -06001165 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glassda0af362013-05-07 06:11:53 +00001166 }
1167
1168 return 0;
1169}
1170
1171/**
1172 * calculate_hash - calculate and return hash for provided input data
1173 * @data: pointer to the input data
1174 * @data_len: data length
1175 * @algo: requested hash algorithm
1176 * @value: pointer to the char, will hold hash value data (caller must
1177 * allocate enough free space)
1178 * value_len: length of the calculated hash
1179 *
1180 * calculate_hash() computes input data hash according to the requested
1181 * algorithm.
1182 * Resulting hash value is placed in caller provided 'value' buffer, length
1183 * of the calculated hash is returned via value_len pointer argument.
1184 *
1185 * returns:
1186 * 0, on success
1187 * -1, when algo is unsupported
1188 */
Simon Glass10a1eca2013-05-07 06:11:54 +00001189int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glassda0af362013-05-07 06:11:53 +00001190 uint8_t *value, int *value_len)
1191{
Simon Glass82d94532013-05-08 08:05:59 +00001192 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001193 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1194 CHUNKSZ_CRC32);
1195 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1196 *value_len = 4;
Simon Glass82d94532013-05-08 08:05:59 +00001197 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001198 sha1_csum_wd((unsigned char *)data, data_len,
1199 (unsigned char *)value, CHUNKSZ_SHA1);
1200 *value_len = 20;
Heiko Schocher8ae33802014-03-03 12:19:25 +01001201 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1202 sha256_csum_wd((unsigned char *)data, data_len,
1203 (unsigned char *)value, CHUNKSZ_SHA256);
1204 *value_len = SHA256_SUM_LEN;
Simon Glass82d94532013-05-08 08:05:59 +00001205 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001206 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1207 *value_len = 16;
1208 } else {
1209 debug("Unsupported hash alogrithm\n");
1210 return -1;
1211 }
1212 return 0;
1213}
1214
Simon Glassf0fd5112013-05-07 06:11:58 +00001215static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1216 size_t size, char **err_msgp)
1217{
1218 uint8_t value[FIT_MAX_HASH_LEN];
1219 int value_len;
1220 char *algo;
1221 uint8_t *fit_value;
1222 int fit_value_len;
1223 int ignore;
1224
1225 *err_msgp = NULL;
1226
1227 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001228 *err_msgp = "Can't get hash algo property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001229 return -1;
1230 }
1231 printf("%s", algo);
1232
1233 if (IMAGE_ENABLE_IGNORE) {
1234 fit_image_hash_get_ignore(fit, noffset, &ignore);
1235 if (ignore) {
1236 printf("-skipped ");
1237 return 0;
1238 }
1239 }
1240
1241 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1242 &fit_value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001243 *err_msgp = "Can't get hash value property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001244 return -1;
1245 }
1246
1247 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001248 *err_msgp = "Unsupported hash algorithm";
Simon Glassf0fd5112013-05-07 06:11:58 +00001249 return -1;
1250 }
1251
1252 if (value_len != fit_value_len) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001253 *err_msgp = "Bad hash value len";
Simon Glassf0fd5112013-05-07 06:11:58 +00001254 return -1;
1255 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001256 *err_msgp = "Bad hash value";
Simon Glassf0fd5112013-05-07 06:11:58 +00001257 return -1;
1258 }
1259
1260 return 0;
1261}
1262
Jun Nieadf5d1c2018-02-27 16:55:58 +08001263int fit_image_verify_with_data(const void *fit, int image_noffset,
1264 const void *data, size_t size)
Simon Glassda0af362013-05-07 06:11:53 +00001265{
Simon Glassfbabc0f2013-06-13 15:10:01 -07001266 int noffset = 0;
Simon Glassda0af362013-05-07 06:11:53 +00001267 char *err_msg = "";
Simon Glassfbabc0f2013-06-13 15:10:01 -07001268 int verify_all = 1;
1269 int ret;
Simon Glassda0af362013-05-07 06:11:53 +00001270
Simon Glassfbabc0f2013-06-13 15:10:01 -07001271 /* Verify all required signatures */
1272 if (IMAGE_ENABLE_VERIFY &&
1273 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1274 gd_fdt_blob(), &verify_all)) {
1275 err_msg = "Unable to verify required signature";
1276 goto error;
1277 }
1278
Simon Glassda0af362013-05-07 06:11:53 +00001279 /* Process all hash subnodes of the component image node */
Simon Glass499c29e2016-10-02 17:59:29 -06001280 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassf0fd5112013-05-07 06:11:58 +00001281 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001282
Simon Glassf0fd5112013-05-07 06:11:58 +00001283 /*
1284 * Check subnode name, must be equal to "hash".
1285 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +00001286 * names, e.g. hash-1, hash-2, etc.
Simon Glassf0fd5112013-05-07 06:11:58 +00001287 */
1288 if (!strncmp(name, FIT_HASH_NODENAME,
1289 strlen(FIT_HASH_NODENAME))) {
1290 if (fit_image_check_hash(fit, noffset, data, size,
1291 &err_msg))
Simon Glassda0af362013-05-07 06:11:53 +00001292 goto error;
Simon Glassf0fd5112013-05-07 06:11:58 +00001293 puts("+ ");
Simon Glassfbabc0f2013-06-13 15:10:01 -07001294 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1295 !strncmp(name, FIT_SIG_NODENAME,
1296 strlen(FIT_SIG_NODENAME))) {
1297 ret = fit_image_check_sig(fit, noffset, data,
1298 size, -1, &err_msg);
Simon Glass51026aa2016-02-24 09:14:43 -07001299
1300 /*
1301 * Show an indication on failure, but do not return
1302 * an error. Only keys marked 'required' can cause
1303 * an image validation failure. See the call to
1304 * fit_image_verify_required_sigs() above.
1305 */
1306 if (ret)
Simon Glassfbabc0f2013-06-13 15:10:01 -07001307 puts("- ");
1308 else
1309 puts("+ ");
Simon Glassda0af362013-05-07 06:11:53 +00001310 }
1311 }
1312
1313 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001314 err_msg = "Corrupted or truncated tree";
Simon Glassda0af362013-05-07 06:11:53 +00001315 goto error;
1316 }
1317
1318 return 1;
1319
1320error:
Simon Glass8f3aa462013-05-07 06:11:59 +00001321 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glassda0af362013-05-07 06:11:53 +00001322 err_msg, fit_get_name(fit, noffset, NULL),
1323 fit_get_name(fit, image_noffset, NULL));
1324 return 0;
1325}
1326
1327/**
Jun Nieadf5d1c2018-02-27 16:55:58 +08001328 * fit_image_verify - verify data integrity
1329 * @fit: pointer to the FIT format image header
1330 * @image_noffset: component image node offset
1331 *
1332 * fit_image_verify() goes over component image hash nodes,
1333 * re-calculates each data hash and compares with the value stored in hash
1334 * node.
1335 *
1336 * returns:
1337 * 1, if all hashes are valid
1338 * 0, otherwise (or on error)
1339 */
1340int fit_image_verify(const void *fit, int image_noffset)
1341{
1342 const void *data;
1343 size_t size;
1344 int noffset = 0;
1345 char *err_msg = "";
1346
1347 /* Get image data and data length */
Kelvin Cheung186cc992018-05-19 18:21:37 +08001348 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nieadf5d1c2018-02-27 16:55:58 +08001349 err_msg = "Can't get image data/size";
1350 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1351 err_msg, fit_get_name(fit, noffset, NULL),
1352 fit_get_name(fit, image_noffset, NULL));
1353 return 0;
1354 }
1355
1356 return fit_image_verify_with_data(fit, image_noffset, data, size);
1357}
1358
1359/**
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001360 * fit_all_image_verify - verify data integrity for all images
Simon Glassda0af362013-05-07 06:11:53 +00001361 * @fit: pointer to the FIT format image header
1362 *
Simon Glass7428ad12013-05-07 06:11:57 +00001363 * fit_all_image_verify() goes over all images in the FIT and
Simon Glassda0af362013-05-07 06:11:53 +00001364 * for every images checks if all it's hashes are valid.
1365 *
1366 * returns:
1367 * 1, if all hashes of all images are valid
1368 * 0, otherwise (or on error)
1369 */
Simon Glass7428ad12013-05-07 06:11:57 +00001370int fit_all_image_verify(const void *fit)
Simon Glassda0af362013-05-07 06:11:53 +00001371{
1372 int images_noffset;
1373 int noffset;
1374 int ndepth;
1375 int count;
1376
1377 /* Find images parent node offset */
1378 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1379 if (images_noffset < 0) {
1380 printf("Can't find images parent node '%s' (%s)\n",
1381 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1382 return 0;
1383 }
1384
1385 /* Process all image subnodes, check hashes for each */
1386 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1387 (ulong)fit);
1388 for (ndepth = 0, count = 0,
1389 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1390 (noffset >= 0) && (ndepth > 0);
1391 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1392 if (ndepth == 1) {
1393 /*
1394 * Direct child node of the images parent node,
1395 * i.e. component image node.
1396 */
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001397 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glassda0af362013-05-07 06:11:53 +00001398 fit_get_name(fit, noffset, NULL));
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001399 count++;
Simon Glassda0af362013-05-07 06:11:53 +00001400
Simon Glass7428ad12013-05-07 06:11:57 +00001401 if (!fit_image_verify(fit, noffset))
Simon Glassda0af362013-05-07 06:11:53 +00001402 return 0;
1403 printf("\n");
1404 }
1405 }
1406 return 1;
1407}
1408
Philippe Reynes3d964702019-12-18 18:25:42 +01001409#ifdef CONFIG_FIT_CIPHER
1410static int fit_image_uncipher(const void *fit, int image_noffset,
1411 void **data, size_t *size)
1412{
1413 int cipher_noffset, ret;
1414 void *dst;
1415 size_t size_dst;
1416
1417 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1418 FIT_CIPHER_NODENAME);
1419 if (cipher_noffset < 0)
1420 return 0;
1421
1422 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1423 *data, *size, &dst, &size_dst);
1424 if (ret)
1425 goto out;
1426
1427 *data = dst;
1428 *size = size_dst;
1429
1430 out:
1431 return ret;
1432}
1433#endif /* CONFIG_FIT_CIPHER */
1434
Simon Glassda0af362013-05-07 06:11:53 +00001435/**
1436 * fit_image_check_os - check whether image node is of a given os type
1437 * @fit: pointer to the FIT format image header
1438 * @noffset: component image node offset
1439 * @os: requested image os
1440 *
1441 * fit_image_check_os() reads image os property and compares its numeric
1442 * id with the requested os. Comparison result is returned to the caller.
1443 *
1444 * returns:
1445 * 1 if image is of given os type
1446 * 0 otherwise (or on error)
1447 */
1448int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1449{
1450 uint8_t image_os;
1451
1452 if (fit_image_get_os(fit, noffset, &image_os))
1453 return 0;
1454 return (os == image_os);
1455}
1456
1457/**
1458 * fit_image_check_arch - check whether image node is of a given arch
1459 * @fit: pointer to the FIT format image header
1460 * @noffset: component image node offset
1461 * @arch: requested imagearch
1462 *
1463 * fit_image_check_arch() reads image arch property and compares its numeric
1464 * id with the requested arch. Comparison result is returned to the caller.
1465 *
1466 * returns:
1467 * 1 if image is of given arch
1468 * 0 otherwise (or on error)
1469 */
1470int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1471{
1472 uint8_t image_arch;
Alison Wang73818d52016-11-10 10:49:03 +08001473 int aarch32_support = 0;
1474
1475#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1476 aarch32_support = 1;
1477#endif
Simon Glassda0af362013-05-07 06:11:53 +00001478
1479 if (fit_image_get_arch(fit, noffset, &image_arch))
1480 return 0;
Simon Glass9d428302014-10-10 08:21:57 -06001481 return (arch == image_arch) ||
Alison Wang73818d52016-11-10 10:49:03 +08001482 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1483 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1484 aarch32_support);
Simon Glassda0af362013-05-07 06:11:53 +00001485}
1486
1487/**
1488 * fit_image_check_type - check whether image node is of a given type
1489 * @fit: pointer to the FIT format image header
1490 * @noffset: component image node offset
1491 * @type: requested image type
1492 *
1493 * fit_image_check_type() reads image type property and compares its numeric
1494 * id with the requested type. Comparison result is returned to the caller.
1495 *
1496 * returns:
1497 * 1 if image is of given type
1498 * 0 otherwise (or on error)
1499 */
1500int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1501{
1502 uint8_t image_type;
1503
1504 if (fit_image_get_type(fit, noffset, &image_type))
1505 return 0;
1506 return (type == image_type);
1507}
1508
1509/**
1510 * fit_image_check_comp - check whether image node uses given compression
1511 * @fit: pointer to the FIT format image header
1512 * @noffset: component image node offset
1513 * @comp: requested image compression type
1514 *
1515 * fit_image_check_comp() reads image compression property and compares its
1516 * numeric id with the requested compression type. Comparison result is
1517 * returned to the caller.
1518 *
1519 * returns:
1520 * 1 if image uses requested compression
1521 * 0 otherwise (or on error)
1522 */
1523int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1524{
1525 uint8_t image_comp;
1526
1527 if (fit_image_get_comp(fit, noffset, &image_comp))
1528 return 0;
1529 return (comp == image_comp);
1530}
1531
1532/**
1533 * fit_check_format - sanity check FIT image format
1534 * @fit: pointer to the FIT format image header
1535 *
1536 * fit_check_format() runs a basic sanity FIT image verification.
1537 * Routine checks for mandatory properties, nodes, etc.
1538 *
1539 * returns:
1540 * 1, on success
1541 * 0, on failure
1542 */
1543int fit_check_format(const void *fit)
1544{
1545 /* mandatory / node 'description' property */
1546 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1547 debug("Wrong FIT format: no description\n");
1548 return 0;
1549 }
1550
1551 if (IMAGE_ENABLE_TIMESTAMP) {
1552 /* mandatory / node 'timestamp' property */
1553 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1554 debug("Wrong FIT format: no timestamp\n");
1555 return 0;
1556 }
1557 }
1558
1559 /* mandatory subimages parent '/images' node */
1560 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1561 debug("Wrong FIT format: no images parent node\n");
1562 return 0;
1563 }
1564
1565 return 1;
1566}
1567
1568
1569/**
1570 * fit_conf_find_compat
1571 * @fit: pointer to the FIT format image header
1572 * @fdt: pointer to the device tree to compare against
1573 *
1574 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1575 * most compatible with the passed in device tree.
1576 *
1577 * Example:
1578 *
1579 * / o image-tree
1580 * |-o images
Andre Przywara3234ecd2017-12-04 02:05:10 +00001581 * | |-o fdt-1
1582 * | |-o fdt-2
Simon Glassda0af362013-05-07 06:11:53 +00001583 * |
1584 * |-o configurations
Andre Przywara3234ecd2017-12-04 02:05:10 +00001585 * |-o config-1
1586 * | |-fdt = fdt-1
Simon Glassda0af362013-05-07 06:11:53 +00001587 * |
Andre Przywara3234ecd2017-12-04 02:05:10 +00001588 * |-o config-2
1589 * |-fdt = fdt-2
Simon Glassda0af362013-05-07 06:11:53 +00001590 *
1591 * / o U-Boot fdt
1592 * |-compatible = "foo,bar", "bim,bam"
1593 *
1594 * / o kernel fdt1
1595 * |-compatible = "foo,bar",
1596 *
1597 * / o kernel fdt2
1598 * |-compatible = "bim,bam", "baz,biz"
1599 *
1600 * Configuration 1 would be picked because the first string in U-Boot's
1601 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1602 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1603 *
Julius Werner4e823522019-07-24 19:37:56 -07001604 * As an optimization, the compatible property from the FDT's root node can be
1605 * copied into the configuration node in the FIT image. This is required to
1606 * match configurations with compressed FDTs.
1607 *
Simon Glassda0af362013-05-07 06:11:53 +00001608 * returns:
1609 * offset to the configuration to use if one was found
1610 * -1 otherwise
1611 */
1612int fit_conf_find_compat(const void *fit, const void *fdt)
1613{
1614 int ndepth = 0;
1615 int noffset, confs_noffset, images_noffset;
1616 const void *fdt_compat;
1617 int fdt_compat_len;
1618 int best_match_offset = 0;
1619 int best_match_pos = 0;
1620
1621 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1622 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1623 if (confs_noffset < 0 || images_noffset < 0) {
1624 debug("Can't find configurations or images nodes.\n");
1625 return -1;
1626 }
1627
1628 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1629 if (!fdt_compat) {
1630 debug("Fdt for comparison has no \"compatible\" property.\n");
1631 return -1;
1632 }
1633
1634 /*
1635 * Loop over the configurations in the FIT image.
1636 */
1637 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1638 (noffset >= 0) && (ndepth > 0);
1639 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner4e823522019-07-24 19:37:56 -07001640 const void *fdt;
Simon Glassda0af362013-05-07 06:11:53 +00001641 const char *kfdt_name;
Julius Werner4e823522019-07-24 19:37:56 -07001642 int kfdt_noffset, compat_noffset;
Simon Glassda0af362013-05-07 06:11:53 +00001643 const char *cur_fdt_compat;
1644 int len;
Julius Werner4e823522019-07-24 19:37:56 -07001645 size_t sz;
Simon Glassda0af362013-05-07 06:11:53 +00001646 int i;
1647
1648 if (ndepth > 1)
1649 continue;
1650
Julius Werner4e823522019-07-24 19:37:56 -07001651 /* If there's a compat property in the config node, use that. */
1652 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1653 fdt = fit; /* search in FIT image */
1654 compat_noffset = noffset; /* search under config node */
1655 } else { /* Otherwise extract it from the kernel FDT. */
1656 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1657 if (!kfdt_name) {
1658 debug("No fdt property found.\n");
1659 continue;
1660 }
1661 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1662 kfdt_name);
1663 if (kfdt_noffset < 0) {
1664 debug("No image node named \"%s\" found.\n",
1665 kfdt_name);
1666 continue;
1667 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001668
Julius Werner4e823522019-07-24 19:37:56 -07001669 if (!fit_image_check_comp(fit, kfdt_noffset,
1670 IH_COMP_NONE)) {
1671 debug("Can't extract compat from \"%s\" "
1672 "(compressed)\n", kfdt_name);
1673 continue;
1674 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001675
Julius Werner4e823522019-07-24 19:37:56 -07001676 /* search in this config's kernel FDT */
1677 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1678 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1679 continue;
1680 }
1681
1682 compat_noffset = 0; /* search kFDT under root node */
Simon Glassda0af362013-05-07 06:11:53 +00001683 }
1684
1685 len = fdt_compat_len;
1686 cur_fdt_compat = fdt_compat;
1687 /*
1688 * Look for a match for each U-Boot compatibility string in
Julius Werner4e823522019-07-24 19:37:56 -07001689 * turn in the compat string property.
Simon Glassda0af362013-05-07 06:11:53 +00001690 */
1691 for (i = 0; len > 0 &&
1692 (!best_match_offset || best_match_pos > i); i++) {
1693 int cur_len = strlen(cur_fdt_compat) + 1;
1694
Julius Werner4e823522019-07-24 19:37:56 -07001695 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001696 cur_fdt_compat)) {
1697 best_match_offset = noffset;
1698 best_match_pos = i;
1699 break;
1700 }
1701 len -= cur_len;
1702 cur_fdt_compat += cur_len;
1703 }
1704 }
1705 if (!best_match_offset) {
1706 debug("No match found.\n");
1707 return -1;
1708 }
1709
1710 return best_match_offset;
1711}
1712
1713/**
1714 * fit_conf_get_node - get node offset for configuration of a given unit name
1715 * @fit: pointer to the FIT format image header
1716 * @conf_uname: configuration node unit name
1717 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001718 * fit_conf_get_node() finds a configuration (within the '/configurations'
1719 * parent node) of a provided unit name. If configuration is found its node
Simon Glassda0af362013-05-07 06:11:53 +00001720 * offset is returned to the caller.
1721 *
1722 * When NULL is provided in second argument fit_conf_get_node() will search
1723 * for a default configuration node instead. Default configuration node unit
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001724 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glassda0af362013-05-07 06:11:53 +00001725 * node.
1726 *
1727 * returns:
1728 * configuration node offset when found (>=0)
1729 * negative number on failure (FDT_ERR_* code)
1730 */
1731int fit_conf_get_node(const void *fit, const char *conf_uname)
1732{
1733 int noffset, confs_noffset;
1734 int len;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001735 const char *s;
1736 char *conf_uname_copy = NULL;
Simon Glassda0af362013-05-07 06:11:53 +00001737
1738 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1739 if (confs_noffset < 0) {
1740 debug("Can't find configurations parent node '%s' (%s)\n",
1741 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1742 return confs_noffset;
1743 }
1744
1745 if (conf_uname == NULL) {
1746 /* get configuration unit name from the default property */
1747 debug("No configuration specified, trying default...\n");
1748 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1749 FIT_DEFAULT_PROP, &len);
1750 if (conf_uname == NULL) {
1751 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1752 len);
1753 return len;
1754 }
1755 debug("Found default configuration: '%s'\n", conf_uname);
1756 }
1757
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001758 s = strchr(conf_uname, '#');
1759 if (s) {
1760 len = s - conf_uname;
1761 conf_uname_copy = malloc(len + 1);
1762 if (!conf_uname_copy) {
1763 debug("Can't allocate uname copy: '%s'\n",
1764 conf_uname);
1765 return -ENOMEM;
1766 }
1767 memcpy(conf_uname_copy, conf_uname, len);
1768 conf_uname_copy[len] = '\0';
1769 conf_uname = conf_uname_copy;
1770 }
1771
Simon Glassda0af362013-05-07 06:11:53 +00001772 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1773 if (noffset < 0) {
1774 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1775 conf_uname, fdt_strerror(noffset));
1776 }
1777
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001778 if (conf_uname_copy)
1779 free(conf_uname_copy);
1780
Simon Glassda0af362013-05-07 06:11:53 +00001781 return noffset;
1782}
1783
Pantelis Antonioua4880742017-09-04 23:12:14 +03001784int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001785 const char *prop_name)
1786{
Pantelis Antonioua4880742017-09-04 23:12:14 +03001787 return fdt_stringlist_count(fit, noffset, prop_name);
1788}
1789
1790int fit_conf_get_prop_node_index(const void *fit, int noffset,
1791 const char *prop_name, int index)
1792{
1793 const char *uname;
Simon Glassda0af362013-05-07 06:11:53 +00001794 int len;
1795
1796 /* get kernel image unit name from configuration kernel property */
Pantelis Antonioua4880742017-09-04 23:12:14 +03001797 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glassda0af362013-05-07 06:11:53 +00001798 if (uname == NULL)
1799 return len;
1800
1801 return fit_image_get_node(fit, uname);
1802}
1803
Pantelis Antonioua4880742017-09-04 23:12:14 +03001804int fit_conf_get_prop_node(const void *fit, int noffset,
1805 const char *prop_name)
1806{
1807 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1808}
Simon Glassda0af362013-05-07 06:11:53 +00001809
Jeroen Hofsteeffa60da2014-10-08 22:57:38 +02001810static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass384d86d2013-05-16 13:53:21 +00001811{
1812 fit_image_print(fit, rd_noffset, " ");
1813
1814 if (verify) {
1815 puts(" Verifying Hash Integrity ... ");
1816 if (!fit_image_verify(fit, rd_noffset)) {
1817 puts("Bad Data Hash\n");
1818 return -EACCES;
1819 }
1820 puts("OK\n");
1821 }
1822
1823 return 0;
1824}
1825
Simon Glass384d86d2013-05-16 13:53:21 +00001826int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1827 ulong addr)
1828{
1829 int cfg_noffset;
1830 void *fit_hdr;
1831 int noffset;
1832
1833 debug("* %s: using config '%s' from image at 0x%08lx\n",
1834 prop_name, images->fit_uname_cfg, addr);
1835
1836 /* Check whether configuration has this property defined */
1837 fit_hdr = map_sysmem(addr, 0);
1838 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1839 if (cfg_noffset < 0) {
1840 debug("* %s: no such config\n", prop_name);
Paul Burton14171b12016-09-20 18:17:12 +01001841 return -EINVAL;
Simon Glass384d86d2013-05-16 13:53:21 +00001842 }
1843
1844 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1845 if (noffset < 0) {
1846 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Grayae6a02f2016-09-03 08:30:14 +10001847 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00001848 }
1849
1850 return noffset;
1851}
1852
Simon Glassa0c0b632014-06-12 07:24:47 -06001853/**
1854 * fit_get_image_type_property() - get property name for IH_TYPE_...
1855 *
1856 * @return the properly name where we expect to find the image in the
1857 * config node
1858 */
1859static const char *fit_get_image_type_property(int type)
1860{
1861 /*
1862 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001863 * but we don't have access to the short name, and "fdt" is different
Simon Glassa0c0b632014-06-12 07:24:47 -06001864 * anyway. So let's just keep it here.
1865 */
1866 switch (type) {
1867 case IH_TYPE_FLATDT:
1868 return FIT_FDT_PROP;
1869 case IH_TYPE_KERNEL:
1870 return FIT_KERNEL_PROP;
1871 case IH_TYPE_RAMDISK:
1872 return FIT_RAMDISK_PROP;
Simon Glass0129b522014-10-19 21:11:24 -06001873 case IH_TYPE_X86_SETUP:
1874 return FIT_SETUP_PROP;
Karl Apsite1b21c282015-05-21 09:52:48 -04001875 case IH_TYPE_LOADABLE:
1876 return FIT_LOADABLE_PROP;
Michal Simekebae78b2016-05-17 14:03:50 +02001877 case IH_TYPE_FPGA:
1878 return FIT_FPGA_PROP;
Marek Vasut93e95752018-05-13 00:22:54 +02001879 case IH_TYPE_STANDALONE:
1880 return FIT_STANDALONE_PROP;
Simon Glassa0c0b632014-06-12 07:24:47 -06001881 }
1882
1883 return "unknown";
1884}
1885
1886int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassad68fc32013-07-10 23:08:10 -07001887 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass384d86d2013-05-16 13:53:21 +00001888 int arch, int image_type, int bootstage_id,
1889 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1890{
1891 int cfg_noffset, noffset;
1892 const char *fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07001893 const char *fit_uname_config;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001894 const char *fit_base_uname_config;
Simon Glass384d86d2013-05-16 13:53:21 +00001895 const void *fit;
Julius Werner97b09cd2019-07-24 19:37:55 -07001896 void *buf;
1897 void *loadbuf;
Simon Glass384d86d2013-05-16 13:53:21 +00001898 size_t size;
1899 int type_ok, os_ok;
Julius Werner97b09cd2019-07-24 19:37:55 -07001900 ulong load, load_end, data, len;
1901 uint8_t os, comp;
Alison Wang73818d52016-11-10 10:49:03 +08001902#ifndef USE_HOSTCC
1903 uint8_t os_arch;
1904#endif
Simon Glassa0c0b632014-06-12 07:24:47 -06001905 const char *prop_name;
Simon Glass384d86d2013-05-16 13:53:21 +00001906 int ret;
1907
1908 fit = map_sysmem(addr, 0);
1909 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassad68fc32013-07-10 23:08:10 -07001910 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001911 fit_base_uname_config = NULL;
Simon Glassa0c0b632014-06-12 07:24:47 -06001912 prop_name = fit_get_image_type_property(image_type);
Simon Glass384d86d2013-05-16 13:53:21 +00001913 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1914
1915 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1916 if (!fit_check_format(fit)) {
1917 printf("Bad FIT %s image format!\n", prop_name);
1918 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1919 return -ENOEXEC;
1920 }
1921 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1922 if (fit_uname) {
Masahiro Yamadaaa58eec2014-02-18 15:39:21 +09001923 /* get FIT component image node offset */
Simon Glass384d86d2013-05-16 13:53:21 +00001924 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1925 noffset = fit_image_get_node(fit, fit_uname);
1926 } else {
1927 /*
1928 * no image node unit name, try to get config
1929 * node first. If config unit node name is NULL
1930 * fit_conf_get_node() will try to find default config node
1931 */
1932 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1933 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1934 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1935 } else {
1936 cfg_noffset = fit_conf_get_node(fit,
1937 fit_uname_config);
1938 }
1939 if (cfg_noffset < 0) {
1940 puts("Could not find configuration node\n");
1941 bootstage_error(bootstage_id +
1942 BOOTSTAGE_SUB_NO_UNIT_NAME);
1943 return -ENOENT;
1944 }
Marek Vasut5dee1342018-05-31 17:59:07 +02001945
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001946 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1947 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut5dee1342018-05-31 17:59:07 +02001948 /* Remember this config */
1949 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001950 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut5dee1342018-05-31 17:59:07 +02001951
1952 if (IMAGE_ENABLE_VERIFY && images->verify) {
1953 puts(" Verifying Hash Integrity ... ");
1954 if (fit_config_verify(fit, cfg_noffset)) {
1955 puts("Bad Data Hash\n");
1956 bootstage_error(bootstage_id +
1957 BOOTSTAGE_SUB_HASH);
1958 return -EACCES;
Simon Glass384d86d2013-05-16 13:53:21 +00001959 }
Marek Vasut5dee1342018-05-31 17:59:07 +02001960 puts("OK\n");
Simon Glass384d86d2013-05-16 13:53:21 +00001961 }
1962
Marek Vasut5dee1342018-05-31 17:59:07 +02001963 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1964
Simon Glass384d86d2013-05-16 13:53:21 +00001965 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1966 prop_name);
1967 fit_uname = fit_get_name(fit, noffset, NULL);
1968 }
1969 if (noffset < 0) {
1970 puts("Could not find subimage node\n");
1971 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1972 return -ENOENT;
1973 }
1974
1975 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1976
1977 ret = fit_image_select(fit, noffset, images->verify);
1978 if (ret) {
1979 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1980 return ret;
1981 }
1982
1983 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass3c1e7502014-10-19 21:11:22 -06001984#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass384d86d2013-05-16 13:53:21 +00001985 if (!fit_image_check_target_arch(fit, noffset)) {
1986 puts("Unsupported Architecture\n");
1987 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1988 return -ENOEXEC;
1989 }
Simon Glassa51991d2014-06-12 07:24:53 -06001990#endif
Alison Wang73818d52016-11-10 10:49:03 +08001991
1992#ifndef USE_HOSTCC
1993 fit_image_get_arch(fit, noffset, &os_arch);
1994 images->os.arch = os_arch;
1995#endif
1996
Simon Glass384d86d2013-05-16 13:53:21 +00001997 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1998 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02001999 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2000 (image_type == IH_TYPE_KERNEL &&
2001 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasutcaee41f2014-12-16 14:07:22 +01002002
Andreas Bießmannb4cc60d2016-08-14 20:31:24 +02002003 os_ok = image_type == IH_TYPE_FLATDT ||
2004 image_type == IH_TYPE_FPGA ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01002005 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02002006 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Cristian Ciocaltea217f9642019-12-24 18:05:38 +02002007 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
2008 fit_image_check_os(fit, noffset, IH_OS_EFI);
Karl Apsite1b21c282015-05-21 09:52:48 -04002009
2010 /*
2011 * If either of the checks fail, we should report an error, but
2012 * if the image type is coming from the "loadables" field, we
2013 * don't care what it is
2014 */
2015 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasutcaee41f2014-12-16 14:07:22 +01002016 fit_image_get_os(fit, noffset, &os);
2017 printf("No %s %s %s Image\n",
2018 genimg_get_os_name(os),
2019 genimg_get_arch_name(arch),
Simon Glass384d86d2013-05-16 13:53:21 +00002020 genimg_get_type_name(image_type));
2021 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2022 return -EIO;
2023 }
2024
2025 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2026
2027 /* get image data address and length */
Julius Werner97b09cd2019-07-24 19:37:55 -07002028 if (fit_image_get_data_and_size(fit, noffset,
2029 (const void **)&buf, &size)) {
Simon Glass384d86d2013-05-16 13:53:21 +00002030 printf("Could not find %s subimage data!\n", prop_name);
2031 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass9d0da5b2013-06-16 07:46:49 -07002032 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00002033 }
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002034
Philippe Reynes3d964702019-12-18 18:25:42 +01002035#ifdef CONFIG_FIT_CIPHER
2036 /* Decrypt data before uncompress/move */
2037 if (IMAGE_ENABLE_DECRYPT) {
2038 puts(" Decrypting Data ... ");
2039 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2040 puts("Error\n");
2041 return -EACCES;
2042 }
2043 puts("OK\n");
2044 }
2045#endif
2046
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002047#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2048 /* perform any post-processing on the image data */
Julius Werner97b09cd2019-07-24 19:37:55 -07002049 board_fit_image_post_process(&buf, &size);
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06002050#endif
2051
Simon Glass384d86d2013-05-16 13:53:21 +00002052 len = (ulong)size;
2053
Simon Glass384d86d2013-05-16 13:53:21 +00002054 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2055
Julius Werner97b09cd2019-07-24 19:37:55 -07002056 data = map_to_sysmem(buf);
2057 load = data;
Simon Glass384d86d2013-05-16 13:53:21 +00002058 if (load_op == FIT_LOAD_IGNORED) {
2059 /* Don't load */
2060 } else if (fit_image_get_load(fit, noffset, &load)) {
2061 if (load_op == FIT_LOAD_REQUIRED) {
2062 printf("Can't get %s subimage load address!\n",
2063 prop_name);
2064 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2065 return -EBADF;
2066 }
Simon Glass05a9ad72014-08-22 14:26:43 -06002067 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass384d86d2013-05-16 13:53:21 +00002068 ulong image_start, image_end;
Simon Glass384d86d2013-05-16 13:53:21 +00002069
2070 /*
2071 * move image data to the load address,
2072 * make sure we don't overwrite initial image
2073 */
2074 image_start = addr;
2075 image_end = addr + fit_get_size(fit);
2076
2077 load_end = load + len;
2078 if (image_type != IH_TYPE_KERNEL &&
2079 load < image_end && load_end > image_start) {
2080 printf("Error: %s overwritten\n", prop_name);
2081 return -EXDEV;
2082 }
2083
2084 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2085 prop_name, data, load);
Julius Werner97b09cd2019-07-24 19:37:55 -07002086 } else {
2087 load = data; /* No load address specified */
2088 }
2089
2090 comp = IH_COMP_NONE;
2091 loadbuf = buf;
2092 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Werner88040022019-08-02 15:52:28 -07002093 if (!fit_image_get_comp(fit, noffset, &comp) &&
2094 comp != IH_COMP_NONE &&
2095 !(image_type == IH_TYPE_KERNEL ||
2096 image_type == IH_TYPE_KERNEL_NOLOAD ||
2097 image_type == IH_TYPE_RAMDISK)) {
Julius Werner97b09cd2019-07-24 19:37:55 -07002098 ulong max_decomp_len = len * 20;
2099 if (load == data) {
2100 loadbuf = malloc(max_decomp_len);
2101 load = map_to_sysmem(loadbuf);
2102 } else {
2103 loadbuf = map_sysmem(load, max_decomp_len);
2104 }
2105 if (image_decomp(comp, load, data, image_type,
2106 loadbuf, buf, len, max_decomp_len, &load_end)) {
2107 printf("Error decompressing %s\n", prop_name);
Simon Glass384d86d2013-05-16 13:53:21 +00002108
Julius Werner97b09cd2019-07-24 19:37:55 -07002109 return -ENOEXEC;
2110 }
2111 len = load_end - load;
2112 } else if (load != data) {
2113 loadbuf = map_sysmem(load, len);
2114 memcpy(loadbuf, buf, len);
Simon Glass384d86d2013-05-16 13:53:21 +00002115 }
Julius Werner97b09cd2019-07-24 19:37:55 -07002116
Julius Werner88040022019-08-02 15:52:28 -07002117 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2118 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2119 " please fix your .its file!\n");
2120
Julius Werner97b09cd2019-07-24 19:37:55 -07002121 /* verify that image data is a proper FDT blob */
2122 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2123 puts("Subimage data is not a FDT");
2124 return -ENOEXEC;
2125 }
2126
Simon Glass384d86d2013-05-16 13:53:21 +00002127 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2128
Julius Werner97b09cd2019-07-24 19:37:55 -07002129 *datap = load;
Simon Glass384d86d2013-05-16 13:53:21 +00002130 *lenp = len;
2131 if (fit_unamep)
2132 *fit_unamep = (char *)fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07002133 if (fit_uname_configp)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002134 *fit_uname_configp = (char *)(fit_uname_config ? :
2135 fit_base_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00002136
2137 return noffset;
2138}
Simon Glass0129b522014-10-19 21:11:24 -06002139
2140int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2141 ulong *setup_start, ulong *setup_len)
2142{
2143 int noffset;
2144 ulong addr;
2145 ulong len;
2146 int ret;
2147
2148 addr = map_to_sysmem(images->fit_hdr_os);
2149 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2150 if (noffset < 0)
2151 return noffset;
2152
2153 ret = fit_image_load(images, addr, NULL, NULL, arch,
2154 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2155 FIT_LOAD_REQUIRED, setup_start, &len);
2156
2157 return ret;
2158}
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002159
2160#ifndef USE_HOSTCC
2161int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2162 const char **fit_unamep, const char **fit_uname_configp,
2163 int arch, ulong *datap, ulong *lenp)
2164{
2165 int fdt_noffset, cfg_noffset, count;
2166 const void *fit;
2167 const char *fit_uname = NULL;
2168 const char *fit_uname_config = NULL;
2169 char *fit_uname_config_copy = NULL;
2170 char *next_config = NULL;
2171 ulong load, len;
2172#ifdef CONFIG_OF_LIBFDT_OVERLAY
2173 ulong image_start, image_end;
2174 ulong ovload, ovlen;
2175 const char *uconfig;
2176 const char *uname;
2177 void *base, *ov;
2178 int i, err, noffset, ov_noffset;
2179#endif
2180
2181 fit_uname = fit_unamep ? *fit_unamep : NULL;
2182
2183 if (fit_uname_configp && *fit_uname_configp) {
2184 fit_uname_config_copy = strdup(*fit_uname_configp);
2185 if (!fit_uname_config_copy)
2186 return -ENOMEM;
2187
2188 next_config = strchr(fit_uname_config_copy, '#');
2189 if (next_config)
2190 *next_config++ = '\0';
2191 if (next_config - 1 > fit_uname_config_copy)
2192 fit_uname_config = fit_uname_config_copy;
2193 }
2194
2195 fdt_noffset = fit_image_load(images,
2196 addr, &fit_uname, &fit_uname_config,
2197 arch, IH_TYPE_FLATDT,
2198 BOOTSTAGE_ID_FIT_FDT_START,
2199 FIT_LOAD_OPTIONAL, &load, &len);
2200
2201 if (fdt_noffset < 0)
2202 goto out;
2203
2204 debug("fit_uname=%s, fit_uname_config=%s\n",
2205 fit_uname ? fit_uname : "<NULL>",
2206 fit_uname_config ? fit_uname_config : "<NULL>");
2207
2208 fit = map_sysmem(addr, 0);
2209
2210 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2211
2212 /* single blob, or error just return as well */
2213 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2214 if (count <= 1 && !next_config)
2215 goto out;
2216
2217 /* we need to apply overlays */
2218
2219#ifdef CONFIG_OF_LIBFDT_OVERLAY
2220 image_start = addr;
2221 image_end = addr + fit_get_size(fit);
2222 /* verify that relocation took place by load address not being in fit */
2223 if (load >= image_start && load < image_end) {
2224 /* check is simplified; fit load checks for overlaps */
2225 printf("Overlayed FDT requires relocation\n");
2226 fdt_noffset = -EBADF;
2227 goto out;
2228 }
2229
2230 base = map_sysmem(load, len);
2231
2232 /* apply extra configs in FIT first, followed by args */
2233 for (i = 1; ; i++) {
2234 if (i < count) {
2235 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2236 FIT_FDT_PROP, i);
2237 uname = fit_get_name(fit, noffset, NULL);
2238 uconfig = NULL;
2239 } else {
2240 if (!next_config)
2241 break;
2242 uconfig = next_config;
2243 next_config = strchr(next_config, '#');
2244 if (next_config)
2245 *next_config++ = '\0';
2246 uname = NULL;
Peter Ujfalusiaf15dc42019-03-06 15:52:27 +02002247
2248 /*
2249 * fit_image_load() would load the first FDT from the
2250 * extra config only when uconfig is specified.
2251 * Check if the extra config contains multiple FDTs and
2252 * if so, load them.
2253 */
2254 cfg_noffset = fit_conf_get_node(fit, uconfig);
2255
2256 i = 0;
2257 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2258 FIT_FDT_PROP);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002259 }
2260
2261 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2262
2263 ov_noffset = fit_image_load(images,
2264 addr, &uname, &uconfig,
2265 arch, IH_TYPE_FLATDT,
2266 BOOTSTAGE_ID_FIT_FDT_START,
2267 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2268 if (ov_noffset < 0) {
2269 printf("load of %s failed\n", uname);
2270 continue;
2271 }
2272 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2273 uname, ovload, ovlen);
2274 ov = map_sysmem(ovload, ovlen);
2275
2276 base = map_sysmem(load, len + ovlen);
2277 err = fdt_open_into(base, base, len + ovlen);
2278 if (err < 0) {
2279 printf("failed on fdt_open_into\n");
2280 fdt_noffset = err;
2281 goto out;
2282 }
2283 /* the verbose method prints out messages on error */
2284 err = fdt_overlay_apply_verbose(base, ov);
2285 if (err < 0) {
2286 fdt_noffset = err;
2287 goto out;
2288 }
2289 fdt_pack(base);
2290 len = fdt_totalsize(base);
2291 }
2292#else
2293 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2294 fdt_noffset = -EBADF;
2295#endif
2296
2297out:
2298 if (datap)
2299 *datap = load;
2300 if (lenp)
2301 *lenp = len;
2302 if (fit_unamep)
2303 *fit_unamep = fit_uname;
2304 if (fit_uname_configp)
2305 *fit_uname_configp = fit_uname_config;
2306
2307 if (fit_uname_config_copy)
2308 free(fit_uname_config_copy);
2309 return fdt_noffset;
2310}
2311#endif