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