blob: 7f17fd1410ed8a3b37e48f33f7ddf14dd4b54f65 [file] [log] [blame]
Simon Glassda0af362013-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 Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Simon Glassda0af362013-05-07 06:11:53 +000010 */
11
12#ifdef USE_HOSTCC
13#include "mkimage.h"
Simon Glassda0af362013-05-07 06:11:53 +000014#include <time.h>
15#else
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050016#include <linux/compiler.h>
York Sun48cd6ab2016-11-23 09:25:09 -080017#include <linux/kconfig.h>
Simon Glassda0af362013-05-07 06:11:53 +000018#include <common.h>
Simon Glass384d86d2013-05-16 13:53:21 +000019#include <errno.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050020#include <mapmem.h>
Simon Glass384d86d2013-05-16 13:53:21 +000021#include <asm/io.h>
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +030022#include <malloc.h>
Simon Glass384d86d2013-05-16 13:53:21 +000023DECLARE_GLOBAL_DATA_PTR;
Simon Glassda0af362013-05-07 06:11:53 +000024#endif /* !USE_HOSTCC*/
25
Andreas Dannenberg67aaa6d2016-07-27 12:12:39 -050026#include <image.h>
Simon Glassda0af362013-05-07 06:11:53 +000027#include <bootstage.h>
Simon Glassda0af362013-05-07 06:11:53 +000028#include <u-boot/crc.h>
29#include <u-boot/md5.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020030#include <u-boot/sha1.h>
31#include <u-boot/sha256.h>
Simon Glassda0af362013-05-07 06:11:53 +000032
33/*****************************************************************************/
34/* New uImage format routines */
35/*****************************************************************************/
36#ifndef USE_HOSTCC
37static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
38 ulong *addr, const char **name)
39{
40 const char *sep;
41
42 *addr = addr_curr;
43 *name = NULL;
44
45 sep = strchr(spec, sepc);
46 if (sep) {
47 if (sep - spec > 0)
48 *addr = simple_strtoul(spec, NULL, 16);
49
50 *name = sep + 1;
51 return 1;
52 }
53
54 return 0;
55}
56
57/**
58 * fit_parse_conf - parse FIT configuration spec
59 * @spec: input string, containing configuration spec
60 * @add_curr: current image address (to be used as a possible default)
61 * @addr: pointer to a ulong variable, will hold FIT image address of a given
62 * configuration
63 * @conf_name double pointer to a char, will hold pointer to a configuration
64 * unit name
65 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090066 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glassda0af362013-05-07 06:11:53 +000067 * where <addr> is a FIT image address that contains configuration
68 * with a <conf> unit name.
69 *
70 * Address part is optional, and if omitted default add_curr will
71 * be used instead.
72 *
73 * returns:
74 * 1 if spec is a valid configuration string,
75 * addr and conf_name are set accordingly
76 * 0 otherwise
77 */
78int fit_parse_conf(const char *spec, ulong addr_curr,
79 ulong *addr, const char **conf_name)
80{
81 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
82}
83
84/**
85 * fit_parse_subimage - parse FIT subimage spec
86 * @spec: input string, containing subimage spec
87 * @add_curr: current image address (to be used as a possible default)
88 * @addr: pointer to a ulong variable, will hold FIT image address of a given
89 * subimage
90 * @image_name: double pointer to a char, will hold pointer to a subimage name
91 *
Masahiro Yamada4ac739a2013-09-18 09:36:38 +090092 * fit_parse_subimage() expects subimage spec in the form of
Simon Glassda0af362013-05-07 06:11:53 +000093 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
94 * subimage with a <subimg> unit name.
95 *
96 * Address part is optional, and if omitted default add_curr will
97 * be used instead.
98 *
99 * returns:
100 * 1 if spec is a valid subimage string,
101 * addr and image_name are set accordingly
102 * 0 otherwise
103 */
104int fit_parse_subimage(const char *spec, ulong addr_curr,
105 ulong *addr, const char **image_name)
106{
107 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
108}
109#endif /* !USE_HOSTCC */
110
111static void fit_get_debug(const void *fit, int noffset,
112 char *prop_name, int err)
113{
114 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
115 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
116 fdt_strerror(err));
117}
118
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200119/**
120 * fit_get_subimage_count - get component (sub-image) count
121 * @fit: pointer to the FIT format image header
122 * @images_noffset: offset of images node
123 *
124 * returns:
125 * number of image components
126 */
127int fit_get_subimage_count(const void *fit, int images_noffset)
128{
129 int noffset;
130 int ndepth;
131 int count = 0;
132
133 /* Process its subnodes, print out component images details */
134 for (ndepth = 0, count = 0,
135 noffset = fdt_next_node(fit, images_noffset, &ndepth);
136 (noffset >= 0) && (ndepth > 0);
137 noffset = fdt_next_node(fit, noffset, &ndepth)) {
138 if (ndepth == 1) {
139 count++;
140 }
141 }
142
143 return count;
144}
145
Simon Glass82d94532013-05-08 08:05:59 +0000146#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glassda0af362013-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 Dannenberg8ca5a902016-06-15 17:00:19 -0500153 * The routine prints out FIT image properties (root node level) followed by
Simon Glassda0af362013-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 Glass1030f162013-05-08 08:05:58 +0000172 /* Indent string is defined in header image.h */
173 p = IMAGE_INDENT_STRING;
Simon Glassda0af362013-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 Glasse61aa442013-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 Glassfbabc0f2013-06-13 15:10:01 -0700253 * @type: Type of information to print ("hash" or "sign")
Simon Glasse61aa442013-05-07 06:12:02 +0000254 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500255 * fit_image_print_data() lists properties for the processed hash node
Simon Glasse61aa442013-05-07 06:12:02 +0000256 *
Simon Glassfbabc0f2013-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 Glasse61aa442013-05-07 06:12:02 +0000260 * returns:
261 * no returned results
262 */
Simon Glassfbabc0f2013-06-13 15:10:01 -0700263static void fit_image_print_data(const void *fit, int noffset, const char *p,
264 const char *type)
Simon Glasse61aa442013-05-07 06:12:02 +0000265{
Simon Glassfbabc0f2013-06-13 15:10:01 -0700266 const char *keyname;
Simon Glasse61aa442013-05-07 06:12:02 +0000267 uint8_t *value;
268 int value_len;
Simon Glassfbabc0f2013-06-13 15:10:01 -0700269 char *algo;
270 int required;
271 int ret, i;
Simon Glasse61aa442013-05-07 06:12:02 +0000272
Simon Glassfbabc0f2013-06-13 15:10:01 -0700273 debug("%s %s node: '%s'\n", p, type,
Simon Glasse61aa442013-05-07 06:12:02 +0000274 fit_get_name(fit, noffset, NULL));
Simon Glassfbabc0f2013-06-13 15:10:01 -0700275 printf("%s %s algo: ", p, type);
Simon Glasse61aa442013-05-07 06:12:02 +0000276 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
277 printf("invalid/unsupported\n");
278 return;
279 }
Simon Glassfbabc0f2013-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 Glasse61aa442013-05-07 06:12:02 +0000288
289 ret = fit_image_hash_get_value(fit, noffset, &value,
290 &value_len);
Simon Glassfbabc0f2013-06-13 15:10:01 -0700291 printf("%s %s value: ", p, type);
Simon Glasse61aa442013-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 Glassfbabc0f2013-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 Glasse61aa442013-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 Dannenberg8ca5a902016-06-15 17:00:19 -0500320 * This lists properties for the processed hash node
Simon Glasse61aa442013-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
333 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
334 */
335 name = fit_get_name(fit, noffset, NULL);
Simon Glassfbabc0f2013-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 Glasse61aa442013-05-07 06:12:02 +0000342}
343
344/**
Simon Glassda0af362013-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 Dannenberg8ca5a902016-06-15 17:00:19 -0500350 * fit_image_print() lists all mandatory properties for the processed component
Simon Glassda0af362013-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 Glass749a6e72013-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 Glassda0af362013-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 Glasse8d6a5b2013-05-16 13:53:26 +0000399 if (ret) {
Simon Glassda0af362013-05-07 06:11:53 +0000400 printf("unavailable\n");
Simon Glasse8d6a5b2013-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 Glassda0af362013-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
422 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
423 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 Simekebae78b2016-05-17 14:03:50 +0200428 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
429 (type == IH_TYPE_FPGA)) {
Simon Glassda0af362013-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 Antoniou6e51fb22017-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 Glassda0af362013-05-07 06:11:53 +0000442 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
443 (type == IH_TYPE_RAMDISK)) {
York Sun2b0464e2016-02-29 15:48:40 -0800444 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glassda0af362013-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 Glasse61aa442013-05-07 06:12:02 +0000458 fit_image_print_verification_data(fit, noffset, p);
Simon Glassda0af362013-05-07 06:11:53 +0000459 }
460 }
Simon Glassda0af362013-05-07 06:11:53 +0000461}
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200462
463#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glassda0af362013-05-07 06:11:53 +0000464
465/**
466 * fit_get_desc - get node description property
467 * @fit: pointer to the FIT format image header
468 * @noffset: node offset
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500469 * @desc: double pointer to the char, will hold pointer to the description
Simon Glassda0af362013-05-07 06:11:53 +0000470 *
471 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -0500472 * description is found pointer to it is returned in third call argument.
Simon Glassda0af362013-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 Dannenberg8ca5a902016-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 Glassda0af362013-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 Dannenberg8ca5a902016-06-15 17:00:19 -0500530 * fit_image_get_node() finds a component image (within the '/images'
Simon Glassda0af362013-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 Sun2b0464e2016-02-29 15:48:40 -0800686static int fit_image_get_address(const void *fit, int noffset, char *name,
687 ulong *load)
688{
York Sun31e5add2016-02-29 15:48:41 -0800689 int len, cell_len;
690 const fdt32_t *cell;
691 uint64_t load64 = 0;
York Sun2b0464e2016-02-29 15:48:40 -0800692
York Sun31e5add2016-02-29 15:48:41 -0800693 cell = fdt_getprop(fit, noffset, name, &len);
694 if (cell == NULL) {
York Sun2b0464e2016-02-29 15:48:40 -0800695 fit_get_debug(fit, noffset, name, len);
696 return -1;
697 }
698
York Sun31e5add2016-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 Sun2b0464e2016-02-29 15:48:40 -0800711
712 return 0;
713}
Simon Glassda0af362013-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 Sun2b0464e2016-02-29 15:48:40 -0800729 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glassda0af362013-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 Sun2b0464e2016-02-29 15:48:40 -0800751 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glassda0af362013-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.com45ee7902017-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/**
810 * Get 'data-size' property from a given image node.
811 *
812 * @fit: pointer to the FIT image header
813 * @noffset: component image node offset
814 * @data_size: holds the data-size property
815 *
816 * returns:
817 * 0, on success
818 * -ENOENT if the property could not be found
819 */
820int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
821{
822 const fdt32_t *val;
823
824 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
825 if (!val)
826 return -ENOENT;
827
828 *data_size = fdt32_to_cpu(*val);
829
830 return 0;
831}
832
833/**
Simon Glassda0af362013-05-07 06:11:53 +0000834 * fit_image_hash_get_algo - get hash algorithm name
835 * @fit: pointer to the FIT format image header
836 * @noffset: hash node offset
837 * @algo: double pointer to char, will hold pointer to the algorithm name
838 *
839 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
840 * If the property is found its data start address is returned to the caller.
841 *
842 * returns:
843 * 0, on success
844 * -1, on failure
845 */
846int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
847{
848 int len;
849
850 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
851 if (*algo == NULL) {
852 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
853 return -1;
854 }
855
856 return 0;
857}
858
859/**
860 * fit_image_hash_get_value - get hash value and length
861 * @fit: pointer to the FIT format image header
862 * @noffset: hash node offset
863 * @value: double pointer to uint8_t, will hold address of a hash value data
864 * @value_len: pointer to an int, will hold hash data length
865 *
866 * fit_image_hash_get_value() finds hash value property in a given hash node.
867 * If the property is found its data start address and size are returned to
868 * the caller.
869 *
870 * returns:
871 * 0, on success
872 * -1, on failure
873 */
874int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
875 int *value_len)
876{
877 int len;
878
879 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
880 if (*value == NULL) {
881 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
882 *value_len = 0;
883 return -1;
884 }
885
886 *value_len = len;
887 return 0;
888}
889
Simon Glassda0af362013-05-07 06:11:53 +0000890/**
891 * fit_image_hash_get_ignore - get hash ignore flag
892 * @fit: pointer to the FIT format image header
893 * @noffset: hash node offset
894 * @ignore: pointer to an int, will hold hash ignore flag
895 *
896 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
897 * If the property is found and non-zero, the hash algorithm is not verified by
898 * u-boot automatically.
899 *
900 * returns:
901 * 0, on ignore not found
902 * value, on ignore found
903 */
Simon Glassf0fd5112013-05-07 06:11:58 +0000904static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glassda0af362013-05-07 06:11:53 +0000905{
906 int len;
907 int *value;
908
909 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
910 if (value == NULL || len != sizeof(int))
911 *ignore = 0;
912 else
913 *ignore = *value;
914
915 return 0;
916}
Simon Glassda0af362013-05-07 06:11:53 +0000917
Simon Glass5b539a02016-02-24 09:14:42 -0700918ulong fit_get_end(const void *fit)
919{
920 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
921}
922
Simon Glassda0af362013-05-07 06:11:53 +0000923/**
924 * fit_set_timestamp - set node timestamp property
925 * @fit: pointer to the FIT format image header
926 * @noffset: node offset
927 * @timestamp: timestamp value to be set
928 *
929 * fit_set_timestamp() attempts to set timestamp property in the requested
930 * node and returns operation status to the caller.
931 *
932 * returns:
933 * 0, on success
Simon Glassaf2f9d52014-06-02 22:04:51 -0600934 * -ENOSPC if no space in device tree, -1 for other error
Simon Glassda0af362013-05-07 06:11:53 +0000935 */
936int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
937{
938 uint32_t t;
939 int ret;
940
941 t = cpu_to_uimage(timestamp);
942 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
943 sizeof(uint32_t));
944 if (ret) {
Simon Glasse4016fa2016-05-01 13:55:37 -0600945 debug("Can't set '%s' property for '%s' node (%s)\n",
946 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
947 fdt_strerror(ret));
Simon Glassaf2f9d52014-06-02 22:04:51 -0600948 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glassda0af362013-05-07 06:11:53 +0000949 }
950
951 return 0;
952}
953
954/**
955 * calculate_hash - calculate and return hash for provided input data
956 * @data: pointer to the input data
957 * @data_len: data length
958 * @algo: requested hash algorithm
959 * @value: pointer to the char, will hold hash value data (caller must
960 * allocate enough free space)
961 * value_len: length of the calculated hash
962 *
963 * calculate_hash() computes input data hash according to the requested
964 * algorithm.
965 * Resulting hash value is placed in caller provided 'value' buffer, length
966 * of the calculated hash is returned via value_len pointer argument.
967 *
968 * returns:
969 * 0, on success
970 * -1, when algo is unsupported
971 */
Simon Glass10a1eca2013-05-07 06:11:54 +0000972int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glassda0af362013-05-07 06:11:53 +0000973 uint8_t *value, int *value_len)
974{
Simon Glass82d94532013-05-08 08:05:59 +0000975 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +0000976 *((uint32_t *)value) = crc32_wd(0, data, data_len,
977 CHUNKSZ_CRC32);
978 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
979 *value_len = 4;
Simon Glass82d94532013-05-08 08:05:59 +0000980 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +0000981 sha1_csum_wd((unsigned char *)data, data_len,
982 (unsigned char *)value, CHUNKSZ_SHA1);
983 *value_len = 20;
Heiko Schocher8ae33802014-03-03 12:19:25 +0100984 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
985 sha256_csum_wd((unsigned char *)data, data_len,
986 (unsigned char *)value, CHUNKSZ_SHA256);
987 *value_len = SHA256_SUM_LEN;
Simon Glass82d94532013-05-08 08:05:59 +0000988 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glassda0af362013-05-07 06:11:53 +0000989 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
990 *value_len = 16;
991 } else {
992 debug("Unsupported hash alogrithm\n");
993 return -1;
994 }
995 return 0;
996}
997
Simon Glassf0fd5112013-05-07 06:11:58 +0000998static int fit_image_check_hash(const void *fit, int noffset, const void *data,
999 size_t size, char **err_msgp)
1000{
1001 uint8_t value[FIT_MAX_HASH_LEN];
1002 int value_len;
1003 char *algo;
1004 uint8_t *fit_value;
1005 int fit_value_len;
1006 int ignore;
1007
1008 *err_msgp = NULL;
1009
1010 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001011 *err_msgp = "Can't get hash algo property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001012 return -1;
1013 }
1014 printf("%s", algo);
1015
1016 if (IMAGE_ENABLE_IGNORE) {
1017 fit_image_hash_get_ignore(fit, noffset, &ignore);
1018 if (ignore) {
1019 printf("-skipped ");
1020 return 0;
1021 }
1022 }
1023
1024 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1025 &fit_value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001026 *err_msgp = "Can't get hash value property";
Simon Glassf0fd5112013-05-07 06:11:58 +00001027 return -1;
1028 }
1029
1030 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001031 *err_msgp = "Unsupported hash algorithm";
Simon Glassf0fd5112013-05-07 06:11:58 +00001032 return -1;
1033 }
1034
1035 if (value_len != fit_value_len) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001036 *err_msgp = "Bad hash value len";
Simon Glassf0fd5112013-05-07 06:11:58 +00001037 return -1;
1038 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001039 *err_msgp = "Bad hash value";
Simon Glassf0fd5112013-05-07 06:11:58 +00001040 return -1;
1041 }
1042
1043 return 0;
1044}
1045
Simon Glassda0af362013-05-07 06:11:53 +00001046/**
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001047 * fit_image_verify - verify data integrity
Simon Glassda0af362013-05-07 06:11:53 +00001048 * @fit: pointer to the FIT format image header
1049 * @image_noffset: component image node offset
1050 *
Simon Glass7428ad12013-05-07 06:11:57 +00001051 * fit_image_verify() goes over component image hash nodes,
Simon Glassda0af362013-05-07 06:11:53 +00001052 * re-calculates each data hash and compares with the value stored in hash
1053 * node.
1054 *
1055 * returns:
1056 * 1, if all hashes are valid
1057 * 0, otherwise (or on error)
1058 */
Simon Glass7428ad12013-05-07 06:11:57 +00001059int fit_image_verify(const void *fit, int image_noffset)
Simon Glassda0af362013-05-07 06:11:53 +00001060{
1061 const void *data;
1062 size_t size;
Simon Glassfbabc0f2013-06-13 15:10:01 -07001063 int noffset = 0;
Simon Glassda0af362013-05-07 06:11:53 +00001064 char *err_msg = "";
Simon Glassfbabc0f2013-06-13 15:10:01 -07001065 int verify_all = 1;
1066 int ret;
Simon Glassda0af362013-05-07 06:11:53 +00001067
1068 /* Get image data and data length */
1069 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001070 err_msg = "Can't get image data/size";
Simon Glassfbabc0f2013-06-13 15:10:01 -07001071 goto error;
Simon Glassda0af362013-05-07 06:11:53 +00001072 }
1073
Simon Glassfbabc0f2013-06-13 15:10:01 -07001074 /* Verify all required signatures */
1075 if (IMAGE_ENABLE_VERIFY &&
1076 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1077 gd_fdt_blob(), &verify_all)) {
1078 err_msg = "Unable to verify required signature";
1079 goto error;
1080 }
1081
Simon Glassda0af362013-05-07 06:11:53 +00001082 /* Process all hash subnodes of the component image node */
Simon Glass499c29e2016-10-02 17:59:29 -06001083 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glassf0fd5112013-05-07 06:11:58 +00001084 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001085
Simon Glassf0fd5112013-05-07 06:11:58 +00001086 /*
1087 * Check subnode name, must be equal to "hash".
1088 * Multiple hash nodes require unique unit node
1089 * names, e.g. hash@1, hash@2, etc.
1090 */
1091 if (!strncmp(name, FIT_HASH_NODENAME,
1092 strlen(FIT_HASH_NODENAME))) {
1093 if (fit_image_check_hash(fit, noffset, data, size,
1094 &err_msg))
Simon Glassda0af362013-05-07 06:11:53 +00001095 goto error;
Simon Glassf0fd5112013-05-07 06:11:58 +00001096 puts("+ ");
Simon Glassfbabc0f2013-06-13 15:10:01 -07001097 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1098 !strncmp(name, FIT_SIG_NODENAME,
1099 strlen(FIT_SIG_NODENAME))) {
1100 ret = fit_image_check_sig(fit, noffset, data,
1101 size, -1, &err_msg);
Simon Glass51026aa2016-02-24 09:14:43 -07001102
1103 /*
1104 * Show an indication on failure, but do not return
1105 * an error. Only keys marked 'required' can cause
1106 * an image validation failure. See the call to
1107 * fit_image_verify_required_sigs() above.
1108 */
1109 if (ret)
Simon Glassfbabc0f2013-06-13 15:10:01 -07001110 puts("- ");
1111 else
1112 puts("+ ");
Simon Glassda0af362013-05-07 06:11:53 +00001113 }
1114 }
1115
1116 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glass8f3aa462013-05-07 06:11:59 +00001117 err_msg = "Corrupted or truncated tree";
Simon Glassda0af362013-05-07 06:11:53 +00001118 goto error;
1119 }
1120
1121 return 1;
1122
1123error:
Simon Glass8f3aa462013-05-07 06:11:59 +00001124 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glassda0af362013-05-07 06:11:53 +00001125 err_msg, fit_get_name(fit, noffset, NULL),
1126 fit_get_name(fit, image_noffset, NULL));
1127 return 0;
1128}
1129
1130/**
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001131 * fit_all_image_verify - verify data integrity for all images
Simon Glassda0af362013-05-07 06:11:53 +00001132 * @fit: pointer to the FIT format image header
1133 *
Simon Glass7428ad12013-05-07 06:11:57 +00001134 * fit_all_image_verify() goes over all images in the FIT and
Simon Glassda0af362013-05-07 06:11:53 +00001135 * for every images checks if all it's hashes are valid.
1136 *
1137 * returns:
1138 * 1, if all hashes of all images are valid
1139 * 0, otherwise (or on error)
1140 */
Simon Glass7428ad12013-05-07 06:11:57 +00001141int fit_all_image_verify(const void *fit)
Simon Glassda0af362013-05-07 06:11:53 +00001142{
1143 int images_noffset;
1144 int noffset;
1145 int ndepth;
1146 int count;
1147
1148 /* Find images parent node offset */
1149 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1150 if (images_noffset < 0) {
1151 printf("Can't find images parent node '%s' (%s)\n",
1152 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1153 return 0;
1154 }
1155
1156 /* Process all image subnodes, check hashes for each */
1157 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1158 (ulong)fit);
1159 for (ndepth = 0, count = 0,
1160 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1161 (noffset >= 0) && (ndepth > 0);
1162 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1163 if (ndepth == 1) {
1164 /*
1165 * Direct child node of the images parent node,
1166 * i.e. component image node.
1167 */
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001168 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glassda0af362013-05-07 06:11:53 +00001169 fit_get_name(fit, noffset, NULL));
Simon Glasse3ee2fb2016-02-22 22:55:43 -07001170 count++;
Simon Glassda0af362013-05-07 06:11:53 +00001171
Simon Glass7428ad12013-05-07 06:11:57 +00001172 if (!fit_image_verify(fit, noffset))
Simon Glassda0af362013-05-07 06:11:53 +00001173 return 0;
1174 printf("\n");
1175 }
1176 }
1177 return 1;
1178}
1179
1180/**
1181 * fit_image_check_os - check whether image node is of a given os type
1182 * @fit: pointer to the FIT format image header
1183 * @noffset: component image node offset
1184 * @os: requested image os
1185 *
1186 * fit_image_check_os() reads image os property and compares its numeric
1187 * id with the requested os. Comparison result is returned to the caller.
1188 *
1189 * returns:
1190 * 1 if image is of given os type
1191 * 0 otherwise (or on error)
1192 */
1193int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1194{
1195 uint8_t image_os;
1196
1197 if (fit_image_get_os(fit, noffset, &image_os))
1198 return 0;
1199 return (os == image_os);
1200}
1201
1202/**
1203 * fit_image_check_arch - check whether image node is of a given arch
1204 * @fit: pointer to the FIT format image header
1205 * @noffset: component image node offset
1206 * @arch: requested imagearch
1207 *
1208 * fit_image_check_arch() reads image arch property and compares its numeric
1209 * id with the requested arch. Comparison result is returned to the caller.
1210 *
1211 * returns:
1212 * 1 if image is of given arch
1213 * 0 otherwise (or on error)
1214 */
1215int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1216{
1217 uint8_t image_arch;
Alison Wang73818d52016-11-10 10:49:03 +08001218 int aarch32_support = 0;
1219
1220#ifdef CONFIG_ARM64_SUPPORT_AARCH32
1221 aarch32_support = 1;
1222#endif
Simon Glassda0af362013-05-07 06:11:53 +00001223
1224 if (fit_image_get_arch(fit, noffset, &image_arch))
1225 return 0;
Simon Glass9d428302014-10-10 08:21:57 -06001226 return (arch == image_arch) ||
Alison Wang73818d52016-11-10 10:49:03 +08001227 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1228 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1229 aarch32_support);
Simon Glassda0af362013-05-07 06:11:53 +00001230}
1231
1232/**
1233 * fit_image_check_type - check whether image node is of a given type
1234 * @fit: pointer to the FIT format image header
1235 * @noffset: component image node offset
1236 * @type: requested image type
1237 *
1238 * fit_image_check_type() reads image type property and compares its numeric
1239 * id with the requested type. Comparison result is returned to the caller.
1240 *
1241 * returns:
1242 * 1 if image is of given type
1243 * 0 otherwise (or on error)
1244 */
1245int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1246{
1247 uint8_t image_type;
1248
1249 if (fit_image_get_type(fit, noffset, &image_type))
1250 return 0;
1251 return (type == image_type);
1252}
1253
1254/**
1255 * fit_image_check_comp - check whether image node uses given compression
1256 * @fit: pointer to the FIT format image header
1257 * @noffset: component image node offset
1258 * @comp: requested image compression type
1259 *
1260 * fit_image_check_comp() reads image compression property and compares its
1261 * numeric id with the requested compression type. Comparison result is
1262 * returned to the caller.
1263 *
1264 * returns:
1265 * 1 if image uses requested compression
1266 * 0 otherwise (or on error)
1267 */
1268int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1269{
1270 uint8_t image_comp;
1271
1272 if (fit_image_get_comp(fit, noffset, &image_comp))
1273 return 0;
1274 return (comp == image_comp);
1275}
1276
1277/**
1278 * fit_check_format - sanity check FIT image format
1279 * @fit: pointer to the FIT format image header
1280 *
1281 * fit_check_format() runs a basic sanity FIT image verification.
1282 * Routine checks for mandatory properties, nodes, etc.
1283 *
1284 * returns:
1285 * 1, on success
1286 * 0, on failure
1287 */
1288int fit_check_format(const void *fit)
1289{
1290 /* mandatory / node 'description' property */
1291 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1292 debug("Wrong FIT format: no description\n");
1293 return 0;
1294 }
1295
1296 if (IMAGE_ENABLE_TIMESTAMP) {
1297 /* mandatory / node 'timestamp' property */
1298 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1299 debug("Wrong FIT format: no timestamp\n");
1300 return 0;
1301 }
1302 }
1303
1304 /* mandatory subimages parent '/images' node */
1305 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1306 debug("Wrong FIT format: no images parent node\n");
1307 return 0;
1308 }
1309
1310 return 1;
1311}
1312
1313
1314/**
1315 * fit_conf_find_compat
1316 * @fit: pointer to the FIT format image header
1317 * @fdt: pointer to the device tree to compare against
1318 *
1319 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1320 * most compatible with the passed in device tree.
1321 *
1322 * Example:
1323 *
1324 * / o image-tree
1325 * |-o images
1326 * | |-o fdt@1
1327 * | |-o fdt@2
1328 * |
1329 * |-o configurations
1330 * |-o config@1
1331 * | |-fdt = fdt@1
1332 * |
1333 * |-o config@2
1334 * |-fdt = fdt@2
1335 *
1336 * / o U-Boot fdt
1337 * |-compatible = "foo,bar", "bim,bam"
1338 *
1339 * / o kernel fdt1
1340 * |-compatible = "foo,bar",
1341 *
1342 * / o kernel fdt2
1343 * |-compatible = "bim,bam", "baz,biz"
1344 *
1345 * Configuration 1 would be picked because the first string in U-Boot's
1346 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1347 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1348 *
1349 * returns:
1350 * offset to the configuration to use if one was found
1351 * -1 otherwise
1352 */
1353int fit_conf_find_compat(const void *fit, const void *fdt)
1354{
1355 int ndepth = 0;
1356 int noffset, confs_noffset, images_noffset;
1357 const void *fdt_compat;
1358 int fdt_compat_len;
1359 int best_match_offset = 0;
1360 int best_match_pos = 0;
1361
1362 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1363 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1364 if (confs_noffset < 0 || images_noffset < 0) {
1365 debug("Can't find configurations or images nodes.\n");
1366 return -1;
1367 }
1368
1369 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1370 if (!fdt_compat) {
1371 debug("Fdt for comparison has no \"compatible\" property.\n");
1372 return -1;
1373 }
1374
1375 /*
1376 * Loop over the configurations in the FIT image.
1377 */
1378 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1379 (noffset >= 0) && (ndepth > 0);
1380 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1381 const void *kfdt;
1382 const char *kfdt_name;
1383 int kfdt_noffset;
1384 const char *cur_fdt_compat;
1385 int len;
1386 size_t size;
1387 int i;
1388
1389 if (ndepth > 1)
1390 continue;
1391
1392 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1393 if (!kfdt_name) {
1394 debug("No fdt property found.\n");
1395 continue;
1396 }
1397 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1398 kfdt_name);
1399 if (kfdt_noffset < 0) {
1400 debug("No image node named \"%s\" found.\n",
1401 kfdt_name);
1402 continue;
1403 }
1404 /*
1405 * Get a pointer to this configuration's fdt.
1406 */
1407 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1408 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1409 continue;
1410 }
1411
1412 len = fdt_compat_len;
1413 cur_fdt_compat = fdt_compat;
1414 /*
1415 * Look for a match for each U-Boot compatibility string in
1416 * turn in this configuration's fdt.
1417 */
1418 for (i = 0; len > 0 &&
1419 (!best_match_offset || best_match_pos > i); i++) {
1420 int cur_len = strlen(cur_fdt_compat) + 1;
1421
1422 if (!fdt_node_check_compatible(kfdt, 0,
1423 cur_fdt_compat)) {
1424 best_match_offset = noffset;
1425 best_match_pos = i;
1426 break;
1427 }
1428 len -= cur_len;
1429 cur_fdt_compat += cur_len;
1430 }
1431 }
1432 if (!best_match_offset) {
1433 debug("No match found.\n");
1434 return -1;
1435 }
1436
1437 return best_match_offset;
1438}
1439
1440/**
1441 * fit_conf_get_node - get node offset for configuration of a given unit name
1442 * @fit: pointer to the FIT format image header
1443 * @conf_uname: configuration node unit name
1444 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001445 * fit_conf_get_node() finds a configuration (within the '/configurations'
1446 * parent node) of a provided unit name. If configuration is found its node
Simon Glassda0af362013-05-07 06:11:53 +00001447 * offset is returned to the caller.
1448 *
1449 * When NULL is provided in second argument fit_conf_get_node() will search
1450 * for a default configuration node instead. Default configuration node unit
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001451 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glassda0af362013-05-07 06:11:53 +00001452 * node.
1453 *
1454 * returns:
1455 * configuration node offset when found (>=0)
1456 * negative number on failure (FDT_ERR_* code)
1457 */
1458int fit_conf_get_node(const void *fit, const char *conf_uname)
1459{
1460 int noffset, confs_noffset;
1461 int len;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001462 const char *s;
1463 char *conf_uname_copy = NULL;
Simon Glassda0af362013-05-07 06:11:53 +00001464
1465 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1466 if (confs_noffset < 0) {
1467 debug("Can't find configurations parent node '%s' (%s)\n",
1468 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1469 return confs_noffset;
1470 }
1471
1472 if (conf_uname == NULL) {
1473 /* get configuration unit name from the default property */
1474 debug("No configuration specified, trying default...\n");
1475 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1476 FIT_DEFAULT_PROP, &len);
1477 if (conf_uname == NULL) {
1478 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1479 len);
1480 return len;
1481 }
1482 debug("Found default configuration: '%s'\n", conf_uname);
1483 }
1484
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001485 s = strchr(conf_uname, '#');
1486 if (s) {
1487 len = s - conf_uname;
1488 conf_uname_copy = malloc(len + 1);
1489 if (!conf_uname_copy) {
1490 debug("Can't allocate uname copy: '%s'\n",
1491 conf_uname);
1492 return -ENOMEM;
1493 }
1494 memcpy(conf_uname_copy, conf_uname, len);
1495 conf_uname_copy[len] = '\0';
1496 conf_uname = conf_uname_copy;
1497 }
1498
Simon Glassda0af362013-05-07 06:11:53 +00001499 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1500 if (noffset < 0) {
1501 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1502 conf_uname, fdt_strerror(noffset));
1503 }
1504
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001505 if (conf_uname_copy)
1506 free(conf_uname_copy);
1507
Simon Glassda0af362013-05-07 06:11:53 +00001508 return noffset;
1509}
1510
Pantelis Antonioua4880742017-09-04 23:12:14 +03001511int fit_conf_get_prop_node_count(const void *fit, int noffset,
Simon Glassda0af362013-05-07 06:11:53 +00001512 const char *prop_name)
1513{
Pantelis Antonioua4880742017-09-04 23:12:14 +03001514 return fdt_stringlist_count(fit, noffset, prop_name);
1515}
1516
1517int fit_conf_get_prop_node_index(const void *fit, int noffset,
1518 const char *prop_name, int index)
1519{
1520 const char *uname;
Simon Glassda0af362013-05-07 06:11:53 +00001521 int len;
1522
1523 /* get kernel image unit name from configuration kernel property */
Pantelis Antonioua4880742017-09-04 23:12:14 +03001524 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
Simon Glassda0af362013-05-07 06:11:53 +00001525 if (uname == NULL)
1526 return len;
1527
1528 return fit_image_get_node(fit, uname);
1529}
1530
Pantelis Antonioua4880742017-09-04 23:12:14 +03001531int fit_conf_get_prop_node(const void *fit, int noffset,
1532 const char *prop_name)
1533{
1534 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1535}
1536
Simon Glassda0af362013-05-07 06:11:53 +00001537/**
Simon Glassda0af362013-05-07 06:11:53 +00001538 * fit_conf_print - prints out the FIT configuration details
1539 * @fit: pointer to the FIT format image header
1540 * @noffset: offset of the configuration node
1541 * @p: pointer to prefix string
1542 *
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001543 * fit_conf_print() lists all mandatory properties for the processed
Simon Glassda0af362013-05-07 06:11:53 +00001544 * configuration node.
1545 *
1546 * returns:
1547 * no returned results
1548 */
1549void fit_conf_print(const void *fit, int noffset, const char *p)
1550{
1551 char *desc;
Simon Glassb0ea7402016-10-02 17:59:28 -06001552 const char *uname;
Simon Glassda0af362013-05-07 06:11:53 +00001553 int ret;
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001554 int fdt_index, loadables_index;
Simon Glassda0af362013-05-07 06:11:53 +00001555
1556 /* Mandatory properties */
1557 ret = fit_get_desc(fit, noffset, &desc);
1558 printf("%s Description: ", p);
1559 if (ret)
1560 printf("unavailable\n");
1561 else
1562 printf("%s\n", desc);
1563
Simon Glassb0ea7402016-10-02 17:59:28 -06001564 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001565 printf("%s Kernel: ", p);
1566 if (uname == NULL)
1567 printf("unavailable\n");
1568 else
1569 printf("%s\n", uname);
1570
1571 /* Optional properties */
Simon Glassb0ea7402016-10-02 17:59:28 -06001572 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Simon Glassda0af362013-05-07 06:11:53 +00001573 if (uname)
1574 printf("%s Init Ramdisk: %s\n", p, uname);
1575
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001576 for (fdt_index = 0;
1577 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
1578 fdt_index, NULL), uname;
1579 fdt_index++) {
1580
1581 if (fdt_index == 0)
1582 printf("%s FDT: ", p);
1583 else
1584 printf("%s ", p);
1585 printf("%s\n", uname);
1586 }
Karl Apsite8f0537d2015-05-21 09:52:47 -04001587
Simon Glassb0ea7402016-10-02 17:59:28 -06001588 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
Michal Simek0a130b12016-05-17 13:58:44 +02001589 if (uname)
1590 printf("%s FPGA: %s\n", p, uname);
1591
Karl Apsite8f0537d2015-05-21 09:52:47 -04001592 /* Print out all of the specified loadables */
1593 for (loadables_index = 0;
Simon Glassb0ea7402016-10-02 17:59:28 -06001594 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
1595 loadables_index, NULL), uname;
1596 loadables_index++) {
Karl Apsite8f0537d2015-05-21 09:52:47 -04001597 if (loadables_index == 0) {
1598 printf("%s Loadables: ", p);
1599 } else {
1600 printf("%s ", p);
1601 }
1602 printf("%s\n", uname);
1603 }
Simon Glassda0af362013-05-07 06:11:53 +00001604}
1605
Jeroen Hofsteeffa60da2014-10-08 22:57:38 +02001606static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass384d86d2013-05-16 13:53:21 +00001607{
1608 fit_image_print(fit, rd_noffset, " ");
1609
1610 if (verify) {
1611 puts(" Verifying Hash Integrity ... ");
1612 if (!fit_image_verify(fit, rd_noffset)) {
1613 puts("Bad Data Hash\n");
1614 return -EACCES;
1615 }
1616 puts("OK\n");
1617 }
1618
1619 return 0;
1620}
1621
Simon Glass384d86d2013-05-16 13:53:21 +00001622int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1623 ulong addr)
1624{
1625 int cfg_noffset;
1626 void *fit_hdr;
1627 int noffset;
1628
1629 debug("* %s: using config '%s' from image at 0x%08lx\n",
1630 prop_name, images->fit_uname_cfg, addr);
1631
1632 /* Check whether configuration has this property defined */
1633 fit_hdr = map_sysmem(addr, 0);
1634 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1635 if (cfg_noffset < 0) {
1636 debug("* %s: no such config\n", prop_name);
Paul Burton14171b12016-09-20 18:17:12 +01001637 return -EINVAL;
Simon Glass384d86d2013-05-16 13:53:21 +00001638 }
1639
1640 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1641 if (noffset < 0) {
1642 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Grayae6a02f2016-09-03 08:30:14 +10001643 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00001644 }
1645
1646 return noffset;
1647}
1648
Simon Glassa0c0b632014-06-12 07:24:47 -06001649/**
1650 * fit_get_image_type_property() - get property name for IH_TYPE_...
1651 *
1652 * @return the properly name where we expect to find the image in the
1653 * config node
1654 */
1655static const char *fit_get_image_type_property(int type)
1656{
1657 /*
1658 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberg8ca5a902016-06-15 17:00:19 -05001659 * but we don't have access to the short name, and "fdt" is different
Simon Glassa0c0b632014-06-12 07:24:47 -06001660 * anyway. So let's just keep it here.
1661 */
1662 switch (type) {
1663 case IH_TYPE_FLATDT:
1664 return FIT_FDT_PROP;
1665 case IH_TYPE_KERNEL:
1666 return FIT_KERNEL_PROP;
1667 case IH_TYPE_RAMDISK:
1668 return FIT_RAMDISK_PROP;
Simon Glass0129b522014-10-19 21:11:24 -06001669 case IH_TYPE_X86_SETUP:
1670 return FIT_SETUP_PROP;
Karl Apsite1b21c282015-05-21 09:52:48 -04001671 case IH_TYPE_LOADABLE:
1672 return FIT_LOADABLE_PROP;
Michal Simekebae78b2016-05-17 14:03:50 +02001673 case IH_TYPE_FPGA:
1674 return FIT_FPGA_PROP;
Simon Glassa0c0b632014-06-12 07:24:47 -06001675 }
1676
1677 return "unknown";
1678}
1679
1680int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassad68fc32013-07-10 23:08:10 -07001681 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass384d86d2013-05-16 13:53:21 +00001682 int arch, int image_type, int bootstage_id,
1683 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1684{
1685 int cfg_noffset, noffset;
1686 const char *fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07001687 const char *fit_uname_config;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001688 const char *fit_base_uname_config;
Simon Glass384d86d2013-05-16 13:53:21 +00001689 const void *fit;
1690 const void *buf;
1691 size_t size;
1692 int type_ok, os_ok;
1693 ulong load, data, len;
Marek Vasutcaee41f2014-12-16 14:07:22 +01001694 uint8_t os;
Alison Wang73818d52016-11-10 10:49:03 +08001695#ifndef USE_HOSTCC
1696 uint8_t os_arch;
1697#endif
Simon Glassa0c0b632014-06-12 07:24:47 -06001698 const char *prop_name;
Simon Glass384d86d2013-05-16 13:53:21 +00001699 int ret;
1700
1701 fit = map_sysmem(addr, 0);
1702 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassad68fc32013-07-10 23:08:10 -07001703 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001704 fit_base_uname_config = NULL;
Simon Glassa0c0b632014-06-12 07:24:47 -06001705 prop_name = fit_get_image_type_property(image_type);
Simon Glass384d86d2013-05-16 13:53:21 +00001706 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1707
1708 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1709 if (!fit_check_format(fit)) {
1710 printf("Bad FIT %s image format!\n", prop_name);
1711 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1712 return -ENOEXEC;
1713 }
1714 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1715 if (fit_uname) {
Masahiro Yamadaaa58eec2014-02-18 15:39:21 +09001716 /* get FIT component image node offset */
Simon Glass384d86d2013-05-16 13:53:21 +00001717 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1718 noffset = fit_image_get_node(fit, fit_uname);
1719 } else {
1720 /*
1721 * no image node unit name, try to get config
1722 * node first. If config unit node name is NULL
1723 * fit_conf_get_node() will try to find default config node
1724 */
1725 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1726 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1727 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1728 } else {
1729 cfg_noffset = fit_conf_get_node(fit,
1730 fit_uname_config);
1731 }
1732 if (cfg_noffset < 0) {
1733 puts("Could not find configuration node\n");
1734 bootstage_error(bootstage_id +
1735 BOOTSTAGE_SUB_NO_UNIT_NAME);
1736 return -ENOENT;
1737 }
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001738 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1739 printf(" Using '%s' configuration\n", fit_base_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00001740 if (image_type == IH_TYPE_KERNEL) {
1741 /* Remember (and possibly verify) this config */
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001742 images->fit_uname_cfg = fit_base_uname_config;
Simon Glass384d86d2013-05-16 13:53:21 +00001743 if (IMAGE_ENABLE_VERIFY && images->verify) {
1744 puts(" Verifying Hash Integrity ... ");
Simon Glass5da42d92014-06-12 07:24:45 -06001745 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass384d86d2013-05-16 13:53:21 +00001746 puts("Bad Data Hash\n");
1747 bootstage_error(bootstage_id +
1748 BOOTSTAGE_SUB_HASH);
1749 return -EACCES;
1750 }
1751 puts("OK\n");
1752 }
1753 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1754 }
1755
1756 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1757 prop_name);
1758 fit_uname = fit_get_name(fit, noffset, NULL);
1759 }
1760 if (noffset < 0) {
1761 puts("Could not find subimage node\n");
1762 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1763 return -ENOENT;
1764 }
1765
1766 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1767
1768 ret = fit_image_select(fit, noffset, images->verify);
1769 if (ret) {
1770 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1771 return ret;
1772 }
1773
1774 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass3c1e7502014-10-19 21:11:22 -06001775#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass384d86d2013-05-16 13:53:21 +00001776 if (!fit_image_check_target_arch(fit, noffset)) {
1777 puts("Unsupported Architecture\n");
1778 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1779 return -ENOEXEC;
1780 }
Simon Glassa51991d2014-06-12 07:24:53 -06001781#endif
Alison Wang73818d52016-11-10 10:49:03 +08001782
1783#ifndef USE_HOSTCC
1784 fit_image_get_arch(fit, noffset, &os_arch);
1785 images->os.arch = os_arch;
1786#endif
1787
Simon Glass384d86d2013-05-16 13:53:21 +00001788 if (image_type == IH_TYPE_FLATDT &&
1789 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1790 puts("FDT image is compressed");
1791 return -EPROTONOSUPPORT;
1792 }
1793
1794 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1795 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02001796 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1797 (image_type == IH_TYPE_KERNEL &&
1798 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasutcaee41f2014-12-16 14:07:22 +01001799
Andreas Bießmannb4cc60d2016-08-14 20:31:24 +02001800 os_ok = image_type == IH_TYPE_FLATDT ||
1801 image_type == IH_TYPE_FPGA ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01001802 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cca82eeaf2016-07-20 08:32:50 +02001803 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasutcaee41f2014-12-16 14:07:22 +01001804 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite1b21c282015-05-21 09:52:48 -04001805
1806 /*
1807 * If either of the checks fail, we should report an error, but
1808 * if the image type is coming from the "loadables" field, we
1809 * don't care what it is
1810 */
1811 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasutcaee41f2014-12-16 14:07:22 +01001812 fit_image_get_os(fit, noffset, &os);
1813 printf("No %s %s %s Image\n",
1814 genimg_get_os_name(os),
1815 genimg_get_arch_name(arch),
Simon Glass384d86d2013-05-16 13:53:21 +00001816 genimg_get_type_name(image_type));
1817 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1818 return -EIO;
1819 }
1820
1821 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1822
1823 /* get image data address and length */
1824 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1825 printf("Could not find %s subimage data!\n", prop_name);
1826 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass9d0da5b2013-06-16 07:46:49 -07001827 return -ENOENT;
Simon Glass384d86d2013-05-16 13:53:21 +00001828 }
Andrew F. Davisc6d8cc02016-11-21 14:37:09 -06001829
1830#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1831 /* perform any post-processing on the image data */
1832 board_fit_image_post_process((void **)&buf, &size);
1833#endif
1834
Simon Glass384d86d2013-05-16 13:53:21 +00001835 len = (ulong)size;
1836
1837 /* verify that image data is a proper FDT blob */
Masahiro Yamadabc2a07d2013-09-19 12:10:18 +09001838 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass384d86d2013-05-16 13:53:21 +00001839 puts("Subimage data is not a FDT");
1840 return -ENOEXEC;
1841 }
1842
1843 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1844
1845 /*
1846 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasscd65fb12014-06-12 07:24:49 -06001847 * cast in the unmap_sysmem() call:
Simon Glass384d86d2013-05-16 13:53:21 +00001848 * warning: initialization discards qualifiers from pointer target type
1849 */
1850 {
1851 void *vbuf = (void *)buf;
1852
1853 data = map_to_sysmem(vbuf);
1854 }
1855
1856 if (load_op == FIT_LOAD_IGNORED) {
1857 /* Don't load */
1858 } else if (fit_image_get_load(fit, noffset, &load)) {
1859 if (load_op == FIT_LOAD_REQUIRED) {
1860 printf("Can't get %s subimage load address!\n",
1861 prop_name);
1862 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1863 return -EBADF;
1864 }
Simon Glass05a9ad72014-08-22 14:26:43 -06001865 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass384d86d2013-05-16 13:53:21 +00001866 ulong image_start, image_end;
1867 ulong load_end;
1868 void *dst;
1869
1870 /*
1871 * move image data to the load address,
1872 * make sure we don't overwrite initial image
1873 */
1874 image_start = addr;
1875 image_end = addr + fit_get_size(fit);
1876
1877 load_end = load + len;
1878 if (image_type != IH_TYPE_KERNEL &&
1879 load < image_end && load_end > image_start) {
1880 printf("Error: %s overwritten\n", prop_name);
1881 return -EXDEV;
1882 }
1883
1884 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1885 prop_name, data, load);
1886
1887 dst = map_sysmem(load, len);
1888 memmove(dst, buf, len);
1889 data = load;
1890 }
1891 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1892
1893 *datap = data;
1894 *lenp = len;
1895 if (fit_unamep)
1896 *fit_unamep = (char *)fit_uname;
Simon Glassad68fc32013-07-10 23:08:10 -07001897 if (fit_uname_configp)
Pantelis Antoniou5e671d62017-09-04 23:12:15 +03001898 *fit_uname_configp = (char *)(fit_uname_config ? :
1899 fit_base_uname_config);
Simon Glass384d86d2013-05-16 13:53:21 +00001900
1901 return noffset;
1902}
Simon Glass0129b522014-10-19 21:11:24 -06001903
1904int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1905 ulong *setup_start, ulong *setup_len)
1906{
1907 int noffset;
1908 ulong addr;
1909 ulong len;
1910 int ret;
1911
1912 addr = map_to_sysmem(images->fit_hdr_os);
1913 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1914 if (noffset < 0)
1915 return noffset;
1916
1917 ret = fit_image_load(images, addr, NULL, NULL, arch,
1918 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1919 FIT_LOAD_REQUIRED, setup_start, &len);
1920
1921 return ret;
1922}
Pantelis Antoniou6e51fb22017-09-04 23:12:16 +03001923
1924#ifndef USE_HOSTCC
1925int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
1926 const char **fit_unamep, const char **fit_uname_configp,
1927 int arch, ulong *datap, ulong *lenp)
1928{
1929 int fdt_noffset, cfg_noffset, count;
1930 const void *fit;
1931 const char *fit_uname = NULL;
1932 const char *fit_uname_config = NULL;
1933 char *fit_uname_config_copy = NULL;
1934 char *next_config = NULL;
1935 ulong load, len;
1936#ifdef CONFIG_OF_LIBFDT_OVERLAY
1937 ulong image_start, image_end;
1938 ulong ovload, ovlen;
1939 const char *uconfig;
1940 const char *uname;
1941 void *base, *ov;
1942 int i, err, noffset, ov_noffset;
1943#endif
1944
1945 fit_uname = fit_unamep ? *fit_unamep : NULL;
1946
1947 if (fit_uname_configp && *fit_uname_configp) {
1948 fit_uname_config_copy = strdup(*fit_uname_configp);
1949 if (!fit_uname_config_copy)
1950 return -ENOMEM;
1951
1952 next_config = strchr(fit_uname_config_copy, '#');
1953 if (next_config)
1954 *next_config++ = '\0';
1955 if (next_config - 1 > fit_uname_config_copy)
1956 fit_uname_config = fit_uname_config_copy;
1957 }
1958
1959 fdt_noffset = fit_image_load(images,
1960 addr, &fit_uname, &fit_uname_config,
1961 arch, IH_TYPE_FLATDT,
1962 BOOTSTAGE_ID_FIT_FDT_START,
1963 FIT_LOAD_OPTIONAL, &load, &len);
1964
1965 if (fdt_noffset < 0)
1966 goto out;
1967
1968 debug("fit_uname=%s, fit_uname_config=%s\n",
1969 fit_uname ? fit_uname : "<NULL>",
1970 fit_uname_config ? fit_uname_config : "<NULL>");
1971
1972 fit = map_sysmem(addr, 0);
1973
1974 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
1975
1976 /* single blob, or error just return as well */
1977 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
1978 if (count <= 1 && !next_config)
1979 goto out;
1980
1981 /* we need to apply overlays */
1982
1983#ifdef CONFIG_OF_LIBFDT_OVERLAY
1984 image_start = addr;
1985 image_end = addr + fit_get_size(fit);
1986 /* verify that relocation took place by load address not being in fit */
1987 if (load >= image_start && load < image_end) {
1988 /* check is simplified; fit load checks for overlaps */
1989 printf("Overlayed FDT requires relocation\n");
1990 fdt_noffset = -EBADF;
1991 goto out;
1992 }
1993
1994 base = map_sysmem(load, len);
1995
1996 /* apply extra configs in FIT first, followed by args */
1997 for (i = 1; ; i++) {
1998 if (i < count) {
1999 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2000 FIT_FDT_PROP, i);
2001 uname = fit_get_name(fit, noffset, NULL);
2002 uconfig = NULL;
2003 } else {
2004 if (!next_config)
2005 break;
2006 uconfig = next_config;
2007 next_config = strchr(next_config, '#');
2008 if (next_config)
2009 *next_config++ = '\0';
2010 uname = NULL;
2011 }
2012
2013 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2014
2015 ov_noffset = fit_image_load(images,
2016 addr, &uname, &uconfig,
2017 arch, IH_TYPE_FLATDT,
2018 BOOTSTAGE_ID_FIT_FDT_START,
2019 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2020 if (ov_noffset < 0) {
2021 printf("load of %s failed\n", uname);
2022 continue;
2023 }
2024 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2025 uname, ovload, ovlen);
2026 ov = map_sysmem(ovload, ovlen);
2027
2028 base = map_sysmem(load, len + ovlen);
2029 err = fdt_open_into(base, base, len + ovlen);
2030 if (err < 0) {
2031 printf("failed on fdt_open_into\n");
2032 fdt_noffset = err;
2033 goto out;
2034 }
2035 /* the verbose method prints out messages on error */
2036 err = fdt_overlay_apply_verbose(base, ov);
2037 if (err < 0) {
2038 fdt_noffset = err;
2039 goto out;
2040 }
2041 fdt_pack(base);
2042 len = fdt_totalsize(base);
2043 }
2044#else
2045 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2046 fdt_noffset = -EBADF;
2047#endif
2048
2049out:
2050 if (datap)
2051 *datap = load;
2052 if (lenp)
2053 *lenp = len;
2054 if (fit_unamep)
2055 *fit_unamep = fit_uname;
2056 if (fit_uname_configp)
2057 *fit_uname_configp = fit_uname_config;
2058
2059 if (fit_uname_config_copy)
2060 free(fit_uname_config_copy);
2061 return fdt_noffset;
2062}
2063#endif