image.h 44 KB

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