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