image.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2005
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. ********************************************************************
  8. * NOTE: This header file defines an interface to U-Boot. Including
  9. * this (unmodified) header file in another file is considered normal
  10. * use of U-Boot, and does *not* fall under the heading of "derived
  11. * work".
  12. ********************************************************************
  13. */
  14. #ifndef __IMAGE_H__
  15. #define __IMAGE_H__
  16. #include "compiler.h"
  17. #include <asm/byteorder.h>
  18. /* Define this to avoid #ifdefs later on */
  19. struct lmb;
  20. struct fdt_region;
  21. #ifdef USE_HOSTCC
  22. #include <sys/types.h>
  23. /* new uImage format support enabled on host */
  24. #define IMAGE_ENABLE_FIT 1
  25. #define IMAGE_ENABLE_OF_LIBFDT 1
  26. #define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */
  27. #define CONFIG_FIT_ENABLE_SHA256_SUPPORT
  28. #define CONFIG_SHA1
  29. #define CONFIG_SHA256
  30. #define IMAGE_ENABLE_IGNORE 0
  31. #define IMAGE_INDENT_STRING ""
  32. #else
  33. #include <lmb.h>
  34. #include <asm/u-boot.h>
  35. #include <command.h>
  36. /* Take notice of the 'ignore' property for hashes */
  37. #define IMAGE_ENABLE_IGNORE 1
  38. #define IMAGE_INDENT_STRING " "
  39. #define IMAGE_ENABLE_FIT CONFIG_IS_ENABLED(FIT)
  40. #define IMAGE_ENABLE_OF_LIBFDT CONFIG_IS_ENABLED(OF_LIBFDT)
  41. #endif /* USE_HOSTCC */
  42. #if IMAGE_ENABLE_FIT
  43. #include <hash.h>
  44. #include <linux/libfdt.h>
  45. #include <fdt_support.h>
  46. # ifdef CONFIG_SPL_BUILD
  47. # ifdef CONFIG_SPL_CRC32_SUPPORT
  48. # define IMAGE_ENABLE_CRC32 1
  49. # endif
  50. # ifdef CONFIG_SPL_MD5_SUPPORT
  51. # define IMAGE_ENABLE_MD5 1
  52. # endif
  53. # ifdef CONFIG_SPL_SHA1_SUPPORT
  54. # define IMAGE_ENABLE_SHA1 1
  55. # endif
  56. # else
  57. # define CONFIG_CRC32 /* FIT images need CRC32 support */
  58. # define IMAGE_ENABLE_CRC32 1
  59. # define IMAGE_ENABLE_MD5 1
  60. # define IMAGE_ENABLE_SHA1 1
  61. # endif
  62. #ifndef IMAGE_ENABLE_CRC32
  63. #define IMAGE_ENABLE_CRC32 0
  64. #endif
  65. #ifndef IMAGE_ENABLE_MD5
  66. #define IMAGE_ENABLE_MD5 0
  67. #endif
  68. #ifndef IMAGE_ENABLE_SHA1
  69. #define IMAGE_ENABLE_SHA1 0
  70. #endif
  71. #if defined(CONFIG_FIT_ENABLE_SHA256_SUPPORT) || \
  72. defined(CONFIG_SPL_SHA256_SUPPORT)
  73. #define IMAGE_ENABLE_SHA256 1
  74. #else
  75. #define IMAGE_ENABLE_SHA256 0
  76. #endif
  77. #endif /* IMAGE_ENABLE_FIT */
  78. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  79. # define IMAGE_BOOT_GET_CMDLINE 1
  80. #else
  81. # define IMAGE_BOOT_GET_CMDLINE 0
  82. #endif
  83. #ifdef CONFIG_OF_BOARD_SETUP
  84. # define IMAGE_OF_BOARD_SETUP 1
  85. #else
  86. # define IMAGE_OF_BOARD_SETUP 0
  87. #endif
  88. #ifdef CONFIG_OF_SYSTEM_SETUP
  89. # define IMAGE_OF_SYSTEM_SETUP 1
  90. #else
  91. # define IMAGE_OF_SYSTEM_SETUP 0
  92. #endif
  93. enum ih_category {
  94. IH_ARCH,
  95. IH_COMP,
  96. IH_OS,
  97. IH_TYPE,
  98. IH_COUNT,
  99. };
  100. /*
  101. * Operating System Codes
  102. *
  103. * The following are exposed to uImage header.
  104. * Do not change values for backward compatibility.
  105. */
  106. enum {
  107. IH_OS_INVALID = 0, /* Invalid OS */
  108. IH_OS_OPENBSD, /* OpenBSD */
  109. IH_OS_NETBSD, /* NetBSD */
  110. IH_OS_FREEBSD, /* FreeBSD */
  111. IH_OS_4_4BSD, /* 4.4BSD */
  112. IH_OS_LINUX, /* Linux */
  113. IH_OS_SVR4, /* SVR4 */
  114. IH_OS_ESIX, /* Esix */
  115. IH_OS_SOLARIS, /* Solaris */
  116. IH_OS_IRIX, /* Irix */
  117. IH_OS_SCO, /* SCO */
  118. IH_OS_DELL, /* Dell */
  119. IH_OS_NCR, /* NCR */
  120. IH_OS_LYNXOS, /* LynxOS */
  121. IH_OS_VXWORKS, /* VxWorks */
  122. IH_OS_PSOS, /* pSOS */
  123. IH_OS_QNX, /* QNX */
  124. IH_OS_U_BOOT, /* Firmware */
  125. IH_OS_RTEMS, /* RTEMS */
  126. IH_OS_ARTOS, /* ARTOS */
  127. IH_OS_UNITY, /* Unity OS */
  128. IH_OS_INTEGRITY, /* INTEGRITY */
  129. IH_OS_OSE, /* OSE */
  130. IH_OS_PLAN9, /* Plan 9 */
  131. IH_OS_OPENRTOS, /* OpenRTOS */
  132. IH_OS_ARM_TRUSTED_FIRMWARE, /* ARM Trusted Firmware */
  133. IH_OS_TEE, /* Trusted Execution Environment */
  134. IH_OS_COUNT,
  135. };
  136. /*
  137. * CPU Architecture Codes (supported by Linux)
  138. *
  139. * The following are exposed to uImage header.
  140. * Do not change values for backward compatibility.
  141. */
  142. enum {
  143. IH_ARCH_INVALID = 0, /* Invalid CPU */
  144. IH_ARCH_ALPHA, /* Alpha */
  145. IH_ARCH_ARM, /* ARM */
  146. IH_ARCH_I386, /* Intel x86 */
  147. IH_ARCH_IA64, /* IA64 */
  148. IH_ARCH_MIPS, /* MIPS */
  149. IH_ARCH_MIPS64, /* MIPS 64 Bit */
  150. IH_ARCH_PPC, /* PowerPC */
  151. IH_ARCH_S390, /* IBM S390 */
  152. IH_ARCH_SH, /* SuperH */
  153. IH_ARCH_SPARC, /* Sparc */
  154. IH_ARCH_SPARC64, /* Sparc 64 Bit */
  155. IH_ARCH_M68K, /* M68K */
  156. IH_ARCH_NIOS, /* Nios-32 */
  157. IH_ARCH_MICROBLAZE, /* MicroBlaze */
  158. IH_ARCH_NIOS2, /* Nios-II */
  159. IH_ARCH_BLACKFIN, /* Blackfin */
  160. IH_ARCH_AVR32, /* AVR32 */
  161. IH_ARCH_ST200, /* STMicroelectronics ST200 */
  162. IH_ARCH_SANDBOX, /* Sandbox architecture (test only) */
  163. IH_ARCH_NDS32, /* ANDES Technology - NDS32 */
  164. IH_ARCH_OPENRISC, /* OpenRISC 1000 */
  165. IH_ARCH_ARM64, /* ARM64 */
  166. IH_ARCH_ARC, /* Synopsys DesignWare ARC */
  167. IH_ARCH_X86_64, /* AMD x86_64, Intel and Via */
  168. IH_ARCH_XTENSA, /* Xtensa */
  169. IH_ARCH_RISCV, /* RISC-V */
  170. IH_ARCH_COUNT,
  171. };
  172. /*
  173. * Image Types
  174. *
  175. * "Standalone Programs" are directly runnable in the environment
  176. * provided by U-Boot; it is expected that (if they behave
  177. * well) you can continue to work in U-Boot after return from
  178. * the Standalone Program.
  179. * "OS Kernel Images" are usually images of some Embedded OS which
  180. * will take over control completely. Usually these programs
  181. * will install their own set of exception handlers, device
  182. * drivers, set up the MMU, etc. - this means, that you cannot
  183. * expect to re-enter U-Boot except by resetting the CPU.
  184. * "RAMDisk Images" are more or less just data blocks, and their
  185. * parameters (address, size) are passed to an OS kernel that is
  186. * being started.
  187. * "Multi-File Images" contain several images, typically an OS
  188. * (Linux) kernel image and one or more data images like
  189. * RAMDisks. This construct is useful for instance when you want
  190. * to boot over the network using BOOTP etc., where the boot
  191. * server provides just a single image file, but you want to get
  192. * for instance an OS kernel and a RAMDisk image.
  193. *
  194. * "Multi-File Images" start with a list of image sizes, each
  195. * image size (in bytes) specified by an "uint32_t" in network
  196. * byte order. This list is terminated by an "(uint32_t)0".
  197. * Immediately after the terminating 0 follow the images, one by
  198. * one, all aligned on "uint32_t" boundaries (size rounded up to
  199. * a multiple of 4 bytes - except for the last file).
  200. *
  201. * "Firmware Images" are binary images containing firmware (like
  202. * U-Boot or FPGA images) which usually will be programmed to
  203. * flash memory.
  204. *
  205. * "Script files" are command sequences that will be executed by
  206. * U-Boot's command interpreter; this feature is especially
  207. * useful when you configure U-Boot to use a real shell (hush)
  208. * as command interpreter (=> Shell Scripts).
  209. *
  210. * The following are exposed to uImage header.
  211. * Do not change values for backward compatibility.
  212. */
  213. enum {
  214. IH_TYPE_INVALID = 0, /* Invalid Image */
  215. IH_TYPE_STANDALONE, /* Standalone Program */
  216. IH_TYPE_KERNEL, /* OS Kernel Image */
  217. IH_TYPE_RAMDISK, /* RAMDisk Image */
  218. IH_TYPE_MULTI, /* Multi-File Image */
  219. IH_TYPE_FIRMWARE, /* Firmware Image */
  220. IH_TYPE_SCRIPT, /* Script file */
  221. IH_TYPE_FILESYSTEM, /* Filesystem Image (any type) */
  222. IH_TYPE_FLATDT, /* Binary Flat Device Tree Blob */
  223. IH_TYPE_KWBIMAGE, /* Kirkwood Boot Image */
  224. IH_TYPE_IMXIMAGE, /* Freescale IMXBoot Image */
  225. IH_TYPE_UBLIMAGE, /* Davinci UBL Image */
  226. IH_TYPE_OMAPIMAGE, /* TI OMAP Config Header Image */
  227. IH_TYPE_AISIMAGE, /* TI Davinci AIS Image */
  228. /* OS Kernel Image, can run from any load address */
  229. IH_TYPE_KERNEL_NOLOAD,
  230. IH_TYPE_PBLIMAGE, /* Freescale PBL Boot Image */
  231. IH_TYPE_MXSIMAGE, /* Freescale MXSBoot Image */
  232. IH_TYPE_GPIMAGE, /* TI Keystone GPHeader Image */
  233. IH_TYPE_ATMELIMAGE, /* ATMEL ROM bootable Image */
  234. IH_TYPE_SOCFPGAIMAGE, /* Altera SOCFPGA Preloader */
  235. IH_TYPE_X86_SETUP, /* x86 setup.bin Image */
  236. IH_TYPE_LPC32XXIMAGE, /* x86 setup.bin Image */
  237. IH_TYPE_LOADABLE, /* A list of typeless images */
  238. IH_TYPE_RKIMAGE, /* Rockchip Boot Image */
  239. IH_TYPE_RKSD, /* Rockchip SD card */
  240. IH_TYPE_RKSPI, /* Rockchip SPI image */
  241. IH_TYPE_ZYNQIMAGE, /* Xilinx Zynq Boot Image */
  242. IH_TYPE_ZYNQMPIMAGE, /* Xilinx ZynqMP Boot Image */
  243. IH_TYPE_ZYNQMPBIF, /* Xilinx ZynqMP Boot Image (bif) */
  244. IH_TYPE_FPGA, /* FPGA Image */
  245. IH_TYPE_VYBRIDIMAGE, /* VYBRID .vyb Image */
  246. IH_TYPE_TEE, /* Trusted Execution Environment OS Image */
  247. IH_TYPE_FIRMWARE_IVT, /* Firmware Image with HABv4 IVT */
  248. IH_TYPE_PMMC, /* TI Power Management Micro-Controller Firmware */
  249. IH_TYPE_STM32IMAGE, /* STMicroelectronics STM32 Image */
  250. IH_TYPE_COUNT, /* Number of image types */
  251. };
  252. /*
  253. * Compression Types
  254. *
  255. * The following are exposed to uImage header.
  256. * Do not change values for backward compatibility.
  257. */
  258. enum {
  259. IH_COMP_NONE = 0, /* No Compression Used */
  260. IH_COMP_GZIP, /* gzip Compression Used */
  261. IH_COMP_BZIP2, /* bzip2 Compression Used */
  262. IH_COMP_LZMA, /* lzma Compression Used */
  263. IH_COMP_LZO, /* lzo Compression Used */
  264. IH_COMP_LZ4, /* lz4 Compression Used */
  265. IH_COMP_COUNT,
  266. };
  267. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  268. #define IH_NMLEN 32 /* Image Name Length */
  269. /* Reused from common.h */
  270. #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
  271. /*
  272. * Legacy format image header,
  273. * all data in network byte order (aka natural aka bigendian).
  274. */
  275. typedef struct image_header {
  276. __be32 ih_magic; /* Image Header Magic Number */
  277. __be32 ih_hcrc; /* Image Header CRC Checksum */
  278. __be32 ih_time; /* Image Creation Timestamp */
  279. __be32 ih_size; /* Image Data Size */
  280. __be32 ih_load; /* Data Load Address */
  281. __be32 ih_ep; /* Entry Point Address */
  282. __be32 ih_dcrc; /* Image Data CRC Checksum */
  283. uint8_t ih_os; /* Operating System */
  284. uint8_t ih_arch; /* CPU architecture */
  285. uint8_t ih_type; /* Image Type */
  286. uint8_t ih_comp; /* Compression Type */
  287. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  288. } image_header_t;
  289. typedef struct image_info {
  290. ulong start, end; /* start/end of blob */
  291. ulong image_start, image_len; /* start of image within blob, len of image */
  292. ulong load; /* load addr for the image */
  293. uint8_t comp, type, os; /* compression, type of image, os type */
  294. uint8_t arch; /* CPU architecture */
  295. } image_info_t;
  296. /*
  297. * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
  298. * routines.
  299. */
  300. typedef struct bootm_headers {
  301. /*
  302. * Legacy os image header, if it is a multi component image
  303. * then boot_get_ramdisk() and get_fdt() will attempt to get
  304. * data from second and third component accordingly.
  305. */
  306. image_header_t *legacy_hdr_os; /* image header pointer */
  307. image_header_t legacy_hdr_os_copy; /* header copy */
  308. ulong legacy_hdr_valid;
  309. #if IMAGE_ENABLE_FIT
  310. const char *fit_uname_cfg; /* configuration node unit name */
  311. void *fit_hdr_os; /* os FIT image header */
  312. const char *fit_uname_os; /* os subimage node unit name */
  313. int fit_noffset_os; /* os subimage node offset */
  314. void *fit_hdr_rd; /* init ramdisk FIT image header */
  315. const char *fit_uname_rd; /* init ramdisk subimage node unit name */
  316. int fit_noffset_rd; /* init ramdisk subimage node offset */
  317. void *fit_hdr_fdt; /* FDT blob FIT image header */
  318. const char *fit_uname_fdt; /* FDT blob subimage node unit name */
  319. int fit_noffset_fdt;/* FDT blob subimage node offset */
  320. void *fit_hdr_setup; /* x86 setup FIT image header */
  321. const char *fit_uname_setup; /* x86 setup subimage node name */
  322. int fit_noffset_setup;/* x86 setup subimage node offset */
  323. #endif
  324. #ifndef USE_HOSTCC
  325. image_info_t os; /* os image info */
  326. ulong ep; /* entry point of OS */
  327. ulong rd_start, rd_end;/* ramdisk start/end */
  328. char *ft_addr; /* flat dev tree address */
  329. ulong ft_len; /* length of flat device tree */
  330. ulong initrd_start;
  331. ulong initrd_end;
  332. ulong cmdline_start;
  333. ulong cmdline_end;
  334. bd_t *kbd;
  335. #endif
  336. int verify; /* env_get("verify")[0] != 'n' */
  337. #define BOOTM_STATE_START (0x00000001)
  338. #define BOOTM_STATE_FINDOS (0x00000002)
  339. #define BOOTM_STATE_FINDOTHER (0x00000004)
  340. #define BOOTM_STATE_LOADOS (0x00000008)
  341. #define BOOTM_STATE_RAMDISK (0x00000010)
  342. #define BOOTM_STATE_FDT (0x00000020)
  343. #define BOOTM_STATE_OS_CMDLINE (0x00000040)
  344. #define BOOTM_STATE_OS_BD_T (0x00000080)
  345. #define BOOTM_STATE_OS_PREP (0x00000100)
  346. #define BOOTM_STATE_OS_FAKE_GO (0x00000200) /* 'Almost' run the OS */
  347. #define BOOTM_STATE_OS_GO (0x00000400)
  348. int state;
  349. #ifdef CONFIG_LMB
  350. struct lmb lmb; /* for memory mgmt */
  351. #endif
  352. } bootm_headers_t;
  353. extern bootm_headers_t images;
  354. /*
  355. * Some systems (for example LWMON) have very short watchdog periods;
  356. * we must make sure to split long operations like memmove() or
  357. * checksum calculations into reasonable chunks.
  358. */
  359. #ifndef CHUNKSZ
  360. #define CHUNKSZ (64 * 1024)
  361. #endif
  362. #ifndef CHUNKSZ_CRC32
  363. #define CHUNKSZ_CRC32 (64 * 1024)
  364. #endif
  365. #ifndef CHUNKSZ_MD5
  366. #define CHUNKSZ_MD5 (64 * 1024)
  367. #endif
  368. #ifndef CHUNKSZ_SHA1
  369. #define CHUNKSZ_SHA1 (64 * 1024)
  370. #endif
  371. #define uimage_to_cpu(x) be32_to_cpu(x)
  372. #define cpu_to_uimage(x) cpu_to_be32(x)
  373. /*
  374. * Translation table for entries of a specific type; used by
  375. * get_table_entry_id() and get_table_entry_name().
  376. */
  377. typedef struct table_entry {
  378. int id;
  379. char *sname; /* short (input) name to find table entry */
  380. char *lname; /* long (output) name to print for messages */
  381. } table_entry_t;
  382. /*
  383. * get_table_entry_id() scans the translation table trying to find an
  384. * entry that matches the given short name. If a matching entry is
  385. * found, it's id is returned to the caller.
  386. */
  387. int get_table_entry_id(const table_entry_t *table,
  388. const char *table_name, const char *name);
  389. /*
  390. * get_table_entry_name() scans the translation table trying to find
  391. * an entry that matches the given id. If a matching entry is found,
  392. * its long name is returned to the caller.
  393. */
  394. char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
  395. const char *genimg_get_os_name(uint8_t os);
  396. /**
  397. * genimg_get_os_short_name() - get the short name for an OS
  398. *
  399. * @param os OS (IH_OS_...)
  400. * @return OS short name, or "unknown" if unknown
  401. */
  402. const char *genimg_get_os_short_name(uint8_t comp);
  403. const char *genimg_get_arch_name(uint8_t arch);
  404. /**
  405. * genimg_get_arch_short_name() - get the short name for an architecture
  406. *
  407. * @param arch Architecture type (IH_ARCH_...)
  408. * @return architecture short name, or "unknown" if unknown
  409. */
  410. const char *genimg_get_arch_short_name(uint8_t arch);
  411. const char *genimg_get_type_name(uint8_t type);
  412. /**
  413. * genimg_get_type_short_name() - get the short name for an image type
  414. *
  415. * @param type Image type (IH_TYPE_...)
  416. * @return image short name, or "unknown" if unknown
  417. */
  418. const char *genimg_get_type_short_name(uint8_t type);
  419. const char *genimg_get_comp_name(uint8_t comp);
  420. /**
  421. * genimg_get_comp_short_name() - get the short name for a compression method
  422. *
  423. * @param comp compression method (IH_COMP_...)
  424. * @return compression method short name, or "unknown" if unknown
  425. */
  426. const char *genimg_get_comp_short_name(uint8_t comp);
  427. /**
  428. * genimg_get_cat_name() - Get the name of an item in a category
  429. *
  430. * @category: Category of item
  431. * @id: Item ID
  432. * @return name of item, or "Unknown ..." if unknown
  433. */
  434. const char *genimg_get_cat_name(enum ih_category category, uint id);
  435. /**
  436. * genimg_get_cat_short_name() - Get the short name of an item in a category
  437. *
  438. * @category: Category of item
  439. * @id: Item ID
  440. * @return short name of item, or "Unknown ..." if unknown
  441. */
  442. const char *genimg_get_cat_short_name(enum ih_category category, uint id);
  443. /**
  444. * genimg_get_cat_count() - Get the number of items in a category
  445. *
  446. * @category: Category to check
  447. * @return the number of items in the category (IH_xxx_COUNT)
  448. */
  449. int genimg_get_cat_count(enum ih_category category);
  450. /**
  451. * genimg_get_cat_desc() - Get the description of a category
  452. *
  453. * @return the description of a category, e.g. "architecture". This
  454. * effectively converts the enum to a string.
  455. */
  456. const char *genimg_get_cat_desc(enum ih_category category);
  457. int genimg_get_os_id(const char *name);
  458. int genimg_get_arch_id(const char *name);
  459. int genimg_get_type_id(const char *name);
  460. int genimg_get_comp_id(const char *name);
  461. void genimg_print_size(uint32_t size);
  462. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
  463. defined(USE_HOSTCC)
  464. #define IMAGE_ENABLE_TIMESTAMP 1
  465. #else
  466. #define IMAGE_ENABLE_TIMESTAMP 0
  467. #endif
  468. void genimg_print_time(time_t timestamp);
  469. /* What to do with a image load address ('load = <> 'in the FIT) */
  470. enum fit_load_op {
  471. FIT_LOAD_IGNORED, /* Ignore load address */
  472. FIT_LOAD_OPTIONAL, /* Can be provided, but optional */
  473. FIT_LOAD_OPTIONAL_NON_ZERO, /* Optional, a value of 0 is ignored */
  474. FIT_LOAD_REQUIRED, /* Must be provided */
  475. };
  476. int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
  477. ulong *setup_len);
  478. #ifndef USE_HOSTCC
  479. /* Image format types, returned by _get_format() routine */
  480. #define IMAGE_FORMAT_INVALID 0x00
  481. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  482. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  483. #endif
  484. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  485. #define IMAGE_FORMAT_ANDROID 0x03 /* Android boot image */
  486. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  487. const char **fit_uname_config,
  488. const char **fit_uname_kernel);
  489. ulong genimg_get_kernel_addr(char * const img_addr);
  490. int genimg_get_format(const void *img_addr);
  491. int genimg_has_config(bootm_headers_t *images);
  492. int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
  493. uint8_t arch, const ulong *ld_start, ulong * const ld_len);
  494. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  495. uint8_t arch, ulong *rd_start, ulong *rd_end);
  496. /**
  497. * boot_get_loadable - routine to load a list of binaries to memory
  498. * @argc: Ignored Argument
  499. * @argv: Ignored Argument
  500. * @images: pointer to the bootm images structure
  501. * @arch: expected architecture for the image
  502. * @ld_start: Ignored Argument
  503. * @ld_len: Ignored Argument
  504. *
  505. * boot_get_loadable() will take the given FIT configuration, and look
  506. * for a field named "loadables". Loadables, is a list of elements in
  507. * the FIT given as strings. exe:
  508. * loadables = "linux_kernel", "fdt-2";
  509. * this function will attempt to parse each string, and load the
  510. * corresponding element from the FIT into memory. Once placed,
  511. * no aditional actions are taken.
  512. *
  513. * @return:
  514. * 0, if only valid images or no images are found
  515. * error code, if an error occurs during fit_image_load
  516. */
  517. int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
  518. uint8_t arch, const ulong *ld_start, ulong * const ld_len);
  519. #endif /* !USE_HOSTCC */
  520. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  521. ulong *setup_start, ulong *setup_len);
  522. /**
  523. * boot_get_fdt_fit() - load a DTB from a FIT file (applying overlays)
  524. *
  525. * This deals with all aspects of loading an DTB from a FIT.
  526. * The correct base image based on configuration will be selected, and
  527. * then any overlays specified will be applied (as present in fit_uname_configp).
  528. *
  529. * @param images Boot images structure
  530. * @param addr Address of FIT in memory
  531. * @param fit_unamep On entry this is the requested image name
  532. * (e.g. "kernel") or NULL to use the default. On exit
  533. * points to the selected image name
  534. * @param fit_uname_configp On entry this is the requested configuration
  535. * name (e.g. "conf-1") or NULL to use the default. On
  536. * exit points to the selected configuration name.
  537. * @param arch Expected architecture (IH_ARCH_...)
  538. * @param datap Returns address of loaded image
  539. * @param lenp Returns length of loaded image
  540. *
  541. * @return node offset of base image, or -ve error code on error
  542. */
  543. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  544. const char **fit_unamep, const char **fit_uname_configp,
  545. int arch, ulong *datap, ulong *lenp);
  546. /**
  547. * fit_image_load() - load an image from a FIT
  548. *
  549. * This deals with all aspects of loading an image from a FIT, including
  550. * selecting the right image based on configuration, verifying it, printing
  551. * out progress messages, checking the type/arch/os and optionally copying it
  552. * to the right load address.
  553. *
  554. * The property to look up is defined by image_type.
  555. *
  556. * @param images Boot images structure
  557. * @param addr Address of FIT in memory
  558. * @param fit_unamep On entry this is the requested image name
  559. * (e.g. "kernel") or NULL to use the default. On exit
  560. * points to the selected image name
  561. * @param fit_uname_configp On entry this is the requested configuration
  562. * name (e.g. "conf-1") or NULL to use the default. On
  563. * exit points to the selected configuration name.
  564. * @param arch Expected architecture (IH_ARCH_...)
  565. * @param image_type Required image type (IH_TYPE_...). If this is
  566. * IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
  567. * also.
  568. * @param bootstage_id ID of starting bootstage to use for progress updates.
  569. * This will be added to the BOOTSTAGE_SUB values when
  570. * calling bootstage_mark()
  571. * @param load_op Decribes what to do with the load address
  572. * @param datap Returns address of loaded image
  573. * @param lenp Returns length of loaded image
  574. * @return node offset of image, or -ve error code on error
  575. */
  576. int fit_image_load(bootm_headers_t *images, ulong addr,
  577. const char **fit_unamep, const char **fit_uname_configp,
  578. int arch, int image_type, int bootstage_id,
  579. enum fit_load_op load_op, ulong *datap, ulong *lenp);
  580. #ifndef USE_HOSTCC
  581. /**
  582. * fit_get_node_from_config() - Look up an image a FIT by type
  583. *
  584. * This looks in the selected conf- node (images->fit_uname_cfg) for a
  585. * particular image type (e.g. "kernel") and then finds the image that is
  586. * referred to.
  587. *
  588. * For example, for something like:
  589. *
  590. * images {
  591. * kernel {
  592. * ...
  593. * };
  594. * };
  595. * configurations {
  596. * conf-1 {
  597. * kernel = "kernel";
  598. * };
  599. * };
  600. *
  601. * the function will return the node offset of the kernel@1 node, assuming
  602. * that conf-1 is the chosen configuration.
  603. *
  604. * @param images Boot images structure
  605. * @param prop_name Property name to look up (FIT_..._PROP)
  606. * @param addr Address of FIT in memory
  607. */
  608. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  609. ulong addr);
  610. int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
  611. bootm_headers_t *images,
  612. char **of_flat_tree, ulong *of_size);
  613. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
  614. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
  615. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  616. ulong *initrd_start, ulong *initrd_end);
  617. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
  618. #ifdef CONFIG_SYS_BOOT_GET_KBD
  619. int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
  620. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  621. #endif /* !USE_HOSTCC */
  622. /*******************************************************************/
  623. /* Legacy format specific code (prefixed with image_) */
  624. /*******************************************************************/
  625. static inline uint32_t image_get_header_size(void)
  626. {
  627. return (sizeof(image_header_t));
  628. }
  629. #define image_get_hdr_l(f) \
  630. static inline uint32_t image_get_##f(const image_header_t *hdr) \
  631. { \
  632. return uimage_to_cpu(hdr->ih_##f); \
  633. }
  634. image_get_hdr_l(magic) /* image_get_magic */
  635. image_get_hdr_l(hcrc) /* image_get_hcrc */
  636. image_get_hdr_l(time) /* image_get_time */
  637. image_get_hdr_l(size) /* image_get_size */
  638. image_get_hdr_l(load) /* image_get_load */
  639. image_get_hdr_l(ep) /* image_get_ep */
  640. image_get_hdr_l(dcrc) /* image_get_dcrc */
  641. #define image_get_hdr_b(f) \
  642. static inline uint8_t image_get_##f(const image_header_t *hdr) \
  643. { \
  644. return hdr->ih_##f; \
  645. }
  646. image_get_hdr_b(os) /* image_get_os */
  647. image_get_hdr_b(arch) /* image_get_arch */
  648. image_get_hdr_b(type) /* image_get_type */
  649. image_get_hdr_b(comp) /* image_get_comp */
  650. static inline char *image_get_name(const image_header_t *hdr)
  651. {
  652. return (char *)hdr->ih_name;
  653. }
  654. static inline uint32_t image_get_data_size(const image_header_t *hdr)
  655. {
  656. return image_get_size(hdr);
  657. }
  658. /**
  659. * image_get_data - get image payload start address
  660. * @hdr: image header
  661. *
  662. * image_get_data() returns address of the image payload. For single
  663. * component images it is image data start. For multi component
  664. * images it points to the null terminated table of sub-images sizes.
  665. *
  666. * returns:
  667. * image payload data start address
  668. */
  669. static inline ulong image_get_data(const image_header_t *hdr)
  670. {
  671. return ((ulong)hdr + image_get_header_size());
  672. }
  673. static inline uint32_t image_get_image_size(const image_header_t *hdr)
  674. {
  675. return (image_get_size(hdr) + image_get_header_size());
  676. }
  677. static inline ulong image_get_image_end(const image_header_t *hdr)
  678. {
  679. return ((ulong)hdr + image_get_image_size(hdr));
  680. }
  681. #define image_set_hdr_l(f) \
  682. static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
  683. { \
  684. hdr->ih_##f = cpu_to_uimage(val); \
  685. }
  686. image_set_hdr_l(magic) /* image_set_magic */
  687. image_set_hdr_l(hcrc) /* image_set_hcrc */
  688. image_set_hdr_l(time) /* image_set_time */
  689. image_set_hdr_l(size) /* image_set_size */
  690. image_set_hdr_l(load) /* image_set_load */
  691. image_set_hdr_l(ep) /* image_set_ep */
  692. image_set_hdr_l(dcrc) /* image_set_dcrc */
  693. #define image_set_hdr_b(f) \
  694. static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
  695. { \
  696. hdr->ih_##f = val; \
  697. }
  698. image_set_hdr_b(os) /* image_set_os */
  699. image_set_hdr_b(arch) /* image_set_arch */
  700. image_set_hdr_b(type) /* image_set_type */
  701. image_set_hdr_b(comp) /* image_set_comp */
  702. static inline void image_set_name(image_header_t *hdr, const char *name)
  703. {
  704. strncpy(image_get_name(hdr), name, IH_NMLEN);
  705. }
  706. int image_check_hcrc(const image_header_t *hdr);
  707. int image_check_dcrc(const image_header_t *hdr);
  708. #ifndef USE_HOSTCC
  709. ulong env_get_bootm_low(void);
  710. phys_size_t env_get_bootm_size(void);
  711. phys_size_t env_get_bootm_mapsize(void);
  712. #endif
  713. void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
  714. static inline int image_check_magic(const image_header_t *hdr)
  715. {
  716. return (image_get_magic(hdr) == IH_MAGIC);
  717. }
  718. static inline int image_check_type(const image_header_t *hdr, uint8_t type)
  719. {
  720. return (image_get_type(hdr) == type);
  721. }
  722. static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
  723. {
  724. return (image_get_arch(hdr) == arch) ||
  725. (image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
  726. }
  727. static inline int image_check_os(const image_header_t *hdr, uint8_t os)
  728. {
  729. return (image_get_os(hdr) == os);
  730. }
  731. ulong image_multi_count(const image_header_t *hdr);
  732. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  733. ulong *data, ulong *len);
  734. void image_print_contents(const void *hdr);
  735. #ifndef USE_HOSTCC
  736. static inline int image_check_target_arch(const image_header_t *hdr)
  737. {
  738. #ifndef IH_ARCH_DEFAULT
  739. # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
  740. #endif
  741. return image_check_arch(hdr, IH_ARCH_DEFAULT);
  742. }
  743. #endif /* USE_HOSTCC */
  744. /**
  745. * Set up properties in the FDT
  746. *
  747. * This sets up properties in the FDT that is to be passed to linux.
  748. *
  749. * @images: Images information
  750. * @blob: FDT to update
  751. * @of_size: Size of the FDT
  752. * @lmb: Points to logical memory block structure
  753. * @return 0 if ok, <0 on failure
  754. */
  755. int image_setup_libfdt(bootm_headers_t *images, void *blob,
  756. int of_size, struct lmb *lmb);
  757. /**
  758. * Set up the FDT to use for booting a kernel
  759. *
  760. * This performs ramdisk setup, sets up the FDT if required, and adds
  761. * paramters to the FDT if libfdt is available.
  762. *
  763. * @param images Images information
  764. * @return 0 if ok, <0 on failure
  765. */
  766. int image_setup_linux(bootm_headers_t *images);
  767. /**
  768. * bootz_setup() - Extract stat and size of a Linux xImage
  769. *
  770. * @image: Address of image
  771. * @start: Returns start address of image
  772. * @end : Returns end address of image
  773. * @return 0 if OK, 1 if the image was not recognised
  774. */
  775. int bootz_setup(ulong image, ulong *start, ulong *end);
  776. /**
  777. * Return the correct start address and size of a Linux aarch64 Image.
  778. *
  779. * @image: Address of image
  780. * @start: Returns start address of image
  781. * @size : Returns size image
  782. * @return 0 if OK, 1 if the image was not recognised
  783. */
  784. int booti_setup(ulong image, ulong *relocated_addr, ulong *size);
  785. /*******************************************************************/
  786. /* New uImage format specific code (prefixed with fit_) */
  787. /*******************************************************************/
  788. #define FIT_IMAGES_PATH "/images"
  789. #define FIT_CONFS_PATH "/configurations"
  790. /* hash/signature node */
  791. #define FIT_HASH_NODENAME "hash"
  792. #define FIT_ALGO_PROP "algo"
  793. #define FIT_VALUE_PROP "value"
  794. #define FIT_IGNORE_PROP "uboot-ignore"
  795. #define FIT_SIG_NODENAME "signature"
  796. /* image node */
  797. #define FIT_DATA_PROP "data"
  798. #define FIT_DATA_POSITION_PROP "data-position"
  799. #define FIT_DATA_OFFSET_PROP "data-offset"
  800. #define FIT_DATA_SIZE_PROP "data-size"
  801. #define FIT_TIMESTAMP_PROP "timestamp"
  802. #define FIT_DESC_PROP "description"
  803. #define FIT_ARCH_PROP "arch"
  804. #define FIT_TYPE_PROP "type"
  805. #define FIT_OS_PROP "os"
  806. #define FIT_COMP_PROP "compression"
  807. #define FIT_ENTRY_PROP "entry"
  808. #define FIT_LOAD_PROP "load"
  809. /* configuration node */
  810. #define FIT_KERNEL_PROP "kernel"
  811. #define FIT_RAMDISK_PROP "ramdisk"
  812. #define FIT_FDT_PROP "fdt"
  813. #define FIT_LOADABLE_PROP "loadables"
  814. #define FIT_DEFAULT_PROP "default"
  815. #define FIT_SETUP_PROP "setup"
  816. #define FIT_FPGA_PROP "fpga"
  817. #define FIT_FIRMWARE_PROP "firmware"
  818. #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
  819. #if IMAGE_ENABLE_FIT
  820. /* cmdline argument format parsing */
  821. int fit_parse_conf(const char *spec, ulong addr_curr,
  822. ulong *addr, const char **conf_name);
  823. int fit_parse_subimage(const char *spec, ulong addr_curr,
  824. ulong *addr, const char **image_name);
  825. int fit_get_subimage_count(const void *fit, int images_noffset);
  826. void fit_print_contents(const void *fit);
  827. void fit_image_print(const void *fit, int noffset, const char *p);
  828. /**
  829. * fit_get_end - get FIT image size
  830. * @fit: pointer to the FIT format image header
  831. *
  832. * returns:
  833. * size of the FIT image (blob) in memory
  834. */
  835. static inline ulong fit_get_size(const void *fit)
  836. {
  837. return fdt_totalsize(fit);
  838. }
  839. /**
  840. * fit_get_end - get FIT image end
  841. * @fit: pointer to the FIT format image header
  842. *
  843. * returns:
  844. * end address of the FIT image (blob) in memory
  845. */
  846. ulong fit_get_end(const void *fit);
  847. /**
  848. * fit_get_name - get FIT node name
  849. * @fit: pointer to the FIT format image header
  850. *
  851. * returns:
  852. * NULL, on error
  853. * pointer to node name, on success
  854. */
  855. static inline const char *fit_get_name(const void *fit_hdr,
  856. int noffset, int *len)
  857. {
  858. return fdt_get_name(fit_hdr, noffset, len);
  859. }
  860. int fit_get_desc(const void *fit, int noffset, char **desc);
  861. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
  862. int fit_image_get_node(const void *fit, const char *image_uname);
  863. int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
  864. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
  865. int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
  866. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
  867. int fit_image_get_load(const void *fit, int noffset, ulong *load);
  868. int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
  869. int fit_image_get_data(const void *fit, int noffset,
  870. const void **data, size_t *size);
  871. int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
  872. int fit_image_get_data_position(const void *fit, int noffset,
  873. int *data_position);
  874. int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
  875. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
  876. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  877. int *value_len);
  878. int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
  879. /**
  880. * fit_add_verification_data() - add verification data to FIT image nodes
  881. *
  882. * @keydir: Directory containing keys
  883. * @kwydest: FDT blob to write public key information to
  884. * @fit: Pointer to the FIT format image header
  885. * @comment: Comment to add to signature nodes
  886. * @require_keys: Mark all keys as 'required'
  887. * @engine_id: Engine to use for signing
  888. *
  889. * Adds hash values for all component images in the FIT blob.
  890. * Hashes are calculated for all component images which have hash subnodes
  891. * with algorithm property set to one of the supported hash algorithms.
  892. *
  893. * Also add signatures if signature nodes are present.
  894. *
  895. * returns
  896. * 0, on success
  897. * libfdt error code, on failure
  898. */
  899. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  900. const char *comment, int require_keys,
  901. const char *engine_id);
  902. int fit_image_verify_with_data(const void *fit, int image_noffset,
  903. const void *data, size_t size);
  904. int fit_image_verify(const void *fit, int noffset);
  905. int fit_config_verify(const void *fit, int conf_noffset);
  906. int fit_all_image_verify(const void *fit);
  907. int fit_image_check_os(const void *fit, int noffset, uint8_t os);
  908. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
  909. int fit_image_check_type(const void *fit, int noffset, uint8_t type);
  910. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
  911. int fit_check_format(const void *fit);
  912. int fit_conf_find_compat(const void *fit, const void *fdt);
  913. int fit_conf_get_node(const void *fit, const char *conf_uname);
  914. /**
  915. * fit_conf_get_prop_node() - Get node refered to by a configuration
  916. * @fit: FIT to check
  917. * @noffset: Offset of conf@xxx node to check
  918. * @prop_name: Property to read from the conf node
  919. *
  920. * The conf- nodes contain references to other nodes, using properties
  921. * like 'kernel = "kernel"'. Given such a property name (e.g. "kernel"),
  922. * return the offset of the node referred to (e.g. offset of node
  923. * "/images/kernel".
  924. */
  925. int fit_conf_get_prop_node(const void *fit, int noffset,
  926. const char *prop_name);
  927. void fit_conf_print(const void *fit, int noffset, const char *p);
  928. int fit_check_ramdisk(const void *fit, int os_noffset,
  929. uint8_t arch, int verify);
  930. int calculate_hash(const void *data, int data_len, const char *algo,
  931. uint8_t *value, int *value_len);
  932. /*
  933. * At present we only support signing on the host, and verification on the
  934. * device
  935. */
  936. #if defined(CONFIG_FIT_SIGNATURE)
  937. # ifdef USE_HOSTCC
  938. # define IMAGE_ENABLE_SIGN 1
  939. # define IMAGE_ENABLE_VERIFY 1
  940. # include <openssl/evp.h>
  941. #else
  942. # define IMAGE_ENABLE_SIGN 0
  943. # define IMAGE_ENABLE_VERIFY 1
  944. # endif
  945. #else
  946. # define IMAGE_ENABLE_SIGN 0
  947. # define IMAGE_ENABLE_VERIFY 0
  948. #endif
  949. #ifdef USE_HOSTCC
  950. void *image_get_host_blob(void);
  951. void image_set_host_blob(void *host_blob);
  952. # define gd_fdt_blob() image_get_host_blob()
  953. #else
  954. # define gd_fdt_blob() (gd->fdt_blob)
  955. #endif
  956. #ifdef CONFIG_FIT_BEST_MATCH
  957. #define IMAGE_ENABLE_BEST_MATCH 1
  958. #else
  959. #define IMAGE_ENABLE_BEST_MATCH 0
  960. #endif
  961. /* Information passed to the signing routines */
  962. struct image_sign_info {
  963. const char *keydir; /* Directory conaining keys */
  964. const char *keyname; /* Name of key to use */
  965. void *fit; /* Pointer to FIT blob */
  966. int node_offset; /* Offset of signature node */
  967. const char *name; /* Algorithm name */
  968. struct checksum_algo *checksum; /* Checksum algorithm information */
  969. struct crypto_algo *crypto; /* Crypto algorithm information */
  970. const void *fdt_blob; /* FDT containing public keys */
  971. int required_keynode; /* Node offset of key to use: -1=any */
  972. const char *require_keys; /* Value for 'required' property */
  973. const char *engine_id; /* Engine to use for signing */
  974. };
  975. #endif /* Allow struct image_region to always be defined for rsa.h */
  976. /* A part of an image, used for hashing */
  977. struct image_region {
  978. const void *data;
  979. int size;
  980. };
  981. #if IMAGE_ENABLE_FIT
  982. #if IMAGE_ENABLE_VERIFY
  983. # include <u-boot/rsa-checksum.h>
  984. #endif
  985. struct checksum_algo {
  986. const char *name;
  987. const int checksum_len;
  988. const int der_len;
  989. const uint8_t *der_prefix;
  990. #if IMAGE_ENABLE_SIGN
  991. const EVP_MD *(*calculate_sign)(void);
  992. #endif
  993. int (*calculate)(const char *name,
  994. const struct image_region region[],
  995. int region_count, uint8_t *checksum);
  996. };
  997. struct crypto_algo {
  998. const char *name; /* Name of algorithm */
  999. const int key_len;
  1000. /**
  1001. * sign() - calculate and return signature for given input data
  1002. *
  1003. * @info: Specifies key and FIT information
  1004. * @data: Pointer to the input data
  1005. * @data_len: Data length
  1006. * @sigp: Set to an allocated buffer holding the signature
  1007. * @sig_len: Set to length of the calculated hash
  1008. *
  1009. * This computes input data signature according to selected algorithm.
  1010. * Resulting signature value is placed in an allocated buffer, the
  1011. * pointer is returned as *sigp. The length of the calculated
  1012. * signature is returned via the sig_len pointer argument. The caller
  1013. * should free *sigp.
  1014. *
  1015. * @return: 0, on success, -ve on error
  1016. */
  1017. int (*sign)(struct image_sign_info *info,
  1018. const struct image_region region[],
  1019. int region_count, uint8_t **sigp, uint *sig_len);
  1020. /**
  1021. * add_verify_data() - Add verification information to FDT
  1022. *
  1023. * Add public key information to the FDT node, suitable for
  1024. * verification at run-time. The information added depends on the
  1025. * algorithm being used.
  1026. *
  1027. * @info: Specifies key and FIT information
  1028. * @keydest: Destination FDT blob for public key data
  1029. * @return: 0, on success, -ve on error
  1030. */
  1031. int (*add_verify_data)(struct image_sign_info *info, void *keydest);
  1032. /**
  1033. * verify() - Verify a signature against some data
  1034. *
  1035. * @info: Specifies key and FIT information
  1036. * @data: Pointer to the input data
  1037. * @data_len: Data length
  1038. * @sig: Signature
  1039. * @sig_len: Number of bytes in signature
  1040. * @return 0 if verified, -ve on error
  1041. */
  1042. int (*verify)(struct image_sign_info *info,
  1043. const struct image_region region[], int region_count,
  1044. uint8_t *sig, uint sig_len);
  1045. };
  1046. /**
  1047. * image_get_checksum_algo() - Look up a checksum algorithm
  1048. *
  1049. * @param full_name Name of algorithm in the form "checksum,crypto"
  1050. * @return pointer to algorithm information, or NULL if not found
  1051. */
  1052. struct checksum_algo *image_get_checksum_algo(const char *full_name);
  1053. /**
  1054. * image_get_crypto_algo() - Look up a cryptosystem algorithm
  1055. *
  1056. * @param full_name Name of algorithm in the form "checksum,crypto"
  1057. * @return pointer to algorithm information, or NULL if not found
  1058. */
  1059. struct crypto_algo *image_get_crypto_algo(const char *full_name);
  1060. /**
  1061. * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  1062. *
  1063. * @fit: FIT to check
  1064. * @image_noffset: Offset of image node to check
  1065. * @data: Image data to check
  1066. * @size: Size of image data
  1067. * @sig_blob: FDT containing public keys
  1068. * @no_sigsp: Returns 1 if no signatures were required, and
  1069. * therefore nothing was checked. The caller may wish
  1070. * to fall back to other mechanisms, or refuse to
  1071. * boot.
  1072. * @return 0 if all verified ok, <0 on error
  1073. */
  1074. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  1075. const char *data, size_t size, const void *sig_blob,
  1076. int *no_sigsp);
  1077. /**
  1078. * fit_image_check_sig() - Check a single image signature node
  1079. *
  1080. * @fit: FIT to check
  1081. * @noffset: Offset of signature node to check
  1082. * @data: Image data to check
  1083. * @size: Size of image data
  1084. * @required_keynode: Offset in the control FDT of the required key node,
  1085. * if any. If this is given, then the image wil not
  1086. * pass verification unless that key is used. If this is
  1087. * -1 then any signature will do.
  1088. * @err_msgp: In the event of an error, this will be pointed to a
  1089. * help error string to display to the user.
  1090. * @return 0 if all verified ok, <0 on error
  1091. */
  1092. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  1093. size_t size, int required_keynode, char **err_msgp);
  1094. /**
  1095. * fit_region_make_list() - Make a list of regions to hash
  1096. *
  1097. * Given a list of FIT regions (offset, size) provided by libfdt, create
  1098. * a list of regions (void *, size) for use by the signature creationg
  1099. * and verification code.
  1100. *
  1101. * @fit: FIT image to process
  1102. * @fdt_regions: Regions as returned by libfdt
  1103. * @count: Number of regions returned by libfdt
  1104. * @region: Place to put list of regions (NULL to allocate it)
  1105. * @return pointer to list of regions, or NULL if out of memory
  1106. */
  1107. struct image_region *fit_region_make_list(const void *fit,
  1108. struct fdt_region *fdt_regions, int count,
  1109. struct image_region *region);
  1110. static inline int fit_image_check_target_arch(const void *fdt, int node)
  1111. {
  1112. #ifndef USE_HOSTCC
  1113. return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
  1114. #else
  1115. return 0;
  1116. #endif
  1117. }
  1118. #ifdef CONFIG_FIT_VERBOSE
  1119. #define fit_unsupported(msg) printf("! %s:%d " \
  1120. "FIT images not supported for '%s'\n", \
  1121. __FILE__, __LINE__, (msg))
  1122. #define fit_unsupported_reset(msg) printf("! %s:%d " \
  1123. "FIT images not supported for '%s' " \
  1124. "- must reset board to recover!\n", \
  1125. __FILE__, __LINE__, (msg))
  1126. #else
  1127. #define fit_unsupported(msg)
  1128. #define fit_unsupported_reset(msg)
  1129. #endif /* CONFIG_FIT_VERBOSE */
  1130. #endif /* CONFIG_FIT */
  1131. #if defined(CONFIG_ANDROID_BOOT_IMAGE)
  1132. struct andr_img_hdr;
  1133. int android_image_check_header(const struct andr_img_hdr *hdr);
  1134. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  1135. ulong *os_data, ulong *os_len);
  1136. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  1137. ulong *rd_data, ulong *rd_len);
  1138. int android_image_get_second(const struct andr_img_hdr *hdr,
  1139. ulong *second_data, ulong *second_len);
  1140. ulong android_image_get_end(const struct andr_img_hdr *hdr);
  1141. ulong android_image_get_kload(const struct andr_img_hdr *hdr);
  1142. void android_print_contents(const struct andr_img_hdr *hdr);
  1143. #endif /* CONFIG_ANDROID_BOOT_IMAGE */
  1144. /**
  1145. * board_fit_config_name_match() - Check for a matching board name
  1146. *
  1147. * This is used when SPL loads a FIT containing multiple device tree files
  1148. * and wants to work out which one to use. The description of each one is
  1149. * passed to this function. The description comes from the 'description' field
  1150. * in each (FDT) image node.
  1151. *
  1152. * @name: Device tree description
  1153. * @return 0 if this device tree should be used, non-zero to try the next
  1154. */
  1155. int board_fit_config_name_match(const char *name);
  1156. #if defined(CONFIG_SPL_FIT_IMAGE_POST_PROCESS) || \
  1157. defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1158. /**
  1159. * board_fit_image_post_process() - Do any post-process on FIT binary data
  1160. *
  1161. * This is used to do any sort of image manipulation, verification, decryption
  1162. * etc. in a platform or board specific way. Obviously, anything done here would
  1163. * need to be comprehended in how the images were prepared before being injected
  1164. * into the FIT creation (i.e. the binary blobs would have been pre-processed
  1165. * before being added to the FIT image).
  1166. *
  1167. * @image: pointer to the image start pointer
  1168. * @size: pointer to the image size
  1169. * @return no return value (failure should be handled internally)
  1170. */
  1171. void board_fit_image_post_process(void **p_image, size_t *p_size);
  1172. #endif /* CONFIG_SPL_FIT_IMAGE_POST_PROCESS */
  1173. #define FDT_ERROR ((ulong)(-1))
  1174. ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
  1175. /**
  1176. * fit_find_config_node() - Find the node for the best DTB in a FIT image
  1177. *
  1178. * A FIT image contains one or more DTBs. This function parses the
  1179. * configurations described in the FIT images and returns the node of
  1180. * the first matching DTB. To check if a DTB matches a board, this function
  1181. * calls board_fit_config_name_match(). If no matching DTB is found, it returns
  1182. * the node described by the default configuration if it exists.
  1183. *
  1184. * @fdt: pointer to flat device tree
  1185. * @return the node if found, -ve otherwise
  1186. */
  1187. int fit_find_config_node(const void *fdt);
  1188. /**
  1189. * Mapping of image types to function handlers to be invoked on the associated
  1190. * loaded images
  1191. *
  1192. * @type: Type of image, I.E. IH_TYPE_*
  1193. * @handler: Function to call on loaded image
  1194. */
  1195. struct fit_loadable_tbl {
  1196. int type;
  1197. /**
  1198. * handler() - Process a loaded image
  1199. *
  1200. * @data: Pointer to start of loaded image data
  1201. * @size: Size of loaded image data
  1202. */
  1203. void (*handler)(ulong data, size_t size);
  1204. };
  1205. /*
  1206. * Define a FIT loadable image type handler
  1207. *
  1208. * _type is a valid uimage_type ID as defined in the "Image Type" enum above
  1209. * _handler is the handler function to call after this image type is loaded
  1210. */
  1211. #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
  1212. ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
  1213. .type = _type, \
  1214. .handler = _handler, \
  1215. }
  1216. #endif /* __IMAGE_H__ */