1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __IMAGE_H__
16#define __IMAGE_H__
17
18#include "compiler.h"
19#include <asm/byteorder.h>
20#include <stdbool.h>
21
22
23struct lmb;
24struct fdt_region;
25
26#ifdef USE_HOSTCC
27#include <sys/types.h>
28
29
30#define IMAGE_ENABLE_FIT 1
31#define IMAGE_ENABLE_OF_LIBFDT 1
32#define CONFIG_FIT_VERBOSE 1
33#define CONFIG_FIT_RSASSA_PSS 1
34#define CONFIG_MD5
35#define CONFIG_SHA1
36#define CONFIG_SHA256
37#define CONFIG_SHA384
38#define CONFIG_SHA512
39
40#define IMAGE_ENABLE_IGNORE 0
41#define IMAGE_INDENT_STRING ""
42
43#else
44
45#include <lmb.h>
46#include <asm/u-boot.h>
47#include <command.h>
48#include <linker_lists.h>
49
50
51#define IMAGE_ENABLE_IGNORE 1
52#define IMAGE_INDENT_STRING " "
53
54#define IMAGE_ENABLE_FIT CONFIG_IS_ENABLED(FIT)
55#define IMAGE_ENABLE_OF_LIBFDT CONFIG_IS_ENABLED(OF_LIBFDT)
56
57#endif
58
59#if IMAGE_ENABLE_FIT
60#include <hash.h>
61#include <linux/libfdt.h>
62#include <fdt_support.h>
63#endif
64
65#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
66# define IMAGE_BOOT_GET_CMDLINE 1
67#else
68# define IMAGE_BOOT_GET_CMDLINE 0
69#endif
70
71#ifdef CONFIG_OF_BOARD_SETUP
72# define IMAGE_OF_BOARD_SETUP 1
73#else
74# define IMAGE_OF_BOARD_SETUP 0
75#endif
76
77#ifdef CONFIG_OF_SYSTEM_SETUP
78# define IMAGE_OF_SYSTEM_SETUP 1
79#else
80# define IMAGE_OF_SYSTEM_SETUP 0
81#endif
82
83extern ulong image_load_addr;
84extern ulong image_save_addr;
85extern ulong image_save_size;
86
87
88#define IMAGE_SIZE_INVAL (-1UL)
89
90enum ih_category {
91 IH_ARCH,
92 IH_COMP,
93 IH_OS,
94 IH_TYPE,
95
96 IH_COUNT,
97};
98
99
100
101
102
103
104
105
106enum {
107 IH_OS_INVALID = 0,
108 IH_OS_OPENBSD,
109 IH_OS_NETBSD,
110 IH_OS_FREEBSD,
111 IH_OS_4_4BSD,
112 IH_OS_LINUX,
113 IH_OS_SVR4,
114 IH_OS_ESIX,
115 IH_OS_SOLARIS,
116 IH_OS_IRIX,
117 IH_OS_SCO,
118 IH_OS_DELL,
119 IH_OS_NCR,
120 IH_OS_LYNXOS,
121 IH_OS_VXWORKS,
122 IH_OS_PSOS,
123 IH_OS_QNX,
124 IH_OS_U_BOOT,
125 IH_OS_RTEMS,
126 IH_OS_ARTOS,
127 IH_OS_UNITY,
128 IH_OS_INTEGRITY,
129 IH_OS_OSE,
130 IH_OS_PLAN9,
131 IH_OS_OPENRTOS,
132 IH_OS_ARM_TRUSTED_FIRMWARE,
133 IH_OS_TEE,
134 IH_OS_OPENSBI,
135 IH_OS_EFI,
136
137 IH_OS_COUNT,
138};
139
140
141
142
143
144
145
146
147enum {
148 IH_ARCH_INVALID = 0,
149 IH_ARCH_ALPHA,
150 IH_ARCH_ARM,
151 IH_ARCH_I386,
152 IH_ARCH_IA64,
153 IH_ARCH_MIPS,
154 IH_ARCH_MIPS64,
155 IH_ARCH_PPC,
156 IH_ARCH_S390,
157 IH_ARCH_SH,
158 IH_ARCH_SPARC,
159 IH_ARCH_SPARC64,
160 IH_ARCH_M68K,
161 IH_ARCH_NIOS,
162 IH_ARCH_MICROBLAZE,
163 IH_ARCH_NIOS2,
164 IH_ARCH_BLACKFIN,
165 IH_ARCH_AVR32,
166 IH_ARCH_ST200,
167 IH_ARCH_SANDBOX,
168 IH_ARCH_NDS32,
169 IH_ARCH_OPENRISC,
170 IH_ARCH_ARM64,
171 IH_ARCH_ARC,
172 IH_ARCH_X86_64,
173 IH_ARCH_XTENSA,
174 IH_ARCH_RISCV,
175
176 IH_ARCH_COUNT,
177};
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222enum {
223 IH_TYPE_INVALID = 0,
224 IH_TYPE_STANDALONE,
225 IH_TYPE_KERNEL,
226 IH_TYPE_RAMDISK,
227 IH_TYPE_MULTI,
228 IH_TYPE_FIRMWARE,
229 IH_TYPE_SCRIPT,
230 IH_TYPE_FILESYSTEM,
231 IH_TYPE_FLATDT,
232 IH_TYPE_KWBIMAGE,
233 IH_TYPE_IMXIMAGE,
234 IH_TYPE_UBLIMAGE,
235 IH_TYPE_OMAPIMAGE,
236 IH_TYPE_AISIMAGE,
237
238 IH_TYPE_KERNEL_NOLOAD,
239 IH_TYPE_PBLIMAGE,
240 IH_TYPE_MXSIMAGE,
241 IH_TYPE_GPIMAGE,
242 IH_TYPE_ATMELIMAGE,
243 IH_TYPE_SOCFPGAIMAGE,
244 IH_TYPE_X86_SETUP,
245 IH_TYPE_LPC32XXIMAGE,
246 IH_TYPE_LOADABLE,
247 IH_TYPE_RKIMAGE,
248 IH_TYPE_RKSD,
249 IH_TYPE_RKSPI,
250 IH_TYPE_ZYNQIMAGE,
251 IH_TYPE_ZYNQMPIMAGE,
252 IH_TYPE_ZYNQMPBIF,
253 IH_TYPE_FPGA,
254 IH_TYPE_VYBRIDIMAGE,
255 IH_TYPE_TEE,
256 IH_TYPE_FIRMWARE_IVT,
257 IH_TYPE_PMMC,
258 IH_TYPE_STM32IMAGE,
259 IH_TYPE_SOCFPGAIMAGE_V1,
260 IH_TYPE_MTKIMAGE,
261 IH_TYPE_IMX8MIMAGE,
262 IH_TYPE_IMX8IMAGE,
263 IH_TYPE_COPRO,
264 IH_TYPE_SUNXI_EGON,
265
266 IH_TYPE_COUNT,
267};
268
269
270
271
272
273
274
275
276enum {
277 IH_COMP_NONE = 0,
278 IH_COMP_GZIP,
279 IH_COMP_BZIP2,
280 IH_COMP_LZMA,
281 IH_COMP_LZO,
282 IH_COMP_LZ4,
283 IH_COMP_ZSTD,
284
285 IH_COMP_COUNT,
286};
287
288#define LZ4F_MAGIC 0x184D2204
289#define IH_MAGIC 0x27051956
290#define IH_NMLEN 32
291
292
293#define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
294
295
296
297
298
299typedef struct image_header {
300 uint32_t ih_magic;
301 uint32_t ih_hcrc;
302 uint32_t ih_time;
303 uint32_t ih_size;
304 uint32_t ih_load;
305 uint32_t ih_ep;
306 uint32_t ih_dcrc;
307 uint8_t ih_os;
308 uint8_t ih_arch;
309 uint8_t ih_type;
310 uint8_t ih_comp;
311 uint8_t ih_name[IH_NMLEN];
312} image_header_t;
313
314typedef struct image_info {
315 ulong start, end;
316 ulong image_start, image_len;
317 ulong load;
318 uint8_t comp, type, os;
319 uint8_t arch;
320} image_info_t;
321
322
323
324
325
326typedef struct bootm_headers {
327
328
329
330
331
332 image_header_t *legacy_hdr_os;
333 image_header_t legacy_hdr_os_copy;
334 ulong legacy_hdr_valid;
335
336#if IMAGE_ENABLE_FIT
337 const char *fit_uname_cfg;
338
339 void *fit_hdr_os;
340 const char *fit_uname_os;
341 int fit_noffset_os;
342
343 void *fit_hdr_rd;
344 const char *fit_uname_rd;
345 int fit_noffset_rd;
346
347 void *fit_hdr_fdt;
348 const char *fit_uname_fdt;
349 int fit_noffset_fdt;
350
351 void *fit_hdr_setup;
352 const char *fit_uname_setup;
353 int fit_noffset_setup;
354#endif
355
356#ifndef USE_HOSTCC
357 image_info_t os;
358 ulong ep;
359
360 ulong rd_start, rd_end;
361
362 char *ft_addr;
363 ulong ft_len;
364
365 ulong initrd_start;
366 ulong initrd_end;
367 ulong cmdline_start;
368 ulong cmdline_end;
369 struct bd_info *kbd;
370#endif
371
372 int verify;
373
374#define BOOTM_STATE_START (0x00000001)
375#define BOOTM_STATE_FINDOS (0x00000002)
376#define BOOTM_STATE_FINDOTHER (0x00000004)
377#define BOOTM_STATE_LOADOS (0x00000008)
378#define BOOTM_STATE_RAMDISK (0x00000010)
379#define BOOTM_STATE_FDT (0x00000020)
380#define BOOTM_STATE_OS_CMDLINE (0x00000040)
381#define BOOTM_STATE_OS_BD_T (0x00000080)
382#define BOOTM_STATE_OS_PREP (0x00000100)
383#define BOOTM_STATE_OS_FAKE_GO (0x00000200)
384#define BOOTM_STATE_OS_GO (0x00000400)
385 int state;
386
387#if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
388 struct lmb lmb;
389#endif
390} bootm_headers_t;
391
392extern bootm_headers_t images;
393
394
395
396
397
398
399#ifndef CHUNKSZ
400#define CHUNKSZ (64 * 1024)
401#endif
402
403#ifndef CHUNKSZ_CRC32
404#define CHUNKSZ_CRC32 (64 * 1024)
405#endif
406
407#ifndef CHUNKSZ_MD5
408#define CHUNKSZ_MD5 (64 * 1024)
409#endif
410
411#ifndef CHUNKSZ_SHA1
412#define CHUNKSZ_SHA1 (64 * 1024)
413#endif
414
415#define uimage_to_cpu(x) be32_to_cpu(x)
416#define cpu_to_uimage(x) cpu_to_be32(x)
417
418
419
420
421
422typedef struct table_entry {
423 int id;
424 char *sname;
425 char *lname;
426} table_entry_t;
427
428
429
430
431struct comp_magic_map {
432 int comp_id;
433 const char *name;
434 unsigned char magic[2];
435};
436
437
438
439
440
441
442int get_table_entry_id(const table_entry_t *table,
443 const char *table_name, const char *name);
444
445
446
447
448
449char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
450
451const char *genimg_get_os_name(uint8_t os);
452
453
454
455
456
457
458
459const char *genimg_get_os_short_name(uint8_t comp);
460
461const char *genimg_get_arch_name(uint8_t arch);
462
463
464
465
466
467
468
469const char *genimg_get_arch_short_name(uint8_t arch);
470
471const char *genimg_get_type_name(uint8_t type);
472
473
474
475
476
477
478
479const char *genimg_get_type_short_name(uint8_t type);
480
481const char *genimg_get_comp_name(uint8_t comp);
482
483
484
485
486
487
488
489const char *genimg_get_comp_short_name(uint8_t comp);
490
491
492
493
494
495
496
497
498const char *genimg_get_cat_name(enum ih_category category, uint id);
499
500
501
502
503
504
505
506
507const char *genimg_get_cat_short_name(enum ih_category category, uint id);
508
509
510
511
512
513
514
515int genimg_get_cat_count(enum ih_category category);
516
517
518
519
520
521
522
523
524const char *genimg_get_cat_desc(enum ih_category category);
525
526
527
528
529
530
531
532
533bool genimg_cat_has_id(enum ih_category category, uint id);
534
535int genimg_get_os_id(const char *name);
536int genimg_get_arch_id(const char *name);
537int genimg_get_type_id(const char *name);
538int genimg_get_comp_id(const char *name);
539void genimg_print_size(uint32_t size);
540
541#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
542 defined(USE_HOSTCC)
543#define IMAGE_ENABLE_TIMESTAMP 1
544#else
545#define IMAGE_ENABLE_TIMESTAMP 0
546#endif
547void genimg_print_time(time_t timestamp);
548
549
550enum fit_load_op {
551 FIT_LOAD_IGNORED,
552 FIT_LOAD_OPTIONAL,
553 FIT_LOAD_OPTIONAL_NON_ZERO,
554 FIT_LOAD_REQUIRED,
555};
556
557int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
558 ulong *setup_len);
559
560#ifndef USE_HOSTCC
561
562#define IMAGE_FORMAT_INVALID 0x00
563#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
564#define IMAGE_FORMAT_LEGACY 0x01
565#endif
566#define IMAGE_FORMAT_FIT 0x02
567#define IMAGE_FORMAT_ANDROID 0x03
568
569ulong genimg_get_kernel_addr_fit(char * const img_addr,
570 const char **fit_uname_config,
571 const char **fit_uname_kernel);
572ulong genimg_get_kernel_addr(char * const img_addr);
573int genimg_get_format(const void *img_addr);
574int genimg_has_config(bootm_headers_t *images);
575
576int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
577 uint8_t arch, const ulong *ld_start, ulong * const ld_len);
578int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
579 uint8_t arch, ulong *rd_start, ulong *rd_end);
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
603 uint8_t arch, const ulong *ld_start, ulong *const ld_len);
604#endif
605
606int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
607 ulong *setup_start, ulong *setup_len);
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
631 const char **fit_unamep, const char **fit_uname_configp,
632 int arch, ulong *datap, ulong *lenp);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664int fit_image_load(bootm_headers_t *images, ulong addr,
665 const char **fit_unamep, const char **fit_uname_configp,
666 int arch, int image_type, int bootstage_id,
667 enum fit_load_op load_op, ulong *datap, ulong *lenp);
668
669
670
671
672
673
674
675
676
677
678
679int image_source_script(ulong addr, const char *fit_uname);
680
681#ifndef USE_HOSTCC
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
710 ulong addr);
711
712int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
713 bootm_headers_t *images,
714 char **of_flat_tree, ulong *of_size);
715void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
716int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
717
718int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
719 ulong *initrd_start, ulong *initrd_end);
720int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
721#ifdef CONFIG_SYS_BOOT_GET_KBD
722int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd);
723#endif
724#endif
725
726
727
728
729static inline uint32_t image_get_header_size(void)
730{
731 return (sizeof(image_header_t));
732}
733
734#define image_get_hdr_l(f) \
735 static inline uint32_t image_get_##f(const image_header_t *hdr) \
736 { \
737 return uimage_to_cpu(hdr->ih_##f); \
738 }
739image_get_hdr_l(magic)
740image_get_hdr_l(hcrc)
741image_get_hdr_l(time)
742image_get_hdr_l(size)
743image_get_hdr_l(load)
744image_get_hdr_l(ep)
745image_get_hdr_l(dcrc)
746
747#define image_get_hdr_b(f) \
748 static inline uint8_t image_get_##f(const image_header_t *hdr) \
749 { \
750 return hdr->ih_##f; \
751 }
752image_get_hdr_b(os)
753image_get_hdr_b(arch)
754image_get_hdr_b(type)
755image_get_hdr_b(comp)
756
757static inline char *image_get_name(const image_header_t *hdr)
758{
759 return (char *)hdr->ih_name;
760}
761
762static inline uint32_t image_get_data_size(const image_header_t *hdr)
763{
764 return image_get_size(hdr);
765}
766
767
768
769
770
771
772
773
774
775
776
777
778static inline ulong image_get_data(const image_header_t *hdr)
779{
780 return ((ulong)hdr + image_get_header_size());
781}
782
783static inline uint32_t image_get_image_size(const image_header_t *hdr)
784{
785 return (image_get_size(hdr) + image_get_header_size());
786}
787static inline ulong image_get_image_end(const image_header_t *hdr)
788{
789 return ((ulong)hdr + image_get_image_size(hdr));
790}
791
792#define image_set_hdr_l(f) \
793 static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
794 { \
795 hdr->ih_##f = cpu_to_uimage(val); \
796 }
797image_set_hdr_l(magic)
798image_set_hdr_l(hcrc)
799image_set_hdr_l(time)
800image_set_hdr_l(size)
801image_set_hdr_l(load)
802image_set_hdr_l(ep)
803image_set_hdr_l(dcrc)
804
805#define image_set_hdr_b(f) \
806 static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
807 { \
808 hdr->ih_##f = val; \
809 }
810image_set_hdr_b(os)
811image_set_hdr_b(arch)
812image_set_hdr_b(type)
813image_set_hdr_b(comp)
814
815static inline void image_set_name(image_header_t *hdr, const char *name)
816{
817 strncpy(image_get_name(hdr), name, IH_NMLEN);
818}
819
820int image_check_hcrc(const image_header_t *hdr);
821int image_check_dcrc(const image_header_t *hdr);
822#ifndef USE_HOSTCC
823ulong env_get_bootm_low(void);
824phys_size_t env_get_bootm_size(void);
825phys_size_t env_get_bootm_mapsize(void);
826#endif
827void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
828
829static inline int image_check_magic(const image_header_t *hdr)
830{
831 return (image_get_magic(hdr) == IH_MAGIC);
832}
833static inline int image_check_type(const image_header_t *hdr, uint8_t type)
834{
835 return (image_get_type(hdr) == type);
836}
837static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
838{
839#ifndef USE_HOSTCC
840
841 if (IS_ENABLED(CONFIG_SANDBOX))
842 return true;
843#endif
844 return (image_get_arch(hdr) == arch) ||
845 (image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
846}
847static inline int image_check_os(const image_header_t *hdr, uint8_t os)
848{
849 return (image_get_os(hdr) == os);
850}
851
852ulong image_multi_count(const image_header_t *hdr);
853void image_multi_getimg(const image_header_t *hdr, ulong idx,
854 ulong *data, ulong *len);
855
856void image_print_contents(const void *hdr);
857
858#ifndef USE_HOSTCC
859static inline int image_check_target_arch(const image_header_t *hdr)
860{
861#ifndef IH_ARCH_DEFAULT
862# error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
863#endif
864 return image_check_arch(hdr, IH_ARCH_DEFAULT);
865}
866#endif
867
868
869
870
871
872
873
874
875
876
877
878int image_decomp_type(const unsigned char *buf, ulong len);
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893int image_decomp(int comp, ulong load, ulong image_start, int type,
894 void *load_buf, void *image_buf, ulong image_len,
895 uint unc_len, ulong *load_end);
896
897
898
899
900
901
902
903
904
905
906
907
908int image_setup_libfdt(bootm_headers_t *images, void *blob,
909 int of_size, struct lmb *lmb);
910
911
912
913
914
915
916
917
918
919
920int image_setup_linux(bootm_headers_t *images);
921
922
923
924
925
926
927
928
929
930int bootz_setup(ulong image, ulong *start, ulong *end);
931
932
933
934
935
936
937
938
939
940
941int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
942 bool force_reloc);
943
944
945
946
947
948#define FIT_IMAGES_PATH "/images"
949#define FIT_CONFS_PATH "/configurations"
950
951
952#define FIT_HASH_NODENAME "hash"
953#define FIT_ALGO_PROP "algo"
954#define FIT_VALUE_PROP "value"
955#define FIT_IGNORE_PROP "uboot-ignore"
956#define FIT_SIG_NODENAME "signature"
957#define FIT_KEY_REQUIRED "required"
958#define FIT_KEY_HINT "key-name-hint"
959
960
961#define FIT_CIPHER_NODENAME "cipher"
962#define FIT_ALGO_PROP "algo"
963
964
965#define FIT_DATA_PROP "data"
966#define FIT_DATA_POSITION_PROP "data-position"
967#define FIT_DATA_OFFSET_PROP "data-offset"
968#define FIT_DATA_SIZE_PROP "data-size"
969#define FIT_TIMESTAMP_PROP "timestamp"
970#define FIT_DESC_PROP "description"
971#define FIT_ARCH_PROP "arch"
972#define FIT_TYPE_PROP "type"
973#define FIT_OS_PROP "os"
974#define FIT_COMP_PROP "compression"
975#define FIT_ENTRY_PROP "entry"
976#define FIT_LOAD_PROP "load"
977
978
979#define FIT_KERNEL_PROP "kernel"
980#define FIT_RAMDISK_PROP "ramdisk"
981#define FIT_FDT_PROP "fdt"
982#define FIT_LOADABLE_PROP "loadables"
983#define FIT_DEFAULT_PROP "default"
984#define FIT_SETUP_PROP "setup"
985#define FIT_FPGA_PROP "fpga"
986#define FIT_FIRMWARE_PROP "firmware"
987#define FIT_STANDALONE_PROP "standalone"
988
989#define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
990
991#if IMAGE_ENABLE_FIT
992
993int fit_parse_conf(const char *spec, ulong addr_curr,
994 ulong *addr, const char **conf_name);
995int fit_parse_subimage(const char *spec, ulong addr_curr,
996 ulong *addr, const char **image_name);
997
998int fit_get_subimage_count(const void *fit, int images_noffset);
999void fit_print_contents(const void *fit);
1000void fit_image_print(const void *fit, int noffset, const char *p);
1001
1002
1003
1004
1005
1006
1007
1008
1009static inline ulong fit_get_size(const void *fit)
1010{
1011 return fdt_totalsize(fit);
1012}
1013
1014
1015
1016
1017
1018
1019
1020
1021ulong fit_get_end(const void *fit);
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031static inline const char *fit_get_name(const void *fit_hdr,
1032 int noffset, int *len)
1033{
1034 return fdt_get_name(fit_hdr, noffset, len);
1035}
1036
1037int fit_get_desc(const void *fit, int noffset, char **desc);
1038int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
1039
1040int fit_image_get_node(const void *fit, const char *image_uname);
1041int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
1042int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
1043int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
1044int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
1045int fit_image_get_load(const void *fit, int noffset, ulong *load);
1046int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
1047int fit_image_get_data(const void *fit, int noffset,
1048 const void **data, size_t *size);
1049int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
1050int fit_image_get_data_position(const void *fit, int noffset,
1051 int *data_position);
1052int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
1053int fit_image_get_data_size_unciphered(const void *fit, int noffset,
1054 size_t *data_size);
1055int fit_image_get_data_and_size(const void *fit, int noffset,
1056 const void **data, size_t *size);
1057
1058int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
1059int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1060 int *value_len);
1061
1062int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
1063
1064int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1065 const char *comment, int require_keys,
1066 const char *engine_id, const char *cmdname);
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089int fit_add_verification_data(const char *keydir, const char *keyfile,
1090 void *keydest, void *fit, const char *comment,
1091 int require_keys, const char *engine_id,
1092 const char *cmdname);
1093
1094int fit_image_verify_with_data(const void *fit, int image_noffset,
1095 const void *data, size_t size);
1096int fit_image_verify(const void *fit, int noffset);
1097int fit_config_verify(const void *fit, int conf_noffset);
1098int fit_all_image_verify(const void *fit);
1099int fit_config_decrypt(const void *fit, int conf_noffset);
1100int fit_image_check_os(const void *fit, int noffset, uint8_t os);
1101int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
1102int fit_image_check_type(const void *fit, int noffset, uint8_t type);
1103int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120int fit_check_format(const void *fit, ulong size);
1121
1122int fit_conf_find_compat(const void *fit, const void *fdt);
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142int fit_conf_get_node(const void *fit, const char *conf_uname);
1143
1144int fit_conf_get_prop_node_count(const void *fit, int noffset,
1145 const char *prop_name);
1146int fit_conf_get_prop_node_index(const void *fit, int noffset,
1147 const char *prop_name, int index);
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160int fit_conf_get_prop_node(const void *fit, int noffset,
1161 const char *prop_name);
1162
1163int fit_check_ramdisk(const void *fit, int os_noffset,
1164 uint8_t arch, int verify);
1165#endif
1166
1167int calculate_hash(const void *data, int data_len, const char *algo,
1168 uint8_t *value, int *value_len);
1169
1170
1171
1172
1173
1174#if defined(USE_HOSTCC)
1175# if defined(CONFIG_FIT_SIGNATURE)
1176# define IMAGE_ENABLE_SIGN 1
1177# define FIT_IMAGE_ENABLE_VERIFY 1
1178# include <openssl/evp.h>
1179# else
1180# define IMAGE_ENABLE_SIGN 0
1181# define FIT_IMAGE_ENABLE_VERIFY 0
1182# endif
1183#else
1184# define IMAGE_ENABLE_SIGN 0
1185# define FIT_IMAGE_ENABLE_VERIFY CONFIG_IS_ENABLED(FIT_SIGNATURE)
1186#endif
1187
1188#if IMAGE_ENABLE_FIT
1189#ifdef USE_HOSTCC
1190void *image_get_host_blob(void);
1191void image_set_host_blob(void *host_blob);
1192# define gd_fdt_blob() image_get_host_blob()
1193#else
1194# define gd_fdt_blob() (gd->fdt_blob)
1195#endif
1196
1197#endif
1198
1199
1200
1201
1202
1203
1204
1205
1206struct image_sign_info {
1207 const char *keydir;
1208 const char *keyname;
1209 const char *keyfile;
1210 void *fit;
1211 int node_offset;
1212 const char *name;
1213 struct checksum_algo *checksum;
1214 struct padding_algo *padding;
1215 struct crypto_algo *crypto;
1216 const void *fdt_blob;
1217 int required_keynode;
1218 const char *require_keys;
1219 const char *engine_id;
1220
1221
1222
1223
1224
1225 const void *key;
1226 int keylen;
1227};
1228
1229
1230struct image_region {
1231 const void *data;
1232 int size;
1233};
1234
1235#if FIT_IMAGE_ENABLE_VERIFY
1236# include <u-boot/hash-checksum.h>
1237#endif
1238struct checksum_algo {
1239 const char *name;
1240 const int checksum_len;
1241 const int der_len;
1242 const uint8_t *der_prefix;
1243#if IMAGE_ENABLE_SIGN
1244 const EVP_MD *(*calculate_sign)(void);
1245#endif
1246 int (*calculate)(const char *name,
1247 const struct image_region region[],
1248 int region_count, uint8_t *checksum);
1249};
1250
1251struct crypto_algo {
1252 const char *name;
1253 const int key_len;
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272 int (*sign)(struct image_sign_info *info,
1273 const struct image_region region[],
1274 int region_count, uint8_t **sigp, uint *sig_len);
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287 int (*add_verify_data)(struct image_sign_info *info, void *keydest);
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299 int (*verify)(struct image_sign_info *info,
1300 const struct image_region region[], int region_count,
1301 uint8_t *sig, uint sig_len);
1302};
1303
1304
1305#define U_BOOT_CRYPTO_ALGO(__name) \
1306ll_entry_declare(struct crypto_algo, __name, cryptos)
1307
1308struct padding_algo {
1309 const char *name;
1310 int (*verify)(struct image_sign_info *info,
1311 uint8_t *pad, int pad_len,
1312 const uint8_t *hash, int hash_len);
1313};
1314
1315
1316#define U_BOOT_PADDING_ALGO(__name) \
1317ll_entry_declare(struct padding_algo, __name, paddings)
1318
1319
1320
1321
1322
1323
1324
1325struct checksum_algo *image_get_checksum_algo(const char *full_name);
1326
1327
1328
1329
1330
1331
1332
1333struct crypto_algo *image_get_crypto_algo(const char *full_name);
1334
1335
1336
1337
1338
1339
1340
1341struct padding_algo *image_get_padding_algo(const char *name);
1342
1343#if IMAGE_ENABLE_FIT
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359int fit_image_verify_required_sigs(const void *fit, int image_noffset,
1360 const char *data, size_t size, const void *sig_blob,
1361 int *no_sigsp);
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378int fit_image_check_sig(const void *fit, int noffset, const void *data,
1379 size_t size, int required_keynode, char **err_msgp);
1380
1381int fit_image_decrypt_data(const void *fit,
1382 int image_noffset, int cipher_noffset,
1383 const void *data, size_t size,
1384 void **data_unciphered, size_t *size_unciphered);
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399struct image_region *fit_region_make_list(const void *fit,
1400 struct fdt_region *fdt_regions, int count,
1401 struct image_region *region);
1402
1403static inline int fit_image_check_target_arch(const void *fdt, int node)
1404{
1405#ifndef USE_HOSTCC
1406 return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
1407#else
1408 return 0;
1409#endif
1410}
1411
1412
1413
1414
1415
1416#if defined(USE_HOSTCC)
1417# if defined(CONFIG_FIT_CIPHER)
1418# define IMAGE_ENABLE_ENCRYPT 1
1419# define IMAGE_ENABLE_DECRYPT 1
1420# include <openssl/evp.h>
1421# else
1422# define IMAGE_ENABLE_ENCRYPT 0
1423# define IMAGE_ENABLE_DECRYPT 0
1424# endif
1425#else
1426# define IMAGE_ENABLE_ENCRYPT 0
1427# define IMAGE_ENABLE_DECRYPT CONFIG_IS_ENABLED(FIT_CIPHER)
1428#endif
1429
1430
1431struct image_cipher_info {
1432 const char *keydir;
1433 const char *keyname;
1434 const char *ivname;
1435 const void *fit;
1436 int node_noffset;
1437 const char *name;
1438 struct cipher_algo *cipher;
1439 const void *fdt_blob;
1440 const void *key;
1441 const void *iv;
1442 size_t size_unciphered;
1443};
1444
1445struct cipher_algo {
1446 const char *name;
1447 int key_len;
1448 int iv_len;
1449
1450#if IMAGE_ENABLE_ENCRYPT
1451 const EVP_CIPHER * (*calculate_type)(void);
1452#endif
1453
1454 int (*encrypt)(struct image_cipher_info *info,
1455 const unsigned char *data, int data_len,
1456 unsigned char **cipher, int *cipher_len);
1457
1458 int (*add_cipher_data)(struct image_cipher_info *info,
1459 void *keydest, void *fit, int node_noffset);
1460
1461 int (*decrypt)(struct image_cipher_info *info,
1462 const void *cipher, size_t cipher_len,
1463 void **data, size_t *data_len);
1464};
1465
1466int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
1467
1468struct cipher_algo *image_get_cipher_algo(const char *full_name);
1469
1470#ifdef CONFIG_FIT_VERBOSE
1471#define fit_unsupported(msg) printf("! %s:%d " \
1472 "FIT images not supported for '%s'\n", \
1473 __FILE__, __LINE__, (msg))
1474
1475#define fit_unsupported_reset(msg) printf("! %s:%d " \
1476 "FIT images not supported for '%s' " \
1477 "- must reset board to recover!\n", \
1478 __FILE__, __LINE__, (msg))
1479#else
1480#define fit_unsupported(msg)
1481#define fit_unsupported_reset(msg)
1482#endif
1483#endif
1484
1485#if !defined(USE_HOSTCC)
1486#if defined(CONFIG_ANDROID_BOOT_IMAGE)
1487struct andr_img_hdr;
1488int android_image_check_header(const struct andr_img_hdr *hdr);
1489int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
1490 ulong *os_data, ulong *os_len);
1491int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
1492 ulong *rd_data, ulong *rd_len);
1493int android_image_get_second(const struct andr_img_hdr *hdr,
1494 ulong *second_data, ulong *second_len);
1495bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
1496bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
1497 u32 *size);
1498ulong android_image_get_end(const struct andr_img_hdr *hdr);
1499ulong android_image_get_kload(const struct andr_img_hdr *hdr);
1500ulong android_image_get_kcomp(const struct andr_img_hdr *hdr);
1501void android_print_contents(const struct andr_img_hdr *hdr);
1502#if !defined(CONFIG_SPL_BUILD)
1503bool android_image_print_dtb_contents(ulong hdr_addr);
1504#endif
1505
1506#endif
1507#endif
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520int board_fit_config_name_match(const char *name);
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537void board_fit_image_post_process(const void *fit, int node, void **p_image,
1538 size_t *p_size);
1539
1540#define FDT_ERROR ((ulong)(-1))
1541
1542ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556int fit_find_config_node(const void *fdt);
1557
1558
1559
1560
1561
1562
1563
1564
1565struct fit_loadable_tbl {
1566 int type;
1567
1568
1569
1570
1571
1572
1573 void (*handler)(ulong data, size_t size);
1574};
1575
1576
1577
1578
1579
1580
1581
1582#define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
1583 ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
1584 .type = _type, \
1585 .handler = _handler, \
1586 }
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598int fit_update(const void *fit);
1599
1600#endif
1601