blob: 5c63c769de274e721ae767fae96da61f013bb8be [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>
14#else
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050015#include <linux/compiler.h>
York Sun48cd6ab2016-11-23 09:25:09 -080016#include <linux/kconfig.h>
Simon Glassda0af362013-05-07 06:11:53 +000017#include <common.h>
Simon Glass384d86d2013-05-16 13:53:21 +000018#include <errno.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glass384d86d2013-05-16 13:53:21 +000020#include <asm/io.h>
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +030021#include <malloc.h>
Simon Glass384d86d2013-05-16 13:53:21 +000022DECLARE_GLOBAL_DATA_PTR;
Simon Glassda0af362013-05-07 06:11:53 +000023#endif /* !USE_HOSTCC*/
24
Julius Werner97b09cd2019-07-24 19:37:55 -070025#include <bootm.h>
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050026#include <image.h>
Simon Glassda0af362013-05-07 06:11:53 +000027#include <bootstage.h>
Simon Glassda0af362013-05-07 06:11:53 +000028#include <u-boot/crc.h>
29#include <u-boot/md5.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020030#include <u-boot/sha1.h>
31#include <u-boot/sha256.h>
Simon Glassda0af362013-05-07 06:11:53 +000032
33/*****************************************************************************/
34/* New uImage format routines */
35/*****************************************************************************/
36#ifndef USE_HOSTCC
37static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
38 ulong *addr, const char **name)
39{
40 const char *sep;
41
42 *addr = addr_curr;
43 *name = NULL;
44
45 sep = strchr(spec, sepc);
46 if (sep) {
47 if (sep - spec > 0)
48 *addr = simple_strtoul(spec, NULL, 16);
49
50 *name = sep + 1;
51 return 1;
52 }
53
54 return 0;
55}
56
57/**
58 * fit_parse_conf - parse FIT configuration spec
59 * @spec: input string, containing configuration spec
60 * @add_curr: current image address (to be used as a possible default)
61 * @addr: pointer to a ulong variable, will hold FIT image address of a given
62 * configuration
63 * @conf_name double pointer to a char, will hold pointer to a configuration
64 * unit name
65 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090066 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glassda0af362013-05-07 06:11:53 +000067 * where <addr> is a FIT image address that contains configuration
68 * with a <conf> unit name.
69 *
70 * Address part is optional, and if omitted default add_curr will
71 * be used instead.
72 *
73 * returns:
74 * 1 if spec is a valid configuration string,
75 * addr and conf_name are set accordingly
76 * 0 otherwise
77 */
78int fit_parse_conf(const char *spec, ulong addr_curr,
79 ulong *addr, const char **conf_name)
80{
81 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
82}
83
84/**
85 * fit_parse_subimage - parse FIT subimage spec
86 * @spec: input string, containing subimage spec
87 * @add_curr: current image address (to be used as a possible default)
88 * @addr: pointer to a ulong variable, will hold FIT image address of a given
89 * subimage
90 * @image_name: double pointer to a char, will hold pointer to a subimage name
91 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090092 * fit_parse_subimage() expects subimage spec in the form of
Simon Glassda0af362013-05-07 06:11:53 +000093 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
94 * subimage with a <subimg> unit name.
95 *
96 * Address part is optional, and if omitted default add_curr will
97 * be used instead.
98 *
99 * returns:
100 * 1 if spec is a valid subimage string,
101 * addr and image_name are set accordingly
102 * 0 otherwise
103 */
104int fit_parse_subimage(const char *spec, ulong addr_curr,
105 ulong *addr, const char **image_name)
106{
107 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
108}
109#endif /* !USE_HOSTCC */
110
111static void fit_get_debug(const void *fit, int noffset,
112 char *prop_name, int err)
113{
114 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
115 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
116 fdt_strerror(err));
117}
118
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200119/**
120 * fit_get_subimage_count - get component (sub-image) count
121 * @fit: pointer to the FIT format image header
122 * @images_noffset: offset of images node
123 *
124 * returns:
125 * number of image components
126 */
127int fit_get_subimage_count(const void *fit, int images_noffset)
128{
129 int noffset;
130 int ndepth;
131 int count = 0;
132
133 /* Process its subnodes, print out component images details */
134 for (ndepth = 0, count = 0,
135 noffset = fdt_next_node(fit, images_noffset, &ndepth);
136 (noffset >= 0) && (ndepth > 0);
137 noffset = fdt_next_node(fit, noffset, &ndepth)) {
138 if (ndepth == 1) {
139 count++;
140 }
141 }
142
143 return count;
144}
145
Marek Vasut9f2e0eb2018-05-13 00:22:52 +0200146#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
Simon Glassda0af362013-05-07 06:11:53 +0000147/**
Tom Rini7d77b662018-05-08 14:34:05 -0400148 * fit_image_print_data() - prints out the hash node details
149 * @fit: pointer to the FIT format image header
150 * @noffset: offset of the hash node
151 * @p: pointer to prefix string
152 * @type: Type of information to print ("hash" or "sign")
153 *
154 * fit_image_print_data() lists properties for the processed hash node
155 *
156 * This function avoid using puts() since it prints a newline on the host
157 * but does not in U-Boot.
158 *
159 * returns:
160 * no returned results
161 */
162static void fit_image_print_data(const void *fit, int noffset, const char *p,
163 const char *type)
164{
165 const char *keyname;
166 uint8_t *value;
167 int value_len;
168 char *algo;
Philippe Reynes12468352018-11-14 13:51:00 +0100169 const char *padding;
Tom Rini7d77b662018-05-08 14:34:05 -0400170 int required;
171 int ret, i;
172
173 debug("%s %s node: '%s'\n", p, type,
174 fit_get_name(fit, noffset, NULL));
175 printf("%s %s algo: ", p, type);
176 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
177 printf("invalid/unsupported\n");
178 return;
179 }
180 printf("%s", algo);
181 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
182 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
183 if (keyname)
184 printf(":%s", keyname);
185 if (required)
186 printf(" (required)");
187 printf("\n");
188
Philippe Reynes12468352018-11-14 13:51:00 +0100189 padding = fdt_getprop(fit, noffset, "padding", NULL);
190 if (padding)
191 printf("%s %s padding: %s\n", p, type, padding);
192
Tom Rini7d77b662018-05-08 14:34:05 -0400193 ret = fit_image_hash_get_value(fit, noffset, &value,
194 &value_len);
195 printf("%s %s value: ", p, type);
196 if (ret) {
197 printf("unavailable\n");
198 } else {
199 for (i = 0; i < value_len; i++)
200 printf("%02x", value[i]);
201 printf("\n");
202 }
203
204 debug("%s %s len: %d\n", p, type, value_len);
205
206 /* Signatures have a time stamp */
207 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
208 time_t timestamp;
209
210 printf("%s Timestamp: ", p);
211 if (fit_get_timestamp(fit, noffset, &timestamp))
212 printf("unavailable\n");
213 else
214 genimg_print_time(timestamp);
215 }
216}
217
218/**
219 * fit_image_print_verification_data() - prints out the hash/signature details
220 * @fit: pointer to the FIT format image header
221 * @noffset: offset of the hash or signature node
222 * @p: pointer to prefix string
223 *
224 * This lists properties for the processed hash node
225 *
226 * returns:
227 * no returned results
228 */
229static void fit_image_print_verification_data(const void *fit, int noffset,
230 const char *p)
231{
232 const char *name;
233
234 /*
235 * Check subnode name, must be equal to "hash" or "signature".
236 * Multiple hash/signature nodes require unique unit node
237 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
238 */
239 name = fit_get_name(fit, noffset, NULL);
240 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
241 fit_image_print_data(fit, noffset, p, "Hash");
242 } else if (!strncmp(name, FIT_SIG_NODENAME,
243 strlen(FIT_SIG_NODENAME))) {
244 fit_image_print_data(fit, noffset, p, "Sign");
245 }
246}
247
248/**
249 * fit_conf_print - prints out the FIT configuration details
250 * @fit: pointer to the FIT format image header
251 * @noffset: offset of the configuration node
252 * @p: pointer to prefix string
253 *
254 * fit_conf_print() lists all mandatory properties for the processed
255 * configuration node.
256 *
257 * returns:
258 * no returned results
259 */
260static void fit_conf_print(const void *fit, int noffset, const char *p)
261{
262 char *desc;
263 const char *uname;
264 int ret;
265 int fdt_index, loadables_index;
266 int ndepth;
267
268 /* Mandatory properties */
269 ret = fit_get_desc(fit, noffset, &desc);
270 printf("%s Description: ", p);
271 if (ret)
272 printf("unavailable\n");
273 else
274 printf("%s\n", desc);
275
276 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
277 printf("%s Kernel: ", p);
278 if (!uname)
279 printf("unavailable\n");
280 else
281 printf("%s\n", uname);
282
283 /* Optional properties */
284 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
285 if (uname)
286 printf("%s Init Ramdisk: %s\n", p, uname);
287
288 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
289 if (uname)
290 printf("%s Firmware: %s\n", p, uname);
291
292 for (fdt_index = 0;
293 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
294 fdt_index, NULL), uname;
295 fdt_index++) {
296 if (fdt_index == 0)
297 printf("%s FDT: ", p);
298 else
299 printf("%s ", p);
300 printf("%s\n", uname);
301 }
302
303 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
304 if (uname)
305 printf("%s FPGA: %s\n", p, uname);
306
307 /* Print out all of the specified loadables */
308 for (loadables_index = 0;
309 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
310 loadables_index, NULL), uname;
311 loadables_index++) {
312 if (loadables_index == 0) {
313 printf("%s Loadables: ", p);
314 } else {
315 printf("%s ", p);
316 }
317 printf("%s\n", uname);
318 }
319
320 /* Process all hash subnodes of the component configuration node */
321 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
322 (noffset >= 0) && (ndepth > 0);
323 noffset = fdt_next_node(fit, noffset, &ndepth)) {
324 if (ndepth == 1) {
325 /* Direct child node of the component configuration node */
326 fit_image_print_verification_data(fit, noffset, p);
327 }
328 }
329}
330
331/**
Simon Glassda0af362013-05-07 06:11:53 +0000332 * fit_print_contents - prints out the contents of the FIT format image
333 * @fit: pointer to the FIT format image header
334 * @p: pointer to prefix string
335 *
336 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500337 * The routine prints out FIT image properties (root node level) followed by
Simon Glassda0af362013-05-07 06:11:53 +0000338 * the details of each component image.
339 *
340 * returns:
341 * no returned results
342 */
343void fit_print_contents(const void *fit)
344{
345 char *desc;
346 char *uname;
347 int images_noffset;
348 int confs_noffset;
349 int noffset;
350 int ndepth;
351 int count = 0;
352 int ret;
353 const char *p;
354 time_t timestamp;
355
Simon Glass1030f162013-05-08 08:05:58 +0000356 /* Indent string is defined in header image.h */
357 p = IMAGE_INDENT_STRING;
Simon Glassda0af362013-05-07 06:11:53 +0000358
359 /* Root node properties */
360 ret = fit_get_desc(fit, 0, &desc);
361 printf("%sFIT description: ", p);
362 if (ret)
363 printf("unavailable\n");
364 else
365 printf("%s\n", desc);
366
367 if (IMAGE_ENABLE_TIMESTAMP) {
368 ret = fit_get_timestamp(fit, 0, &timestamp);
369 printf("%sCreated: ", p);
370 if (ret)
371 printf("unavailable\n");
372 else
373 genimg_print_time(timestamp);
374 }
375
376 /* Find images parent node offset */
377 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
378 if (images_noffset < 0) {
379 printf("Can't find images parent node '%s' (%s)\n",
380 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
381 return;
382 }
383
384 /* Process its subnodes, print out component images details */
385 for (ndepth = 0, count = 0,
386 noffset = fdt_next_node(fit, images_noffset, &ndepth);
387 (noffset >= 0) && (ndepth > 0);
388 noffset = fdt_next_node(fit, noffset, &ndepth)) {
389 if (ndepth == 1) {
390 /*
391 * Direct child node of the images parent node,
392 * i.e. component image node.
393 */
394 printf("%s Image %u (%s)\n", p, count++,
395 fit_get_name(fit, noffset, NULL));
396
397 fit_image_print(fit, noffset, p);
398 }
399 }
400
401 /* Find configurations parent node offset */
402 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
403 if (confs_noffset < 0) {
404 debug("Can't get configurations parent node '%s' (%s)\n",
405 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
406 return;
407 }
408
409 /* get default configuration unit name from default property */
410 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
411 if (uname)
412 printf("%s Default Configuration: '%s'\n", p, uname);
413
414 /* Process its subnodes, print out configurations details */
415 for (ndepth = 0, count = 0,
416 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
417 (noffset >= 0) && (ndepth > 0);
418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
419 if (ndepth == 1) {
420 /*
421 * Direct child node of the configurations parent node,
422 * i.e. configuration node.
423 */
424 printf("%s Configuration %u (%s)\n", p, count++,
425 fit_get_name(fit, noffset, NULL));
426
427 fit_conf_print(fit, noffset, p);
428 }
429 }
430}
431
432/**
433 * fit_image_print - prints out the FIT component image details
434 * @fit: pointer to the FIT format image header
435 * @image_noffset: offset of the component image node
436 * @p: pointer to prefix string
437 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500438 * fit_image_print() lists all mandatory properties for the processed component
Simon Glassda0af362013-05-07 06:11:53 +0000439 * image. If present, hash nodes are printed out as well. Load
440 * address for images of type firmware is also printed out. Since the load
441 * address is not mandatory for firmware images, it will be output as
442 * "unavailable" when not present.
443 *
444 * returns:
445 * no returned results
446 */
447void fit_image_print(const void *fit, int image_noffset, const char *p)
448{
449 char *desc;
450 uint8_t type, arch, os, comp;
451 size_t size;
452 ulong load, entry;
453 const void *data;
454 int noffset;
455 int ndepth;
456 int ret;
457
458 /* Mandatory properties */
459 ret = fit_get_desc(fit, image_noffset, &desc);
460 printf("%s Description: ", p);
461 if (ret)
462 printf("unavailable\n");
463 else
464 printf("%s\n", desc);
465
Simon Glass749a6e72013-07-16 20:10:01 -0700466 if (IMAGE_ENABLE_TIMESTAMP) {
467 time_t timestamp;
468
469 ret = fit_get_timestamp(fit, 0, &timestamp);
470 printf("%s Created: ", p);
471 if (ret)
472 printf("unavailable\n");
473 else
474 genimg_print_time(timestamp);
475 }
476
Simon Glassda0af362013-05-07 06:11:53 +0000477 fit_image_get_type(fit, image_noffset, &type);
478 printf("%s Type: %s\n", p, genimg_get_type_name(type));
479
480 fit_image_get_comp(fit, image_noffset, &comp);
481 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
482
Kelvin Cheung186cc992018-05-19 18:21:37 +0800483 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
Simon Glassda0af362013-05-07 06:11:53 +0000484
485#ifndef USE_HOSTCC
486 printf("%s Data Start: ", p);
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000487 if (ret) {
Simon Glassda0af362013-05-07 06:11:53 +0000488 printf("unavailable\n");
Simon Glasse8d6a5b2013-05-16 13:53:26 +0000489 } else {
490 void *vdata = (void *)data;
491
492 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
493 }
Simon Glassda0af362013-05-07 06:11:53 +0000494#endif
495
496 printf("%s Data Size: ", p);
497 if (ret)
498 printf("unavailable\n");
499 else
500 genimg_print_size(size);
501
502 /* Remaining, type dependent properties */
503 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
504 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
505 (type == IH_TYPE_FLATDT)) {
506 fit_image_get_arch(fit, image_noffset, &arch);
507 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
508 }
509
Michal Simek55f698f2018-03-26 16:31:27 +0200510 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
511 (type == IH_TYPE_FIRMWARE)) {
Simon Glassda0af362013-05-07 06:11:53 +0000512 fit_image_get_os(fit, image_noffset, &os);
513 printf("%s OS: %s\n", p, genimg_get_os_name(os));
514 }
515
516 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simekebae78b2016-05-17 14:03:50 +0200517 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
518 (type == IH_TYPE_FPGA)) {
Simon Glassda0af362013-05-07 06:11:53 +0000519 ret = fit_image_get_load(fit, image_noffset, &load);
520 printf("%s Load Address: ", p);
521 if (ret)
522 printf("unavailable\n");
523 else
524 printf("0x%08lx\n", load);
525 }
526
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +0300527 /* optional load address for FDT */
528 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
529 printf("%s Load Address: 0x%08lx\n", p, load);
530
Simon Glassda0af362013-05-07 06:11:53 +0000531 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
532 (type == IH_TYPE_RAMDISK)) {
York Sun2b0464e2016-02-29 15:48:40 -0800533 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glassda0af362013-05-07 06:11:53 +0000534 printf("%s Entry Point: ", p);
535 if (ret)
536 printf("unavailable\n");
537 else
538 printf("0x%08lx\n", entry);
539 }
540
541 /* Process all hash subnodes of the component image node */
542 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
543 (noffset >= 0) && (ndepth > 0);
544 noffset = fdt_next_node(fit, noffset, &ndepth)) {
545 if (ndepth == 1) {
546 /* Direct child node of the component image node */
Simon Glasse61aa442013-05-07 06:12:02 +0000547 fit_image_print_verification_data(fit, noffset, p);
Simon Glassda0af362013-05-07 06:11:53 +0000548 }
549 }
Simon Glassda0af362013-05-07 06:11:53 +0000550}
Marek Vasut25114a62018-05-13 00:22:53 +0200551#else
552void fit_print_contents(const void *fit) { }
553void fit_image_print(const void *fit, int image_noffset, const char *p) { }
Marek Vasut9f2e0eb2018-05-13 00:22:52 +0200554#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
Simon Glassda0af362013-05-07 06:11:53 +0000555
556/**
557 * fit_get_desc - get node description property
558 * @fit: pointer to the FIT format image header
559 * @noffset: node offset
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500560 * @desc: double pointer to the char, will hold pointer to the description
Simon Glassda0af362013-05-07 06:11:53 +0000561 *
562 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500563 * description is found pointer to it is returned in third call argument.
Simon Glassda0af362013-05-07 06:11:53 +0000564 *
565 * returns:
566 * 0, on success
567 * -1, on failure
568 */
569int fit_get_desc(const void *fit, int noffset, char **desc)
570{
571 int len;
572
573 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
574 if (*desc == NULL) {
575 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
576 return -1;
577 }
578
579 return 0;
580}
581
582/**
583 * fit_get_timestamp - get node timestamp property
584 * @fit: pointer to the FIT format image header
585 * @noffset: node offset
586 * @timestamp: pointer to the time_t, will hold read timestamp
587 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500588 * fit_get_timestamp() reads timestamp property from given node, if timestamp
589 * is found and has a correct size its value is returned in third call
Simon Glassda0af362013-05-07 06:11:53 +0000590 * argument.
591 *
592 * returns:
593 * 0, on success
594 * -1, on property read failure
595 * -2, on wrong timestamp size
596 */
597int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
598{
599 int len;
600 const void *data;
601
602 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
603 if (data == NULL) {
604 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
605 return -1;
606 }
607 if (len != sizeof(uint32_t)) {
608 debug("FIT timestamp with incorrect size of (%u)\n", len);
609 return -2;
610 }
611
612 *timestamp = uimage_to_cpu(*((uint32_t *)data));
613 return 0;
614}
615
616/**
617 * fit_image_get_node - get node offset for component image of a given unit name
618 * @fit: pointer to the FIT format image header
619 * @image_uname: component image node unit name
620 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500621 * fit_image_get_node() finds a component image (within the '/images'
Simon Glassda0af362013-05-07 06:11:53 +0000622 * node) of a provided unit name. If image is found its node offset is
623 * returned to the caller.
624 *
625 * returns:
626 * image node offset when found (>=0)
627 * negative number on failure (FDT_ERR_* code)
628 */
629int fit_image_get_node(const void *fit, const char *image_uname)
630{
631 int noffset, images_noffset;
632
633 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
634 if (images_noffset < 0) {
635 debug("Can't find images parent node '%s' (%s)\n",
636 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
637 return images_noffset;
638 }
639
640 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
641 if (noffset < 0) {
642 debug("Can't get node offset for image unit name: '%s' (%s)\n",
643 image_uname, fdt_strerror(noffset));
644 }
645
646 return noffset;
647}
648
649/**
650 * fit_image_get_os - get os id for a given component image node
651 * @fit: pointer to the FIT format image header
652 * @noffset: component image node offset
653 * @os: pointer to the uint8_t, will hold os numeric id
654 *
655 * fit_image_get_os() finds os property in a given component image node.
656 * If the property is found, its (string) value is translated to the numeric
657 * id which is returned to the caller.
658 *
659 * returns:
660 * 0, on success
661 * -1, on failure
662 */
663int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
664{
665 int len;
666 const void *data;
667
668 /* Get OS name from property data */
669 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
670 if (data == NULL) {
671 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
672 *os = -1;
673 return -1;
674 }
675
676 /* Translate OS name to id */
677 *os = genimg_get_os_id(data);
678 return 0;
679}
680
681/**
682 * fit_image_get_arch - get arch id for a given component image node
683 * @fit: pointer to the FIT format image header
684 * @noffset: component image node offset
685 * @arch: pointer to the uint8_t, will hold arch numeric id
686 *
687 * fit_image_get_arch() finds arch property in a given component image node.
688 * If the property is found, its (string) value is translated to the numeric
689 * id which is returned to the caller.
690 *
691 * returns:
692 * 0, on success
693 * -1, on failure
694 */
695int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
696{
697 int len;
698 const void *data;
699
700 /* Get architecture name from property data */
701 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
702 if (data == NULL) {
703 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
704 *arch = -1;
705 return -1;
706 }
707
708 /* Translate architecture name to id */
709 *arch = genimg_get_arch_id(data);
710 return 0;
711}
712
713/**
714 * fit_image_get_type - get type id for a given component image node
715 * @fit: pointer to the FIT format image header
716 * @noffset: component image node offset
717 * @type: pointer to the uint8_t, will hold type numeric id
718 *
719 * fit_image_get_type() finds type property in a given component image node.
720 * If the property is found, its (string) value is translated to the numeric
721 * id which is returned to the caller.
722 *
723 * returns:
724 * 0, on success
725 * -1, on failure
726 */
727int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
728{
729 int len;
730 const void *data;
731
732 /* Get image type name from property data */
733 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
734 if (data == NULL) {
735 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
736 *type = -1;
737 return -1;
738 }
739
740 /* Translate image type name to id */
741 *type = genimg_get_type_id(data);
742 return 0;
743}
744
745/**
746 * fit_image_get_comp - get comp id for a given component image node
747 * @fit: pointer to the FIT format image header
748 * @noffset: component image node offset
749 * @comp: pointer to the uint8_t, will hold comp numeric id
750 *
751 * fit_image_get_comp() finds comp property in a given component image node.
752 * If the property is found, its (string) value is translated to the numeric
753 * id which is returned to the caller.
754 *
755 * returns:
756 * 0, on success
757 * -1, on failure
758 */
759int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
760{
761 int len;
762 const void *data;
763
764 /* Get compression name from property data */
765 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
766 if (data == NULL) {
767 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
768 *comp = -1;
769 return -1;
770 }
771
772 /* Translate compression name to id */
773 *comp = genimg_get_comp_id(data);
774 return 0;
775}
776
York Sun2b0464e2016-02-29 15:48:40 -0800777static int fit_image_get_address(const void *fit, int noffset, char *name,
778 ulong *load)
779{
York Sun31e5add2016-02-29 15:48:41 -0800780 int len, cell_len;
781 const fdt32_t *cell;
782 uint64_t load64 = 0;
York Sun2b0464e2016-02-29 15:48:40 -0800783
York Sun31e5add2016-02-29 15:48:41 -0800784 cell = fdt_getprop(fit, noffset, name, &len);
785 if (cell == NULL) {
York Sun2b0464e2016-02-29 15:48:40 -0800786 fit_get_debug(fit, noffset, name, len);
787 return -1;
788 }
789
York Sun31e5add2016-02-29 15:48:41 -0800790 if (len > sizeof(ulong)) {
791 printf("Unsupported %s address size\n", name);
792 return -1;
793 }
794
795 cell_len = len >> 2;
796 /* Use load64 to avoid compiling warning for 32-bit target */
797 while (cell_len--) {
798 load64 = (load64 << 32) | uimage_to_cpu(*cell);
799 cell++;
800 }
801 *load = (ulong)load64;
York Sun2b0464e2016-02-29 15:48:40 -0800802
803 return 0;
804}
Simon Glassda0af362013-05-07 06:11:53 +0000805/**
806 * fit_image_get_load() - get load addr property for given component image node
807 * @fit: pointer to the FIT format image header
808 * @noffset: component image node offset
809 * @load: pointer to the uint32_t, will hold load address
810 *
811 * fit_image_get_load() finds load address property in a given component
812 * image node. If the property is found, its value is returned to the caller.
813 *
814 * returns:
815 * 0, on success
816 * -1, on failure
817 */
818int fit_image_get_load(const void *fit, int noffset, ulong *load)
819{
York Sun2b0464e2016-02-29 15:48:40 -0800820 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glassda0af362013-05-07 06:11:53 +0000821}
822
823/**
824 * fit_image_get_entry() - get entry point address property
825 * @fit: pointer to the FIT format image header
826 * @noffset: component image node offset
827 * @entry: pointer to the uint32_t, will hold entry point address
828 *
829 * This gets the entry point address property for a given component image
830 * node.
831 *
832 * fit_image_get_entry() finds entry point address property in a given
833 * component image node. If the property is found, its value is returned
834 * to the caller.
835 *
836 * returns:
837 * 0, on success
838 * -1, on failure
839 */
840int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
841{
York Sun2b0464e2016-02-29 15:48:40 -0800842 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glassda0af362013-05-07 06:11:53 +0000843}
844
845/**
846 * fit_image_get_data - get data property and its size for a given component image node
847 * @fit: pointer to the FIT format image header
848 * @noffset: component image node offset
849 * @data: double pointer to void, will hold data property's data address
850 * @size: pointer to size_t, will hold data property's data size
851 *
852 * fit_image_get_data() finds data property in a given component image node.
853 * If the property is found its data start address and size are returned to
854 * the caller.
855 *
856 * returns:
857 * 0, on success
858 * -1, on failure
859 */
860int fit_image_get_data(const void *fit, int noffset,
861 const void **data, size_t *size)
862{
863 int len;
864
865 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
866 if (*data == NULL) {
867 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
868 *size = 0;
869 return -1;
870 }
871
872 *size = len;
873 return 0;
874}
875
876/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200877 * Get 'data-offset' property from a given image node.
878 *
879 * @fit: pointer to the FIT image header
880 * @noffset: component image node offset
881 * @data_offset: holds the data-offset property
882 *
883 * returns:
884 * 0, on success
885 * -ENOENT if the property could not be found
886 */
887int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
888{
889 const fdt32_t *val;
890
891 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
892 if (!val)
893 return -ENOENT;
894
895 *data_offset = fdt32_to_cpu(*val);
896
897 return 0;
898}
899
900/**
Peng Fan8876c7e2017-12-05 13:20:59 +0800901 * Get 'data-position' property from a given image node.
902 *
903 * @fit: pointer to the FIT image header
904 * @noffset: component image node offset
905 * @data_position: holds the data-position property
906 *
907 * returns:
908 * 0, on success
909 * -ENOENT if the property could not be found
910 */
911int fit_image_get_data_position(const void *fit, int noffset,
912 int *data_position)
913{
914 const fdt32_t *val;
915
916 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
917 if (!val)
918 return -ENOENT;
919
920 *data_position = fdt32_to_cpu(*val);
921
922 return 0;
923}
924
925/**
tomas.melin@vaisala.com45ee7902017-01-13 13:20:14 +0200926 * Get 'data-size' property from a given image node.
927 *
928 * @fit: pointer to the FIT image header
929 * @noffset: component image node offset
930 * @data_size: holds the data-size property
931 *
932 * returns:
933 * 0, on success
934 * -ENOENT if the property could not be found
935 */
936int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
937{
938 const fdt32_t *val;
939
940 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
941 if (!val)
942 return -ENOENT;
943
944 *data_size = fdt32_to_cpu(*val);
945
946 return 0;
947}
948
949/**
Kelvin Cheung186cc992018-05-19 18:21:37 +0800950 * fit_image_get_data_and_size - get data and its size including
951 * both embedded and external data
952 * @fit: pointer to the FIT format image header
953 * @noffset: component image node offset
954 * @data: double pointer to void, will hold data property's data address
955 * @size: pointer to size_t, will hold data property's data size
956 *
957 * fit_image_get_data_and_size() finds data and its size including
958 * both embedded and external data. If the property is found
959 * its data start address and size are returned to the caller.
960 *
961 * returns:
962 * 0, on success
963 * otherwise, on failure
964 */
965int fit_image_get_data_and_size(const void *fit, int noffset,
966 const void **data, size_t *size)
967{
968 bool external_data = false;
969 int offset;
970 int len;
971 int ret;
972
973 if (!fit_image_get_data_position(fit, noffset, &offset)) {
974 external_data = true;
975 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
976 external_data = true;
977 /*
978 * For FIT with external data, figure out where
979 * the external images start. This is the base
980 * for the data-offset properties in each image.
981 */
982 offset += ((fdt_totalsize(fit) + 3) & ~3);
983 }
984
985 if (external_data) {
986 debug("External Data\n");
987 ret = fit_image_get_data_size(fit, noffset, &len);
988 *data = fit + offset;
989 *size = len;
990 } else {
991 ret = fit_image_get_data(fit, noffset, data, size);
992 }
993
994 return ret;
995}
996
997/**
Simon Glassda0af362013-05-07 06:11:53 +0000998 * fit_image_hash_get_algo - get hash algorithm name
999 * @fit: pointer to the FIT format image header
1000 * @noffset: hash node offset
1001 * @algo: double pointer to char, will hold pointer to the algorithm name
1002 *
1003 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1004 * If the property is found its data start address is returned to the caller.
1005 *
1006 * returns:
1007 * 0, on success
1008 * -1, on failure
1009 */
1010int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1011{
1012 int len;
1013
1014 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1015 if (*algo == NULL) {
1016 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1017 return -1;
1018 }
1019
1020 return 0;
1021}
1022
1023/**
1024 * fit_image_hash_get_value - get hash value and length
1025 * @fit: pointer to the FIT format image header
1026 * @noffset: hash node offset
1027 * @value: double pointer to uint8_t, will hold address of a hash value data
1028 * @value_len: pointer to an int, will hold hash data length
1029 *
1030 * fit_image_hash_get_value() finds hash value property in a given hash node.
1031 * If the property is found its data start address and size are returned to
1032 * the caller.
1033 *
1034 * returns:
1035 * 0, on success
1036 * -1, on failure
1037 */
1038int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1039 int *value_len)
1040{
1041 int len;
1042
1043 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1044 if (*value == NULL) {
1045 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1046 *value_len = 0;
1047 return -1;
1048 }
1049
1050 *value_len = len;
1051 return 0;
1052}
1053
Simon Glassda0af362013-05-07 06:11:53 +00001054/**
1055 * fit_image_hash_get_ignore - get hash ignore flag
1056 * @fit: pointer to the FIT format image header
1057 * @noffset: hash node offset
1058 * @ignore: pointer to an int, will hold hash ignore flag
1059 *
1060 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1061 * If the property is found and non-zero, the hash algorithm is not verified by
1062 * u-boot automatically.
1063 *
1064 * returns:
1065 * 0, on ignore not found
1066 * value, on ignore found
1067 */
Simon Glassf0fd5112013-05-07 06:11:58 +00001068static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glassda0af362013-05-07 06:11:53 +00001069{
1070 int len;
1071 int *value;
1072
1073 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1074 if (value == NULL || len != sizeof(int))
1075 *ignore = 0;
1076 else
1077 *ignore = *value;
1078
1079 return 0;
1080}
Simon Glassda0af362013-05-07 06:11:53 +00001081
Simon Glass5b539a02016-02-24 09:14:42 -07001082ulong fit_get_end(const void *fit)
1083{
1084 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1085}
1086
Simon Glassda0af362013-05-07 06:11:53 +00001087/**
1088 * fit_set_timestamp - set node timestamp property
1089 * @fit: pointer to the FIT format image header
1090 * @noffset: node offset
1091 * @timestamp: timestamp value to be set
1092 *
1093 * fit_set_timestamp() attempts to set timestamp property in the requested
1094 * node and returns operation status to the caller.
1095 *
1096 * returns:
1097 * 0, on success
Simon Glassaf2f9d52014-06-02 22:04:51 -06001098 * -ENOSPC if no space in device tree, -1 for other error
Simon Glassda0af362013-05-07 06:11:53 +00001099 */
1100int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1101{
1102 uint32_t t;
1103 int ret;
1104
1105 t = cpu_to_uimage(timestamp);
1106 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1107 sizeof(uint32_t));
1108 if (ret) {
Simon Glasse4016fa2016-05-01 13:55:37 -06001109 debug("Can't set '%s' property for '%s' node (%s)\n",
1110 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1111 fdt_strerror(ret));
Simon Glassaf2f9d52014-06-02 22:04:51 -06001112 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glassda0af362013-05-07 06:11:53 +00001113 }
1114
1115 return 0;
1116}
1117
1118/**
1119 * calculate_hash - calculate and return hash for provided input data
1120 * @data: pointer to the input data
1121 * @data_len: data length
1122 * @algo: requested hash algorithm
1123 * @value: pointer to the char, will hold hash value data (caller must
1124 * allocate enough free space)
1125 * value_len: length of the calculated hash
1126 *
1127 * calculate_hash() computes input data hash according to the requested
1128 * algorithm.
1129 * Resulting hash value is placed in caller provided 'value' buffer, length
1130 * of the calculated hash is returned via value_len pointer argument.
1131 *
1132 * returns:
1133 * 0, on success
1134 * -1, when algo is unsupported
1135 */
Simon Glass10a1eca2013-05-07 06:11:54 +00001136int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glassda0af362013-05-07 06:11:53 +00001137 uint8_t *value, int *value_len)
1138{
Simon Glass82d94532013-05-08 08:05:59 +00001139 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001140 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1141 CHUNKSZ_CRC32);
1142 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1143 *value_len = 4;
Simon Glass82d94532013-05-08 08:05:59 +00001144 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001145 sha1_csum_wd((unsigned char *)data, data_len,
1146 (unsigned char *)value, CHUNKSZ_SHA1);
1147 *value_len = 20;
Heiko Schocher8ae33802014-03-03 12:19:25 +01001148 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1149 sha256_csum_wd((unsigned char *)data, data_len,
1150 (unsigned char *)value, CHUNKSZ_SHA256);
1151 *value_len = SHA256_SUM_LEN;
Simon Glass82d94532013-05-08 08:05:59 +00001152 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +00001153 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1154 *value_len = 16;
1155 } else {
1156 debug("Unsupported hash alogrithm\n");
1157 return -1;
1158 }
1159 return 0;
1160}
1161
Simon Glassf0fd5112013-05-07 06:11:58 +00001162static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1163 size_t size, char **err_msgp)
1164{
1165 uint8_t value[FIT_MAX_HASH_LEN];
1166 int value_len;
1167 char *algo;
1168 uint8_t *fit_value;
1169 int fit_value_len;
1170 int ignore;
1171
1172 *err_msgp = NULL;
1173
1174 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001175 *err_msgp = "Can't get hash algo property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001176 return -1;
1177 }
1178 printf("%s", algo);
1179
1180 if (IMAGE_ENABLE_IGNORE) {
1181 fit_image_hash_get_ignore(fit, noffset, &ignore);
1182 if (ignore) {
1183 printf("-skipped ");
1184 return 0;
1185 }
1186 }
1187
1188 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1189 &fit_value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001190 *err_msgp = "Can't get hash value property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001191 return -1;
1192 }
1193
1194 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001195 *err_msgp = "Unsupported hash algorithm";
Simon Glassf0fd5112013-05-07 06:11:58 +00001196 return -1;
1197 }
1198
1199 if (value_len != fit_value_len) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001200 *err_msgp = "Bad hash value len";
Simon Glassf0fd5112013-05-07 06:11:58 +00001201 return -1;
1202 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001203 *err_msgp = "Bad hash value";
Simon Glassf0fd5112013-05-07 06:11:58 +00001204 return -1;
1205 }
1206
1207 return 0;
1208}
1209
Jun Nieadf5d1c2018-02-27 16:55:58 +08001210int fit_image_verify_with_data(const void *fit, int image_noffset,
1211 const void *data, size_t size)
Simon Glassda0af362013-05-07 06:11:53 +00001212{
Simon Glassfbabc0f2013-06-13 15:10:01 -07001213 int noffset = 0;
Simon Glassda0af362013-05-07 06:11:53 +00001214 char *err_msg = "";
Simon Glassfbabc0f2013-06-13 15:10:01 -07001215 int verify_all = 1;
1216 int ret;
Simon Glassda0af362013-05-07 06:11:53 +00001217
Simon Glassfbabc0f2013-06-13 15:10:01 -07001218 /* Verify all required signatures */
1219 if (IMAGE_ENABLE_VERIFY &&
1220 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1221 gd_fdt_blob(), &verify_all)) {
1222 err_msg = "Unable to verify required signature";
1223 goto error;
1224 }
1225
Simon Glassda0af362013-05-07 06:11:53 +00001226 /* Process all hash subnodes of the component image node */
Simon Glass499c29e2016-10-02 17:59:29 -06001227 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassf0fd5112013-05-07 06:11:58 +00001228 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001229
Simon Glassf0fd5112013-05-07 06:11:58 +00001230 /*
1231 * Check subnode name, must be equal to "hash".
1232 * Multiple hash nodes require unique unit node
Andre Przywara3234ecd2017-12-04 02:05:10 +00001233 * names, e.g. hash-1, hash-2, etc.
Simon Glassf0fd5112013-05-07 06:11:58 +00001234 */
1235 if (!strncmp(name, FIT_HASH_NODENAME,
1236 strlen(FIT_HASH_NODENAME))) {
1237 if (fit_image_check_hash(fit, noffset, data, size,
1238 &err_msg))
Simon Glassda0af362013-05-07 06:11:53 +00001239 goto error;
Simon Glassf0fd5112013-05-07 06:11:58 +00001240 puts("+ ");
Simon Glassfbabc0f2013-06-13 15:10:01 -07001241 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1242 !strncmp(name, FIT_SIG_NODENAME,
1243 strlen(FIT_SIG_NODENAME))) {
1244 ret = fit_image_check_sig(fit, noffset, data,
1245 size, -1, &err_msg);
Simon Glass51026aa2016-02-24 09:14:43 -07001246
1247 /*
1248 * Show an indication on failure, but do not return
1249 * an error. Only keys marked 'required' can cause
1250 * an image validation failure. See the call to
1251 * fit_image_verify_required_sigs() above.
1252 */
1253 if (ret)
Simon Glassfbabc0f2013-06-13 15:10:01 -07001254 puts("- ");
1255 else
1256 puts("+ ");
Simon Glassda0af362013-05-07 06:11:53 +00001257 }
1258 }
1259
1260 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001261 err_msg = "Corrupted or truncated tree";
Simon Glassda0af362013-05-07 06:11:53 +00001262 goto error;
1263 }
1264
1265 return 1;
1266
1267error:
Simon Glass8f3aa462013-05-07 06:11:59 +00001268 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glassda0af362013-05-07 06:11:53 +00001269 err_msg, fit_get_name(fit, noffset, NULL),
1270 fit_get_name(fit, image_noffset, NULL));
1271 return 0;
1272}
1273
1274/**
Jun Nieadf5d1c2018-02-27 16:55:58 +08001275 * fit_image_verify - verify data integrity
1276 * @fit: pointer to the FIT format image header
1277 * @image_noffset: component image node offset
1278 *
1279 * fit_image_verify() goes over component image hash nodes,
1280 * re-calculates each data hash and compares with the value stored in hash
1281 * node.
1282 *
1283 * returns:
1284 * 1, if all hashes are valid
1285 * 0, otherwise (or on error)
1286 */
1287int fit_image_verify(const void *fit, int image_noffset)
1288{
1289 const void *data;
1290 size_t size;
1291 int noffset = 0;
1292 char *err_msg = "";
1293
1294 /* Get image data and data length */
Kelvin Cheung186cc992018-05-19 18:21:37 +08001295 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
Jun Nieadf5d1c2018-02-27 16:55:58 +08001296 err_msg = "Can't get image data/size";
1297 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1298 err_msg, fit_get_name(fit, noffset, NULL),
1299 fit_get_name(fit, image_noffset, NULL));
1300 return 0;
1301 }
1302
1303 return fit_image_verify_with_data(fit, image_noffset, data, size);
1304}
1305
1306/**
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001307 * fit_all_image_verify - verify data integrity for all images
Simon Glassda0af362013-05-07 06:11:53 +00001308 * @fit: pointer to the FIT format image header
1309 *
Simon Glass7428ad12013-05-07 06:11:57 +00001310 * fit_all_image_verify() goes over all images in the FIT and
Simon Glassda0af362013-05-07 06:11:53 +00001311 * for every images checks if all it's hashes are valid.
1312 *
1313 * returns:
1314 * 1, if all hashes of all images are valid
1315 * 0, otherwise (or on error)
1316 */
Simon Glass7428ad12013-05-07 06:11:57 +00001317int fit_all_image_verify(const void *fit)
Simon Glassda0af362013-05-07 06:11:53 +00001318{
1319 int images_noffset;
1320 int noffset;
1321 int ndepth;
1322 int count;
1323
1324 /* Find images parent node offset */
1325 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1326 if (images_noffset < 0) {
1327 printf("Can't find images parent node '%s' (%s)\n",
1328 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1329 return 0;
1330 }
1331
1332 /* Process all image subnodes, check hashes for each */
1333 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1334 (ulong)fit);
1335 for (ndepth = 0, count = 0,
1336 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1337 (noffset >= 0) && (ndepth > 0);
1338 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1339 if (ndepth == 1) {
1340 /*
1341 * Direct child node of the images parent node,
1342 * i.e. component image node.
1343 */
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001344 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glassda0af362013-05-07 06:11:53 +00001345 fit_get_name(fit, noffset, NULL));
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001346 count++;
Simon Glassda0af362013-05-07 06:11:53 +00001347
Simon Glass7428ad12013-05-07 06:11:57 +00001348 if (!fit_image_verify(fit, noffset))
Simon Glassda0af362013-05-07 06:11:53 +00001349 return 0;
1350 printf("\n");
1351 }
1352 }
1353 return 1;
1354}
1355
1356/**
1357 * fit_image_check_os - check whether image node is of a given os type
1358 * @fit: pointer to the FIT format image header
1359 * @noffset: component image node offset
1360 * @os: requested image os
1361 *
1362 * fit_image_check_os() reads image os property and compares its numeric
1363 * id with the requested os. Comparison result is returned to the caller.
1364 *
1365 * returns:
1366 * 1 if image is of given os type
1367 * 0 otherwise (or on error)
1368 */
1369int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1370{
1371 uint8_t image_os;
1372
1373 if (fit_image_get_os(fit, noffset, &image_os))
1374 return 0;
1375 return (os == image_os);
1376}
1377
1378/**
1379 * fit_image_check_arch - check whether image node is of a given arch
1380 * @fit: pointer to the FIT format image header
1381 * @noffset: component image node offset
1382 * @arch: requested imagearch
1383 *
1384 * fit_image_check_arch() reads image arch property and compares its numeric
1385 * id with the requested arch. Comparison result is returned to the caller.
1386 *
1387 * returns:
1388 * 1 if image is of given arch
1389 * 0 otherwise (or on error)
1390 */
1391int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1392{
1393 uint8_t image_arch;
Alison Wang73818d52016-11-10 10:49:03 +08001394 int aarch32_support = 0;
1395
1396#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1397 aarch32_support = 1;
1398#endif
Simon Glassda0af362013-05-07 06:11:53 +00001399
1400 if (fit_image_get_arch(fit, noffset, &image_arch))
1401 return 0;
Simon Glass9d428302014-10-10 08:21:57 -06001402 return (arch == image_arch) ||
Alison Wang73818d52016-11-10 10:49:03 +08001403 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1404 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1405 aarch32_support);
Simon Glassda0af362013-05-07 06:11:53 +00001406}
1407
1408/**
1409 * fit_image_check_type - check whether image node is of a given type
1410 * @fit: pointer to the FIT format image header
1411 * @noffset: component image node offset
1412 * @type: requested image type
1413 *
1414 * fit_image_check_type() reads image type property and compares its numeric
1415 * id with the requested type. Comparison result is returned to the caller.
1416 *
1417 * returns:
1418 * 1 if image is of given type
1419 * 0 otherwise (or on error)
1420 */
1421int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1422{
1423 uint8_t image_type;
1424
1425 if (fit_image_get_type(fit, noffset, &image_type))
1426 return 0;
1427 return (type == image_type);
1428}
1429
1430/**
1431 * fit_image_check_comp - check whether image node uses given compression
1432 * @fit: pointer to the FIT format image header
1433 * @noffset: component image node offset
1434 * @comp: requested image compression type
1435 *
1436 * fit_image_check_comp() reads image compression property and compares its
1437 * numeric id with the requested compression type. Comparison result is
1438 * returned to the caller.
1439 *
1440 * returns:
1441 * 1 if image uses requested compression
1442 * 0 otherwise (or on error)
1443 */
1444int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1445{
1446 uint8_t image_comp;
1447
1448 if (fit_image_get_comp(fit, noffset, &image_comp))
1449 return 0;
1450 return (comp == image_comp);
1451}
1452
1453/**
1454 * fit_check_format - sanity check FIT image format
1455 * @fit: pointer to the FIT format image header
1456 *
1457 * fit_check_format() runs a basic sanity FIT image verification.
1458 * Routine checks for mandatory properties, nodes, etc.
1459 *
1460 * returns:
1461 * 1, on success
1462 * 0, on failure
1463 */
1464int fit_check_format(const void *fit)
1465{
1466 /* mandatory / node 'description' property */
1467 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1468 debug("Wrong FIT format: no description\n");
1469 return 0;
1470 }
1471
1472 if (IMAGE_ENABLE_TIMESTAMP) {
1473 /* mandatory / node 'timestamp' property */
1474 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1475 debug("Wrong FIT format: no timestamp\n");
1476 return 0;
1477 }
1478 }
1479
1480 /* mandatory subimages parent '/images' node */
1481 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1482 debug("Wrong FIT format: no images parent node\n");
1483 return 0;
1484 }
1485
1486 return 1;
1487}
1488
1489
1490/**
1491 * fit_conf_find_compat
1492 * @fit: pointer to the FIT format image header
1493 * @fdt: pointer to the device tree to compare against
1494 *
1495 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1496 * most compatible with the passed in device tree.
1497 *
1498 * Example:
1499 *
1500 * / o image-tree
1501 * |-o images
Andre Przywara3234ecd2017-12-04 02:05:10 +00001502 * | |-o fdt-1
1503 * | |-o fdt-2
Simon Glassda0af362013-05-07 06:11:53 +00001504 * |
1505 * |-o configurations
Andre Przywara3234ecd2017-12-04 02:05:10 +00001506 * |-o config-1
1507 * | |-fdt = fdt-1
Simon Glassda0af362013-05-07 06:11:53 +00001508 * |
Andre Przywara3234ecd2017-12-04 02:05:10 +00001509 * |-o config-2
1510 * |-fdt = fdt-2
Simon Glassda0af362013-05-07 06:11:53 +00001511 *
1512 * / o U-Boot fdt
1513 * |-compatible = "foo,bar", "bim,bam"
1514 *
1515 * / o kernel fdt1
1516 * |-compatible = "foo,bar",
1517 *
1518 * / o kernel fdt2
1519 * |-compatible = "bim,bam", "baz,biz"
1520 *
1521 * Configuration 1 would be picked because the first string in U-Boot's
1522 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1523 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1524 *
Julius Werner4e823522019-07-24 19:37:56 -07001525 * As an optimization, the compatible property from the FDT's root node can be
1526 * copied into the configuration node in the FIT image. This is required to
1527 * match configurations with compressed FDTs.
1528 *
Simon Glassda0af362013-05-07 06:11:53 +00001529 * returns:
1530 * offset to the configuration to use if one was found
1531 * -1 otherwise
1532 */
1533int fit_conf_find_compat(const void *fit, const void *fdt)
1534{
1535 int ndepth = 0;
1536 int noffset, confs_noffset, images_noffset;
1537 const void *fdt_compat;
1538 int fdt_compat_len;
1539 int best_match_offset = 0;
1540 int best_match_pos = 0;
1541
1542 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1543 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1544 if (confs_noffset < 0 || images_noffset < 0) {
1545 debug("Can't find configurations or images nodes.\n");
1546 return -1;
1547 }
1548
1549 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1550 if (!fdt_compat) {
1551 debug("Fdt for comparison has no \"compatible\" property.\n");
1552 return -1;
1553 }
1554
1555 /*
1556 * Loop over the configurations in the FIT image.
1557 */
1558 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1559 (noffset >= 0) && (ndepth > 0);
1560 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Julius Werner4e823522019-07-24 19:37:56 -07001561 const void *fdt;
Simon Glassda0af362013-05-07 06:11:53 +00001562 const char *kfdt_name;
Julius Werner4e823522019-07-24 19:37:56 -07001563 int kfdt_noffset, compat_noffset;
Simon Glassda0af362013-05-07 06:11:53 +00001564 const char *cur_fdt_compat;
1565 int len;
Julius Werner4e823522019-07-24 19:37:56 -07001566 size_t sz;
Simon Glassda0af362013-05-07 06:11:53 +00001567 int i;
1568
1569 if (ndepth > 1)
1570 continue;
1571
Julius Werner4e823522019-07-24 19:37:56 -07001572 /* If there's a compat property in the config node, use that. */
1573 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1574 fdt = fit; /* search in FIT image */
1575 compat_noffset = noffset; /* search under config node */
1576 } else { /* Otherwise extract it from the kernel FDT. */
1577 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1578 if (!kfdt_name) {
1579 debug("No fdt property found.\n");
1580 continue;
1581 }
1582 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1583 kfdt_name);
1584 if (kfdt_noffset < 0) {
1585 debug("No image node named \"%s\" found.\n",
1586 kfdt_name);
1587 continue;
1588 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001589
Julius Werner4e823522019-07-24 19:37:56 -07001590 if (!fit_image_check_comp(fit, kfdt_noffset,
1591 IH_COMP_NONE)) {
1592 debug("Can't extract compat from \"%s\" "
1593 "(compressed)\n", kfdt_name);
1594 continue;
1595 }
Julius Werner97b09cd2019-07-24 19:37:55 -07001596
Julius Werner4e823522019-07-24 19:37:56 -07001597 /* search in this config's kernel FDT */
1598 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1599 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1600 continue;
1601 }
1602
1603 compat_noffset = 0; /* search kFDT under root node */
Simon Glassda0af362013-05-07 06:11:53 +00001604 }
1605
1606 len = fdt_compat_len;
1607 cur_fdt_compat = fdt_compat;
1608 /*
1609 * Look for a match for each U-Boot compatibility string in
Julius Werner4e823522019-07-24 19:37:56 -07001610 * turn in the compat string property.
Simon Glassda0af362013-05-07 06:11:53 +00001611 */
1612 for (i = 0; len > 0 &&
1613 (!best_match_offset || best_match_pos > i); i++) {
1614 int cur_len = strlen(cur_fdt_compat) + 1;
1615
Julius Werner4e823522019-07-24 19:37:56 -07001616 if (!fdt_node_check_compatible(fdt, compat_noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001617 cur_fdt_compat)) {
1618 best_match_offset = noffset;
1619 best_match_pos = i;
1620 break;
1621 }
1622 len -= cur_len;
1623 cur_fdt_compat += cur_len;
1624 }
1625 }
1626 if (!best_match_offset) {
1627 debug("No match found.\n");
1628 return -1;
1629 }
1630
1631 return best_match_offset;
1632}
1633
1634/**
1635 * fit_conf_get_node - get node offset for configuration of a given unit name
1636 * @fit: pointer to the FIT format image header
1637 * @conf_uname: configuration node unit name
1638 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001639 * fit_conf_get_node() finds a configuration (within the '/configurations'
1640 * parent node) of a provided unit name. If configuration is found its node
Simon Glassda0af362013-05-07 06:11:53 +00001641 * offset is returned to the caller.
1642 *
1643 * When NULL is provided in second argument fit_conf_get_node() will search
1644 * for a default configuration node instead. Default configuration node unit
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001645 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glassda0af362013-05-07 06:11:53 +00001646 * node.
1647 *
1648 * returns:
1649 * configuration node offset when found (>=0)
1650 * negative number on failure (FDT_ERR_* code)
1651 */
1652int fit_conf_get_node(const void *fit, const char *conf_uname)
1653{
1654 int noffset, confs_noffset;
1655 int len;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001656 const char *s;
1657 char *conf_uname_copy = NULL;
Simon Glassda0af362013-05-07 06:11:53 +00001658
1659 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1660 if (confs_noffset < 0) {
1661 debug("Can't find configurations parent node '%s' (%s)\n",
1662 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1663 return confs_noffset;
1664 }
1665
1666 if (conf_uname == NULL) {
1667 /* get configuration unit name from the default property */
1668 debug("No configuration specified, trying default...\n");
1669 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1670 FIT_DEFAULT_PROP, &len);
1671 if (conf_uname == NULL) {
1672 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1673 len);
1674 return len;
1675 }
1676 debug("Found default configuration: '%s'\n", conf_uname);
1677 }
1678
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001679 s = strchr(conf_uname, '#');
1680 if (s) {
1681 len = s - conf_uname;
1682 conf_uname_copy = malloc(len + 1);
1683 if (!conf_uname_copy) {
1684 debug("Can't allocate uname copy: '%s'\n",
1685 conf_uname);
1686 return -ENOMEM;
1687 }
1688 memcpy(conf_uname_copy, conf_uname, len);
1689 conf_uname_copy[len] = '\0';
1690 conf_uname = conf_uname_copy;
1691 }
1692
Simon Glassda0af362013-05-07 06:11:53 +00001693 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1694 if (noffset < 0) {
1695 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1696 conf_uname, fdt_strerror(noffset));
1697 }
1698
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001699 if (conf_uname_copy)
1700 free(conf_uname_copy);
1701
Simon Glassda0af362013-05-07 06:11:53 +00001702 return noffset;
1703}
1704
Pantelis Antonioua4880742017-09-04 23:12:14 +03001705int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001706 const char *prop_name)
1707{
Pantelis Antonioua4880742017-09-04 23:12:14 +03001708 return fdt_stringlist_count(fit, noffset, prop_name);
1709}
1710
1711int fit_conf_get_prop_node_index(const void *fit, int noffset,
1712 const char *prop_name, int index)
1713{
1714 const char *uname;
Simon Glassda0af362013-05-07 06:11:53 +00001715 int len;
1716
1717 /* get kernel image unit name from configuration kernel property */
Pantelis Antonioua4880742017-09-04 23:12:14 +03001718 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glassda0af362013-05-07 06:11:53 +00001719 if (uname == NULL)
1720 return len;
1721
1722 return fit_image_get_node(fit, uname);
1723}
1724
Pantelis Antonioua4880742017-09-04 23:12:14 +03001725int fit_conf_get_prop_node(const void *fit, int noffset,
1726 const char *prop_name)
1727{
1728 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1729}
Simon Glassda0af362013-05-07 06:11:53 +00001730
Jeroen Hofsteeffa60da2014-10-08 22:57:38 +02001731static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass384d86d2013-05-16 13:53:21 +00001732{
1733 fit_image_print(fit, rd_noffset, " ");
1734
1735 if (verify) {
1736 puts(" Verifying Hash Integrity ... ");
1737 if (!fit_image_verify(fit, rd_noffset)) {
1738 puts("Bad Data Hash\n");
1739 return -EACCES;
1740 }
1741 puts("OK\n");
1742 }
1743
1744 return 0;
1745}
1746
Simon Glass384d86d2013-05-16 13:53:21 +00001747int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1748 ulong addr)
1749{
1750 int cfg_noffset;
1751 void *fit_hdr;
1752 int noffset;
1753
1754 debug("* %s: using config '%s' from image at 0x%08lx\n",
1755 prop_name, images->fit_uname_cfg, addr);
1756
1757 /* Check whether configuration has this property defined */
1758 fit_hdr = map_sysmem(addr, 0);
1759 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1760 if (cfg_noffset < 0) {
1761 debug("* %s: no such config\n", prop_name);
Paul Burton14171b12016-09-20 18:17:12 +01001762 return -EINVAL;
Simon Glass384d86d2013-05-16 13:53:21 +00001763 }
1764
1765 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1766 if (noffset < 0) {
1767 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Grayae6a02f2016-09-03 08:30:14 +10001768 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00001769 }
1770
1771 return noffset;
1772}
1773
Simon Glassa0c0b632014-06-12 07:24:47 -06001774/**
1775 * fit_get_image_type_property() - get property name for IH_TYPE_...
1776 *
1777 * @return the properly name where we expect to find the image in the
1778 * config node
1779 */
1780static const char *fit_get_image_type_property(int type)
1781{
1782 /*
1783 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001784 * but we don't have access to the short name, and "fdt" is different
Simon Glassa0c0b632014-06-12 07:24:47 -06001785 * anyway. So let's just keep it here.
1786 */
1787 switch (type) {
1788 case IH_TYPE_FLATDT:
1789 return FIT_FDT_PROP;
1790 case IH_TYPE_KERNEL:
1791 return FIT_KERNEL_PROP;
1792 case IH_TYPE_RAMDISK:
1793 return FIT_RAMDISK_PROP;
Simon Glass0129b522014-10-19 21:11:24 -06001794 case IH_TYPE_X86_SETUP:
1795 return FIT_SETUP_PROP;
Karl Apsite1b21c282015-05-21 09:52:48 -04001796 case IH_TYPE_LOADABLE:
1797 return FIT_LOADABLE_PROP;
Michal Simekebae78b2016-05-17 14:03:50 +02001798 case IH_TYPE_FPGA:
1799 return FIT_FPGA_PROP;
Marek Vasut93e95752018-05-13 00:22:54 +02001800 case IH_TYPE_STANDALONE:
1801 return FIT_STANDALONE_PROP;
Simon Glassa0c0b632014-06-12 07:24:47 -06001802 }
1803
1804 return "unknown";
1805}
1806
1807int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassad68fc32013-07-10 23:08:10 -07001808 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass384d86d2013-05-16 13:53:21 +00001809 int arch, int image_type, int bootstage_id,
1810 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1811{
1812 int cfg_noffset, noffset;
1813 const char *fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07001814 const char *fit_uname_config;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001815 const char *fit_base_uname_config;
Simon Glass384d86d2013-05-16 13:53:21 +00001816 const void *fit;
Julius Werner97b09cd2019-07-24 19:37:55 -07001817 void *buf;
1818 void *loadbuf;
Simon Glass384d86d2013-05-16 13:53:21 +00001819 size_t size;
1820 int type_ok, os_ok;
Julius Werner97b09cd2019-07-24 19:37:55 -07001821 ulong load, load_end, data, len;
1822 uint8_t os, comp;
Alison Wang73818d52016-11-10 10:49:03 +08001823#ifndef USE_HOSTCC
1824 uint8_t os_arch;
1825#endif
Simon Glassa0c0b632014-06-12 07:24:47 -06001826 const char *prop_name;
Simon Glass384d86d2013-05-16 13:53:21 +00001827 int ret;
1828
1829 fit = map_sysmem(addr, 0);
1830 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassad68fc32013-07-10 23:08:10 -07001831 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001832 fit_base_uname_config = NULL;
Simon Glassa0c0b632014-06-12 07:24:47 -06001833 prop_name = fit_get_image_type_property(image_type);
Simon Glass384d86d2013-05-16 13:53:21 +00001834 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1835
1836 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1837 if (!fit_check_format(fit)) {
1838 printf("Bad FIT %s image format!\n", prop_name);
1839 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1840 return -ENOEXEC;
1841 }
1842 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1843 if (fit_uname) {
Masahiro Yamadaaa58eec2014-02-18 15:39:21 +09001844 /* get FIT component image node offset */
Simon Glass384d86d2013-05-16 13:53:21 +00001845 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1846 noffset = fit_image_get_node(fit, fit_uname);
1847 } else {
1848 /*
1849 * no image node unit name, try to get config
1850 * node first. If config unit node name is NULL
1851 * fit_conf_get_node() will try to find default config node
1852 */
1853 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1854 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1855 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1856 } else {
1857 cfg_noffset = fit_conf_get_node(fit,
1858 fit_uname_config);
1859 }
1860 if (cfg_noffset < 0) {
1861 puts("Could not find configuration node\n");
1862 bootstage_error(bootstage_id +
1863 BOOTSTAGE_SUB_NO_UNIT_NAME);
1864 return -ENOENT;
1865 }
Marek Vasut5dee1342018-05-31 17:59:07 +02001866
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001867 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1868 printf(" Using '%s' configuration\n", fit_base_uname_config);
Marek Vasut5dee1342018-05-31 17:59:07 +02001869 /* Remember this config */
1870 if (image_type == IH_TYPE_KERNEL)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001871 images->fit_uname_cfg = fit_base_uname_config;
Marek Vasut5dee1342018-05-31 17:59:07 +02001872
1873 if (IMAGE_ENABLE_VERIFY && images->verify) {
1874 puts(" Verifying Hash Integrity ... ");
1875 if (fit_config_verify(fit, cfg_noffset)) {
1876 puts("Bad Data Hash\n");
1877 bootstage_error(bootstage_id +
1878 BOOTSTAGE_SUB_HASH);
1879 return -EACCES;
Simon Glass384d86d2013-05-16 13:53:21 +00001880 }
Marek Vasut5dee1342018-05-31 17:59:07 +02001881 puts("OK\n");
Simon Glass384d86d2013-05-16 13:53:21 +00001882 }
1883
Marek Vasut5dee1342018-05-31 17:59:07 +02001884 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1885
Simon Glass384d86d2013-05-16 13:53:21 +00001886 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1887 prop_name);
1888 fit_uname = fit_get_name(fit, noffset, NULL);
1889 }
1890 if (noffset < 0) {
1891 puts("Could not find subimage node\n");
1892 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1893 return -ENOENT;
1894 }
1895
1896 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1897
1898 ret = fit_image_select(fit, noffset, images->verify);
1899 if (ret) {
1900 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1901 return ret;
1902 }
1903
1904 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass3c1e7502014-10-19 21:11:22 -06001905#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass384d86d2013-05-16 13:53:21 +00001906 if (!fit_image_check_target_arch(fit, noffset)) {
1907 puts("Unsupported Architecture\n");
1908 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1909 return -ENOEXEC;
1910 }
Simon Glassa51991d2014-06-12 07:24:53 -06001911#endif
Alison Wang73818d52016-11-10 10:49:03 +08001912
1913#ifndef USE_HOSTCC
1914 fit_image_get_arch(fit, noffset, &os_arch);
1915 images->os.arch = os_arch;
1916#endif
1917
Simon Glass384d86d2013-05-16 13:53:21 +00001918 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1919 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02001920 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1921 (image_type == IH_TYPE_KERNEL &&
1922 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasutcaee41f2014-12-16 14:07:22 +01001923
Andreas Bießmannb4cc60d2016-08-14 20:31:24 +02001924 os_ok = image_type == IH_TYPE_FLATDT ||
1925 image_type == IH_TYPE_FPGA ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01001926 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02001927 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01001928 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite1b21c282015-05-21 09:52:48 -04001929
1930 /*
1931 * If either of the checks fail, we should report an error, but
1932 * if the image type is coming from the "loadables" field, we
1933 * don't care what it is
1934 */
1935 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasutcaee41f2014-12-16 14:07:22 +01001936 fit_image_get_os(fit, noffset, &os);
1937 printf("No %s %s %s Image\n",
1938 genimg_get_os_name(os),
1939 genimg_get_arch_name(arch),
Simon Glass384d86d2013-05-16 13:53:21 +00001940 genimg_get_type_name(image_type));
1941 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1942 return -EIO;
1943 }
1944
1945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1946
1947 /* get image data address and length */
Julius Werner97b09cd2019-07-24 19:37:55 -07001948 if (fit_image_get_data_and_size(fit, noffset,
1949 (const void **)&buf, &size)) {
Simon Glass384d86d2013-05-16 13:53:21 +00001950 printf("Could not find %s subimage data!\n", prop_name);
1951 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass9d0da5b2013-06-16 07:46:49 -07001952 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00001953 }
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06001954
1955#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1956 /* perform any post-processing on the image data */
Julius Werner97b09cd2019-07-24 19:37:55 -07001957 board_fit_image_post_process(&buf, &size);
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06001958#endif
1959
Simon Glass384d86d2013-05-16 13:53:21 +00001960 len = (ulong)size;
1961
Simon Glass384d86d2013-05-16 13:53:21 +00001962 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1963
Julius Werner97b09cd2019-07-24 19:37:55 -07001964 data = map_to_sysmem(buf);
1965 load = data;
Simon Glass384d86d2013-05-16 13:53:21 +00001966 if (load_op == FIT_LOAD_IGNORED) {
1967 /* Don't load */
1968 } else if (fit_image_get_load(fit, noffset, &load)) {
1969 if (load_op == FIT_LOAD_REQUIRED) {
1970 printf("Can't get %s subimage load address!\n",
1971 prop_name);
1972 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1973 return -EBADF;
1974 }
Simon Glass05a9ad72014-08-22 14:26:43 -06001975 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass384d86d2013-05-16 13:53:21 +00001976 ulong image_start, image_end;
Simon Glass384d86d2013-05-16 13:53:21 +00001977
1978 /*
1979 * move image data to the load address,
1980 * make sure we don't overwrite initial image
1981 */
1982 image_start = addr;
1983 image_end = addr + fit_get_size(fit);
1984
1985 load_end = load + len;
1986 if (image_type != IH_TYPE_KERNEL &&
1987 load < image_end && load_end > image_start) {
1988 printf("Error: %s overwritten\n", prop_name);
1989 return -EXDEV;
1990 }
1991
1992 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1993 prop_name, data, load);
Julius Werner97b09cd2019-07-24 19:37:55 -07001994 } else {
1995 load = data; /* No load address specified */
1996 }
1997
1998 comp = IH_COMP_NONE;
1999 loadbuf = buf;
2000 /* Kernel images get decompressed later in bootm_load_os(). */
Julius Werner88040022019-08-02 15:52:28 -07002001 if (!fit_image_get_comp(fit, noffset, &comp) &&
2002 comp != IH_COMP_NONE &&
2003 !(image_type == IH_TYPE_KERNEL ||
2004 image_type == IH_TYPE_KERNEL_NOLOAD ||
2005 image_type == IH_TYPE_RAMDISK)) {
Julius Werner97b09cd2019-07-24 19:37:55 -07002006 ulong max_decomp_len = len * 20;
2007 if (load == data) {
2008 loadbuf = malloc(max_decomp_len);
2009 load = map_to_sysmem(loadbuf);
2010 } else {
2011 loadbuf = map_sysmem(load, max_decomp_len);
2012 }
2013 if (image_decomp(comp, load, data, image_type,
2014 loadbuf, buf, len, max_decomp_len, &load_end)) {
2015 printf("Error decompressing %s\n", prop_name);
Simon Glass384d86d2013-05-16 13:53:21 +00002016
Julius Werner97b09cd2019-07-24 19:37:55 -07002017 return -ENOEXEC;
2018 }
2019 len = load_end - load;
2020 } else if (load != data) {
2021 loadbuf = map_sysmem(load, len);
2022 memcpy(loadbuf, buf, len);
Simon Glass384d86d2013-05-16 13:53:21 +00002023 }
Julius Werner97b09cd2019-07-24 19:37:55 -07002024
Julius Werner88040022019-08-02 15:52:28 -07002025 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2026 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2027 " please fix your .its file!\n");
2028
Julius Werner97b09cd2019-07-24 19:37:55 -07002029 /* verify that image data is a proper FDT blob */
2030 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2031 puts("Subimage data is not a FDT");
2032 return -ENOEXEC;
2033 }
2034
Simon Glass384d86d2013-05-16 13:53:21 +00002035 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2036
Julius Werner97b09cd2019-07-24 19:37:55 -07002037 *datap = load;
Simon Glass384d86d2013-05-16 13:53:21 +00002038 *lenp = len;
2039 if (fit_unamep)
2040 *fit_unamep = (char *)fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07002041 if (fit_uname_configp)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03002042 *fit_uname_configp = (char *)(fit_uname_config ? :
2043 fit_base_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00002044
2045 return noffset;
2046}
Simon Glass0129b522014-10-19 21:11:24 -06002047
2048int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2049 ulong *setup_start, ulong *setup_len)
2050{
2051 int noffset;
2052 ulong addr;
2053 ulong len;
2054 int ret;
2055
2056 addr = map_to_sysmem(images->fit_hdr_os);
2057 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2058 if (noffset < 0)
2059 return noffset;
2060
2061 ret = fit_image_load(images, addr, NULL, NULL, arch,
2062 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2063 FIT_LOAD_REQUIRED, setup_start, &len);
2064
2065 return ret;
2066}
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002067
2068#ifndef USE_HOSTCC
2069int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2070 const char **fit_unamep, const char **fit_uname_configp,
2071 int arch, ulong *datap, ulong *lenp)
2072{
2073 int fdt_noffset, cfg_noffset, count;
2074 const void *fit;
2075 const char *fit_uname = NULL;
2076 const char *fit_uname_config = NULL;
2077 char *fit_uname_config_copy = NULL;
2078 char *next_config = NULL;
2079 ulong load, len;
2080#ifdef CONFIG_OF_LIBFDT_OVERLAY
2081 ulong image_start, image_end;
2082 ulong ovload, ovlen;
2083 const char *uconfig;
2084 const char *uname;
2085 void *base, *ov;
2086 int i, err, noffset, ov_noffset;
2087#endif
2088
2089 fit_uname = fit_unamep ? *fit_unamep : NULL;
2090
2091 if (fit_uname_configp && *fit_uname_configp) {
2092 fit_uname_config_copy = strdup(*fit_uname_configp);
2093 if (!fit_uname_config_copy)
2094 return -ENOMEM;
2095
2096 next_config = strchr(fit_uname_config_copy, '#');
2097 if (next_config)
2098 *next_config++ = '\0';
2099 if (next_config - 1 > fit_uname_config_copy)
2100 fit_uname_config = fit_uname_config_copy;
2101 }
2102
2103 fdt_noffset = fit_image_load(images,
2104 addr, &fit_uname, &fit_uname_config,
2105 arch, IH_TYPE_FLATDT,
2106 BOOTSTAGE_ID_FIT_FDT_START,
2107 FIT_LOAD_OPTIONAL, &load, &len);
2108
2109 if (fdt_noffset < 0)
2110 goto out;
2111
2112 debug("fit_uname=%s, fit_uname_config=%s\n",
2113 fit_uname ? fit_uname : "<NULL>",
2114 fit_uname_config ? fit_uname_config : "<NULL>");
2115
2116 fit = map_sysmem(addr, 0);
2117
2118 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2119
2120 /* single blob, or error just return as well */
2121 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2122 if (count <= 1 && !next_config)
2123 goto out;
2124
2125 /* we need to apply overlays */
2126
2127#ifdef CONFIG_OF_LIBFDT_OVERLAY
2128 image_start = addr;
2129 image_end = addr + fit_get_size(fit);
2130 /* verify that relocation took place by load address not being in fit */
2131 if (load >= image_start && load < image_end) {
2132 /* check is simplified; fit load checks for overlaps */
2133 printf("Overlayed FDT requires relocation\n");
2134 fdt_noffset = -EBADF;
2135 goto out;
2136 }
2137
2138 base = map_sysmem(load, len);
2139
2140 /* apply extra configs in FIT first, followed by args */
2141 for (i = 1; ; i++) {
2142 if (i < count) {
2143 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2144 FIT_FDT_PROP, i);
2145 uname = fit_get_name(fit, noffset, NULL);
2146 uconfig = NULL;
2147 } else {
2148 if (!next_config)
2149 break;
2150 uconfig = next_config;
2151 next_config = strchr(next_config, '#');
2152 if (next_config)
2153 *next_config++ = '\0';
2154 uname = NULL;
Peter Ujfalusiaf15dc42019-03-06 15:52:27 +02002155
2156 /*
2157 * fit_image_load() would load the first FDT from the
2158 * extra config only when uconfig is specified.
2159 * Check if the extra config contains multiple FDTs and
2160 * if so, load them.
2161 */
2162 cfg_noffset = fit_conf_get_node(fit, uconfig);
2163
2164 i = 0;
2165 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2166 FIT_FDT_PROP);
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03002167 }
2168
2169 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2170
2171 ov_noffset = fit_image_load(images,
2172 addr, &uname, &uconfig,
2173 arch, IH_TYPE_FLATDT,
2174 BOOTSTAGE_ID_FIT_FDT_START,
2175 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2176 if (ov_noffset < 0) {
2177 printf("load of %s failed\n", uname);
2178 continue;
2179 }
2180 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2181 uname, ovload, ovlen);
2182 ov = map_sysmem(ovload, ovlen);
2183
2184 base = map_sysmem(load, len + ovlen);
2185 err = fdt_open_into(base, base, len + ovlen);
2186 if (err < 0) {
2187 printf("failed on fdt_open_into\n");
2188 fdt_noffset = err;
2189 goto out;
2190 }
2191 /* the verbose method prints out messages on error */
2192 err = fdt_overlay_apply_verbose(base, ov);
2193 if (err < 0) {
2194 fdt_noffset = err;
2195 goto out;
2196 }
2197 fdt_pack(base);
2198 len = fdt_totalsize(base);
2199 }
2200#else
2201 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2202 fdt_noffset = -EBADF;
2203#endif
2204
2205out:
2206 if (datap)
2207 *datap = load;
2208 if (lenp)
2209 *lenp = len;
2210 if (fit_unamep)
2211 *fit_unamep = fit_uname;
2212 if (fit_uname_configp)
2213 *fit_uname_configp = fit_uname_config;
2214
2215 if (fit_uname_config_copy)
2216 free(fit_uname_config_copy);
2217 return fdt_noffset;
2218}
2219#endif