1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
9 */
10
11 #define LOG_CATEGORY LOGC_BOOT
12
13 #ifdef USE_HOSTCC
14 #include "mkimage.h"
15 #include <time.h>
16 #include <u-boot/crc.h>
17 #else
18 #include <linux/compiler.h>
19 #include <linux/kconfig.h>
20 #include <common.h>
21 #include <errno.h>
22 #include <mapmem.h>
23 #include <asm/io.h>
24 #include <malloc.h>
25 DECLARE_GLOBAL_DATA_PTR;
26 #endif /* !USE_HOSTCC*/
27
28 #include <bootm.h>
29 #include <image.h>
30 #include <bootstage.h>
31 #include <linux/kconfig.h>
32 #include <u-boot/crc.h>
33 #include <u-boot/md5.h>
34 #include <u-boot/sha1.h>
35 #include <u-boot/sha256.h>
36
37 /*****************************************************************************/
38 /* New uImage format routines */
39 /*****************************************************************************/
40 #ifndef USE_HOSTCC
fit_parse_spec(const char * spec,char sepc,ulong addr_curr,ulong * addr,const char ** name)41 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
42 ulong *addr, const char **name)
43 {
44 const char *sep;
45
46 *addr = addr_curr;
47 *name = NULL;
48
49 sep = strchr(spec, sepc);
50 if (sep) {
51 if (sep - spec > 0)
52 *addr = simple_strtoul(spec, NULL, 16);
53
54 *name = sep + 1;
55 return 1;
56 }
57
58 return 0;
59 }
60
61 /**
62 * fit_parse_conf - parse FIT configuration spec
63 * @spec: input string, containing configuration spec
64 * @add_curr: current image address (to be used as a possible default)
65 * @addr: pointer to a ulong variable, will hold FIT image address of a given
66 * configuration
67 * @conf_name double pointer to a char, will hold pointer to a configuration
68 * unit name
69 *
70 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
71 * where <addr> is a FIT image address that contains configuration
72 * with a <conf> unit name.
73 *
74 * Address part is optional, and if omitted default add_curr will
75 * be used instead.
76 *
77 * returns:
78 * 1 if spec is a valid configuration string,
79 * addr and conf_name are set accordingly
80 * 0 otherwise
81 */
fit_parse_conf(const char * spec,ulong addr_curr,ulong * addr,const char ** conf_name)82 int fit_parse_conf(const char *spec, ulong addr_curr,
83 ulong *addr, const char **conf_name)
84 {
85 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
86 }
87
88 /**
89 * fit_parse_subimage - parse FIT subimage spec
90 * @spec: input string, containing subimage spec
91 * @add_curr: current image address (to be used as a possible default)
92 * @addr: pointer to a ulong variable, will hold FIT image address of a given
93 * subimage
94 * @image_name: double pointer to a char, will hold pointer to a subimage name
95 *
96 * fit_parse_subimage() expects subimage spec in the form of
97 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
98 * subimage with a <subimg> unit name.
99 *
100 * Address part is optional, and if omitted default add_curr will
101 * be used instead.
102 *
103 * returns:
104 * 1 if spec is a valid subimage string,
105 * addr and image_name are set accordingly
106 * 0 otherwise
107 */
fit_parse_subimage(const char * spec,ulong addr_curr,ulong * addr,const char ** image_name)108 int fit_parse_subimage(const char *spec, ulong addr_curr,
109 ulong *addr, const char **image_name)
110 {
111 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
112 }
113 #endif /* !USE_HOSTCC */
114
fit_get_debug(const void * fit,int noffset,char * prop_name,int err)115 static void fit_get_debug(const void *fit, int noffset,
116 char *prop_name, int err)
117 {
118 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
119 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
120 fdt_strerror(err));
121 }
122
123 /**
124 * fit_get_subimage_count - get component (sub-image) count
125 * @fit: pointer to the FIT format image header
126 * @images_noffset: offset of images node
127 *
128 * returns:
129 * number of image components
130 */
fit_get_subimage_count(const void * fit,int images_noffset)131 int fit_get_subimage_count(const void *fit, int images_noffset)
132 {
133 int noffset;
134 int ndepth;
135 int count = 0;
136
137 /* Process its subnodes, print out component images details */
138 for (ndepth = 0, count = 0,
139 noffset = fdt_next_node(fit, images_noffset, &ndepth);
140 (noffset >= 0) && (ndepth > 0);
141 noffset = fdt_next_node(fit, noffset, &ndepth)) {
142 if (ndepth == 1) {
143 count++;
144 }
145 }
146
147 return count;
148 }
149
150 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
151 /**
152 * fit_image_print_data() - prints out the hash node details
153 * @fit: pointer to the FIT format image header
154 * @noffset: offset of the hash node
155 * @p: pointer to prefix string
156 * @type: Type of information to print ("hash" or "sign")
157 *
158 * fit_image_print_data() lists properties for the processed hash node
159 *
160 * This function avoid using puts() since it prints a newline on the host
161 * but does not in U-Boot.
162 *
163 * returns:
164 * no returned results
165 */
fit_image_print_data(const void * fit,int noffset,const char * p,const char * type)166 static void fit_image_print_data(const void *fit, int noffset, const char *p,
167 const char *type)
168 {
169 const char *keyname;
170 uint8_t *value;
171 int value_len;
172 char *algo;
173 const char *padding;
174 bool required;
175 int ret, i;
176
177 debug("%s %s node: '%s'\n", p, type,
178 fit_get_name(fit, noffset, NULL));
179 printf("%s %s algo: ", p, type);
180 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
181 printf("invalid/unsupported\n");
182 return;
183 }
184 printf("%s", algo);
185 keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
186 required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
187 if (keyname)
188 printf(":%s", keyname);
189 if (required)
190 printf(" (required)");
191 printf("\n");
192
193 padding = fdt_getprop(fit, noffset, "padding", NULL);
194 if (padding)
195 printf("%s %s padding: %s\n", p, type, padding);
196
197 ret = fit_image_hash_get_value(fit, noffset, &value,
198 &value_len);
199 printf("%s %s value: ", p, type);
200 if (ret) {
201 printf("unavailable\n");
202 } else {
203 for (i = 0; i < value_len; i++)
204 printf("%02x", value[i]);
205 printf("\n");
206 }
207
208 debug("%s %s len: %d\n", p, type, value_len);
209
210 /* Signatures have a time stamp */
211 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
212 time_t timestamp;
213
214 printf("%s Timestamp: ", p);
215 if (fit_get_timestamp(fit, noffset, ×tamp))
216 printf("unavailable\n");
217 else
218 genimg_print_time(timestamp);
219 }
220 }
221
222 /**
223 * fit_image_print_verification_data() - prints out the hash/signature details
224 * @fit: pointer to the FIT format image header
225 * @noffset: offset of the hash or signature node
226 * @p: pointer to prefix string
227 *
228 * This lists properties for the processed hash node
229 *
230 * returns:
231 * no returned results
232 */
fit_image_print_verification_data(const void * fit,int noffset,const char * p)233 static void fit_image_print_verification_data(const void *fit, int noffset,
234 const char *p)
235 {
236 const char *name;
237
238 /*
239 * Check subnode name, must be equal to "hash" or "signature".
240 * Multiple hash/signature nodes require unique unit node
241 * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
242 */
243 name = fit_get_name(fit, noffset, NULL);
244 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
245 fit_image_print_data(fit, noffset, p, "Hash");
246 } else if (!strncmp(name, FIT_SIG_NODENAME,
247 strlen(FIT_SIG_NODENAME))) {
248 fit_image_print_data(fit, noffset, p, "Sign");
249 }
250 }
251
252 /**
253 * fit_conf_print - prints out the FIT configuration details
254 * @fit: pointer to the FIT format image header
255 * @noffset: offset of the configuration node
256 * @p: pointer to prefix string
257 *
258 * fit_conf_print() lists all mandatory properties for the processed
259 * configuration node.
260 *
261 * returns:
262 * no returned results
263 */
fit_conf_print(const void * fit,int noffset,const char * p)264 static void fit_conf_print(const void *fit, int noffset, const char *p)
265 {
266 char *desc;
267 const char *uname;
268 int ret;
269 int fdt_index, loadables_index;
270 int ndepth;
271
272 /* Mandatory properties */
273 ret = fit_get_desc(fit, noffset, &desc);
274 printf("%s Description: ", p);
275 if (ret)
276 printf("unavailable\n");
277 else
278 printf("%s\n", desc);
279
280 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
281 printf("%s Kernel: ", p);
282 if (!uname)
283 printf("unavailable\n");
284 else
285 printf("%s\n", uname);
286
287 /* Optional properties */
288 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
289 if (uname)
290 printf("%s Init Ramdisk: %s\n", p, uname);
291
292 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
293 if (uname)
294 printf("%s Firmware: %s\n", p, uname);
295
296 for (fdt_index = 0;
297 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
298 fdt_index, NULL), uname;
299 fdt_index++) {
300 if (fdt_index == 0)
301 printf("%s FDT: ", p);
302 else
303 printf("%s ", p);
304 printf("%s\n", uname);
305 }
306
307 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
308 if (uname)
309 printf("%s FPGA: %s\n", p, uname);
310
311 /* Print out all of the specified loadables */
312 for (loadables_index = 0;
313 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
314 loadables_index, NULL), uname;
315 loadables_index++) {
316 if (loadables_index == 0) {
317 printf("%s Loadables: ", p);
318 } else {
319 printf("%s ", p);
320 }
321 printf("%s\n", uname);
322 }
323
324 /* Process all hash subnodes of the component configuration node */
325 for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
326 (noffset >= 0) && (ndepth > 0);
327 noffset = fdt_next_node(fit, noffset, &ndepth)) {
328 if (ndepth == 1) {
329 /* Direct child node of the component configuration node */
330 fit_image_print_verification_data(fit, noffset, p);
331 }
332 }
333 }
334
335 /**
336 * fit_print_contents - prints out the contents of the FIT format image
337 * @fit: pointer to the FIT format image header
338 * @p: pointer to prefix string
339 *
340 * fit_print_contents() formats a multi line FIT image contents description.
341 * The routine prints out FIT image properties (root node level) followed by
342 * the details of each component image.
343 *
344 * returns:
345 * no returned results
346 */
fit_print_contents(const void * fit)347 void fit_print_contents(const void *fit)
348 {
349 char *desc;
350 char *uname;
351 int images_noffset;
352 int confs_noffset;
353 int noffset;
354 int ndepth;
355 int count = 0;
356 int ret;
357 const char *p;
358 time_t timestamp;
359
360 /* Indent string is defined in header image.h */
361 p = IMAGE_INDENT_STRING;
362
363 /* Root node properties */
364 ret = fit_get_desc(fit, 0, &desc);
365 printf("%sFIT description: ", p);
366 if (ret)
367 printf("unavailable\n");
368 else
369 printf("%s\n", desc);
370
371 if (IMAGE_ENABLE_TIMESTAMP) {
372 ret = fit_get_timestamp(fit, 0, ×tamp);
373 printf("%sCreated: ", p);
374 if (ret)
375 printf("unavailable\n");
376 else
377 genimg_print_time(timestamp);
378 }
379
380 /* Find images parent node offset */
381 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
382 if (images_noffset < 0) {
383 printf("Can't find images parent node '%s' (%s)\n",
384 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
385 return;
386 }
387
388 /* Process its subnodes, print out component images details */
389 for (ndepth = 0, count = 0,
390 noffset = fdt_next_node(fit, images_noffset, &ndepth);
391 (noffset >= 0) && (ndepth > 0);
392 noffset = fdt_next_node(fit, noffset, &ndepth)) {
393 if (ndepth == 1) {
394 /*
395 * Direct child node of the images parent node,
396 * i.e. component image node.
397 */
398 printf("%s Image %u (%s)\n", p, count++,
399 fit_get_name(fit, noffset, NULL));
400
401 fit_image_print(fit, noffset, p);
402 }
403 }
404
405 /* Find configurations parent node offset */
406 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
407 if (confs_noffset < 0) {
408 debug("Can't get configurations parent node '%s' (%s)\n",
409 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
410 return;
411 }
412
413 /* get default configuration unit name from default property */
414 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
415 if (uname)
416 printf("%s Default Configuration: '%s'\n", p, uname);
417
418 /* Process its subnodes, print out configurations details */
419 for (ndepth = 0, count = 0,
420 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
421 (noffset >= 0) && (ndepth > 0);
422 noffset = fdt_next_node(fit, noffset, &ndepth)) {
423 if (ndepth == 1) {
424 /*
425 * Direct child node of the configurations parent node,
426 * i.e. configuration node.
427 */
428 printf("%s Configuration %u (%s)\n", p, count++,
429 fit_get_name(fit, noffset, NULL));
430
431 fit_conf_print(fit, noffset, p);
432 }
433 }
434 }
435
436 /**
437 * fit_image_print - prints out the FIT component image details
438 * @fit: pointer to the FIT format image header
439 * @image_noffset: offset of the component image node
440 * @p: pointer to prefix string
441 *
442 * fit_image_print() lists all mandatory properties for the processed component
443 * image. If present, hash nodes are printed out as well. Load
444 * address for images of type firmware is also printed out. Since the load
445 * address is not mandatory for firmware images, it will be output as
446 * "unavailable" when not present.
447 *
448 * returns:
449 * no returned results
450 */
fit_image_print(const void * fit,int image_noffset,const char * p)451 void fit_image_print(const void *fit, int image_noffset, const char *p)
452 {
453 char *desc;
454 uint8_t type, arch, os, comp;
455 size_t size;
456 ulong load, entry;
457 const void *data;
458 int noffset;
459 int ndepth;
460 int ret;
461
462 /* Mandatory properties */
463 ret = fit_get_desc(fit, image_noffset, &desc);
464 printf("%s Description: ", p);
465 if (ret)
466 printf("unavailable\n");
467 else
468 printf("%s\n", desc);
469
470 if (IMAGE_ENABLE_TIMESTAMP) {
471 time_t timestamp;
472
473 ret = fit_get_timestamp(fit, 0, ×tamp);
474 printf("%s Created: ", p);
475 if (ret)
476 printf("unavailable\n");
477 else
478 genimg_print_time(timestamp);
479 }
480
481 fit_image_get_type(fit, image_noffset, &type);
482 printf("%s Type: %s\n", p, genimg_get_type_name(type));
483
484 fit_image_get_comp(fit, image_noffset, &comp);
485 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
486
487 ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
488
489 #ifndef USE_HOSTCC
490 printf("%s Data Start: ", p);
491 if (ret) {
492 printf("unavailable\n");
493 } else {
494 void *vdata = (void *)data;
495
496 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
497 }
498 #endif
499
500 printf("%s Data Size: ", p);
501 if (ret)
502 printf("unavailable\n");
503 else
504 genimg_print_size(size);
505
506 /* Remaining, type dependent properties */
507 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
508 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
509 (type == IH_TYPE_FLATDT)) {
510 fit_image_get_arch(fit, image_noffset, &arch);
511 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
512 }
513
514 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
515 (type == IH_TYPE_FIRMWARE)) {
516 fit_image_get_os(fit, image_noffset, &os);
517 printf("%s OS: %s\n", p, genimg_get_os_name(os));
518 }
519
520 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
521 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
522 (type == IH_TYPE_FPGA)) {
523 ret = fit_image_get_load(fit, image_noffset, &load);
524 printf("%s Load Address: ", p);
525 if (ret)
526 printf("unavailable\n");
527 else
528 printf("0x%08lx\n", load);
529 }
530
531 /* optional load address for FDT */
532 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
533 printf("%s Load Address: 0x%08lx\n", p, load);
534
535 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
536 (type == IH_TYPE_RAMDISK)) {
537 ret = fit_image_get_entry(fit, image_noffset, &entry);
538 printf("%s Entry Point: ", p);
539 if (ret)
540 printf("unavailable\n");
541 else
542 printf("0x%08lx\n", entry);
543 }
544
545 /* Process all hash subnodes of the component image node */
546 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
547 (noffset >= 0) && (ndepth > 0);
548 noffset = fdt_next_node(fit, noffset, &ndepth)) {
549 if (ndepth == 1) {
550 /* Direct child node of the component image node */
551 fit_image_print_verification_data(fit, noffset, p);
552 }
553 }
554 }
555 #else
fit_print_contents(const void * fit)556 void fit_print_contents(const void *fit) { }
fit_image_print(const void * fit,int image_noffset,const char * p)557 void fit_image_print(const void *fit, int image_noffset, const char *p) { }
558 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
559
560 /**
561 * fit_get_desc - get node description property
562 * @fit: pointer to the FIT format image header
563 * @noffset: node offset
564 * @desc: double pointer to the char, will hold pointer to the description
565 *
566 * fit_get_desc() reads description property from a given node, if
567 * description is found pointer to it is returned in third call argument.
568 *
569 * returns:
570 * 0, on success
571 * -1, on failure
572 */
fit_get_desc(const void * fit,int noffset,char ** desc)573 int fit_get_desc(const void *fit, int noffset, char **desc)
574 {
575 int len;
576
577 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
578 if (*desc == NULL) {
579 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
580 return -1;
581 }
582
583 return 0;
584 }
585
586 /**
587 * fit_get_timestamp - get node timestamp property
588 * @fit: pointer to the FIT format image header
589 * @noffset: node offset
590 * @timestamp: pointer to the time_t, will hold read timestamp
591 *
592 * fit_get_timestamp() reads timestamp property from given node, if timestamp
593 * is found and has a correct size its value is returned in third call
594 * argument.
595 *
596 * returns:
597 * 0, on success
598 * -1, on property read failure
599 * -2, on wrong timestamp size
600 */
fit_get_timestamp(const void * fit,int noffset,time_t * timestamp)601 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
602 {
603 int len;
604 const void *data;
605
606 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
607 if (data == NULL) {
608 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
609 return -1;
610 }
611 if (len != sizeof(uint32_t)) {
612 debug("FIT timestamp with incorrect size of (%u)\n", len);
613 return -2;
614 }
615
616 *timestamp = uimage_to_cpu(*((uint32_t *)data));
617 return 0;
618 }
619
620 /**
621 * fit_image_get_node - get node offset for component image of a given unit name
622 * @fit: pointer to the FIT format image header
623 * @image_uname: component image node unit name
624 *
625 * fit_image_get_node() finds a component image (within the '/images'
626 * node) of a provided unit name. If image is found its node offset is
627 * returned to the caller.
628 *
629 * returns:
630 * image node offset when found (>=0)
631 * negative number on failure (FDT_ERR_* code)
632 */
fit_image_get_node(const void * fit,const char * image_uname)633 int fit_image_get_node(const void *fit, const char *image_uname)
634 {
635 int noffset, images_noffset;
636
637 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
638 if (images_noffset < 0) {
639 debug("Can't find images parent node '%s' (%s)\n",
640 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
641 return images_noffset;
642 }
643
644 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
645 if (noffset < 0) {
646 debug("Can't get node offset for image unit name: '%s' (%s)\n",
647 image_uname, fdt_strerror(noffset));
648 }
649
650 return noffset;
651 }
652
653 /**
654 * fit_image_get_os - get os id for a given component image node
655 * @fit: pointer to the FIT format image header
656 * @noffset: component image node offset
657 * @os: pointer to the uint8_t, will hold os numeric id
658 *
659 * fit_image_get_os() finds os property in a given component image node.
660 * If the property is found, its (string) value is translated to the numeric
661 * id which is returned to the caller.
662 *
663 * returns:
664 * 0, on success
665 * -1, on failure
666 */
fit_image_get_os(const void * fit,int noffset,uint8_t * os)667 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
668 {
669 int len;
670 const void *data;
671
672 /* Get OS name from property data */
673 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
674 if (data == NULL) {
675 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
676 *os = -1;
677 return -1;
678 }
679
680 /* Translate OS name to id */
681 *os = genimg_get_os_id(data);
682 return 0;
683 }
684
685 /**
686 * fit_image_get_arch - get arch id for a given component image node
687 * @fit: pointer to the FIT format image header
688 * @noffset: component image node offset
689 * @arch: pointer to the uint8_t, will hold arch numeric id
690 *
691 * fit_image_get_arch() finds arch property in a given component image node.
692 * If the property is found, its (string) value is translated to the numeric
693 * id which is returned to the caller.
694 *
695 * returns:
696 * 0, on success
697 * -1, on failure
698 */
fit_image_get_arch(const void * fit,int noffset,uint8_t * arch)699 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
700 {
701 int len;
702 const void *data;
703
704 /* Get architecture name from property data */
705 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
706 if (data == NULL) {
707 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
708 *arch = -1;
709 return -1;
710 }
711
712 /* Translate architecture name to id */
713 *arch = genimg_get_arch_id(data);
714 return 0;
715 }
716
717 /**
718 * fit_image_get_type - get type id for a given component image node
719 * @fit: pointer to the FIT format image header
720 * @noffset: component image node offset
721 * @type: pointer to the uint8_t, will hold type numeric id
722 *
723 * fit_image_get_type() finds type property in a given component image node.
724 * If the property is found, its (string) value is translated to the numeric
725 * id which is returned to the caller.
726 *
727 * returns:
728 * 0, on success
729 * -1, on failure
730 */
fit_image_get_type(const void * fit,int noffset,uint8_t * type)731 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
732 {
733 int len;
734 const void *data;
735
736 /* Get image type name from property data */
737 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
738 if (data == NULL) {
739 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
740 *type = -1;
741 return -1;
742 }
743
744 /* Translate image type name to id */
745 *type = genimg_get_type_id(data);
746 return 0;
747 }
748
749 /**
750 * fit_image_get_comp - get comp id for a given component image node
751 * @fit: pointer to the FIT format image header
752 * @noffset: component image node offset
753 * @comp: pointer to the uint8_t, will hold comp numeric id
754 *
755 * fit_image_get_comp() finds comp property in a given component image node.
756 * If the property is found, its (string) value is translated to the numeric
757 * id which is returned to the caller.
758 *
759 * returns:
760 * 0, on success
761 * -1, on failure
762 */
fit_image_get_comp(const void * fit,int noffset,uint8_t * comp)763 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
764 {
765 int len;
766 const void *data;
767
768 /* Get compression name from property data */
769 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
770 if (data == NULL) {
771 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
772 *comp = -1;
773 return -1;
774 }
775
776 /* Translate compression name to id */
777 *comp = genimg_get_comp_id(data);
778 return 0;
779 }
780
fit_image_get_address(const void * fit,int noffset,char * name,ulong * load)781 static int fit_image_get_address(const void *fit, int noffset, char *name,
782 ulong *load)
783 {
784 int len, cell_len;
785 const fdt32_t *cell;
786 uint64_t load64 = 0;
787
788 cell = fdt_getprop(fit, noffset, name, &len);
789 if (cell == NULL) {
790 fit_get_debug(fit, noffset, name, len);
791 return -1;
792 }
793
794 if (len > sizeof(ulong)) {
795 printf("Unsupported %s address size\n", name);
796 return -1;
797 }
798
799 cell_len = len >> 2;
800 /* Use load64 to avoid compiling warning for 32-bit target */
801 while (cell_len--) {
802 load64 = (load64 << 32) | uimage_to_cpu(*cell);
803 cell++;
804 }
805 *load = (ulong)load64;
806
807 return 0;
808 }
809 /**
810 * fit_image_get_load() - get load addr property for given component image node
811 * @fit: pointer to the FIT format image header
812 * @noffset: component image node offset
813 * @load: pointer to the uint32_t, will hold load address
814 *
815 * fit_image_get_load() finds load address property in a given component
816 * image node. If the property is found, its value is returned to the caller.
817 *
818 * returns:
819 * 0, on success
820 * -1, on failure
821 */
fit_image_get_load(const void * fit,int noffset,ulong * load)822 int fit_image_get_load(const void *fit, int noffset, ulong *load)
823 {
824 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
825 }
826
827 /**
828 * fit_image_get_entry() - get entry point address property
829 * @fit: pointer to the FIT format image header
830 * @noffset: component image node offset
831 * @entry: pointer to the uint32_t, will hold entry point address
832 *
833 * This gets the entry point address property for a given component image
834 * node.
835 *
836 * fit_image_get_entry() finds entry point address property in a given
837 * component image node. If the property is found, its value is returned
838 * to the caller.
839 *
840 * returns:
841 * 0, on success
842 * -1, on failure
843 */
fit_image_get_entry(const void * fit,int noffset,ulong * entry)844 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
845 {
846 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
847 }
848
849 /**
850 * fit_image_get_data - get data property and its size for a given component image node
851 * @fit: pointer to the FIT format image header
852 * @noffset: component image node offset
853 * @data: double pointer to void, will hold data property's data address
854 * @size: pointer to size_t, will hold data property's data size
855 *
856 * fit_image_get_data() finds data property in a given component image node.
857 * If the property is found its data start address and size are returned to
858 * the caller.
859 *
860 * returns:
861 * 0, on success
862 * -1, on failure
863 */
fit_image_get_data(const void * fit,int noffset,const void ** data,size_t * size)864 int fit_image_get_data(const void *fit, int noffset,
865 const void **data, size_t *size)
866 {
867 int len;
868
869 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
870 if (*data == NULL) {
871 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
872 *size = 0;
873 return -1;
874 }
875
876 *size = len;
877 return 0;
878 }
879
880 /**
881 * Get 'data-offset' property from a given image node.
882 *
883 * @fit: pointer to the FIT image header
884 * @noffset: component image node offset
885 * @data_offset: holds the data-offset property
886 *
887 * returns:
888 * 0, on success
889 * -ENOENT if the property could not be found
890 */
fit_image_get_data_offset(const void * fit,int noffset,int * data_offset)891 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
892 {
893 const fdt32_t *val;
894
895 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
896 if (!val)
897 return -ENOENT;
898
899 *data_offset = fdt32_to_cpu(*val);
900
901 return 0;
902 }
903
904 /**
905 * Get 'data-position' property from a given image node.
906 *
907 * @fit: pointer to the FIT image header
908 * @noffset: component image node offset
909 * @data_position: holds the data-position property
910 *
911 * returns:
912 * 0, on success
913 * -ENOENT if the property could not be found
914 */
fit_image_get_data_position(const void * fit,int noffset,int * data_position)915 int fit_image_get_data_position(const void *fit, int noffset,
916 int *data_position)
917 {
918 const fdt32_t *val;
919
920 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
921 if (!val)
922 return -ENOENT;
923
924 *data_position = fdt32_to_cpu(*val);
925
926 return 0;
927 }
928
929 /**
930 * Get 'data-size' property from a given image node.
931 *
932 * @fit: pointer to the FIT image header
933 * @noffset: component image node offset
934 * @data_size: holds the data-size property
935 *
936 * returns:
937 * 0, on success
938 * -ENOENT if the property could not be found
939 */
fit_image_get_data_size(const void * fit,int noffset,int * data_size)940 int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
941 {
942 const fdt32_t *val;
943
944 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
945 if (!val)
946 return -ENOENT;
947
948 *data_size = fdt32_to_cpu(*val);
949
950 return 0;
951 }
952
953 /**
954 * Get 'data-size-unciphered' property from a given image node.
955 *
956 * @fit: pointer to the FIT image header
957 * @noffset: component image node offset
958 * @data_size: holds the data-size property
959 *
960 * returns:
961 * 0, on success
962 * -ENOENT if the property could not be found
963 */
fit_image_get_data_size_unciphered(const void * fit,int noffset,size_t * data_size)964 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
965 size_t *data_size)
966 {
967 const fdt32_t *val;
968
969 val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
970 if (!val)
971 return -ENOENT;
972
973 *data_size = (size_t)fdt32_to_cpu(*val);
974
975 return 0;
976 }
977
978 /**
979 * fit_image_get_data_and_size - get data and its size including
980 * both embedded and external data
981 * @fit: pointer to the FIT format image header
982 * @noffset: component image node offset
983 * @data: double pointer to void, will hold data property's data address
984 * @size: pointer to size_t, will hold data property's data size
985 *
986 * fit_image_get_data_and_size() finds data and its size including
987 * both embedded and external data. If the property is found
988 * its data start address and size are returned to the caller.
989 *
990 * returns:
991 * 0, on success
992 * otherwise, on failure
993 */
fit_image_get_data_and_size(const void * fit,int noffset,const void ** data,size_t * size)994 int fit_image_get_data_and_size(const void *fit, int noffset,
995 const void **data, size_t *size)
996 {
997 bool external_data = false;
998 int offset;
999 int len;
1000 int ret;
1001
1002 if (!fit_image_get_data_position(fit, noffset, &offset)) {
1003 external_data = true;
1004 } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
1005 external_data = true;
1006 /*
1007 * For FIT with external data, figure out where
1008 * the external images start. This is the base
1009 * for the data-offset properties in each image.
1010 */
1011 offset += ((fdt_totalsize(fit) + 3) & ~3);
1012 }
1013
1014 if (external_data) {
1015 debug("External Data\n");
1016 ret = fit_image_get_data_size(fit, noffset, &len);
1017 *data = fit + offset;
1018 *size = len;
1019 } else {
1020 ret = fit_image_get_data(fit, noffset, data, size);
1021 }
1022
1023 return ret;
1024 }
1025
1026 /**
1027 * fit_image_hash_get_algo - get hash algorithm name
1028 * @fit: pointer to the FIT format image header
1029 * @noffset: hash node offset
1030 * @algo: double pointer to char, will hold pointer to the algorithm name
1031 *
1032 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1033 * If the property is found its data start address is returned to the caller.
1034 *
1035 * returns:
1036 * 0, on success
1037 * -1, on failure
1038 */
fit_image_hash_get_algo(const void * fit,int noffset,char ** algo)1039 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
1040 {
1041 int len;
1042
1043 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1044 if (*algo == NULL) {
1045 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1046 return -1;
1047 }
1048
1049 return 0;
1050 }
1051
1052 /**
1053 * fit_image_hash_get_value - get hash value and length
1054 * @fit: pointer to the FIT format image header
1055 * @noffset: hash node offset
1056 * @value: double pointer to uint8_t, will hold address of a hash value data
1057 * @value_len: pointer to an int, will hold hash data length
1058 *
1059 * fit_image_hash_get_value() finds hash value property in a given hash node.
1060 * If the property is found its data start address and size are returned to
1061 * the caller.
1062 *
1063 * returns:
1064 * 0, on success
1065 * -1, on failure
1066 */
fit_image_hash_get_value(const void * fit,int noffset,uint8_t ** value,int * value_len)1067 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1068 int *value_len)
1069 {
1070 int len;
1071
1072 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
1073 if (*value == NULL) {
1074 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
1075 *value_len = 0;
1076 return -1;
1077 }
1078
1079 *value_len = len;
1080 return 0;
1081 }
1082
1083 /**
1084 * fit_image_hash_get_ignore - get hash ignore flag
1085 * @fit: pointer to the FIT format image header
1086 * @noffset: hash node offset
1087 * @ignore: pointer to an int, will hold hash ignore flag
1088 *
1089 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
1090 * If the property is found and non-zero, the hash algorithm is not verified by
1091 * u-boot automatically.
1092 *
1093 * returns:
1094 * 0, on ignore not found
1095 * value, on ignore found
1096 */
fit_image_hash_get_ignore(const void * fit,int noffset,int * ignore)1097 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
1098 {
1099 int len;
1100 int *value;
1101
1102 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
1103 if (value == NULL || len != sizeof(int))
1104 *ignore = 0;
1105 else
1106 *ignore = *value;
1107
1108 return 0;
1109 }
1110
1111 /**
1112 * fit_image_cipher_get_algo - get cipher algorithm name
1113 * @fit: pointer to the FIT format image header
1114 * @noffset: cipher node offset
1115 * @algo: double pointer to char, will hold pointer to the algorithm name
1116 *
1117 * fit_image_cipher_get_algo() finds cipher algorithm property in a given
1118 * cipher node. If the property is found its data start address is returned
1119 * to the caller.
1120 *
1121 * returns:
1122 * 0, on success
1123 * -1, on failure
1124 */
fit_image_cipher_get_algo(const void * fit,int noffset,char ** algo)1125 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
1126 {
1127 int len;
1128
1129 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
1130 if (!*algo) {
1131 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
1132 return -1;
1133 }
1134
1135 return 0;
1136 }
1137
fit_get_end(const void * fit)1138 ulong fit_get_end(const void *fit)
1139 {
1140 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
1141 }
1142
1143 /**
1144 * fit_set_timestamp - set node timestamp property
1145 * @fit: pointer to the FIT format image header
1146 * @noffset: node offset
1147 * @timestamp: timestamp value to be set
1148 *
1149 * fit_set_timestamp() attempts to set timestamp property in the requested
1150 * node and returns operation status to the caller.
1151 *
1152 * returns:
1153 * 0, on success
1154 * -ENOSPC if no space in device tree, -1 for other error
1155 */
fit_set_timestamp(void * fit,int noffset,time_t timestamp)1156 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
1157 {
1158 uint32_t t;
1159 int ret;
1160
1161 t = cpu_to_uimage(timestamp);
1162 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
1163 sizeof(uint32_t));
1164 if (ret) {
1165 debug("Can't set '%s' property for '%s' node (%s)\n",
1166 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
1167 fdt_strerror(ret));
1168 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
1169 }
1170
1171 return 0;
1172 }
1173
1174 /**
1175 * calculate_hash - calculate and return hash for provided input data
1176 * @data: pointer to the input data
1177 * @data_len: data length
1178 * @algo: requested hash algorithm
1179 * @value: pointer to the char, will hold hash value data (caller must
1180 * allocate enough free space)
1181 * value_len: length of the calculated hash
1182 *
1183 * calculate_hash() computes input data hash according to the requested
1184 * algorithm.
1185 * Resulting hash value is placed in caller provided 'value' buffer, length
1186 * of the calculated hash is returned via value_len pointer argument.
1187 *
1188 * returns:
1189 * 0, on success
1190 * -1, when algo is unsupported
1191 */
calculate_hash(const void * data,int data_len,const char * algo,uint8_t * value,int * value_len)1192 int calculate_hash(const void *data, int data_len, const char *algo,
1193 uint8_t *value, int *value_len)
1194 {
1195 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
1196 *((uint32_t *)value) = crc32_wd(0, data, data_len,
1197 CHUNKSZ_CRC32);
1198 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
1199 *value_len = 4;
1200 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
1201 #ifdef CONFIG_SHA1
1202 sha1_csum_wd((unsigned char *)data, data_len,
1203 (unsigned char *)value, CHUNKSZ_SHA1);
1204 *value_len = 20;
1205 #else
1206 debug("Unsupported hash sha1 alogrithm\n");
1207 return -1;
1208 #endif
1209 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
1210 #ifdef CONFIG_SHA256
1211 sha256_csum_wd((unsigned char *)data, data_len,
1212 (unsigned char *)value, CHUNKSZ_SHA256);
1213 *value_len = SHA256_SUM_LEN;
1214 #else
1215 debug("Unsupported hash sha256 alogrithm\n");
1216 return -1;
1217 #endif
1218 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
1219 #ifdef CONFIG_MD5
1220 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
1221 *value_len = 16;
1222 #else
1223 debug("Unsupported hash mad5 alogrithm\n");
1224 return -1;
1225 #endif
1226 } else {
1227 debug("Unsupported hash alogrithm\n");
1228 return -1;
1229 }
1230 return 0;
1231 }
1232
fit_image_check_hash(const void * fit,int noffset,const void * data,size_t size,char ** err_msgp)1233 static int fit_image_check_hash(const void *fit, int noffset, const void *data,
1234 size_t size, char **err_msgp)
1235 {
1236 uint8_t value[FIT_MAX_HASH_LEN];
1237 int value_len;
1238 char *algo;
1239 uint8_t *fit_value;
1240 int fit_value_len;
1241 int ignore;
1242
1243 *err_msgp = NULL;
1244
1245 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
1246 *err_msgp = "Can't get hash algo property";
1247 return -1;
1248 }
1249 printf("%s", algo);
1250
1251 if (IMAGE_ENABLE_IGNORE) {
1252 fit_image_hash_get_ignore(fit, noffset, &ignore);
1253 if (ignore) {
1254 printf("-skipped ");
1255 return 0;
1256 }
1257 }
1258
1259 if (fit_image_hash_get_value(fit, noffset, &fit_value,
1260 &fit_value_len)) {
1261 *err_msgp = "Can't get hash value property";
1262 return -1;
1263 }
1264
1265 if (calculate_hash(data, size, algo, value, &value_len)) {
1266 *err_msgp = "Unsupported hash algorithm";
1267 return -1;
1268 }
1269
1270 if (value_len != fit_value_len) {
1271 *err_msgp = "Bad hash value len";
1272 return -1;
1273 } else if (memcmp(value, fit_value, value_len) != 0) {
1274 *err_msgp = "Bad hash value";
1275 return -1;
1276 }
1277
1278 return 0;
1279 }
1280
fit_image_verify_with_data(const void * fit,int image_noffset,const void * data,size_t size)1281 int fit_image_verify_with_data(const void *fit, int image_noffset,
1282 const void *data, size_t size)
1283 {
1284 int noffset = 0;
1285 char *err_msg = "";
1286 int verify_all = 1;
1287 int ret;
1288
1289 /* Verify all required signatures */
1290 if (IMAGE_ENABLE_VERIFY &&
1291 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1292 gd_fdt_blob(), &verify_all)) {
1293 err_msg = "Unable to verify required signature";
1294 goto error;
1295 }
1296
1297 /* Process all hash subnodes of the component image node */
1298 fdt_for_each_subnode(noffset, fit, image_noffset) {
1299 const char *name = fit_get_name(fit, noffset, NULL);
1300
1301 /*
1302 * Check subnode name, must be equal to "hash".
1303 * Multiple hash nodes require unique unit node
1304 * names, e.g. hash-1, hash-2, etc.
1305 */
1306 if (!strncmp(name, FIT_HASH_NODENAME,
1307 strlen(FIT_HASH_NODENAME))) {
1308 if (fit_image_check_hash(fit, noffset, data, size,
1309 &err_msg))
1310 goto error;
1311 puts("+ ");
1312 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1313 !strncmp(name, FIT_SIG_NODENAME,
1314 strlen(FIT_SIG_NODENAME))) {
1315 ret = fit_image_check_sig(fit, noffset, data,
1316 size, -1, &err_msg);
1317
1318 /*
1319 * Show an indication on failure, but do not return
1320 * an error. Only keys marked 'required' can cause
1321 * an image validation failure. See the call to
1322 * fit_image_verify_required_sigs() above.
1323 */
1324 if (ret)
1325 puts("- ");
1326 else
1327 puts("+ ");
1328 }
1329 }
1330
1331 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
1332 err_msg = "Corrupted or truncated tree";
1333 goto error;
1334 }
1335
1336 return 1;
1337
1338 error:
1339 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
1340 err_msg, fit_get_name(fit, noffset, NULL),
1341 fit_get_name(fit, image_noffset, NULL));
1342 return 0;
1343 }
1344
1345 /**
1346 * fit_image_verify - verify data integrity
1347 * @fit: pointer to the FIT format image header
1348 * @image_noffset: component image node offset
1349 *
1350 * fit_image_verify() goes over component image hash nodes,
1351 * re-calculates each data hash and compares with the value stored in hash
1352 * node.
1353 *
1354 * returns:
1355 * 1, if all hashes are valid
1356 * 0, otherwise (or on error)
1357 */
fit_image_verify(const void * fit,int image_noffset)1358 int fit_image_verify(const void *fit, int image_noffset)
1359 {
1360 const char *name = fit_get_name(fit, image_noffset, NULL);
1361 const void *data;
1362 size_t size;
1363 char *err_msg = "";
1364
1365 if (strchr(name, '@')) {
1366 /*
1367 * We don't support this since libfdt considers names with the
1368 * name root but different @ suffix to be equal
1369 */
1370 err_msg = "Node name contains @";
1371 goto err;
1372 }
1373 /* Get image data and data length */
1374 if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
1375 err_msg = "Can't get image data/size";
1376 goto err;
1377 }
1378
1379 return fit_image_verify_with_data(fit, image_noffset, data, size);
1380
1381 err:
1382 printf("error!\n%s in '%s' image node\n", err_msg,
1383 fit_get_name(fit, image_noffset, NULL));
1384 return 0;
1385 }
1386
1387 /**
1388 * fit_all_image_verify - verify data integrity for all images
1389 * @fit: pointer to the FIT format image header
1390 *
1391 * fit_all_image_verify() goes over all images in the FIT and
1392 * for every images checks if all it's hashes are valid.
1393 *
1394 * returns:
1395 * 1, if all hashes of all images are valid
1396 * 0, otherwise (or on error)
1397 */
fit_all_image_verify(const void * fit)1398 int fit_all_image_verify(const void *fit)
1399 {
1400 int images_noffset;
1401 int noffset;
1402 int ndepth;
1403 int count;
1404
1405 /* Find images parent node offset */
1406 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1407 if (images_noffset < 0) {
1408 printf("Can't find images parent node '%s' (%s)\n",
1409 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1410 return 0;
1411 }
1412
1413 /* Process all image subnodes, check hashes for each */
1414 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1415 (ulong)fit);
1416 for (ndepth = 0, count = 0,
1417 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1418 (noffset >= 0) && (ndepth > 0);
1419 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1420 if (ndepth == 1) {
1421 /*
1422 * Direct child node of the images parent node,
1423 * i.e. component image node.
1424 */
1425 printf(" Hash(es) for Image %u (%s): ", count,
1426 fit_get_name(fit, noffset, NULL));
1427 count++;
1428
1429 if (!fit_image_verify(fit, noffset))
1430 return 0;
1431 printf("\n");
1432 }
1433 }
1434 return 1;
1435 }
1436
1437 #ifdef CONFIG_FIT_CIPHER
fit_image_uncipher(const void * fit,int image_noffset,void ** data,size_t * size)1438 static int fit_image_uncipher(const void *fit, int image_noffset,
1439 void **data, size_t *size)
1440 {
1441 int cipher_noffset, ret;
1442 void *dst;
1443 size_t size_dst;
1444
1445 cipher_noffset = fdt_subnode_offset(fit, image_noffset,
1446 FIT_CIPHER_NODENAME);
1447 if (cipher_noffset < 0)
1448 return 0;
1449
1450 ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
1451 *data, *size, &dst, &size_dst);
1452 if (ret)
1453 goto out;
1454
1455 *data = dst;
1456 *size = size_dst;
1457
1458 out:
1459 return ret;
1460 }
1461 #endif /* CONFIG_FIT_CIPHER */
1462
1463 /**
1464 * fit_image_check_os - check whether image node is of a given os type
1465 * @fit: pointer to the FIT format image header
1466 * @noffset: component image node offset
1467 * @os: requested image os
1468 *
1469 * fit_image_check_os() reads image os property and compares its numeric
1470 * id with the requested os. Comparison result is returned to the caller.
1471 *
1472 * returns:
1473 * 1 if image is of given os type
1474 * 0 otherwise (or on error)
1475 */
fit_image_check_os(const void * fit,int noffset,uint8_t os)1476 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1477 {
1478 uint8_t image_os;
1479
1480 if (fit_image_get_os(fit, noffset, &image_os))
1481 return 0;
1482 return (os == image_os);
1483 }
1484
1485 /**
1486 * fit_image_check_arch - check whether image node is of a given arch
1487 * @fit: pointer to the FIT format image header
1488 * @noffset: component image node offset
1489 * @arch: requested imagearch
1490 *
1491 * fit_image_check_arch() reads image arch property and compares its numeric
1492 * id with the requested arch. Comparison result is returned to the caller.
1493 *
1494 * returns:
1495 * 1 if image is of given arch
1496 * 0 otherwise (or on error)
1497 */
fit_image_check_arch(const void * fit,int noffset,uint8_t arch)1498 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1499 {
1500 uint8_t image_arch;
1501 int aarch32_support = 0;
1502
1503 #ifdef CONFIG_ARM64_SUPPORT_AARCH32
1504 aarch32_support = 1;
1505 #endif
1506
1507 if (fit_image_get_arch(fit, noffset, &image_arch))
1508 return 0;
1509 return (arch == image_arch) ||
1510 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
1511 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
1512 aarch32_support);
1513 }
1514
1515 /**
1516 * fit_image_check_type - check whether image node is of a given type
1517 * @fit: pointer to the FIT format image header
1518 * @noffset: component image node offset
1519 * @type: requested image type
1520 *
1521 * fit_image_check_type() reads image type property and compares its numeric
1522 * id with the requested type. Comparison result is returned to the caller.
1523 *
1524 * returns:
1525 * 1 if image is of given type
1526 * 0 otherwise (or on error)
1527 */
fit_image_check_type(const void * fit,int noffset,uint8_t type)1528 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1529 {
1530 uint8_t image_type;
1531
1532 if (fit_image_get_type(fit, noffset, &image_type))
1533 return 0;
1534 return (type == image_type);
1535 }
1536
1537 /**
1538 * fit_image_check_comp - check whether image node uses given compression
1539 * @fit: pointer to the FIT format image header
1540 * @noffset: component image node offset
1541 * @comp: requested image compression type
1542 *
1543 * fit_image_check_comp() reads image compression property and compares its
1544 * numeric id with the requested compression type. Comparison result is
1545 * returned to the caller.
1546 *
1547 * returns:
1548 * 1 if image uses requested compression
1549 * 0 otherwise (or on error)
1550 */
fit_image_check_comp(const void * fit,int noffset,uint8_t comp)1551 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1552 {
1553 uint8_t image_comp;
1554
1555 if (fit_image_get_comp(fit, noffset, &image_comp))
1556 return 0;
1557 return (comp == image_comp);
1558 }
1559
1560 /**
1561 * fdt_check_no_at() - Check for nodes whose names contain '@'
1562 *
1563 * This checks the parent node and all subnodes recursively
1564 *
1565 * @fit: FIT to check
1566 * @parent: Parent node to check
1567 * @return 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
1568 */
fdt_check_no_at(const void * fit,int parent)1569 static int fdt_check_no_at(const void *fit, int parent)
1570 {
1571 const char *name;
1572 int node;
1573 int ret;
1574
1575 name = fdt_get_name(fit, parent, NULL);
1576 if (!name || strchr(name, '@'))
1577 return -EADDRNOTAVAIL;
1578
1579 fdt_for_each_subnode(node, fit, parent) {
1580 ret = fdt_check_no_at(fit, node);
1581 if (ret)
1582 return ret;
1583 }
1584
1585 return 0;
1586 }
1587
fit_check_format(const void * fit,ulong size)1588 int fit_check_format(const void *fit, ulong size)
1589 {
1590 int ret;
1591
1592 /* A FIT image must be a valid FDT */
1593 ret = fdt_check_header(fit);
1594 if (ret) {
1595 debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
1596 ret);
1597 return -ENOEXEC;
1598 }
1599
1600 if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
1601 /*
1602 * If we are not given the size, make do wtih calculating it.
1603 * This is not as secure, so we should consider a flag to
1604 * control this.
1605 */
1606 if (size == IMAGE_SIZE_INVAL)
1607 size = fdt_totalsize(fit);
1608 ret = fdt_check_full(fit, size);
1609 if (ret)
1610 ret = -EINVAL;
1611
1612 /*
1613 * U-Boot stopped using unit addressed in 2017. Since libfdt
1614 * can match nodes ignoring any unit address, signature
1615 * verification can see the wrong node if one is inserted with
1616 * the same name as a valid node but with a unit address
1617 * attached. Protect against this by disallowing unit addresses.
1618 */
1619 if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
1620 ret = fdt_check_no_at(fit, 0);
1621
1622 if (ret) {
1623 debug("FIT check error %d\n", ret);
1624 return ret;
1625 }
1626 }
1627 if (ret) {
1628 debug("FIT check error %d\n", ret);
1629 return ret;
1630 }
1631 }
1632
1633 /* mandatory / node 'description' property */
1634 if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
1635 debug("Wrong FIT format: no description\n");
1636 return -ENOMSG;
1637 }
1638
1639 if (IMAGE_ENABLE_TIMESTAMP) {
1640 /* mandatory / node 'timestamp' property */
1641 if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
1642 debug("Wrong FIT format: no timestamp\n");
1643 return -ENODATA;
1644 }
1645 }
1646
1647 /* mandatory subimages parent '/images' node */
1648 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1649 debug("Wrong FIT format: no images parent node\n");
1650 return -ENOENT;
1651 }
1652
1653 return 0;
1654 }
1655
1656 /**
1657 * fit_conf_find_compat
1658 * @fit: pointer to the FIT format image header
1659 * @fdt: pointer to the device tree to compare against
1660 *
1661 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1662 * most compatible with the passed in device tree.
1663 *
1664 * Example:
1665 *
1666 * / o image-tree
1667 * |-o images
1668 * | |-o fdt-1
1669 * | |-o fdt-2
1670 * |
1671 * |-o configurations
1672 * |-o config-1
1673 * | |-fdt = fdt-1
1674 * |
1675 * |-o config-2
1676 * |-fdt = fdt-2
1677 *
1678 * / o U-Boot fdt
1679 * |-compatible = "foo,bar", "bim,bam"
1680 *
1681 * / o kernel fdt1
1682 * |-compatible = "foo,bar",
1683 *
1684 * / o kernel fdt2
1685 * |-compatible = "bim,bam", "baz,biz"
1686 *
1687 * Configuration 1 would be picked because the first string in U-Boot's
1688 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1689 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1690 *
1691 * As an optimization, the compatible property from the FDT's root node can be
1692 * copied into the configuration node in the FIT image. This is required to
1693 * match configurations with compressed FDTs.
1694 *
1695 * returns:
1696 * offset to the configuration to use if one was found
1697 * -1 otherwise
1698 */
fit_conf_find_compat(const void * fit,const void * fdt)1699 int fit_conf_find_compat(const void *fit, const void *fdt)
1700 {
1701 int ndepth = 0;
1702 int noffset, confs_noffset, images_noffset;
1703 const void *fdt_compat;
1704 int fdt_compat_len;
1705 int best_match_offset = 0;
1706 int best_match_pos = 0;
1707
1708 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1709 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1710 if (confs_noffset < 0 || images_noffset < 0) {
1711 debug("Can't find configurations or images nodes.\n");
1712 return -1;
1713 }
1714
1715 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1716 if (!fdt_compat) {
1717 debug("Fdt for comparison has no \"compatible\" property.\n");
1718 return -1;
1719 }
1720
1721 /*
1722 * Loop over the configurations in the FIT image.
1723 */
1724 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1725 (noffset >= 0) && (ndepth > 0);
1726 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1727 const void *fdt;
1728 const char *kfdt_name;
1729 int kfdt_noffset, compat_noffset;
1730 const char *cur_fdt_compat;
1731 int len;
1732 size_t sz;
1733 int i;
1734
1735 if (ndepth > 1)
1736 continue;
1737
1738 /* If there's a compat property in the config node, use that. */
1739 if (fdt_getprop(fit, noffset, "compatible", NULL)) {
1740 fdt = fit; /* search in FIT image */
1741 compat_noffset = noffset; /* search under config node */
1742 } else { /* Otherwise extract it from the kernel FDT. */
1743 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1744 if (!kfdt_name) {
1745 debug("No fdt property found.\n");
1746 continue;
1747 }
1748 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1749 kfdt_name);
1750 if (kfdt_noffset < 0) {
1751 debug("No image node named \"%s\" found.\n",
1752 kfdt_name);
1753 continue;
1754 }
1755
1756 if (!fit_image_check_comp(fit, kfdt_noffset,
1757 IH_COMP_NONE)) {
1758 debug("Can't extract compat from \"%s\" "
1759 "(compressed)\n", kfdt_name);
1760 continue;
1761 }
1762
1763 /* search in this config's kernel FDT */
1764 if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
1765 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1766 continue;
1767 }
1768
1769 compat_noffset = 0; /* search kFDT under root node */
1770 }
1771
1772 len = fdt_compat_len;
1773 cur_fdt_compat = fdt_compat;
1774 /*
1775 * Look for a match for each U-Boot compatibility string in
1776 * turn in the compat string property.
1777 */
1778 for (i = 0; len > 0 &&
1779 (!best_match_offset || best_match_pos > i); i++) {
1780 int cur_len = strlen(cur_fdt_compat) + 1;
1781
1782 if (!fdt_node_check_compatible(fdt, compat_noffset,
1783 cur_fdt_compat)) {
1784 best_match_offset = noffset;
1785 best_match_pos = i;
1786 break;
1787 }
1788 len -= cur_len;
1789 cur_fdt_compat += cur_len;
1790 }
1791 }
1792 if (!best_match_offset) {
1793 debug("No match found.\n");
1794 return -1;
1795 }
1796
1797 return best_match_offset;
1798 }
1799
fit_conf_get_node(const void * fit,const char * conf_uname)1800 int fit_conf_get_node(const void *fit, const char *conf_uname)
1801 {
1802 int noffset, confs_noffset;
1803 int len;
1804 const char *s;
1805 char *conf_uname_copy = NULL;
1806
1807 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1808 if (confs_noffset < 0) {
1809 debug("Can't find configurations parent node '%s' (%s)\n",
1810 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1811 return confs_noffset;
1812 }
1813
1814 if (conf_uname == NULL) {
1815 /* get configuration unit name from the default property */
1816 debug("No configuration specified, trying default...\n");
1817 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1818 FIT_DEFAULT_PROP, &len);
1819 if (conf_uname == NULL) {
1820 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1821 len);
1822 return len;
1823 }
1824 debug("Found default configuration: '%s'\n", conf_uname);
1825 }
1826
1827 s = strchr(conf_uname, '#');
1828 if (s) {
1829 len = s - conf_uname;
1830 conf_uname_copy = malloc(len + 1);
1831 if (!conf_uname_copy) {
1832 debug("Can't allocate uname copy: '%s'\n",
1833 conf_uname);
1834 return -ENOMEM;
1835 }
1836 memcpy(conf_uname_copy, conf_uname, len);
1837 conf_uname_copy[len] = '\0';
1838 conf_uname = conf_uname_copy;
1839 }
1840
1841 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1842 if (noffset < 0) {
1843 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1844 conf_uname, fdt_strerror(noffset));
1845 }
1846
1847 if (conf_uname_copy)
1848 free(conf_uname_copy);
1849
1850 return noffset;
1851 }
1852
fit_conf_get_prop_node_count(const void * fit,int noffset,const char * prop_name)1853 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1854 const char *prop_name)
1855 {
1856 return fdt_stringlist_count(fit, noffset, prop_name);
1857 }
1858
fit_conf_get_prop_node_index(const void * fit,int noffset,const char * prop_name,int index)1859 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1860 const char *prop_name, int index)
1861 {
1862 const char *uname;
1863 int len;
1864
1865 /* get kernel image unit name from configuration kernel property */
1866 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
1867 if (uname == NULL)
1868 return len;
1869
1870 return fit_image_get_node(fit, uname);
1871 }
1872
fit_conf_get_prop_node(const void * fit,int noffset,const char * prop_name)1873 int fit_conf_get_prop_node(const void *fit, int noffset,
1874 const char *prop_name)
1875 {
1876 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
1877 }
1878
fit_image_select(const void * fit,int rd_noffset,int verify)1879 static int fit_image_select(const void *fit, int rd_noffset, int verify)
1880 {
1881 fit_image_print(fit, rd_noffset, " ");
1882
1883 if (verify) {
1884 puts(" Verifying Hash Integrity ... ");
1885 if (!fit_image_verify(fit, rd_noffset)) {
1886 puts("Bad Data Hash\n");
1887 return -EACCES;
1888 }
1889 puts("OK\n");
1890 }
1891
1892 return 0;
1893 }
1894
fit_get_node_from_config(bootm_headers_t * images,const char * prop_name,ulong addr)1895 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1896 ulong addr)
1897 {
1898 int cfg_noffset;
1899 void *fit_hdr;
1900 int noffset;
1901
1902 debug("* %s: using config '%s' from image at 0x%08lx\n",
1903 prop_name, images->fit_uname_cfg, addr);
1904
1905 /* Check whether configuration has this property defined */
1906 fit_hdr = map_sysmem(addr, 0);
1907 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1908 if (cfg_noffset < 0) {
1909 debug("* %s: no such config\n", prop_name);
1910 return -EINVAL;
1911 }
1912
1913 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1914 if (noffset < 0) {
1915 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1916 return -ENOENT;
1917 }
1918
1919 return noffset;
1920 }
1921
1922 /**
1923 * fit_get_image_type_property() - get property name for IH_TYPE_...
1924 *
1925 * @return the properly name where we expect to find the image in the
1926 * config node
1927 */
fit_get_image_type_property(int type)1928 static const char *fit_get_image_type_property(int type)
1929 {
1930 /*
1931 * This is sort-of available in the uimage_type[] table in image.c
1932 * but we don't have access to the short name, and "fdt" is different
1933 * anyway. So let's just keep it here.
1934 */
1935 switch (type) {
1936 case IH_TYPE_FLATDT:
1937 return FIT_FDT_PROP;
1938 case IH_TYPE_KERNEL:
1939 return FIT_KERNEL_PROP;
1940 case IH_TYPE_RAMDISK:
1941 return FIT_RAMDISK_PROP;
1942 case IH_TYPE_X86_SETUP:
1943 return FIT_SETUP_PROP;
1944 case IH_TYPE_LOADABLE:
1945 return FIT_LOADABLE_PROP;
1946 case IH_TYPE_FPGA:
1947 return FIT_FPGA_PROP;
1948 case IH_TYPE_STANDALONE:
1949 return FIT_STANDALONE_PROP;
1950 }
1951
1952 return "unknown";
1953 }
1954
fit_image_load(bootm_headers_t * images,ulong addr,const char ** fit_unamep,const char ** fit_uname_configp,int arch,int image_type,int bootstage_id,enum fit_load_op load_op,ulong * datap,ulong * lenp)1955 int fit_image_load(bootm_headers_t *images, ulong addr,
1956 const char **fit_unamep, const char **fit_uname_configp,
1957 int arch, int image_type, int bootstage_id,
1958 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1959 {
1960 int cfg_noffset, noffset;
1961 const char *fit_uname;
1962 const char *fit_uname_config;
1963 const char *fit_base_uname_config;
1964 const void *fit;
1965 void *buf;
1966 void *loadbuf;
1967 size_t size;
1968 int type_ok, os_ok;
1969 ulong load, load_end, data, len;
1970 uint8_t os, comp;
1971 #ifndef USE_HOSTCC
1972 uint8_t os_arch;
1973 #endif
1974 const char *prop_name;
1975 int ret;
1976
1977 fit = map_sysmem(addr, 0);
1978 fit_uname = fit_unamep ? *fit_unamep : NULL;
1979 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
1980 fit_base_uname_config = NULL;
1981 prop_name = fit_get_image_type_property(image_type);
1982 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1983
1984 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1985 ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
1986 if (ret) {
1987 printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
1988 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
1989 printf("Signature checking prevents use of unit addresses (@) in nodes\n");
1990 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1991 return ret;
1992 }
1993 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1994 if (fit_uname) {
1995 /* get FIT component image node offset */
1996 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1997 noffset = fit_image_get_node(fit, fit_uname);
1998 } else {
1999 /*
2000 * no image node unit name, try to get config
2001 * node first. If config unit node name is NULL
2002 * fit_conf_get_node() will try to find default config node
2003 */
2004 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
2005 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
2006 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
2007 } else {
2008 cfg_noffset = fit_conf_get_node(fit,
2009 fit_uname_config);
2010 }
2011 if (cfg_noffset < 0) {
2012 puts("Could not find configuration node\n");
2013 bootstage_error(bootstage_id +
2014 BOOTSTAGE_SUB_NO_UNIT_NAME);
2015 return -ENOENT;
2016 }
2017
2018 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
2019 printf(" Using '%s' configuration\n", fit_base_uname_config);
2020 /* Remember this config */
2021 if (image_type == IH_TYPE_KERNEL)
2022 images->fit_uname_cfg = fit_base_uname_config;
2023
2024 if (IMAGE_ENABLE_VERIFY && images->verify) {
2025 puts(" Verifying Hash Integrity ... ");
2026 if (fit_config_verify(fit, cfg_noffset)) {
2027 puts("Bad Data Hash\n");
2028 bootstage_error(bootstage_id +
2029 BOOTSTAGE_SUB_HASH);
2030 return -EACCES;
2031 }
2032 puts("OK\n");
2033 }
2034
2035 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
2036
2037 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
2038 prop_name);
2039 fit_uname = fit_get_name(fit, noffset, NULL);
2040 }
2041 if (noffset < 0) {
2042 printf("Could not find subimage node type '%s'\n", prop_name);
2043 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
2044 return -ENOENT;
2045 }
2046
2047 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
2048
2049 ret = fit_image_select(fit, noffset, images->verify);
2050 if (ret) {
2051 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
2052 return ret;
2053 }
2054
2055 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2056 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
2057 if (!fit_image_check_target_arch(fit, noffset)) {
2058 puts("Unsupported Architecture\n");
2059 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
2060 return -ENOEXEC;
2061 }
2062 #endif
2063
2064 #ifndef USE_HOSTCC
2065 fit_image_get_arch(fit, noffset, &os_arch);
2066 images->os.arch = os_arch;
2067 #endif
2068
2069 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2070 type_ok = fit_image_check_type(fit, noffset, image_type) ||
2071 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
2072 (image_type == IH_TYPE_KERNEL &&
2073 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
2074
2075 os_ok = image_type == IH_TYPE_FLATDT ||
2076 image_type == IH_TYPE_FPGA ||
2077 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
2078 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
2079 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
2080
2081 /*
2082 * If either of the checks fail, we should report an error, but
2083 * if the image type is coming from the "loadables" field, we
2084 * don't care what it is
2085 */
2086 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
2087 fit_image_get_os(fit, noffset, &os);
2088 printf("No %s %s %s Image\n",
2089 genimg_get_os_name(os),
2090 genimg_get_arch_name(arch),
2091 genimg_get_type_name(image_type));
2092 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
2093 return -EIO;
2094 }
2095
2096 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
2097
2098 /* get image data address and length */
2099 if (fit_image_get_data_and_size(fit, noffset,
2100 (const void **)&buf, &size)) {
2101 printf("Could not find %s subimage data!\n", prop_name);
2102 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2103 return -ENOENT;
2104 }
2105
2106 #ifdef CONFIG_FIT_CIPHER
2107 /* Decrypt data before uncompress/move */
2108 if (IMAGE_ENABLE_DECRYPT) {
2109 puts(" Decrypting Data ... ");
2110 if (fit_image_uncipher(fit, noffset, &buf, &size)) {
2111 puts("Error\n");
2112 return -EACCES;
2113 }
2114 puts("OK\n");
2115 }
2116 #endif
2117
2118 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
2119 /* perform any post-processing on the image data */
2120 board_fit_image_post_process(&buf, &size);
2121 #endif
2122
2123 len = (ulong)size;
2124
2125 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
2126
2127 data = map_to_sysmem(buf);
2128 load = data;
2129 if (load_op == FIT_LOAD_IGNORED) {
2130 /* Don't load */
2131 } else if (fit_image_get_load(fit, noffset, &load)) {
2132 if (load_op == FIT_LOAD_REQUIRED) {
2133 printf("Can't get %s subimage load address!\n",
2134 prop_name);
2135 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
2136 return -EBADF;
2137 }
2138 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
2139 ulong image_start, image_end;
2140
2141 /*
2142 * move image data to the load address,
2143 * make sure we don't overwrite initial image
2144 */
2145 image_start = addr;
2146 image_end = addr + fit_get_size(fit);
2147
2148 load_end = load + len;
2149 if (image_type != IH_TYPE_KERNEL &&
2150 load < image_end && load_end > image_start) {
2151 printf("Error: %s overwritten\n", prop_name);
2152 return -EXDEV;
2153 }
2154
2155 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
2156 prop_name, data, load);
2157 } else {
2158 load = data; /* No load address specified */
2159 }
2160
2161 comp = IH_COMP_NONE;
2162 loadbuf = buf;
2163 /* Kernel images get decompressed later in bootm_load_os(). */
2164 if (!fit_image_get_comp(fit, noffset, &comp) &&
2165 comp != IH_COMP_NONE &&
2166 !(image_type == IH_TYPE_KERNEL ||
2167 image_type == IH_TYPE_KERNEL_NOLOAD ||
2168 image_type == IH_TYPE_RAMDISK)) {
2169 ulong max_decomp_len = len * 20;
2170 if (load == data) {
2171 loadbuf = malloc(max_decomp_len);
2172 load = map_to_sysmem(loadbuf);
2173 } else {
2174 loadbuf = map_sysmem(load, max_decomp_len);
2175 }
2176 if (image_decomp(comp, load, data, image_type,
2177 loadbuf, buf, len, max_decomp_len, &load_end)) {
2178 printf("Error decompressing %s\n", prop_name);
2179
2180 return -ENOEXEC;
2181 }
2182 len = load_end - load;
2183 } else if (load != data) {
2184 loadbuf = map_sysmem(load, len);
2185 memcpy(loadbuf, buf, len);
2186 }
2187
2188 if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
2189 puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
2190 " please fix your .its file!\n");
2191
2192 /* verify that image data is a proper FDT blob */
2193 if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
2194 puts("Subimage data is not a FDT");
2195 return -ENOEXEC;
2196 }
2197
2198 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
2199
2200 *datap = load;
2201 *lenp = len;
2202 if (fit_unamep)
2203 *fit_unamep = (char *)fit_uname;
2204 if (fit_uname_configp)
2205 *fit_uname_configp = (char *)(fit_uname_config ? :
2206 fit_base_uname_config);
2207
2208 return noffset;
2209 }
2210
boot_get_setup_fit(bootm_headers_t * images,uint8_t arch,ulong * setup_start,ulong * setup_len)2211 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
2212 ulong *setup_start, ulong *setup_len)
2213 {
2214 int noffset;
2215 ulong addr;
2216 ulong len;
2217 int ret;
2218
2219 addr = map_to_sysmem(images->fit_hdr_os);
2220 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
2221 if (noffset < 0)
2222 return noffset;
2223
2224 ret = fit_image_load(images, addr, NULL, NULL, arch,
2225 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
2226 FIT_LOAD_REQUIRED, setup_start, &len);
2227
2228 return ret;
2229 }
2230
2231 #ifndef USE_HOSTCC
boot_get_fdt_fit(bootm_headers_t * images,ulong addr,const char ** fit_unamep,const char ** fit_uname_configp,int arch,ulong * datap,ulong * lenp)2232 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
2233 const char **fit_unamep, const char **fit_uname_configp,
2234 int arch, ulong *datap, ulong *lenp)
2235 {
2236 int fdt_noffset, cfg_noffset, count;
2237 const void *fit;
2238 const char *fit_uname = NULL;
2239 const char *fit_uname_config = NULL;
2240 char *fit_uname_config_copy = NULL;
2241 char *next_config = NULL;
2242 ulong load, len;
2243 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2244 ulong image_start, image_end;
2245 ulong ovload, ovlen;
2246 const char *uconfig;
2247 const char *uname;
2248 void *base, *ov;
2249 int i, err, noffset, ov_noffset;
2250 #endif
2251
2252 fit_uname = fit_unamep ? *fit_unamep : NULL;
2253
2254 if (fit_uname_configp && *fit_uname_configp) {
2255 fit_uname_config_copy = strdup(*fit_uname_configp);
2256 if (!fit_uname_config_copy)
2257 return -ENOMEM;
2258
2259 next_config = strchr(fit_uname_config_copy, '#');
2260 if (next_config)
2261 *next_config++ = '\0';
2262 if (next_config - 1 > fit_uname_config_copy)
2263 fit_uname_config = fit_uname_config_copy;
2264 }
2265
2266 fdt_noffset = fit_image_load(images,
2267 addr, &fit_uname, &fit_uname_config,
2268 arch, IH_TYPE_FLATDT,
2269 BOOTSTAGE_ID_FIT_FDT_START,
2270 FIT_LOAD_OPTIONAL, &load, &len);
2271
2272 if (fdt_noffset < 0)
2273 goto out;
2274
2275 debug("fit_uname=%s, fit_uname_config=%s\n",
2276 fit_uname ? fit_uname : "<NULL>",
2277 fit_uname_config ? fit_uname_config : "<NULL>");
2278
2279 fit = map_sysmem(addr, 0);
2280
2281 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
2282
2283 /* single blob, or error just return as well */
2284 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
2285 if (count <= 1 && !next_config)
2286 goto out;
2287
2288 /* we need to apply overlays */
2289
2290 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2291 image_start = addr;
2292 image_end = addr + fit_get_size(fit);
2293 /* verify that relocation took place by load address not being in fit */
2294 if (load >= image_start && load < image_end) {
2295 /* check is simplified; fit load checks for overlaps */
2296 printf("Overlayed FDT requires relocation\n");
2297 fdt_noffset = -EBADF;
2298 goto out;
2299 }
2300
2301 base = map_sysmem(load, len);
2302
2303 /* apply extra configs in FIT first, followed by args */
2304 for (i = 1; ; i++) {
2305 if (i < count) {
2306 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
2307 FIT_FDT_PROP, i);
2308 uname = fit_get_name(fit, noffset, NULL);
2309 uconfig = NULL;
2310 } else {
2311 if (!next_config)
2312 break;
2313 uconfig = next_config;
2314 next_config = strchr(next_config, '#');
2315 if (next_config)
2316 *next_config++ = '\0';
2317 uname = NULL;
2318
2319 /*
2320 * fit_image_load() would load the first FDT from the
2321 * extra config only when uconfig is specified.
2322 * Check if the extra config contains multiple FDTs and
2323 * if so, load them.
2324 */
2325 cfg_noffset = fit_conf_get_node(fit, uconfig);
2326
2327 i = 0;
2328 count = fit_conf_get_prop_node_count(fit, cfg_noffset,
2329 FIT_FDT_PROP);
2330 }
2331
2332 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
2333
2334 ov_noffset = fit_image_load(images,
2335 addr, &uname, &uconfig,
2336 arch, IH_TYPE_FLATDT,
2337 BOOTSTAGE_ID_FIT_FDT_START,
2338 FIT_LOAD_REQUIRED, &ovload, &ovlen);
2339 if (ov_noffset < 0) {
2340 printf("load of %s failed\n", uname);
2341 continue;
2342 }
2343 debug("%s loaded at 0x%08lx len=0x%08lx\n",
2344 uname, ovload, ovlen);
2345 ov = map_sysmem(ovload, ovlen);
2346
2347 base = map_sysmem(load, len + ovlen);
2348 err = fdt_open_into(base, base, len + ovlen);
2349 if (err < 0) {
2350 printf("failed on fdt_open_into\n");
2351 fdt_noffset = err;
2352 goto out;
2353 }
2354 /* the verbose method prints out messages on error */
2355 err = fdt_overlay_apply_verbose(base, ov);
2356 if (err < 0) {
2357 fdt_noffset = err;
2358 goto out;
2359 }
2360 fdt_pack(base);
2361 len = fdt_totalsize(base);
2362 }
2363 #else
2364 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
2365 fdt_noffset = -EBADF;
2366 #endif
2367
2368 out:
2369 if (datap)
2370 *datap = load;
2371 if (lenp)
2372 *lenp = len;
2373 if (fit_unamep)
2374 *fit_unamep = fit_uname;
2375 if (fit_uname_configp)
2376 *fit_uname_configp = fit_uname_config;
2377
2378 if (fit_uname_config_copy)
2379 free(fit_uname_config_copy);
2380 return fdt_noffset;
2381 }
2382 #endif
2383