image.h 46 KB

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