blob: 5b93dceae15c045aaefc3061c8fa0456da313a1c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass53fbb7e2013-05-07 06:11:53 +00002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass53fbb7e2013-05-07 06:11:53 +00009 */
10
11#ifdef USE_HOSTCC
12#include "mkimage.h"
Simon Glass53fbb7e2013-05-07 06:11:53 +000013#include <time.h>
14#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050015#include <linux/compiler.h>
York Sun020198b2016-11-23 09:25:09 -080016#include <linux/kconfig.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000017#include <common.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000018#include <errno.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000020#include <asm/io.h>
Pantelis Antoniou169043d2017-09-04 23:12:16 +030021#include <malloc.h>
Simon Glass782cfbb2013-05-16 13:53:21 +000022DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000023#endif /* !USE_HOSTCC*/
24
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050025#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000026#include <bootstage.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000027#include <u-boot/crc.h>
28#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020029#include <u-boot/sha1.h>
30#include <u-boot/sha256.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000031
32/*****************************************************************************/
33/* New uImage format routines */
34/*****************************************************************************/
35#ifndef USE_HOSTCC
36static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
37 ulong *addr, const char **name)
38{
39 const char *sep;
40
41 *addr = addr_curr;
42 *name = NULL;
43
44 sep = strchr(spec, sepc);
45 if (sep) {
46 if (sep - spec > 0)
47 *addr = simple_strtoul(spec, NULL, 16);
48
49 *name = sep + 1;
50 return 1;
51 }
52
53 return 0;
54}
55
56/**
57 * fit_parse_conf - parse FIT configuration spec
58 * @spec: input string, containing configuration spec
59 * @add_curr: current image address (to be used as a possible default)
60 * @addr: pointer to a ulong variable, will hold FIT image address of a given
61 * configuration
62 * @conf_name double pointer to a char, will hold pointer to a configuration
63 * unit name
64 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090065 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000066 * where <addr> is a FIT image address that contains configuration
67 * with a <conf> unit name.
68 *
69 * Address part is optional, and if omitted default add_curr will
70 * be used instead.
71 *
72 * returns:
73 * 1 if spec is a valid configuration string,
74 * addr and conf_name are set accordingly
75 * 0 otherwise
76 */
77int fit_parse_conf(const char *spec, ulong addr_curr,
78 ulong *addr, const char **conf_name)
79{
80 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
81}
82
83/**
84 * fit_parse_subimage - parse FIT subimage spec
85 * @spec: input string, containing subimage spec
86 * @add_curr: current image address (to be used as a possible default)
87 * @addr: pointer to a ulong variable, will hold FIT image address of a given
88 * subimage
89 * @image_name: double pointer to a char, will hold pointer to a subimage name
90 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090091 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000092 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
93 * subimage with a <subimg> unit name.
94 *
95 * Address part is optional, and if omitted default add_curr will
96 * be used instead.
97 *
98 * returns:
99 * 1 if spec is a valid subimage string,
100 * addr and image_name are set accordingly
101 * 0 otherwise
102 */
103int fit_parse_subimage(const char *spec, ulong addr_curr,
104 ulong *addr, const char **image_name)
105{
106 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
107}
108#endif /* !USE_HOSTCC */
109
110static void fit_get_debug(const void *fit, int noffset,
111 char *prop_name, int err)
112{
113 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
114 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
115 fdt_strerror(err));
116}
117
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200118/**
119 * fit_get_subimage_count - get component (sub-image) count
120 * @fit: pointer to the FIT format image header
121 * @images_noffset: offset of images node
122 *
123 * returns:
124 * number of image components
125 */
126int fit_get_subimage_count(const void *fit, int images_noffset)
127{
128 int noffset;
129 int ndepth;
130 int count = 0;
131
132 /* Process its subnodes, print out component images details */
133 for (ndepth = 0, count = 0,
134 noffset = fdt_next_node(fit, images_noffset, &ndepth);
135 (noffset >= 0) && (ndepth > 0);
136 noffset = fdt_next_node(fit, noffset, &ndepth)) {
137 if (ndepth == 1) {
138 count++;
139 }
140 }
141
142 return count;
143}
144
Simon Glass87ebee32013-05-08 08:05:59 +0000145#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000146/**
147 * fit_print_contents - prints out the contents of the FIT format image
148 * @fit: pointer to the FIT format image header
149 * @p: pointer to prefix string
150 *
151 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500152 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000153 * the details of each component image.
154 *
155 * returns:
156 * no returned results
157 */
158void fit_print_contents(const void *fit)
159{
160 char *desc;
161 char *uname;
162 int images_noffset;
163 int confs_noffset;
164 int noffset;
165 int ndepth;
166 int count = 0;
167 int ret;
168 const char *p;
169 time_t timestamp;
170
Simon Glass1fe7d932013-05-08 08:05:58 +0000171 /* Indent string is defined in header image.h */
172 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000173
174 /* Root node properties */
175 ret = fit_get_desc(fit, 0, &desc);
176 printf("%sFIT description: ", p);
177 if (ret)
178 printf("unavailable\n");
179 else
180 printf("%s\n", desc);
181
182 if (IMAGE_ENABLE_TIMESTAMP) {
183 ret = fit_get_timestamp(fit, 0, &timestamp);
184 printf("%sCreated: ", p);
185 if (ret)
186 printf("unavailable\n");
187 else
188 genimg_print_time(timestamp);
189 }
190
191 /* Find images parent node offset */
192 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
193 if (images_noffset < 0) {
194 printf("Can't find images parent node '%s' (%s)\n",
195 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
196 return;
197 }
198
199 /* Process its subnodes, print out component images details */
200 for (ndepth = 0, count = 0,
201 noffset = fdt_next_node(fit, images_noffset, &ndepth);
202 (noffset >= 0) && (ndepth > 0);
203 noffset = fdt_next_node(fit, noffset, &ndepth)) {
204 if (ndepth == 1) {
205 /*
206 * Direct child node of the images parent node,
207 * i.e. component image node.
208 */
209 printf("%s Image %u (%s)\n", p, count++,
210 fit_get_name(fit, noffset, NULL));
211
212 fit_image_print(fit, noffset, p);
213 }
214 }
215
216 /* Find configurations parent node offset */
217 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
218 if (confs_noffset < 0) {
219 debug("Can't get configurations parent node '%s' (%s)\n",
220 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
221 return;
222 }
223
224 /* get default configuration unit name from default property */
225 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
226 if (uname)
227 printf("%s Default Configuration: '%s'\n", p, uname);
228
229 /* Process its subnodes, print out configurations details */
230 for (ndepth = 0, count = 0,
231 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
232 (noffset >= 0) && (ndepth > 0);
233 noffset = fdt_next_node(fit, noffset, &ndepth)) {
234 if (ndepth == 1) {
235 /*
236 * Direct child node of the configurations parent node,
237 * i.e. configuration node.
238 */
239 printf("%s Configuration %u (%s)\n", p, count++,
240 fit_get_name(fit, noffset, NULL));
241
242 fit_conf_print(fit, noffset, p);
243 }
244 }
245}
246
247/**
Simon Glassd8b75362013-05-07 06:12:02 +0000248 * fit_image_print_data() - prints out the hash node details
249 * @fit: pointer to the FIT format image header
250 * @noffset: offset of the hash node
251 * @p: pointer to prefix string
Simon Glass56518e72013-06-13 15:10:01 -0700252 * @type: Type of information to print ("hash" or "sign")
Simon Glassd8b75362013-05-07 06:12:02 +0000253 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500254 * fit_image_print_data() lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000255 *
Simon Glass56518e72013-06-13 15:10:01 -0700256 * This function avoid using puts() since it prints a newline on the host
257 * but does not in U-Boot.
258 *
Simon Glassd8b75362013-05-07 06:12:02 +0000259 * returns:
260 * no returned results
261 */
Simon Glass56518e72013-06-13 15:10:01 -0700262static void fit_image_print_data(const void *fit, int noffset, const char *p,
263 const char *type)
Simon Glassd8b75362013-05-07 06:12:02 +0000264{
Simon Glass56518e72013-06-13 15:10:01 -0700265 const char *keyname;
Simon Glassd8b75362013-05-07 06:12:02 +0000266 uint8_t *value;
267 int value_len;
Simon Glass56518e72013-06-13 15:10:01 -0700268 char *algo;
269 int required;
270 int ret, i;
Simon Glassd8b75362013-05-07 06:12:02 +0000271
Simon Glass56518e72013-06-13 15:10:01 -0700272 debug("%s %s node: '%s'\n", p, type,
Simon Glassd8b75362013-05-07 06:12:02 +0000273 fit_get_name(fit, noffset, NULL));
Simon Glass56518e72013-06-13 15:10:01 -0700274 printf("%s %s algo: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000275 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
276 printf("invalid/unsupported\n");
277 return;
278 }
Simon Glass56518e72013-06-13 15:10:01 -0700279 printf("%s", algo);
280 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
281 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
282 if (keyname)
283 printf(":%s", keyname);
284 if (required)
285 printf(" (required)");
286 printf("\n");
Simon Glassd8b75362013-05-07 06:12:02 +0000287
288 ret = fit_image_hash_get_value(fit, noffset, &value,
289 &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700290 printf("%s %s value: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000291 if (ret) {
292 printf("unavailable\n");
293 } else {
294 for (i = 0; i < value_len; i++)
295 printf("%02x", value[i]);
296 printf("\n");
297 }
298
Simon Glass56518e72013-06-13 15:10:01 -0700299 debug("%s %s len: %d\n", p, type, value_len);
300
301 /* Signatures have a time stamp */
302 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
303 time_t timestamp;
304
305 printf("%s Timestamp: ", p);
306 if (fit_get_timestamp(fit, noffset, &timestamp))
307 printf("unavailable\n");
308 else
309 genimg_print_time(timestamp);
310 }
Simon Glassd8b75362013-05-07 06:12:02 +0000311}
312
313/**
314 * fit_image_print_verification_data() - prints out the hash/signature details
315 * @fit: pointer to the FIT format image header
316 * @noffset: offset of the hash or signature node
317 * @p: pointer to prefix string
318 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500319 * This lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000320 *
321 * returns:
322 * no returned results
323 */
324static void fit_image_print_verification_data(const void *fit, int noffset,
325 const char *p)
326{
327 const char *name;
328
329 /*
330 * Check subnode name, must be equal to "hash" or "signature".
331 * Multiple hash/signature nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +0000332 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
Simon Glassd8b75362013-05-07 06:12:02 +0000333 */
334 name = fit_get_name(fit, noffset, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700335 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
336 fit_image_print_data(fit, noffset, p, "Hash");
337 } else if (!strncmp(name, FIT_SIG_NODENAME,
338 strlen(FIT_SIG_NODENAME))) {
339 fit_image_print_data(fit, noffset, p, "Sign");
340 }
Simon Glassd8b75362013-05-07 06:12:02 +0000341}
342
343/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000344 * fit_image_print - prints out the FIT component image details
345 * @fit: pointer to the FIT format image header
346 * @image_noffset: offset of the component image node
347 * @p: pointer to prefix string
348 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500349 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000350 * image. If present, hash nodes are printed out as well. Load
351 * address for images of type firmware is also printed out. Since the load
352 * address is not mandatory for firmware images, it will be output as
353 * "unavailable" when not present.
354 *
355 * returns:
356 * no returned results
357 */
358void fit_image_print(const void *fit, int image_noffset, const char *p)
359{
360 char *desc;
361 uint8_t type, arch, os, comp;
362 size_t size;
363 ulong load, entry;
364 const void *data;
365 int noffset;
366 int ndepth;
367 int ret;
368
369 /* Mandatory properties */
370 ret = fit_get_desc(fit, image_noffset, &desc);
371 printf("%s Description: ", p);
372 if (ret)
373 printf("unavailable\n");
374 else
375 printf("%s\n", desc);
376
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700377 if (IMAGE_ENABLE_TIMESTAMP) {
378 time_t timestamp;
379
380 ret = fit_get_timestamp(fit, 0, &timestamp);
381 printf("%s Created: ", p);
382 if (ret)
383 printf("unavailable\n");
384 else
385 genimg_print_time(timestamp);
386 }
387
Simon Glass53fbb7e2013-05-07 06:11:53 +0000388 fit_image_get_type(fit, image_noffset, &type);
389 printf("%s Type: %s\n", p, genimg_get_type_name(type));
390
391 fit_image_get_comp(fit, image_noffset, &comp);
392 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
393
394 ret = fit_image_get_data(fit, image_noffset, &data, &size);
395
396#ifndef USE_HOSTCC
397 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000398 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000399 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000400 } else {
401 void *vdata = (void *)data;
402
403 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
404 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000405#endif
406
407 printf("%s Data Size: ", p);
408 if (ret)
409 printf("unavailable\n");
410 else
411 genimg_print_size(size);
412
413 /* Remaining, type dependent properties */
414 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
415 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
416 (type == IH_TYPE_FLATDT)) {
417 fit_image_get_arch(fit, image_noffset, &arch);
418 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
419 }
420
Michal Simek6faf4622018-03-26 16:31:27 +0200421 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
422 (type == IH_TYPE_FIRMWARE)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000423 fit_image_get_os(fit, image_noffset, &os);
424 printf("%s OS: %s\n", p, genimg_get_os_name(os));
425 }
426
427 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200428 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
429 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000430 ret = fit_image_get_load(fit, image_noffset, &load);
431 printf("%s Load Address: ", p);
432 if (ret)
433 printf("unavailable\n");
434 else
435 printf("0x%08lx\n", load);
436 }
437
Pantelis Antoniou169043d2017-09-04 23:12:16 +0300438 /* optional load address for FDT */
439 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
440 printf("%s Load Address: 0x%08lx\n", p, load);
441
Simon Glass53fbb7e2013-05-07 06:11:53 +0000442 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
443 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800444 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000445 printf("%s Entry Point: ", p);
446 if (ret)
447 printf("unavailable\n");
448 else
449 printf("0x%08lx\n", entry);
450 }
451
452 /* Process all hash subnodes of the component image node */
453 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
454 (noffset >= 0) && (ndepth > 0);
455 noffset = fdt_next_node(fit, noffset, &ndepth)) {
456 if (ndepth == 1) {
457 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000458 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000459 }
460 }
461}
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200462
463#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000464
465/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000466 * fit_get_desc - get node description property
467 * @fit: pointer to the FIT format image header
468 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500469 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000470 *
471 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500472 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000473 *
474 * returns:
475 * 0, on success
476 * -1, on failure
477 */
478int fit_get_desc(const void *fit, int noffset, char **desc)
479{
480 int len;
481
482 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
483 if (*desc == NULL) {
484 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
485 return -1;
486 }
487
488 return 0;
489}
490
491/**
492 * fit_get_timestamp - get node timestamp property
493 * @fit: pointer to the FIT format image header
494 * @noffset: node offset
495 * @timestamp: pointer to the time_t, will hold read timestamp
496 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500497 * fit_get_timestamp() reads timestamp property from given node, if timestamp
498 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000499 * argument.
500 *
501 * returns:
502 * 0, on success
503 * -1, on property read failure
504 * -2, on wrong timestamp size
505 */
506int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
507{
508 int len;
509 const void *data;
510
511 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
512 if (data == NULL) {
513 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
514 return -1;
515 }
516 if (len != sizeof(uint32_t)) {
517 debug("FIT timestamp with incorrect size of (%u)\n", len);
518 return -2;
519 }
520
521 *timestamp = uimage_to_cpu(*((uint32_t *)data));
522 return 0;
523}
524
525/**
526 * fit_image_get_node - get node offset for component image of a given unit name
527 * @fit: pointer to the FIT format image header
528 * @image_uname: component image node unit name
529 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500530 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000531 * node) of a provided unit name. If image is found its node offset is
532 * returned to the caller.
533 *
534 * returns:
535 * image node offset when found (>=0)
536 * negative number on failure (FDT_ERR_* code)
537 */
538int fit_image_get_node(const void *fit, const char *image_uname)
539{
540 int noffset, images_noffset;
541
542 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
543 if (images_noffset < 0) {
544 debug("Can't find images parent node '%s' (%s)\n",
545 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
546 return images_noffset;
547 }
548
549 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
550 if (noffset < 0) {
551 debug("Can't get node offset for image unit name: '%s' (%s)\n",
552 image_uname, fdt_strerror(noffset));
553 }
554
555 return noffset;
556}
557
558/**
559 * fit_image_get_os - get os id for a given component image node
560 * @fit: pointer to the FIT format image header
561 * @noffset: component image node offset
562 * @os: pointer to the uint8_t, will hold os numeric id
563 *
564 * fit_image_get_os() finds os property in a given component image node.
565 * If the property is found, its (string) value is translated to the numeric
566 * id which is returned to the caller.
567 *
568 * returns:
569 * 0, on success
570 * -1, on failure
571 */
572int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
573{
574 int len;
575 const void *data;
576
577 /* Get OS name from property data */
578 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
579 if (data == NULL) {
580 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
581 *os = -1;
582 return -1;
583 }
584
585 /* Translate OS name to id */
586 *os = genimg_get_os_id(data);
587 return 0;
588}
589
590/**
591 * fit_image_get_arch - get arch id for a given component image node
592 * @fit: pointer to the FIT format image header
593 * @noffset: component image node offset
594 * @arch: pointer to the uint8_t, will hold arch numeric id
595 *
596 * fit_image_get_arch() finds arch property in a given component image node.
597 * If the property is found, its (string) value is translated to the numeric
598 * id which is returned to the caller.
599 *
600 * returns:
601 * 0, on success
602 * -1, on failure
603 */
604int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
605{
606 int len;
607 const void *data;
608
609 /* Get architecture name from property data */
610 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
611 if (data == NULL) {
612 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
613 *arch = -1;
614 return -1;
615 }
616
617 /* Translate architecture name to id */
618 *arch = genimg_get_arch_id(data);
619 return 0;
620}
621
622/**
623 * fit_image_get_type - get type id for a given component image node
624 * @fit: pointer to the FIT format image header
625 * @noffset: component image node offset
626 * @type: pointer to the uint8_t, will hold type numeric id
627 *
628 * fit_image_get_type() finds type property in a given component image node.
629 * If the property is found, its (string) value is translated to the numeric
630 * id which is returned to the caller.
631 *
632 * returns:
633 * 0, on success
634 * -1, on failure
635 */
636int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
637{
638 int len;
639 const void *data;
640
641 /* Get image type name from property data */
642 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
643 if (data == NULL) {
644 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
645 *type = -1;
646 return -1;
647 }
648
649 /* Translate image type name to id */
650 *type = genimg_get_type_id(data);
651 return 0;
652}
653
654/**
655 * fit_image_get_comp - get comp id for a given component image node
656 * @fit: pointer to the FIT format image header
657 * @noffset: component image node offset
658 * @comp: pointer to the uint8_t, will hold comp numeric id
659 *
660 * fit_image_get_comp() finds comp property in a given component image node.
661 * If the property is found, its (string) value is translated to the numeric
662 * id which is returned to the caller.
663 *
664 * returns:
665 * 0, on success
666 * -1, on failure
667 */
668int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
669{
670 int len;
671 const void *data;
672
673 /* Get compression name from property data */
674 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
675 if (data == NULL) {
676 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
677 *comp = -1;
678 return -1;
679 }
680
681 /* Translate compression name to id */
682 *comp = genimg_get_comp_id(data);
683 return 0;
684}
685
York Sun60047652016-02-29 15:48:40 -0800686static int fit_image_get_address(const void *fit, int noffset, char *name,
687 ulong *load)
688{
York Sunc1913cb2016-02-29 15:48:41 -0800689 int len, cell_len;
690 const fdt32_t *cell;
691 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800692
York Sunc1913cb2016-02-29 15:48:41 -0800693 cell = fdt_getprop(fit, noffset, name, &len);
694 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800695 fit_get_debug(fit, noffset, name, len);
696 return -1;
697 }
698
York Sunc1913cb2016-02-29 15:48:41 -0800699 if (len > sizeof(ulong)) {
700 printf("Unsupported %s address size\n", name);
701 return -1;
702 }
703
704 cell_len = len >> 2;
705 /* Use load64 to avoid compiling warning for 32-bit target */
706 while (cell_len--) {
707 load64 = (load64 << 32) | uimage_to_cpu(*cell);
708 cell++;
709 }
710 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800711
712 return 0;
713}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000714/**
715 * fit_image_get_load() - get load addr property for given component image node
716 * @fit: pointer to the FIT format image header
717 * @noffset: component image node offset
718 * @load: pointer to the uint32_t, will hold load address
719 *
720 * fit_image_get_load() finds load address property in a given component
721 * image node. If the property is found, its value is returned to the caller.
722 *
723 * returns:
724 * 0, on success
725 * -1, on failure
726 */
727int fit_image_get_load(const void *fit, int noffset, ulong *load)
728{
York Sun60047652016-02-29 15:48:40 -0800729 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000730}
731
732/**
733 * fit_image_get_entry() - get entry point address property
734 * @fit: pointer to the FIT format image header
735 * @noffset: component image node offset
736 * @entry: pointer to the uint32_t, will hold entry point address
737 *
738 * This gets the entry point address property for a given component image
739 * node.
740 *
741 * fit_image_get_entry() finds entry point address property in a given
742 * component image node. If the property is found, its value is returned
743 * to the caller.
744 *
745 * returns:
746 * 0, on success
747 * -1, on failure
748 */
749int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
750{
York Sun60047652016-02-29 15:48:40 -0800751 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000752}
753
754/**
755 * fit_image_get_data - get data property and its size for a given component image node
756 * @fit: pointer to the FIT format image header
757 * @noffset: component image node offset
758 * @data: double pointer to void, will hold data property's data address
759 * @size: pointer to size_t, will hold data property's data size
760 *
761 * fit_image_get_data() finds data property in a given component image node.
762 * If the property is found its data start address and size are returned to
763 * the caller.
764 *
765 * returns:
766 * 0, on success
767 * -1, on failure
768 */
769int fit_image_get_data(const void *fit, int noffset,
770 const void **data, size_t *size)
771{
772 int len;
773
774 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
775 if (*data == NULL) {
776 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
777 *size = 0;
778 return -1;
779 }
780
781 *size = len;
782 return 0;
783}
784
785/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200786 * Get 'data-offset' property from a given image node.
787 *
788 * @fit: pointer to the FIT image header
789 * @noffset: component image node offset
790 * @data_offset: holds the data-offset property
791 *
792 * returns:
793 * 0, on success
794 * -ENOENT if the property could not be found
795 */
796int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
797{
798 const fdt32_t *val;
799
800 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
801 if (!val)
802 return -ENOENT;
803
804 *data_offset = fdt32_to_cpu(*val);
805
806 return 0;
807}
808
809/**
Peng Fana1be94b2017-12-05 13:20:59 +0800810 * Get 'data-position' property from a given image node.
811 *
812 * @fit: pointer to the FIT image header
813 * @noffset: component image node offset
814 * @data_position: holds the data-position property
815 *
816 * returns:
817 * 0, on success
818 * -ENOENT if the property could not be found
819 */
820int fit_image_get_data_position(const void *fit, int noffset,
821 int *data_position)
822{
823 const fdt32_t *val;
824
825 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
826 if (!val)
827 return -ENOENT;
828
829 *data_position = fdt32_to_cpu(*val);
830
831 return 0;
832}
833
834/**
tomas.melin@vaisala.comdb1b79b2017-01-13 13:20:14 +0200835 * Get 'data-size' property from a given image node.
836 *
837 * @fit: pointer to the FIT image header
838 * @noffset: component image node offset
839 * @data_size: holds the data-size property
840 *
841 * returns:
842 * 0, on success
843 * -ENOENT if the property could not be found
844 */
845int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
846{
847 const fdt32_t *val;
848
849 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
850 if (!val)
851 return -ENOENT;
852
853 *data_size = fdt32_to_cpu(*val);
854
855 return 0;
856}
857
858/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000859 * fit_image_hash_get_algo - get hash algorithm name
860 * @fit: pointer to the FIT format image header
861 * @noffset: hash node offset
862 * @algo: double pointer to char, will hold pointer to the algorithm name
863 *
864 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
865 * If the property is found its data start address is returned to the caller.
866 *
867 * returns:
868 * 0, on success
869 * -1, on failure
870 */
871int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
872{
873 int len;
874
875 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
876 if (*algo == NULL) {
877 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
878 return -1;
879 }
880
881 return 0;
882}
883
884/**
885 * fit_image_hash_get_value - get hash value and length
886 * @fit: pointer to the FIT format image header
887 * @noffset: hash node offset
888 * @value: double pointer to uint8_t, will hold address of a hash value data
889 * @value_len: pointer to an int, will hold hash data length
890 *
891 * fit_image_hash_get_value() finds hash value property in a given hash node.
892 * If the property is found its data start address and size are returned to
893 * the caller.
894 *
895 * returns:
896 * 0, on success
897 * -1, on failure
898 */
899int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
900 int *value_len)
901{
902 int len;
903
904 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
905 if (*value == NULL) {
906 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
907 *value_len = 0;
908 return -1;
909 }
910
911 *value_len = len;
912 return 0;
913}
914
Simon Glass53fbb7e2013-05-07 06:11:53 +0000915/**
916 * fit_image_hash_get_ignore - get hash ignore flag
917 * @fit: pointer to the FIT format image header
918 * @noffset: hash node offset
919 * @ignore: pointer to an int, will hold hash ignore flag
920 *
921 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
922 * If the property is found and non-zero, the hash algorithm is not verified by
923 * u-boot automatically.
924 *
925 * returns:
926 * 0, on ignore not found
927 * value, on ignore found
928 */
Simon Glassab9efc62013-05-07 06:11:58 +0000929static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000930{
931 int len;
932 int *value;
933
934 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
935 if (value == NULL || len != sizeof(int))
936 *ignore = 0;
937 else
938 *ignore = *value;
939
940 return 0;
941}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000942
Simon Glass7a80de42016-02-24 09:14:42 -0700943ulong fit_get_end(const void *fit)
944{
945 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
946}
947
Simon Glass53fbb7e2013-05-07 06:11:53 +0000948/**
949 * fit_set_timestamp - set node timestamp property
950 * @fit: pointer to the FIT format image header
951 * @noffset: node offset
952 * @timestamp: timestamp value to be set
953 *
954 * fit_set_timestamp() attempts to set timestamp property in the requested
955 * node and returns operation status to the caller.
956 *
957 * returns:
958 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -0600959 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +0000960 */
961int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
962{
963 uint32_t t;
964 int ret;
965
966 t = cpu_to_uimage(timestamp);
967 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
968 sizeof(uint32_t));
969 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -0600970 debug("Can't set '%s' property for '%s' node (%s)\n",
971 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
972 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -0600973 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000974 }
975
976 return 0;
977}
978
979/**
980 * calculate_hash - calculate and return hash for provided input data
981 * @data: pointer to the input data
982 * @data_len: data length
983 * @algo: requested hash algorithm
984 * @value: pointer to the char, will hold hash value data (caller must
985 * allocate enough free space)
986 * value_len: length of the calculated hash
987 *
988 * calculate_hash() computes input data hash according to the requested
989 * algorithm.
990 * Resulting hash value is placed in caller provided 'value' buffer, length
991 * of the calculated hash is returned via value_len pointer argument.
992 *
993 * returns:
994 * 0, on success
995 * -1, when algo is unsupported
996 */
Simon Glass604f23d2013-05-07 06:11:54 +0000997int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000998 uint8_t *value, int *value_len)
999{
Simon Glass87ebee32013-05-08 08:05:59 +00001000 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001001 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1002 CHUNKSZ_CRC32);
1003 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1004 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +00001005 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001006 sha1_csum_wd((unsigned char *)data, data_len,
1007 (unsigned char *)value, CHUNKSZ_SHA1);
1008 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +01001009 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1010 sha256_csum_wd((unsigned char *)data, data_len,
1011 (unsigned char *)value, CHUNKSZ_SHA256);
1012 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +00001013 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +00001014 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1015 *value_len = 16;
1016 } else {
1017 debug("Unsupported hash alogrithm\n");
1018 return -1;
1019 }
1020 return 0;
1021}
1022
Simon Glassab9efc62013-05-07 06:11:58 +00001023static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1024 size_t size, char **err_msgp)
1025{
1026 uint8_t value[FIT_MAX_HASH_LEN];
1027 int value_len;
1028 char *algo;
1029 uint8_t *fit_value;
1030 int fit_value_len;
1031 int ignore;
1032
1033 *err_msgp = NULL;
1034
1035 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +00001036 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +00001037 return -1;
1038 }
1039 printf("%s", algo);
1040
1041 if (IMAGE_ENABLE_IGNORE) {
1042 fit_image_hash_get_ignore(fit, noffset, &ignore);
1043 if (ignore) {
1044 printf("-skipped ");
1045 return 0;
1046 }
1047 }
1048
1049 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1050 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001051 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +00001052 return -1;
1053 }
1054
1055 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +00001056 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +00001057 return -1;
1058 }
1059
1060 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +00001061 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +00001062 return -1;
1063 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +00001064 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +00001065 return -1;
1066 }
1067
1068 return 0;
1069}
1070
Jun Nie5c643db2018-02-27 16:55:58 +08001071int fit_image_verify_with_data(const void *fit, int image_noffset,
1072 const void *data, size_t size)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001073{
Simon Glass56518e72013-06-13 15:10:01 -07001074 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001075 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001076 int verify_all = 1;
1077 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001078
Simon Glass56518e72013-06-13 15:10:01 -07001079 /* Verify all required signatures */
1080 if (IMAGE_ENABLE_VERIFY &&
1081 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1082 gd_fdt_blob(), &verify_all)) {
1083 err_msg = "Unable to verify required signature";
1084 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001085 }
1086
1087 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001088 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001089 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001090
Simon Glassab9efc62013-05-07 06:11:58 +00001091 /*
1092 * Check subnode name, must be equal to "hash".
1093 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +00001094 * names, e.g. hash-1, hash-2, etc.
Simon Glassab9efc62013-05-07 06:11:58 +00001095 */
1096 if (!strncmp(name, FIT_HASH_NODENAME,
1097 strlen(FIT_HASH_NODENAME))) {
1098 if (fit_image_check_hash(fit, noffset, data, size,
1099 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001100 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001101 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001102 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1103 !strncmp(name, FIT_SIG_NODENAME,
1104 strlen(FIT_SIG_NODENAME))) {
1105 ret = fit_image_check_sig(fit, noffset, data,
1106 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001107
1108 /*
1109 * Show an indication on failure, but do not return
1110 * an error. Only keys marked 'required' can cause
1111 * an image validation failure. See the call to
1112 * fit_image_verify_required_sigs() above.
1113 */
1114 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001115 puts("- ");
1116 else
1117 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001118 }
1119 }
1120
1121 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001122 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001123 goto error;
1124 }
1125
1126 return 1;
1127
1128error:
Simon Glasse754da22013-05-07 06:11:59 +00001129 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001130 err_msg, fit_get_name(fit, noffset, NULL),
1131 fit_get_name(fit, image_noffset, NULL));
1132 return 0;
1133}
1134
1135/**
Jun Nie5c643db2018-02-27 16:55:58 +08001136 * fit_image_verify - verify data integrity
1137 * @fit: pointer to the FIT format image header
1138 * @image_noffset: component image node offset
1139 *
1140 * fit_image_verify() goes over component image hash nodes,
1141 * re-calculates each data hash and compares with the value stored in hash
1142 * node.
1143 *
1144 * returns:
1145 * 1, if all hashes are valid
1146 * 0, otherwise (or on error)
1147 */
1148int fit_image_verify(const void *fit, int image_noffset)
1149{
1150 const void *data;
1151 size_t size;
1152 int noffset = 0;
1153 char *err_msg = "";
1154
1155 /* Get image data and data length */
1156 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
1157 err_msg = "Can't get image data/size";
1158 printf("error!\n%s for '%s' hash node in '%s' image node\n",
1159 err_msg, fit_get_name(fit, noffset, NULL),
1160 fit_get_name(fit, image_noffset, NULL));
1161 return 0;
1162 }
1163
1164 return fit_image_verify_with_data(fit, image_noffset, data, size);
1165}
1166
1167/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001168 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001169 * @fit: pointer to the FIT format image header
1170 *
Simon Glassb8da8362013-05-07 06:11:57 +00001171 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001172 * for every images checks if all it's hashes are valid.
1173 *
1174 * returns:
1175 * 1, if all hashes of all images are valid
1176 * 0, otherwise (or on error)
1177 */
Simon Glassb8da8362013-05-07 06:11:57 +00001178int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001179{
1180 int images_noffset;
1181 int noffset;
1182 int ndepth;
1183 int count;
1184
1185 /* Find images parent node offset */
1186 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1187 if (images_noffset < 0) {
1188 printf("Can't find images parent node '%s' (%s)\n",
1189 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1190 return 0;
1191 }
1192
1193 /* Process all image subnodes, check hashes for each */
1194 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1195 (ulong)fit);
1196 for (ndepth = 0, count = 0,
1197 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1198 (noffset >= 0) && (ndepth > 0);
1199 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1200 if (ndepth == 1) {
1201 /*
1202 * Direct child node of the images parent node,
1203 * i.e. component image node.
1204 */
Simon Glass73223f02016-02-22 22:55:43 -07001205 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001206 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001207 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001208
Simon Glassb8da8362013-05-07 06:11:57 +00001209 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001210 return 0;
1211 printf("\n");
1212 }
1213 }
1214 return 1;
1215}
1216
1217/**
1218 * fit_image_check_os - check whether image node is of a given os type
1219 * @fit: pointer to the FIT format image header
1220 * @noffset: component image node offset
1221 * @os: requested image os
1222 *
1223 * fit_image_check_os() reads image os property and compares its numeric
1224 * id with the requested os. Comparison result is returned to the caller.
1225 *
1226 * returns:
1227 * 1 if image is of given os type
1228 * 0 otherwise (or on error)
1229 */
1230int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1231{
1232 uint8_t image_os;
1233
1234 if (fit_image_get_os(fit, noffset, &image_os))
1235 return 0;
1236 return (os == image_os);
1237}
1238
1239/**
1240 * fit_image_check_arch - check whether image node is of a given arch
1241 * @fit: pointer to the FIT format image header
1242 * @noffset: component image node offset
1243 * @arch: requested imagearch
1244 *
1245 * fit_image_check_arch() reads image arch property and compares its numeric
1246 * id with the requested arch. Comparison result is returned to the caller.
1247 *
1248 * returns:
1249 * 1 if image is of given arch
1250 * 0 otherwise (or on error)
1251 */
1252int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1253{
1254 uint8_t image_arch;
Alison Wangec6617c2016-11-10 10:49:03 +08001255 int aarch32_support = 0;
1256
1257#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1258 aarch32_support = 1;
1259#endif
Simon Glass53fbb7e2013-05-07 06:11:53 +00001260
1261 if (fit_image_get_arch(fit, noffset, &image_arch))
1262 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001263 return (arch == image_arch) ||
Alison Wangec6617c2016-11-10 10:49:03 +08001264 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1265 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1266 aarch32_support);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001267}
1268
1269/**
1270 * fit_image_check_type - check whether image node is of a given type
1271 * @fit: pointer to the FIT format image header
1272 * @noffset: component image node offset
1273 * @type: requested image type
1274 *
1275 * fit_image_check_type() reads image type property and compares its numeric
1276 * id with the requested type. Comparison result is returned to the caller.
1277 *
1278 * returns:
1279 * 1 if image is of given type
1280 * 0 otherwise (or on error)
1281 */
1282int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1283{
1284 uint8_t image_type;
1285
1286 if (fit_image_get_type(fit, noffset, &image_type))
1287 return 0;
1288 return (type == image_type);
1289}
1290
1291/**
1292 * fit_image_check_comp - check whether image node uses given compression
1293 * @fit: pointer to the FIT format image header
1294 * @noffset: component image node offset
1295 * @comp: requested image compression type
1296 *
1297 * fit_image_check_comp() reads image compression property and compares its
1298 * numeric id with the requested compression type. Comparison result is
1299 * returned to the caller.
1300 *
1301 * returns:
1302 * 1 if image uses requested compression
1303 * 0 otherwise (or on error)
1304 */
1305int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1306{
1307 uint8_t image_comp;
1308
1309 if (fit_image_get_comp(fit, noffset, &image_comp))
1310 return 0;
1311 return (comp == image_comp);
1312}
1313
1314/**
1315 * fit_check_format - sanity check FIT image format
1316 * @fit: pointer to the FIT format image header
1317 *
1318 * fit_check_format() runs a basic sanity FIT image verification.
1319 * Routine checks for mandatory properties, nodes, etc.
1320 *
1321 * returns:
1322 * 1, on success
1323 * 0, on failure
1324 */
1325int fit_check_format(const void *fit)
1326{
1327 /* mandatory / node 'description' property */
1328 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1329 debug("Wrong FIT format: no description\n");
1330 return 0;
1331 }
1332
1333 if (IMAGE_ENABLE_TIMESTAMP) {
1334 /* mandatory / node 'timestamp' property */
1335 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1336 debug("Wrong FIT format: no timestamp\n");
1337 return 0;
1338 }
1339 }
1340
1341 /* mandatory subimages parent '/images' node */
1342 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1343 debug("Wrong FIT format: no images parent node\n");
1344 return 0;
1345 }
1346
1347 return 1;
1348}
1349
1350
1351/**
1352 * fit_conf_find_compat
1353 * @fit: pointer to the FIT format image header
1354 * @fdt: pointer to the device tree to compare against
1355 *
1356 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1357 * most compatible with the passed in device tree.
1358 *
1359 * Example:
1360 *
1361 * / o image-tree
1362 * |-o images
Andre Przywarab2267e82017-12-04 02:05:10 +00001363 * | |-o fdt-1
1364 * | |-o fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001365 * |
1366 * |-o configurations
Andre Przywarab2267e82017-12-04 02:05:10 +00001367 * |-o config-1
1368 * | |-fdt = fdt-1
Simon Glass53fbb7e2013-05-07 06:11:53 +00001369 * |
Andre Przywarab2267e82017-12-04 02:05:10 +00001370 * |-o config-2
1371 * |-fdt = fdt-2
Simon Glass53fbb7e2013-05-07 06:11:53 +00001372 *
1373 * / o U-Boot fdt
1374 * |-compatible = "foo,bar", "bim,bam"
1375 *
1376 * / o kernel fdt1
1377 * |-compatible = "foo,bar",
1378 *
1379 * / o kernel fdt2
1380 * |-compatible = "bim,bam", "baz,biz"
1381 *
1382 * Configuration 1 would be picked because the first string in U-Boot's
1383 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1384 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1385 *
1386 * returns:
1387 * offset to the configuration to use if one was found
1388 * -1 otherwise
1389 */
1390int fit_conf_find_compat(const void *fit, const void *fdt)
1391{
1392 int ndepth = 0;
1393 int noffset, confs_noffset, images_noffset;
1394 const void *fdt_compat;
1395 int fdt_compat_len;
1396 int best_match_offset = 0;
1397 int best_match_pos = 0;
1398
1399 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1400 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1401 if (confs_noffset < 0 || images_noffset < 0) {
1402 debug("Can't find configurations or images nodes.\n");
1403 return -1;
1404 }
1405
1406 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1407 if (!fdt_compat) {
1408 debug("Fdt for comparison has no \"compatible\" property.\n");
1409 return -1;
1410 }
1411
1412 /*
1413 * Loop over the configurations in the FIT image.
1414 */
1415 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1416 (noffset >= 0) && (ndepth > 0);
1417 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1418 const void *kfdt;
1419 const char *kfdt_name;
1420 int kfdt_noffset;
1421 const char *cur_fdt_compat;
1422 int len;
1423 size_t size;
1424 int i;
1425
1426 if (ndepth > 1)
1427 continue;
1428
1429 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1430 if (!kfdt_name) {
1431 debug("No fdt property found.\n");
1432 continue;
1433 }
1434 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1435 kfdt_name);
1436 if (kfdt_noffset < 0) {
1437 debug("No image node named \"%s\" found.\n",
1438 kfdt_name);
1439 continue;
1440 }
1441 /*
1442 * Get a pointer to this configuration's fdt.
1443 */
1444 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1445 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1446 continue;
1447 }
1448
1449 len = fdt_compat_len;
1450 cur_fdt_compat = fdt_compat;
1451 /*
1452 * Look for a match for each U-Boot compatibility string in
1453 * turn in this configuration's fdt.
1454 */
1455 for (i = 0; len > 0 &&
1456 (!best_match_offset || best_match_pos > i); i++) {
1457 int cur_len = strlen(cur_fdt_compat) + 1;
1458
1459 if (!fdt_node_check_compatible(kfdt, 0,
1460 cur_fdt_compat)) {
1461 best_match_offset = noffset;
1462 best_match_pos = i;
1463 break;
1464 }
1465 len -= cur_len;
1466 cur_fdt_compat += cur_len;
1467 }
1468 }
1469 if (!best_match_offset) {
1470 debug("No match found.\n");
1471 return -1;
1472 }
1473
1474 return best_match_offset;
1475}
1476
1477/**
1478 * fit_conf_get_node - get node offset for configuration of a given unit name
1479 * @fit: pointer to the FIT format image header
1480 * @conf_uname: configuration node unit name
1481 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001482 * fit_conf_get_node() finds a configuration (within the '/configurations'
1483 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001484 * offset is returned to the caller.
1485 *
1486 * When NULL is provided in second argument fit_conf_get_node() will search
1487 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001488 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001489 * node.
1490 *
1491 * returns:
1492 * configuration node offset when found (>=0)
1493 * negative number on failure (FDT_ERR_* code)
1494 */
1495int fit_conf_get_node(const void *fit, const char *conf_uname)
1496{
1497 int noffset, confs_noffset;
1498 int len;
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001499 const char *s;
1500 char *conf_uname_copy = NULL;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001501
1502 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1503 if (confs_noffset < 0) {
1504 debug("Can't find configurations parent node '%s' (%s)\n",
1505 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1506 return confs_noffset;
1507 }
1508
1509 if (conf_uname == NULL) {
1510 /* get configuration unit name from the default property */
1511 debug("No configuration specified, trying default...\n");
1512 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1513 FIT_DEFAULT_PROP, &len);
1514 if (conf_uname == NULL) {
1515 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1516 len);
1517 return len;
1518 }
1519 debug("Found default configuration: '%s'\n", conf_uname);
1520 }
1521
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001522 s = strchr(conf_uname, '#');
1523 if (s) {
1524 len = s - conf_uname;
1525 conf_uname_copy = malloc(len + 1);
1526 if (!conf_uname_copy) {
1527 debug("Can't allocate uname copy: '%s'\n",
1528 conf_uname);
1529 return -ENOMEM;
1530 }
1531 memcpy(conf_uname_copy, conf_uname, len);
1532 conf_uname_copy[len] = '\0';
1533 conf_uname = conf_uname_copy;
1534 }
1535
Simon Glass53fbb7e2013-05-07 06:11:53 +00001536 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1537 if (noffset < 0) {
1538 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1539 conf_uname, fdt_strerror(noffset));
1540 }
1541
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001542 if (conf_uname_copy)
1543 free(conf_uname_copy);
1544
Simon Glass53fbb7e2013-05-07 06:11:53 +00001545 return noffset;
1546}
1547
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001548int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001549 const char *prop_name)
1550{
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001551 return fdt_stringlist_count(fit, noffset, prop_name);
1552}
1553
1554int fit_conf_get_prop_node_index(const void *fit, int noffset,
1555 const char *prop_name, int index)
1556{
1557 const char *uname;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001558 int len;
1559
1560 /* get kernel image unit name from configuration kernel property */
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001561 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001562 if (uname == NULL)
1563 return len;
1564
1565 return fit_image_get_node(fit, uname);
1566}
1567
Pantelis Antoniouad026ad2017-09-04 23:12:14 +03001568int fit_conf_get_prop_node(const void *fit, int noffset,
1569 const char *prop_name)
1570{
1571 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1572}
1573
Clément Péron71412d72018-04-30 11:06:40 +02001574#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
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;
Clément Péron71412d72018-04-30 11:06:40 +02001593 int ndepth;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001594
1595 /* Mandatory properties */
1596 ret = fit_get_desc(fit, noffset, &desc);
1597 printf("%s Description: ", p);
1598 if (ret)
1599 printf("unavailable\n");
1600 else
1601 printf("%s\n", desc);
1602
Simon Glassb02e4042016-10-02 17:59:28 -06001603 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001604 printf("%s Kernel: ", p);
1605 if (uname == NULL)
1606 printf("unavailable\n");
1607 else
1608 printf("%s\n", uname);
1609
1610 /* Optional properties */
Simon Glassb02e4042016-10-02 17:59:28 -06001611 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001612 if (uname)
1613 printf("%s Init Ramdisk: %s\n", p, uname);
1614
Michal Simek1f8e4bf2018-03-26 16:31:26 +02001615 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
1616 if (uname)
1617 printf("%s Firmware: %s\n", p, uname);
1618
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001619 for (fdt_index = 0;
1620 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1621 fdt_index, NULL), uname;
1622 fdt_index++) {
1623
1624 if (fdt_index == 0)
1625 printf("%s FDT: ", p);
1626 else
1627 printf("%s ", p);
1628 printf("%s\n", uname);
1629 }
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001630
Simon Glassb02e4042016-10-02 17:59:28 -06001631 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
Michal Simeked0cea72016-05-17 13:58:44 +02001632 if (uname)
1633 printf("%s FPGA: %s\n", p, uname);
1634
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001635 /* Print out all of the specified loadables */
1636 for (loadables_index = 0;
Simon Glassb02e4042016-10-02 17:59:28 -06001637 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1638 loadables_index, NULL), uname;
1639 loadables_index++) {
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001640 if (loadables_index == 0) {
1641 printf("%s Loadables: ", p);
1642 } else {
1643 printf("%s ", p);
1644 }
1645 printf("%s\n", uname);
1646 }
Clément Péron71412d72018-04-30 11:06:40 +02001647
1648 /* Process all hash subnodes of the component configuration node */
1649 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
1650 (noffset >= 0) && (ndepth > 0);
1651 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1652 if (ndepth == 1) {
1653 /* Direct child node of the component configuration node */
1654 fit_image_print_verification_data(fit, noffset, p);
1655 }
1656 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001657}
Clément Péron71412d72018-04-30 11:06:40 +02001658#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +00001659
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001660static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001661{
1662 fit_image_print(fit, rd_noffset, " ");
1663
1664 if (verify) {
1665 puts(" Verifying Hash Integrity ... ");
1666 if (!fit_image_verify(fit, rd_noffset)) {
1667 puts("Bad Data Hash\n");
1668 return -EACCES;
1669 }
1670 puts("OK\n");
1671 }
1672
1673 return 0;
1674}
1675
Simon Glass782cfbb2013-05-16 13:53:21 +00001676int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1677 ulong addr)
1678{
1679 int cfg_noffset;
1680 void *fit_hdr;
1681 int noffset;
1682
1683 debug("* %s: using config '%s' from image at 0x%08lx\n",
1684 prop_name, images->fit_uname_cfg, addr);
1685
1686 /* Check whether configuration has this property defined */
1687 fit_hdr = map_sysmem(addr, 0);
1688 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1689 if (cfg_noffset < 0) {
1690 debug("* %s: no such config\n", prop_name);
Paul Burtonbd86ef12016-09-20 18:17:12 +01001691 return -EINVAL;
Simon Glass782cfbb2013-05-16 13:53:21 +00001692 }
1693
1694 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1695 if (noffset < 0) {
1696 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001697 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001698 }
1699
1700 return noffset;
1701}
1702
Simon Glass126cc862014-06-12 07:24:47 -06001703/**
1704 * fit_get_image_type_property() - get property name for IH_TYPE_...
1705 *
1706 * @return the properly name where we expect to find the image in the
1707 * config node
1708 */
1709static const char *fit_get_image_type_property(int type)
1710{
1711 /*
1712 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001713 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001714 * anyway. So let's just keep it here.
1715 */
1716 switch (type) {
1717 case IH_TYPE_FLATDT:
1718 return FIT_FDT_PROP;
1719 case IH_TYPE_KERNEL:
1720 return FIT_KERNEL_PROP;
1721 case IH_TYPE_RAMDISK:
1722 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001723 case IH_TYPE_X86_SETUP:
1724 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001725 case IH_TYPE_LOADABLE:
1726 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001727 case IH_TYPE_FPGA:
1728 return FIT_FPGA_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001729 }
1730
1731 return "unknown";
1732}
1733
1734int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001735 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001736 int arch, int image_type, int bootstage_id,
1737 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1738{
1739 int cfg_noffset, noffset;
1740 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001741 const char *fit_uname_config;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001742 const char *fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001743 const void *fit;
1744 const void *buf;
1745 size_t size;
1746 int type_ok, os_ok;
1747 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001748 uint8_t os;
Alison Wangec6617c2016-11-10 10:49:03 +08001749#ifndef USE_HOSTCC
1750 uint8_t os_arch;
1751#endif
Simon Glass126cc862014-06-12 07:24:47 -06001752 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001753 int ret;
1754
1755 fit = map_sysmem(addr, 0);
1756 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001757 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001758 fit_base_uname_config = NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001759 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001760 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1761
1762 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1763 if (!fit_check_format(fit)) {
1764 printf("Bad FIT %s image format!\n", prop_name);
1765 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1766 return -ENOEXEC;
1767 }
1768 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1769 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001770 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001771 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1772 noffset = fit_image_get_node(fit, fit_uname);
1773 } else {
1774 /*
1775 * no image node unit name, try to get config
1776 * node first. If config unit node name is NULL
1777 * fit_conf_get_node() will try to find default config node
1778 */
1779 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1780 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1781 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1782 } else {
1783 cfg_noffset = fit_conf_get_node(fit,
1784 fit_uname_config);
1785 }
1786 if (cfg_noffset < 0) {
1787 puts("Could not find configuration node\n");
1788 bootstage_error(bootstage_id +
1789 BOOTSTAGE_SUB_NO_UNIT_NAME);
1790 return -ENOENT;
1791 }
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001792 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1793 printf(" Using '%s' configuration\n", fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001794 if (image_type == IH_TYPE_KERNEL) {
1795 /* Remember (and possibly verify) this config */
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001796 images->fit_uname_cfg = fit_base_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001797 if (IMAGE_ENABLE_VERIFY && images->verify) {
1798 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001799 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001800 puts("Bad Data Hash\n");
1801 bootstage_error(bootstage_id +
1802 BOOTSTAGE_SUB_HASH);
1803 return -EACCES;
1804 }
1805 puts("OK\n");
1806 }
1807 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1808 }
1809
1810 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1811 prop_name);
1812 fit_uname = fit_get_name(fit, noffset, NULL);
1813 }
1814 if (noffset < 0) {
1815 puts("Could not find subimage node\n");
1816 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1817 return -ENOENT;
1818 }
1819
1820 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1821
1822 ret = fit_image_select(fit, noffset, images->verify);
1823 if (ret) {
1824 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1825 return ret;
1826 }
1827
1828 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001829#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001830 if (!fit_image_check_target_arch(fit, noffset)) {
1831 puts("Unsupported Architecture\n");
1832 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1833 return -ENOEXEC;
1834 }
Simon Glassce1400f2014-06-12 07:24:53 -06001835#endif
Alison Wangec6617c2016-11-10 10:49:03 +08001836
1837#ifndef USE_HOSTCC
1838 fit_image_get_arch(fit, noffset, &os_arch);
1839 images->os.arch = os_arch;
1840#endif
1841
Simon Glass782cfbb2013-05-16 13:53:21 +00001842 if (image_type == IH_TYPE_FLATDT &&
1843 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1844 puts("FDT image is compressed");
1845 return -EPROTONOSUPPORT;
1846 }
1847
1848 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1849 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001850 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1851 (image_type == IH_TYPE_KERNEL &&
1852 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001853
Andreas Bießmann950fe262016-08-14 20:31:24 +02001854 os_ok = image_type == IH_TYPE_FLATDT ||
1855 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001856 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001857 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001858 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001859
1860 /*
1861 * If either of the checks fail, we should report an error, but
1862 * if the image type is coming from the "loadables" field, we
1863 * don't care what it is
1864 */
1865 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001866 fit_image_get_os(fit, noffset, &os);
1867 printf("No %s %s %s Image\n",
1868 genimg_get_os_name(os),
1869 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001870 genimg_get_type_name(image_type));
1871 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1872 return -EIO;
1873 }
1874
1875 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1876
1877 /* get image data address and length */
1878 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1879 printf("Could not find %s subimage data!\n", prop_name);
1880 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001881 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001882 }
Andrew F. Davis44402fe2016-11-21 14:37:09 -06001883
1884#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1885 /* perform any post-processing on the image data */
1886 board_fit_image_post_process((void **)&buf, &size);
1887#endif
1888
Simon Glass782cfbb2013-05-16 13:53:21 +00001889 len = (ulong)size;
1890
1891 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001892 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001893 puts("Subimage data is not a FDT");
1894 return -ENOEXEC;
1895 }
1896
1897 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1898
1899 /*
1900 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001901 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001902 * warning: initialization discards qualifiers from pointer target type
1903 */
1904 {
1905 void *vbuf = (void *)buf;
1906
1907 data = map_to_sysmem(vbuf);
1908 }
1909
1910 if (load_op == FIT_LOAD_IGNORED) {
1911 /* Don't load */
1912 } else if (fit_image_get_load(fit, noffset, &load)) {
1913 if (load_op == FIT_LOAD_REQUIRED) {
1914 printf("Can't get %s subimage load address!\n",
1915 prop_name);
1916 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1917 return -EBADF;
1918 }
Simon Glassfe20a812014-08-22 14:26:43 -06001919 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001920 ulong image_start, image_end;
1921 ulong load_end;
1922 void *dst;
1923
1924 /*
1925 * move image data to the load address,
1926 * make sure we don't overwrite initial image
1927 */
1928 image_start = addr;
1929 image_end = addr + fit_get_size(fit);
1930
1931 load_end = load + len;
1932 if (image_type != IH_TYPE_KERNEL &&
1933 load < image_end && load_end > image_start) {
1934 printf("Error: %s overwritten\n", prop_name);
1935 return -EXDEV;
1936 }
1937
1938 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1939 prop_name, data, load);
1940
1941 dst = map_sysmem(load, len);
1942 memmove(dst, buf, len);
1943 data = load;
1944 }
1945 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1946
1947 *datap = data;
1948 *lenp = len;
1949 if (fit_unamep)
1950 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001951 if (fit_uname_configp)
Pantelis Antoniou7c3dc772017-09-04 23:12:15 +03001952 *fit_uname_configp = (char *)(fit_uname_config ? :
1953 fit_base_uname_config);
Simon Glass782cfbb2013-05-16 13:53:21 +00001954
1955 return noffset;
1956}
Simon Glass90268b82014-10-19 21:11:24 -06001957
1958int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1959 ulong *setup_start, ulong *setup_len)
1960{
1961 int noffset;
1962 ulong addr;
1963 ulong len;
1964 int ret;
1965
1966 addr = map_to_sysmem(images->fit_hdr_os);
1967 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1968 if (noffset < 0)
1969 return noffset;
1970
1971 ret = fit_image_load(images, addr, NULL, NULL, arch,
1972 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1973 FIT_LOAD_REQUIRED, setup_start, &len);
1974
1975 return ret;
1976}
Pantelis Antoniou169043d2017-09-04 23:12:16 +03001977
1978#ifndef USE_HOSTCC
1979int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1980 const char **fit_unamep, const char **fit_uname_configp,
1981 int arch, ulong *datap, ulong *lenp)
1982{
1983 int fdt_noffset, cfg_noffset, count;
1984 const void *fit;
1985 const char *fit_uname = NULL;
1986 const char *fit_uname_config = NULL;
1987 char *fit_uname_config_copy = NULL;
1988 char *next_config = NULL;
1989 ulong load, len;
1990#ifdef CONFIG_OF_LIBFDT_OVERLAY
1991 ulong image_start, image_end;
1992 ulong ovload, ovlen;
1993 const char *uconfig;
1994 const char *uname;
1995 void *base, *ov;
1996 int i, err, noffset, ov_noffset;
1997#endif
1998
1999 fit_uname = fit_unamep ? *fit_unamep : NULL;
2000
2001 if (fit_uname_configp && *fit_uname_configp) {
2002 fit_uname_config_copy = strdup(*fit_uname_configp);
2003 if (!fit_uname_config_copy)
2004 return -ENOMEM;
2005
2006 next_config = strchr(fit_uname_config_copy, '#');
2007 if (next_config)
2008 *next_config++ = '\0';
2009 if (next_config - 1 > fit_uname_config_copy)
2010 fit_uname_config = fit_uname_config_copy;
2011 }
2012
2013 fdt_noffset = fit_image_load(images,
2014 addr, &fit_uname, &fit_uname_config,
2015 arch, IH_TYPE_FLATDT,
2016 BOOTSTAGE_ID_FIT_FDT_START,
2017 FIT_LOAD_OPTIONAL, &load, &len);
2018
2019 if (fdt_noffset < 0)
2020 goto out;
2021
2022 debug("fit_uname=%s, fit_uname_config=%s\n",
2023 fit_uname ? fit_uname : "<NULL>",
2024 fit_uname_config ? fit_uname_config : "<NULL>");
2025
2026 fit = map_sysmem(addr, 0);
2027
2028 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2029
2030 /* single blob, or error just return as well */
2031 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2032 if (count <= 1 && !next_config)
2033 goto out;
2034
2035 /* we need to apply overlays */
2036
2037#ifdef CONFIG_OF_LIBFDT_OVERLAY
2038 image_start = addr;
2039 image_end = addr + fit_get_size(fit);
2040 /* verify that relocation took place by load address not being in fit */
2041 if (load >= image_start && load < image_end) {
2042 /* check is simplified; fit load checks for overlaps */
2043 printf("Overlayed FDT requires relocation\n");
2044 fdt_noffset = -EBADF;
2045 goto out;
2046 }
2047
2048 base = map_sysmem(load, len);
2049
2050 /* apply extra configs in FIT first, followed by args */
2051 for (i = 1; ; i++) {
2052 if (i < count) {
2053 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2054 FIT_FDT_PROP, i);
2055 uname = fit_get_name(fit, noffset, NULL);
2056 uconfig = NULL;
2057 } else {
2058 if (!next_config)
2059 break;
2060 uconfig = next_config;
2061 next_config = strchr(next_config, '#');
2062 if (next_config)
2063 *next_config++ = '\0';
2064 uname = NULL;
2065 }
2066
2067 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2068
2069 ov_noffset = fit_image_load(images,
2070 addr, &uname, &uconfig,
2071 arch, IH_TYPE_FLATDT,
2072 BOOTSTAGE_ID_FIT_FDT_START,
2073 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2074 if (ov_noffset < 0) {
2075 printf("load of %s failed\n", uname);
2076 continue;
2077 }
2078 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2079 uname, ovload, ovlen);
2080 ov = map_sysmem(ovload, ovlen);
2081
2082 base = map_sysmem(load, len + ovlen);
2083 err = fdt_open_into(base, base, len + ovlen);
2084 if (err < 0) {
2085 printf("failed on fdt_open_into\n");
2086 fdt_noffset = err;
2087 goto out;
2088 }
2089 /* the verbose method prints out messages on error */
2090 err = fdt_overlay_apply_verbose(base, ov);
2091 if (err < 0) {
2092 fdt_noffset = err;
2093 goto out;
2094 }
2095 fdt_pack(base);
2096 len = fdt_totalsize(base);
2097 }
2098#else
2099 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2100 fdt_noffset = -EBADF;
2101#endif
2102
2103out:
2104 if (datap)
2105 *datap = load;
2106 if (lenp)
2107 *lenp = len;
2108 if (fit_unamep)
2109 *fit_unamep = fit_uname;
2110 if (fit_uname_configp)
2111 *fit_uname_configp = fit_uname_config;
2112
2113 if (fit_uname_config_copy)
2114 free(fit_uname_config_copy);
2115 return fdt_noffset;
2116}
2117#endif