blob: 030a3e579f5214fff809b15f398f1314e6153d8c [file] [log] [blame]
Simon Glass53fbb7e2013-05-07 06:11:53 +00001/*
2 * Copyright (c) 2013, Google Inc.
3 *
4 * (C) Copyright 2008 Semihalf
5 *
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Simon Glass53fbb7e2013-05-07 06:11:53 +000010 */
11
12#ifdef USE_HOSTCC
13#include "mkimage.h"
Simon Glass53fbb7e2013-05-07 06:11:53 +000014#include <time.h>
15#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050016#include <linux/compiler.h>
York Sun020198b2016-11-23 09:25:09 -080017#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000018#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000019#include <errno.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000021#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030022#include <malloc.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000023DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000024#endif /* !USE_HOSTCC*/
25
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050026#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000027#include <bootstage.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000028#include <u-boot/crc.h>
29#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020030#include <u-boot/sha1.h>
31#include <u-boot/sha256.h>
Simon Glass53fbb7e2013-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 Yamada069d5942013-09-18 09:36:38 +090066 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-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 Yamada069d5942013-09-18 09:36:38 +090092 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-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 Ferreira39931f92015-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
Simon Glass87ebee32013-05-08 08:05:59 +0000146#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000147/**
148 * fit_print_contents - prints out the contents of the FIT format image
149 * @fit: pointer to the FIT format image header
150 * @p: pointer to prefix string
151 *
152 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500153 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000154 * the details of each component image.
155 *
156 * returns:
157 * no returned results
158 */
159void fit_print_contents(const void *fit)
160{
161 char *desc;
162 char *uname;
163 int images_noffset;
164 int confs_noffset;
165 int noffset;
166 int ndepth;
167 int count = 0;
168 int ret;
169 const char *p;
170 time_t timestamp;
171
Simon Glass1fe7d932013-05-08 08:05:58 +0000172 /* Indent string is defined in header image.h */
173 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000174
175 /* Root node properties */
176 ret = fit_get_desc(fit, 0, &desc);
177 printf("%sFIT description: ", p);
178 if (ret)
179 printf("unavailable\n");
180 else
181 printf("%s\n", desc);
182
183 if (IMAGE_ENABLE_TIMESTAMP) {
184 ret = fit_get_timestamp(fit, 0, &timestamp);
185 printf("%sCreated: ", p);
186 if (ret)
187 printf("unavailable\n");
188 else
189 genimg_print_time(timestamp);
190 }
191
192 /* Find images parent node offset */
193 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
194 if (images_noffset < 0) {
195 printf("Can't find images parent node '%s' (%s)\n",
196 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
197 return;
198 }
199
200 /* Process its subnodes, print out component images details */
201 for (ndepth = 0, count = 0,
202 noffset = fdt_next_node(fit, images_noffset, &ndepth);
203 (noffset >= 0) && (ndepth > 0);
204 noffset = fdt_next_node(fit, noffset, &ndepth)) {
205 if (ndepth == 1) {
206 /*
207 * Direct child node of the images parent node,
208 * i.e. component image node.
209 */
210 printf("%s Image %u (%s)\n", p, count++,
211 fit_get_name(fit, noffset, NULL));
212
213 fit_image_print(fit, noffset, p);
214 }
215 }
216
217 /* Find configurations parent node offset */
218 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
219 if (confs_noffset < 0) {
220 debug("Can't get configurations parent node '%s' (%s)\n",
221 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
222 return;
223 }
224
225 /* get default configuration unit name from default property */
226 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
227 if (uname)
228 printf("%s Default Configuration: '%s'\n", p, uname);
229
230 /* Process its subnodes, print out configurations details */
231 for (ndepth = 0, count = 0,
232 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
233 (noffset >= 0) && (ndepth > 0);
234 noffset = fdt_next_node(fit, noffset, &ndepth)) {
235 if (ndepth == 1) {
236 /*
237 * Direct child node of the configurations parent node,
238 * i.e. configuration node.
239 */
240 printf("%s Configuration %u (%s)\n", p, count++,
241 fit_get_name(fit, noffset, NULL));
242
243 fit_conf_print(fit, noffset, p);
244 }
245 }
246}
247
248/**
Simon Glassd8b75362013-05-07 06:12:02 +0000249 * fit_image_print_data() - prints out the hash node details
250 * @fit: pointer to the FIT format image header
251 * @noffset: offset of the hash node
252 * @p: pointer to prefix string
Simon Glass56518e72013-06-13 15:10:01 -0700253 * @type: Type of information to print ("hash" or "sign")
Simon Glassd8b75362013-05-07 06:12:02 +0000254 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500255 * fit_image_print_data() lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000256 *
Simon Glass56518e72013-06-13 15:10:01 -0700257 * This function avoid using puts() since it prints a newline on the host
258 * but does not in U-Boot.
259 *
Simon Glassd8b75362013-05-07 06:12:02 +0000260 * returns:
261 * no returned results
262 */
Simon Glass56518e72013-06-13 15:10:01 -0700263static void fit_image_print_data(const void *fit, int noffset, const char *p,
264 const char *type)
Simon Glassd8b75362013-05-07 06:12:02 +0000265{
Simon Glass56518e72013-06-13 15:10:01 -0700266 const char *keyname;
Simon Glassd8b75362013-05-07 06:12:02 +0000267 uint8_t *value;
268 int value_len;
Simon Glass56518e72013-06-13 15:10:01 -0700269 char *algo;
270 int required;
271 int ret, i;
Simon Glassd8b75362013-05-07 06:12:02 +0000272
Simon Glass56518e72013-06-13 15:10:01 -0700273 debug("%s %s node: '%s'\n", p, type,
Simon Glassd8b75362013-05-07 06:12:02 +0000274 fit_get_name(fit, noffset, NULL));
Simon Glass56518e72013-06-13 15:10:01 -0700275 printf("%s %s algo: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000276 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
277 printf("invalid/unsupported\n");
278 return;
279 }
Simon Glass56518e72013-06-13 15:10:01 -0700280 printf("%s", algo);
281 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
282 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
283 if (keyname)
284 printf(":%s", keyname);
285 if (required)
286 printf(" (required)");
287 printf("\n");
Simon Glassd8b75362013-05-07 06:12:02 +0000288
289 ret = fit_image_hash_get_value(fit, noffset, &value,
290 &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700291 printf("%s %s value: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000292 if (ret) {
293 printf("unavailable\n");
294 } else {
295 for (i = 0; i < value_len; i++)
296 printf("%02x", value[i]);
297 printf("\n");
298 }
299
Simon Glass56518e72013-06-13 15:10:01 -0700300 debug("%s %s len: %d\n", p, type, value_len);
301
302 /* Signatures have a time stamp */
303 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
304 time_t timestamp;
305
306 printf("%s Timestamp: ", p);
307 if (fit_get_timestamp(fit, noffset, &timestamp))
308 printf("unavailable\n");
309 else
310 genimg_print_time(timestamp);
311 }
Simon Glassd8b75362013-05-07 06:12:02 +0000312}
313
314/**
315 * fit_image_print_verification_data() - prints out the hash/signature details
316 * @fit: pointer to the FIT format image header
317 * @noffset: offset of the hash or signature node
318 * @p: pointer to prefix string
319 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500320 * This lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000321 *
322 * returns:
323 * no returned results
324 */
325static void fit_image_print_verification_data(const void *fit, int noffset,
326 const char *p)
327{
328 const char *name;
329
330 /*
331 * Check subnode name, must be equal to "hash" or "signature".
332 * Multiple hash/signature nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +0000333 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
Simon Glassd8b75362013-05-07 06:12:02 +0000334 */
335 name = fit_get_name(fit, noffset, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700336 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
337 fit_image_print_data(fit, noffset, p, "Hash");
338 } else if (!strncmp(name, FIT_SIG_NODENAME,
339 strlen(FIT_SIG_NODENAME))) {
340 fit_image_print_data(fit, noffset, p, "Sign");
341 }
Simon Glassd8b75362013-05-07 06:12:02 +0000342}
343
344/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000345 * fit_image_print - prints out the FIT component image details
346 * @fit: pointer to the FIT format image header
347 * @image_noffset: offset of the component image node
348 * @p: pointer to prefix string
349 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500350 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000351 * image. If present, hash nodes are printed out as well. Load
352 * address for images of type firmware is also printed out. Since the load
353 * address is not mandatory for firmware images, it will be output as
354 * "unavailable" when not present.
355 *
356 * returns:
357 * no returned results
358 */
359void fit_image_print(const void *fit, int image_noffset, const char *p)
360{
361 char *desc;
362 uint8_t type, arch, os, comp;
363 size_t size;
364 ulong load, entry;
365 const void *data;
366 int noffset;
367 int ndepth;
368 int ret;
369
370 /* Mandatory properties */
371 ret = fit_get_desc(fit, image_noffset, &desc);
372 printf("%s Description: ", p);
373 if (ret)
374 printf("unavailable\n");
375 else
376 printf("%s\n", desc);
377
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700378 if (IMAGE_ENABLE_TIMESTAMP) {
379 time_t timestamp;
380
381 ret = fit_get_timestamp(fit, 0, &timestamp);
382 printf("%s Created: ", p);
383 if (ret)
384 printf("unavailable\n");
385 else
386 genimg_print_time(timestamp);
387 }
388
Simon Glass53fbb7e2013-05-07 06:11:53 +0000389 fit_image_get_type(fit, image_noffset, &type);
390 printf("%s Type: %s\n", p, genimg_get_type_name(type));
391
392 fit_image_get_comp(fit, image_noffset, &comp);
393 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
394
395 ret = fit_image_get_data(fit, image_noffset, &data, &size);
396
397#ifndef USE_HOSTCC
398 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000399 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000400 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000401 } else {
402 void *vdata = (void *)data;
403
404 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
405 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000406#endif
407
408 printf("%s Data Size: ", p);
409 if (ret)
410 printf("unavailable\n");
411 else
412 genimg_print_size(size);
413
414 /* Remaining, type dependent properties */
415 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
416 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
417 (type == IH_TYPE_FLATDT)) {
418 fit_image_get_arch(fit, image_noffset, &arch);
419 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
420 }
421
Michal Simek6faf4622018-03-26 16:31:27 +0200422 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
423 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000424 fit_image_get_os(fit, image_noffset, &os);
425 printf("%s OS: %s\n", p, genimg_get_os_name(os));
426 }
427
428 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200429 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
430 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000431 ret = fit_image_get_load(fit, image_noffset, &load);
432 printf("%s Load Address: ", p);
433 if (ret)
434 printf("unavailable\n");
435 else
436 printf("0x%08lx\n", load);
437 }
438
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300439 /* optional load address for FDT */
440 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
441 printf("%s Load Address: 0x%08lx\n", p, load);
442
Simon Glass53fbb7e2013-05-07 06:11:53 +0000443 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
444 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800445 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000446 printf("%s Entry Point: ", p);
447 if (ret)
448 printf("unavailable\n");
449 else
450 printf("0x%08lx\n", entry);
451 }
452
453 /* Process all hash subnodes of the component image node */
454 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
455 (noffset >= 0) && (ndepth > 0);
456 noffset = fdt_next_node(fit, noffset, &ndepth)) {
457 if (ndepth == 1) {
458 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000459 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000460 }
461 }
462}
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200463
464#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000465
466/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000467 * fit_get_desc - get node description property
468 * @fit: pointer to the FIT format image header
469 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500470 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000471 *
472 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500473 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000474 *
475 * returns:
476 * 0, on success
477 * -1, on failure
478 */
479int fit_get_desc(const void *fit, int noffset, char **desc)
480{
481 int len;
482
483 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
484 if (*desc == NULL) {
485 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
486 return -1;
487 }
488
489 return 0;
490}
491
492/**
493 * fit_get_timestamp - get node timestamp property
494 * @fit: pointer to the FIT format image header
495 * @noffset: node offset
496 * @timestamp: pointer to the time_t, will hold read timestamp
497 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500498 * fit_get_timestamp() reads timestamp property from given node, if timestamp
499 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000500 * argument.
501 *
502 * returns:
503 * 0, on success
504 * -1, on property read failure
505 * -2, on wrong timestamp size
506 */
507int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
508{
509 int len;
510 const void *data;
511
512 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
513 if (data == NULL) {
514 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
515 return -1;
516 }
517 if (len != sizeof(uint32_t)) {
518 debug("FIT timestamp with incorrect size of (%u)\n", len);
519 return -2;
520 }
521
522 *timestamp = uimage_to_cpu(*((uint32_t *)data));
523 return 0;
524}
525
526/**
527 * fit_image_get_node - get node offset for component image of a given unit name
528 * @fit: pointer to the FIT format image header
529 * @image_uname: component image node unit name
530 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500531 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000532 * node) of a provided unit name. If image is found its node offset is
533 * returned to the caller.
534 *
535 * returns:
536 * image node offset when found (>=0)
537 * negative number on failure (FDT_ERR_* code)
538 */
539int fit_image_get_node(const void *fit, const char *image_uname)
540{
541 int noffset, images_noffset;
542
543 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
544 if (images_noffset < 0) {
545 debug("Can't find images parent node '%s' (%s)\n",
546 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
547 return images_noffset;
548 }
549
550 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
551 if (noffset < 0) {
552 debug("Can't get node offset for image unit name: '%s' (%s)\n",
553 image_uname, fdt_strerror(noffset));
554 }
555
556 return noffset;
557}
558
559/**
560 * fit_image_get_os - get os id for a given component image node
561 * @fit: pointer to the FIT format image header
562 * @noffset: component image node offset
563 * @os: pointer to the uint8_t, will hold os numeric id
564 *
565 * fit_image_get_os() finds os property in a given component image node.
566 * If the property is found, its (string) value is translated to the numeric
567 * id which is returned to the caller.
568 *
569 * returns:
570 * 0, on success
571 * -1, on failure
572 */
573int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
574{
575 int len;
576 const void *data;
577
578 /* Get OS name from property data */
579 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
580 if (data == NULL) {
581 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
582 *os = -1;
583 return -1;
584 }
585
586 /* Translate OS name to id */
587 *os = genimg_get_os_id(data);
588 return 0;
589}
590
591/**
592 * fit_image_get_arch - get arch id for a given component image node
593 * @fit: pointer to the FIT format image header
594 * @noffset: component image node offset
595 * @arch: pointer to the uint8_t, will hold arch numeric id
596 *
597 * fit_image_get_arch() finds arch property in a given component image node.
598 * If the property is found, its (string) value is translated to the numeric
599 * id which is returned to the caller.
600 *
601 * returns:
602 * 0, on success
603 * -1, on failure
604 */
605int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
606{
607 int len;
608 const void *data;
609
610 /* Get architecture name from property data */
611 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
612 if (data == NULL) {
613 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
614 *arch = -1;
615 return -1;
616 }
617
618 /* Translate architecture name to id */
619 *arch = genimg_get_arch_id(data);
620 return 0;
621}
622
623/**
624 * fit_image_get_type - get type id for a given component image node
625 * @fit: pointer to the FIT format image header
626 * @noffset: component image node offset
627 * @type: pointer to the uint8_t, will hold type numeric id
628 *
629 * fit_image_get_type() finds type property in a given component image node.
630 * If the property is found, its (string) value is translated to the numeric
631 * id which is returned to the caller.
632 *
633 * returns:
634 * 0, on success
635 * -1, on failure
636 */
637int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
638{
639 int len;
640 const void *data;
641
642 /* Get image type name from property data */
643 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
644 if (data == NULL) {
645 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
646 *type = -1;
647 return -1;
648 }
649
650 /* Translate image type name to id */
651 *type = genimg_get_type_id(data);
652 return 0;
653}
654
655/**
656 * fit_image_get_comp - get comp id for a given component image node
657 * @fit: pointer to the FIT format image header
658 * @noffset: component image node offset
659 * @comp: pointer to the uint8_t, will hold comp numeric id
660 *
661 * fit_image_get_comp() finds comp property in a given component image node.
662 * If the property is found, its (string) value is translated to the numeric
663 * id which is returned to the caller.
664 *
665 * returns:
666 * 0, on success
667 * -1, on failure
668 */
669int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
670{
671 int len;
672 const void *data;
673
674 /* Get compression name from property data */
675 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
676 if (data == NULL) {
677 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
678 *comp = -1;
679 return -1;
680 }
681
682 /* Translate compression name to id */
683 *comp = genimg_get_comp_id(data);
684 return 0;
685}
686
York Sun60047652016-02-29 15:48:40 -0800687static int fit_image_get_address(const void *fit, int noffset, char *name,
688 ulong *load)
689{
York Sunc1913cb2016-02-29 15:48:41 -0800690 int len, cell_len;
691 const fdt32_t *cell;
692 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800693
York Sunc1913cb2016-02-29 15:48:41 -0800694 cell = fdt_getprop(fit, noffset, name, &len);
695 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800696 fit_get_debug(fit, noffset, name, len);
697 return -1;
698 }
699
York Sunc1913cb2016-02-29 15:48:41 -0800700 if (len > sizeof(ulong)) {
701 printf("Unsupported %s address size\n", name);
702 return -1;
703 }
704
705 cell_len = len >> 2;
706 /* Use load64 to avoid compiling warning for 32-bit target */
707 while (cell_len--) {
708 load64 = (load64 << 32) | uimage_to_cpu(*cell);
709 cell++;
710 }
711 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800712
713 return 0;
714}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000715/**
716 * fit_image_get_load() - get load addr property for given component image node
717 * @fit: pointer to the FIT format image header
718 * @noffset: component image node offset
719 * @load: pointer to the uint32_t, will hold load address
720 *
721 * fit_image_get_load() finds load address property in a given component
722 * image node. If the property is found, its value is returned to the caller.
723 *
724 * returns:
725 * 0, on success
726 * -1, on failure
727 */
728int fit_image_get_load(const void *fit, int noffset, ulong *load)
729{
York Sun60047652016-02-29 15:48:40 -0800730 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000731}
732
733/**
734 * fit_image_get_entry() - get entry point address property
735 * @fit: pointer to the FIT format image header
736 * @noffset: component image node offset
737 * @entry: pointer to the uint32_t, will hold entry point address
738 *
739 * This gets the entry point address property for a given component image
740 * node.
741 *
742 * fit_image_get_entry() finds entry point address property in a given
743 * component image node. If the property is found, its value is returned
744 * to the caller.
745 *
746 * returns:
747 * 0, on success
748 * -1, on failure
749 */
750int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
751{
York Sun60047652016-02-29 15:48:40 -0800752 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000753}
754
755/**
756 * fit_image_get_data - get data property and its size for a given component image node
757 * @fit: pointer to the FIT format image header
758 * @noffset: component image node offset
759 * @data: double pointer to void, will hold data property's data address
760 * @size: pointer to size_t, will hold data property's data size
761 *
762 * fit_image_get_data() finds data property in a given component image node.
763 * If the property is found its data start address and size are returned to
764 * the caller.
765 *
766 * returns:
767 * 0, on success
768 * -1, on failure
769 */
770int fit_image_get_data(const void *fit, int noffset,
771 const void **data, size_t *size)
772{
773 int len;
774
775 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
776 if (*data == NULL) {
777 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
778 *size = 0;
779 return -1;
780 }
781
782 *size = len;
783 return 0;
784}
785
786/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200787 * Get 'data-offset' property from a given image node.
788 *
789 * @fit: pointer to the FIT image header
790 * @noffset: component image node offset
791 * @data_offset: holds the data-offset property
792 *
793 * returns:
794 * 0, on success
795 * -ENOENT if the property could not be found
796 */
797int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
798{
799 const fdt32_t *val;
800
801 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
802 if (!val)
803 return -ENOENT;
804
805 *data_offset = fdt32_to_cpu(*val);
806
807 return 0;
808}
809
810/**
Peng Fana1be94b2017-12-05 13:20:59 +0800811 * Get 'data-position' property from a given image node.
812 *
813 * @fit: pointer to the FIT image header
814 * @noffset: component image node offset
815 * @data_position: holds the data-position property
816 *
817 * returns:
818 * 0, on success
819 * -ENOENT if the property could not be found
820 */
821int fit_image_get_data_position(const void *fit, int noffset,
822 int *data_position)
823{
824 const fdt32_t *val;
825
826 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
827 if (!val)
828 return -ENOENT;
829
830 *data_position = fdt32_to_cpu(*val);
831
832 return 0;
833}
834
835/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200836 * Get 'data-size' property from a given image node.
837 *
838 * @fit: pointer to the FIT image header
839 * @noffset: component image node offset
840 * @data_size: holds the data-size property
841 *
842 * returns:
843 * 0, on success
844 * -ENOENT if the property could not be found
845 */
846int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
847{
848 const fdt32_t *val;
849
850 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
851 if (!val)
852 return -ENOENT;
853
854 *data_size = fdt32_to_cpu(*val);
855
856 return 0;
857}
858
859/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000860 * fit_image_hash_get_algo - get hash algorithm name
861 * @fit: pointer to the FIT format image header
862 * @noffset: hash node offset
863 * @algo: double pointer to char, will hold pointer to the algorithm name
864 *
865 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
866 * If the property is found its data start address is returned to the caller.
867 *
868 * returns:
869 * 0, on success
870 * -1, on failure
871 */
872int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
873{
874 int len;
875
876 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
877 if (*algo == NULL) {
878 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
879 return -1;
880 }
881
882 return 0;
883}
884
885/**
886 * fit_image_hash_get_value - get hash value and length
887 * @fit: pointer to the FIT format image header
888 * @noffset: hash node offset
889 * @value: double pointer to uint8_t, will hold address of a hash value data
890 * @value_len: pointer to an int, will hold hash data length
891 *
892 * fit_image_hash_get_value() finds hash value property in a given hash node.
893 * If the property is found its data start address and size are returned to
894 * the caller.
895 *
896 * returns:
897 * 0, on success
898 * -1, on failure
899 */
900int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
901 int *value_len)
902{
903 int len;
904
905 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
906 if (*value == NULL) {
907 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
908 *value_len = 0;
909 return -1;
910 }
911
912 *value_len = len;
913 return 0;
914}
915
Simon Glass53fbb7e2013-05-07 06:11:53 +0000916/**
917 * fit_image_hash_get_ignore - get hash ignore flag
918 * @fit: pointer to the FIT format image header
919 * @noffset: hash node offset
920 * @ignore: pointer to an int, will hold hash ignore flag
921 *
922 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
923 * If the property is found and non-zero, the hash algorithm is not verified by
924 * u-boot automatically.
925 *
926 * returns:
927 * 0, on ignore not found
928 * value, on ignore found
929 */
Simon Glassab9efc62013-05-07 06:11:58 +0000930static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000931{
932 int len;
933 int *value;
934
935 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
936 if (value == NULL || len != sizeof(int))
937 *ignore = 0;
938 else
939 *ignore = *value;
940
941 return 0;
942}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000943
Simon Glass7a80de42016-02-24 09:14:42 -0700944ulong fit_get_end(const void *fit)
945{
946 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
947}
948
Simon Glass53fbb7e2013-05-07 06:11:53 +0000949/**
950 * fit_set_timestamp - set node timestamp property
951 * @fit: pointer to the FIT format image header
952 * @noffset: node offset
953 * @timestamp: timestamp value to be set
954 *
955 * fit_set_timestamp() attempts to set timestamp property in the requested
956 * node and returns operation status to the caller.
957 *
958 * returns:
959 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -0600960 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +0000961 */
962int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
963{
964 uint32_t t;
965 int ret;
966
967 t = cpu_to_uimage(timestamp);
968 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
969 sizeof(uint32_t));
970 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -0600971 debug("Can't set '%s' property for '%s' node (%s)\n",
972 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
973 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -0600974 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000975 }
976
977 return 0;
978}
979
980/**
981 * calculate_hash - calculate and return hash for provided input data
982 * @data: pointer to the input data
983 * @data_len: data length
984 * @algo: requested hash algorithm
985 * @value: pointer to the char, will hold hash value data (caller must
986 * allocate enough free space)
987 * value_len: length of the calculated hash
988 *
989 * calculate_hash() computes input data hash according to the requested
990 * algorithm.
991 * Resulting hash value is placed in caller provided 'value' buffer, length
992 * of the calculated hash is returned via value_len pointer argument.
993 *
994 * returns:
995 * 0, on success
996 * -1, when algo is unsupported
997 */
Simon Glass604f23d2013-05-07 06:11:54 +0000998int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000999 uint8_t *value, int *value_len)
1000{
Simon Glass87ebee32013-05-08 08:05:59 +00001001 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001002 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1003 CHUNKSZ_CRC32);
1004 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1005 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001006 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001007 sha1_csum_wd((unsigned char *)data, data_len,
1008 (unsigned char *)value, CHUNKSZ_SHA1);
1009 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001010 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1011 sha256_csum_wd((unsigned char *)data, data_len,
1012 (unsigned char *)value, CHUNKSZ_SHA256);
1013 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001014 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001015 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1016 *value_len = 16;
1017 } else {
1018 debug("Unsupported hash alogrithm\n");
1019 return -1;
1020 }
1021 return 0;
1022}
1023
Simon Glassab9efc62013-05-07 06:11:58 +00001024static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1025 size_t size, char **err_msgp)
1026{
1027 uint8_t value[FIT_MAX_HASH_LEN];
1028 int value_len;
1029 char *algo;
1030 uint8_t *fit_value;
1031 int fit_value_len;
1032 int ignore;
1033
1034 *err_msgp = NULL;
1035
1036 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001037 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001038 return -1;
1039 }
1040 printf("%s", algo);
1041
1042 if (IMAGE_ENABLE_IGNORE) {
1043 fit_image_hash_get_ignore(fit, noffset, &ignore);
1044 if (ignore) {
1045 printf("-skipped ");
1046 return 0;
1047 }
1048 }
1049
1050 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1051 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001052 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001053 return -1;
1054 }
1055
1056 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001057 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001058 return -1;
1059 }
1060
1061 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001062 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001063 return -1;
1064 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001065 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001066 return -1;
1067 }
1068
1069 return 0;
1070}
1071
Jun Nie5c643db2018-02-27 16:55:58 +08001072int fit_image_verify_with_data(const void *fit, int image_noffset,
1073 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001074{
Simon Glass56518e72013-06-13 15:10:01 -07001075 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001076 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001077 int verify_all = 1;
1078 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001079
Simon Glass56518e72013-06-13 15:10:01 -07001080 /* Verify all required signatures */
1081 if (IMAGE_ENABLE_VERIFY &&
1082 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1083 gd_fdt_blob(), &verify_all)) {
1084 err_msg = "Unable to verify required signature";
1085 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001086 }
1087
1088 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001089 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001090 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001091
Simon Glassab9efc62013-05-07 06:11:58 +00001092 /*
1093 * Check subnode name, must be equal to "hash".
1094 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001095 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001096 */
1097 if (!strncmp(name, FIT_HASH_NODENAME,
1098 strlen(FIT_HASH_NODENAME))) {
1099 if (fit_image_check_hash(fit, noffset, data, size,
1100 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001101 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001102 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001103 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1104 !strncmp(name, FIT_SIG_NODENAME,
1105 strlen(FIT_SIG_NODENAME))) {
1106 ret = fit_image_check_sig(fit, noffset, data,
1107 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001108
1109 /*
1110 * Show an indication on failure, but do not return
1111 * an error. Only keys marked 'required' can cause
1112 * an image validation failure. See the call to
1113 * fit_image_verify_required_sigs() above.
1114 */
1115 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001116 puts("- ");
1117 else
1118 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001119 }
1120 }
1121
1122 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001123 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001124 goto error;
1125 }
1126
1127 return 1;
1128
1129error:
Simon Glasse754da22013-05-07 06:11:59 +00001130 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001131 err_msg, fit_get_name(fit, noffset, NULL),
1132 fit_get_name(fit, image_noffset, NULL));
1133 return 0;
1134}
1135
1136/**
Jun Nie5c643db2018-02-27 16:55:58 +08001137 * fit_image_verify - verify data integrity
1138 * @fit: pointer to the FIT format image header
1139 * @image_noffset: component image node offset
1140 *
1141 * fit_image_verify() goes over component image hash nodes,
1142 * re-calculates each data hash and compares with the value stored in hash
1143 * node.
1144 *
1145 * returns:
1146 * 1, if all hashes are valid
1147 * 0, otherwise (or on error)
1148 */
1149int fit_image_verify(const void *fit, int image_noffset)
1150{
1151 const void *data;
1152 size_t size;
1153 int noffset = 0;
1154 char *err_msg = "";
1155
1156 /* Get image data and data length */
1157 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1158 err_msg = "Can't get image data/size";
1159 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1160 err_msg, fit_get_name(fit, noffset, NULL),
1161 fit_get_name(fit, image_noffset, NULL));
1162 return 0;
1163 }
1164
1165 return fit_image_verify_with_data(fit, image_noffset, data, size);
1166}
1167
1168/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001169 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001170 * @fit: pointer to the FIT format image header
1171 *
Simon Glassb8da8362013-05-07 06:11:57 +00001172 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001173 * for every images checks if all it's hashes are valid.
1174 *
1175 * returns:
1176 * 1, if all hashes of all images are valid
1177 * 0, otherwise (or on error)
1178 */
Simon Glassb8da8362013-05-07 06:11:57 +00001179int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001180{
1181 int images_noffset;
1182 int noffset;
1183 int ndepth;
1184 int count;
1185
1186 /* Find images parent node offset */
1187 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1188 if (images_noffset < 0) {
1189 printf("Can't find images parent node '%s' (%s)\n",
1190 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1191 return 0;
1192 }
1193
1194 /* Process all image subnodes, check hashes for each */
1195 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1196 (ulong)fit);
1197 for (ndepth = 0, count = 0,
1198 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1199 (noffset >= 0) && (ndepth > 0);
1200 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1201 if (ndepth == 1) {
1202 /*
1203 * Direct child node of the images parent node,
1204 * i.e. component image node.
1205 */
Simon Glass73223f02016-02-22 22:55:43 -07001206 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001207 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001208 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001209
Simon Glassb8da8362013-05-07 06:11:57 +00001210 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001211 return 0;
1212 printf("\n");
1213 }
1214 }
1215 return 1;
1216}
1217
1218/**
1219 * fit_image_check_os - check whether image node is of a given os type
1220 * @fit: pointer to the FIT format image header
1221 * @noffset: component image node offset
1222 * @os: requested image os
1223 *
1224 * fit_image_check_os() reads image os property and compares its numeric
1225 * id with the requested os. Comparison result is returned to the caller.
1226 *
1227 * returns:
1228 * 1 if image is of given os type
1229 * 0 otherwise (or on error)
1230 */
1231int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1232{
1233 uint8_t image_os;
1234
1235 if (fit_image_get_os(fit, noffset, &image_os))
1236 return 0;
1237 return (os == image_os);
1238}
1239
1240/**
1241 * fit_image_check_arch - check whether image node is of a given arch
1242 * @fit: pointer to the FIT format image header
1243 * @noffset: component image node offset
1244 * @arch: requested imagearch
1245 *
1246 * fit_image_check_arch() reads image arch property and compares its numeric
1247 * id with the requested arch. Comparison result is returned to the caller.
1248 *
1249 * returns:
1250 * 1 if image is of given arch
1251 * 0 otherwise (or on error)
1252 */
1253int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1254{
1255 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001256 int aarch32_support = 0;
1257
1258#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1259 aarch32_support = 1;
1260#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001261
1262 if (fit_image_get_arch(fit, noffset, &image_arch))
1263 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001264 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001265 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1266 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1267 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001268}
1269
1270/**
1271 * fit_image_check_type - check whether image node is of a given type
1272 * @fit: pointer to the FIT format image header
1273 * @noffset: component image node offset
1274 * @type: requested image type
1275 *
1276 * fit_image_check_type() reads image type property and compares its numeric
1277 * id with the requested type. Comparison result is returned to the caller.
1278 *
1279 * returns:
1280 * 1 if image is of given type
1281 * 0 otherwise (or on error)
1282 */
1283int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1284{
1285 uint8_t image_type;
1286
1287 if (fit_image_get_type(fit, noffset, &image_type))
1288 return 0;
1289 return (type == image_type);
1290}
1291
1292/**
1293 * fit_image_check_comp - check whether image node uses given compression
1294 * @fit: pointer to the FIT format image header
1295 * @noffset: component image node offset
1296 * @comp: requested image compression type
1297 *
1298 * fit_image_check_comp() reads image compression property and compares its
1299 * numeric id with the requested compression type. Comparison result is
1300 * returned to the caller.
1301 *
1302 * returns:
1303 * 1 if image uses requested compression
1304 * 0 otherwise (or on error)
1305 */
1306int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1307{
1308 uint8_t image_comp;
1309
1310 if (fit_image_get_comp(fit, noffset, &image_comp))
1311 return 0;
1312 return (comp == image_comp);
1313}
1314
1315/**
1316 * fit_check_format - sanity check FIT image format
1317 * @fit: pointer to the FIT format image header
1318 *
1319 * fit_check_format() runs a basic sanity FIT image verification.
1320 * Routine checks for mandatory properties, nodes, etc.
1321 *
1322 * returns:
1323 * 1, on success
1324 * 0, on failure
1325 */
1326int fit_check_format(const void *fit)
1327{
1328 /* mandatory / node 'description' property */
1329 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1330 debug("Wrong FIT format: no description\n");
1331 return 0;
1332 }
1333
1334 if (IMAGE_ENABLE_TIMESTAMP) {
1335 /* mandatory / node 'timestamp' property */
1336 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1337 debug("Wrong FIT format: no timestamp\n");
1338 return 0;
1339 }
1340 }
1341
1342 /* mandatory subimages parent '/images' node */
1343 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1344 debug("Wrong FIT format: no images parent node\n");
1345 return 0;
1346 }
1347
1348 return 1;
1349}
1350
1351
1352/**
1353 * fit_conf_find_compat
1354 * @fit: pointer to the FIT format image header
1355 * @fdt: pointer to the device tree to compare against
1356 *
1357 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1358 * most compatible with the passed in device tree.
1359 *
1360 * Example:
1361 *
1362 * / o image-tree
1363 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001364 * | |-o fdt-1
1365 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001366 * |
1367 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001368 * |-o config-1
1369 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001370 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001371 * |-o config-2
1372 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001373 *
1374 * / o U-Boot fdt
1375 * |-compatible = "foo,bar", "bim,bam"
1376 *
1377 * / o kernel fdt1
1378 * |-compatible = "foo,bar",
1379 *
1380 * / o kernel fdt2
1381 * |-compatible = "bim,bam", "baz,biz"
1382 *
1383 * Configuration 1 would be picked because the first string in U-Boot's
1384 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1385 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1386 *
1387 * returns:
1388 * offset to the configuration to use if one was found
1389 * -1 otherwise
1390 */
1391int fit_conf_find_compat(const void *fit, const void *fdt)
1392{
1393 int ndepth = 0;
1394 int noffset, confs_noffset, images_noffset;
1395 const void *fdt_compat;
1396 int fdt_compat_len;
1397 int best_match_offset = 0;
1398 int best_match_pos = 0;
1399
1400 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1401 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1402 if (confs_noffset < 0 || images_noffset < 0) {
1403 debug("Can't find configurations or images nodes.\n");
1404 return -1;
1405 }
1406
1407 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1408 if (!fdt_compat) {
1409 debug("Fdt for comparison has no \"compatible\" property.\n");
1410 return -1;
1411 }
1412
1413 /*
1414 * Loop over the configurations in the FIT image.
1415 */
1416 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1417 (noffset >= 0) && (ndepth > 0);
1418 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1419 const void *kfdt;
1420 const char *kfdt_name;
1421 int kfdt_noffset;
1422 const char *cur_fdt_compat;
1423 int len;
1424 size_t size;
1425 int i;
1426
1427 if (ndepth > 1)
1428 continue;
1429
1430 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1431 if (!kfdt_name) {
1432 debug("No fdt property found.\n");
1433 continue;
1434 }
1435 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1436 kfdt_name);
1437 if (kfdt_noffset < 0) {
1438 debug("No image node named \"%s\" found.\n",
1439 kfdt_name);
1440 continue;
1441 }
1442 /*
1443 * Get a pointer to this configuration's fdt.
1444 */
1445 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1446 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1447 continue;
1448 }
1449
1450 len = fdt_compat_len;
1451 cur_fdt_compat = fdt_compat;
1452 /*
1453 * Look for a match for each U-Boot compatibility string in
1454 * turn in this configuration's fdt.
1455 */
1456 for (i = 0; len > 0 &&
1457 (!best_match_offset || best_match_pos > i); i++) {
1458 int cur_len = strlen(cur_fdt_compat) + 1;
1459
1460 if (!fdt_node_check_compatible(kfdt, 0,
1461 cur_fdt_compat)) {
1462 best_match_offset = noffset;
1463 best_match_pos = i;
1464 break;
1465 }
1466 len -= cur_len;
1467 cur_fdt_compat += cur_len;
1468 }
1469 }
1470 if (!best_match_offset) {
1471 debug("No match found.\n");
1472 return -1;
1473 }
1474
1475 return best_match_offset;
1476}
1477
1478/**
1479 * fit_conf_get_node - get node offset for configuration of a given unit name
1480 * @fit: pointer to the FIT format image header
1481 * @conf_uname: configuration node unit name
1482 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001483 * fit_conf_get_node() finds a configuration (within the '/configurations'
1484 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001485 * offset is returned to the caller.
1486 *
1487 * When NULL is provided in second argument fit_conf_get_node() will search
1488 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001489 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001490 * node.
1491 *
1492 * returns:
1493 * configuration node offset when found (>=0)
1494 * negative number on failure (FDT_ERR_* code)
1495 */
1496int fit_conf_get_node(const void *fit, const char *conf_uname)
1497{
1498 int noffset, confs_noffset;
1499 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001500 const char *s;
1501 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001502
1503 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1504 if (confs_noffset < 0) {
1505 debug("Can't find configurations parent node '%s' (%s)\n",
1506 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1507 return confs_noffset;
1508 }
1509
1510 if (conf_uname == NULL) {
1511 /* get configuration unit name from the default property */
1512 debug("No configuration specified, trying default...\n");
1513 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1514 FIT_DEFAULT_PROP, &len);
1515 if (conf_uname == NULL) {
1516 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1517 len);
1518 return len;
1519 }
1520 debug("Found default configuration: '%s'\n", conf_uname);
1521 }
1522
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001523 s = strchr(conf_uname, '#');
1524 if (s) {
1525 len = s - conf_uname;
1526 conf_uname_copy = malloc(len + 1);
1527 if (!conf_uname_copy) {
1528 debug("Can't allocate uname copy: '%s'\n",
1529 conf_uname);
1530 return -ENOMEM;
1531 }
1532 memcpy(conf_uname_copy, conf_uname, len);
1533 conf_uname_copy[len] = '\0';
1534 conf_uname = conf_uname_copy;
1535 }
1536
Simon Glass53fbb7e2013-05-07 06:11:53 +00001537 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1538 if (noffset < 0) {
1539 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1540 conf_uname, fdt_strerror(noffset));
1541 }
1542
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001543 if (conf_uname_copy)
1544 free(conf_uname_copy);
1545
Simon Glass53fbb7e2013-05-07 06:11:53 +00001546 return noffset;
1547}
1548
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001549int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001550 const char *prop_name)
1551{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001552 return fdt_stringlist_count(fit, noffset, prop_name);
1553}
1554
1555int fit_conf_get_prop_node_index(const void *fit, int noffset,
1556 const char *prop_name, int index)
1557{
1558 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001559 int len;
1560
1561 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001562 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001563 if (uname == NULL)
1564 return len;
1565
1566 return fit_image_get_node(fit, uname);
1567}
1568
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001569int fit_conf_get_prop_node(const void *fit, int noffset,
1570 const char *prop_name)
1571{
1572 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1573}
1574
Simon Glass53fbb7e2013-05-07 06:11:53 +00001575/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001576 * fit_conf_print - prints out the FIT configuration details
1577 * @fit: pointer to the FIT format image header
1578 * @noffset: offset of the configuration node
1579 * @p: pointer to prefix string
1580 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001581 * fit_conf_print() lists all mandatory properties for the processed
Simon Glass53fbb7e2013-05-07 06:11:53 +00001582 * configuration node.
1583 *
1584 * returns:
1585 * no returned results
1586 */
1587void fit_conf_print(const void *fit, int noffset, const char *p)
1588{
1589 char *desc;
Simon Glassb02e4042016-10-02 17:59:28 -06001590 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001591 int ret;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001592 int fdt_index, loadables_index;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001593
1594 /* Mandatory properties */
1595 ret = fit_get_desc(fit, noffset, &desc);
1596 printf("%s Description: ", p);
1597 if (ret)
1598 printf("unavailable\n");
1599 else
1600 printf("%s\n", desc);
1601
Simon Glassb02e4042016-10-02 17:59:28 -06001602 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001603 printf("%s Kernel: ", p);
1604 if (uname == NULL)
1605 printf("unavailable\n");
1606 else
1607 printf("%s\n", uname);
1608
1609 /* Optional properties */
Simon Glassb02e4042016-10-02 17:59:28 -06001610 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001611 if (uname)
1612 printf("%s Init Ramdisk: %s\n", p, uname);
1613
Michal Simek1f8e4bf2018-03-26 16:31:26 +02001614 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1615 if (uname)
1616 printf("%s Firmware: %s\n", p, uname);
1617
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001618 for (fdt_index = 0;
1619 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1620 fdt_index, NULL), uname;
1621 fdt_index++) {
1622
1623 if (fdt_index == 0)
1624 printf("%s FDT: ", p);
1625 else
1626 printf("%s ", p);
1627 printf("%s\n", uname);
1628 }
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001629
Simon Glassb02e4042016-10-02 17:59:28 -06001630 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
Michal Simeked0cea72016-05-17 13:58:44 +02001631 if (uname)
1632 printf("%s FPGA: %s\n", p, uname);
1633
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001634 /* Print out all of the specified loadables */
1635 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001636 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1637 loadables_index, NULL), uname;
1638 loadables_index++) {
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001639 if (loadables_index == 0) {
1640 printf("%s Loadables: ", p);
1641 } else {
1642 printf("%s ", p);
1643 }
1644 printf("%s\n", uname);
1645 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001646}
1647
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001648static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001649{
1650 fit_image_print(fit, rd_noffset, " ");
1651
1652 if (verify) {
1653 puts(" Verifying Hash Integrity ... ");
1654 if (!fit_image_verify(fit, rd_noffset)) {
1655 puts("Bad Data Hash\n");
1656 return -EACCES;
1657 }
1658 puts("OK\n");
1659 }
1660
1661 return 0;
1662}
1663
Simon Glass782cfbb2013-05-16 13:53:21 +00001664int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1665 ulong addr)
1666{
1667 int cfg_noffset;
1668 void *fit_hdr;
1669 int noffset;
1670
1671 debug("* %s: using config '%s' from image at 0x%08lx\n",
1672 prop_name, images->fit_uname_cfg, addr);
1673
1674 /* Check whether configuration has this property defined */
1675 fit_hdr = map_sysmem(addr, 0);
1676 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1677 if (cfg_noffset < 0) {
1678 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001679 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001680 }
1681
1682 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1683 if (noffset < 0) {
1684 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001685 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001686 }
1687
1688 return noffset;
1689}
1690
Simon Glass126cc862014-06-12 07:24:47 -06001691/**
1692 * fit_get_image_type_property() - get property name for IH_TYPE_...
1693 *
1694 * @return the properly name where we expect to find the image in the
1695 * config node
1696 */
1697static const char *fit_get_image_type_property(int type)
1698{
1699 /*
1700 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001701 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001702 * anyway. So let's just keep it here.
1703 */
1704 switch (type) {
1705 case IH_TYPE_FLATDT:
1706 return FIT_FDT_PROP;
1707 case IH_TYPE_KERNEL:
1708 return FIT_KERNEL_PROP;
1709 case IH_TYPE_RAMDISK:
1710 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001711 case IH_TYPE_X86_SETUP:
1712 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001713 case IH_TYPE_LOADABLE:
1714 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001715 case IH_TYPE_FPGA:
1716 return FIT_FPGA_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001717 }
1718
1719 return "unknown";
1720}
1721
1722int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001723 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001724 int arch, int image_type, int bootstage_id,
1725 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1726{
1727 int cfg_noffset, noffset;
1728 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001729 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001730 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001731 const void *fit;
1732 const void *buf;
1733 size_t size;
1734 int type_ok, os_ok;
1735 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001736 uint8_t os;
Alison Wangec6617c2016-11-10 10:49:03 +08001737#ifndef USE_HOSTCC
1738 uint8_t os_arch;
1739#endif
Simon Glass126cc862014-06-12 07:24:47 -06001740 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001741 int ret;
1742
1743 fit = map_sysmem(addr, 0);
1744 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001745 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001746 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001747 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001748 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1749
1750 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1751 if (!fit_check_format(fit)) {
1752 printf("Bad FIT %s image format!\n", prop_name);
1753 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1754 return -ENOEXEC;
1755 }
1756 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1757 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001758 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001759 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1760 noffset = fit_image_get_node(fit, fit_uname);
1761 } else {
1762 /*
1763 * no image node unit name, try to get config
1764 * node first. If config unit node name is NULL
1765 * fit_conf_get_node() will try to find default config node
1766 */
1767 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1768 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1769 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1770 } else {
1771 cfg_noffset = fit_conf_get_node(fit,
1772 fit_uname_config);
1773 }
1774 if (cfg_noffset < 0) {
1775 puts("Could not find configuration node\n");
1776 bootstage_error(bootstage_id +
1777 BOOTSTAGE_SUB_NO_UNIT_NAME);
1778 return -ENOENT;
1779 }
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001780 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1781 printf(" Using '%s' configuration\n", fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001782 if (image_type == IH_TYPE_KERNEL) {
1783 /* Remember (and possibly verify) this config */
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001784 images->fit_uname_cfg = fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001785 if (IMAGE_ENABLE_VERIFY && images->verify) {
1786 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001787 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001788 puts("Bad Data Hash\n");
1789 bootstage_error(bootstage_id +
1790 BOOTSTAGE_SUB_HASH);
1791 return -EACCES;
1792 }
1793 puts("OK\n");
1794 }
1795 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1796 }
1797
1798 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1799 prop_name);
1800 fit_uname = fit_get_name(fit, noffset, NULL);
1801 }
1802 if (noffset < 0) {
1803 puts("Could not find subimage node\n");
1804 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1805 return -ENOENT;
1806 }
1807
1808 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1809
1810 ret = fit_image_select(fit, noffset, images->verify);
1811 if (ret) {
1812 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1813 return ret;
1814 }
1815
1816 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001817#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001818 if (!fit_image_check_target_arch(fit, noffset)) {
1819 puts("Unsupported Architecture\n");
1820 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1821 return -ENOEXEC;
1822 }
Simon Glassce1400f2014-06-12 07:24:53 -06001823#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001824
1825#ifndef USE_HOSTCC
1826 fit_image_get_arch(fit, noffset, &os_arch);
1827 images->os.arch = os_arch;
1828#endif
1829
Simon Glass782cfbb2013-05-16 13:53:21 +00001830 if (image_type == IH_TYPE_FLATDT &&
1831 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1832 puts("FDT image is compressed");
1833 return -EPROTONOSUPPORT;
1834 }
1835
1836 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1837 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001838 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1839 (image_type == IH_TYPE_KERNEL &&
1840 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001841
Andreas Bießmann950fe262016-08-14 20:31:24 +02001842 os_ok = image_type == IH_TYPE_FLATDT ||
1843 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001844 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001845 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001846 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001847
1848 /*
1849 * If either of the checks fail, we should report an error, but
1850 * if the image type is coming from the "loadables" field, we
1851 * don't care what it is
1852 */
1853 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001854 fit_image_get_os(fit, noffset, &os);
1855 printf("No %s %s %s Image\n",
1856 genimg_get_os_name(os),
1857 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001858 genimg_get_type_name(image_type));
1859 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1860 return -EIO;
1861 }
1862
1863 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1864
1865 /* get image data address and length */
1866 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1867 printf("Could not find %s subimage data!\n", prop_name);
1868 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001869 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001870 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06001871
1872#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1873 /* perform any post-processing on the image data */
1874 board_fit_image_post_process((void **)&buf, &size);
1875#endif
1876
Simon Glass782cfbb2013-05-16 13:53:21 +00001877 len = (ulong)size;
1878
1879 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001880 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001881 puts("Subimage data is not a FDT");
1882 return -ENOEXEC;
1883 }
1884
1885 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1886
1887 /*
1888 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001889 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001890 * warning: initialization discards qualifiers from pointer target type
1891 */
1892 {
1893 void *vbuf = (void *)buf;
1894
1895 data = map_to_sysmem(vbuf);
1896 }
1897
1898 if (load_op == FIT_LOAD_IGNORED) {
1899 /* Don't load */
1900 } else if (fit_image_get_load(fit, noffset, &load)) {
1901 if (load_op == FIT_LOAD_REQUIRED) {
1902 printf("Can't get %s subimage load address!\n",
1903 prop_name);
1904 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1905 return -EBADF;
1906 }
Simon Glassfe20a812014-08-22 14:26:43 -06001907 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001908 ulong image_start, image_end;
1909 ulong load_end;
1910 void *dst;
1911
1912 /*
1913 * move image data to the load address,
1914 * make sure we don't overwrite initial image
1915 */
1916 image_start = addr;
1917 image_end = addr + fit_get_size(fit);
1918
1919 load_end = load + len;
1920 if (image_type != IH_TYPE_KERNEL &&
1921 load < image_end && load_end > image_start) {
1922 printf("Error: %s overwritten\n", prop_name);
1923 return -EXDEV;
1924 }
1925
1926 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1927 prop_name, data, load);
1928
1929 dst = map_sysmem(load, len);
1930 memmove(dst, buf, len);
1931 data = load;
1932 }
1933 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1934
1935 *datap = data;
1936 *lenp = len;
1937 if (fit_unamep)
1938 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001939 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001940 *fit_uname_configp = (char *)(fit_uname_config ? :
1941 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001942
1943 return noffset;
1944}
Simon Glass90268b82014-10-19 21:11:24 -06001945
1946int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1947 ulong *setup_start, ulong *setup_len)
1948{
1949 int noffset;
1950 ulong addr;
1951 ulong len;
1952 int ret;
1953
1954 addr = map_to_sysmem(images->fit_hdr_os);
1955 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1956 if (noffset < 0)
1957 return noffset;
1958
1959 ret = fit_image_load(images, addr, NULL, NULL, arch,
1960 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1961 FIT_LOAD_REQUIRED, setup_start, &len);
1962
1963 return ret;
1964}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001965
1966#ifndef USE_HOSTCC
1967int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1968 const char **fit_unamep, const char **fit_uname_configp,
1969 int arch, ulong *datap, ulong *lenp)
1970{
1971 int fdt_noffset, cfg_noffset, count;
1972 const void *fit;
1973 const char *fit_uname = NULL;
1974 const char *fit_uname_config = NULL;
1975 char *fit_uname_config_copy = NULL;
1976 char *next_config = NULL;
1977 ulong load, len;
1978#ifdef CONFIG_OF_LIBFDT_OVERLAY
1979 ulong image_start, image_end;
1980 ulong ovload, ovlen;
1981 const char *uconfig;
1982 const char *uname;
1983 void *base, *ov;
1984 int i, err, noffset, ov_noffset;
1985#endif
1986
1987 fit_uname = fit_unamep ? *fit_unamep : NULL;
1988
1989 if (fit_uname_configp && *fit_uname_configp) {
1990 fit_uname_config_copy = strdup(*fit_uname_configp);
1991 if (!fit_uname_config_copy)
1992 return -ENOMEM;
1993
1994 next_config = strchr(fit_uname_config_copy, '#');
1995 if (next_config)
1996 *next_config++ = '\0';
1997 if (next_config - 1 > fit_uname_config_copy)
1998 fit_uname_config = fit_uname_config_copy;
1999 }
2000
2001 fdt_noffset = fit_image_load(images,
2002 addr, &fit_uname, &fit_uname_config,
2003 arch, IH_TYPE_FLATDT,
2004 BOOTSTAGE_ID_FIT_FDT_START,
2005 FIT_LOAD_OPTIONAL, &load, &len);
2006
2007 if (fdt_noffset < 0)
2008 goto out;
2009
2010 debug("fit_uname=%s, fit_uname_config=%s\n",
2011 fit_uname ? fit_uname : "<NULL>",
2012 fit_uname_config ? fit_uname_config : "<NULL>");
2013
2014 fit = map_sysmem(addr, 0);
2015
2016 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2017
2018 /* single blob, or error just return as well */
2019 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2020 if (count <= 1 && !next_config)
2021 goto out;
2022
2023 /* we need to apply overlays */
2024
2025#ifdef CONFIG_OF_LIBFDT_OVERLAY
2026 image_start = addr;
2027 image_end = addr + fit_get_size(fit);
2028 /* verify that relocation took place by load address not being in fit */
2029 if (load >= image_start && load < image_end) {
2030 /* check is simplified; fit load checks for overlaps */
2031 printf("Overlayed FDT requires relocation\n");
2032 fdt_noffset = -EBADF;
2033 goto out;
2034 }
2035
2036 base = map_sysmem(load, len);
2037
2038 /* apply extra configs in FIT first, followed by args */
2039 for (i = 1; ; i++) {
2040 if (i < count) {
2041 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2042 FIT_FDT_PROP, i);
2043 uname = fit_get_name(fit, noffset, NULL);
2044 uconfig = NULL;
2045 } else {
2046 if (!next_config)
2047 break;
2048 uconfig = next_config;
2049 next_config = strchr(next_config, '#');
2050 if (next_config)
2051 *next_config++ = '\0';
2052 uname = NULL;
2053 }
2054
2055 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2056
2057 ov_noffset = fit_image_load(images,
2058 addr, &uname, &uconfig,
2059 arch, IH_TYPE_FLATDT,
2060 BOOTSTAGE_ID_FIT_FDT_START,
2061 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2062 if (ov_noffset < 0) {
2063 printf("load of %s failed\n", uname);
2064 continue;
2065 }
2066 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2067 uname, ovload, ovlen);
2068 ov = map_sysmem(ovload, ovlen);
2069
2070 base = map_sysmem(load, len + ovlen);
2071 err = fdt_open_into(base, base, len + ovlen);
2072 if (err < 0) {
2073 printf("failed on fdt_open_into\n");
2074 fdt_noffset = err;
2075 goto out;
2076 }
2077 /* the verbose method prints out messages on error */
2078 err = fdt_overlay_apply_verbose(base, ov);
2079 if (err < 0) {
2080 fdt_noffset = err;
2081 goto out;
2082 }
2083 fdt_pack(base);
2084 len = fdt_totalsize(base);
2085 }
2086#else
2087 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2088 fdt_noffset = -EBADF;
2089#endif
2090
2091out:
2092 if (datap)
2093 *datap = load;
2094 if (lenp)
2095 *lenp = len;
2096 if (fit_unamep)
2097 *fit_unamep = fit_uname;
2098 if (fit_uname_configp)
2099 *fit_uname_configp = fit_uname_config;
2100
2101 if (fit_uname_config_copy)
2102 free(fit_uname_config_copy);
2103 return fdt_noffset;
2104}
2105#endif