image.h 46 KB

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