image.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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_RSASSA_PSS 1
  29. #define CONFIG_FIT_SHA256
  30. #define CONFIG_FIT_SHA384
  31. #define CONFIG_FIT_SHA512
  32. #define CONFIG_SHA1
  33. #define CONFIG_SHA256
  34. #define CONFIG_SHA384
  35. #define CONFIG_SHA512
  36. #define IMAGE_ENABLE_IGNORE 0
  37. #define IMAGE_INDENT_STRING ""
  38. #else
  39. #include <lmb.h>
  40. #include <asm/u-boot.h>
  41. #include <command.h>
  42. #include <linker_lists.h>
  43. /* Take notice of the 'ignore' property for hashes */
  44. #define IMAGE_ENABLE_IGNORE 1
  45. #define IMAGE_INDENT_STRING " "
  46. #define IMAGE_ENABLE_FIT CONFIG_IS_ENABLED(FIT)
  47. #define IMAGE_ENABLE_OF_LIBFDT CONFIG_IS_ENABLED(OF_LIBFDT)
  48. #endif /* USE_HOSTCC */
  49. #if IMAGE_ENABLE_FIT
  50. #include <hash.h>
  51. #include <linux/libfdt.h>
  52. #include <fdt_support.h>
  53. # ifdef CONFIG_SPL_BUILD
  54. # ifdef CONFIG_SPL_CRC32
  55. # define IMAGE_ENABLE_CRC32 1
  56. # endif
  57. # ifdef CONFIG_SPL_MD5
  58. # define IMAGE_ENABLE_MD5 1
  59. # endif
  60. # else
  61. # define IMAGE_ENABLE_CRC32 1
  62. # define IMAGE_ENABLE_MD5 1
  63. # endif
  64. #ifndef IMAGE_ENABLE_CRC32
  65. #define IMAGE_ENABLE_CRC32 0
  66. #endif
  67. #ifndef IMAGE_ENABLE_MD5
  68. #define IMAGE_ENABLE_MD5 0
  69. #endif
  70. #endif /* IMAGE_ENABLE_FIT */
  71. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  72. # define IMAGE_BOOT_GET_CMDLINE 1
  73. #else
  74. # define IMAGE_BOOT_GET_CMDLINE 0
  75. #endif
  76. #ifdef CONFIG_OF_BOARD_SETUP
  77. # define IMAGE_OF_BOARD_SETUP 1
  78. #else
  79. # define IMAGE_OF_BOARD_SETUP 0
  80. #endif
  81. #ifdef CONFIG_OF_SYSTEM_SETUP
  82. # define IMAGE_OF_SYSTEM_SETUP 1
  83. #else
  84. # define IMAGE_OF_SYSTEM_SETUP 0
  85. #endif
  86. extern ulong image_load_addr; /* Default Load Address */
  87. extern ulong image_save_addr; /* Default Save Address */
  88. extern ulong image_save_size; /* Default Save Size */
  89. /* An invalid size, meaning that the image size is not known */
  90. #define IMAGE_SIZE_INVAL (-1UL)
  91. enum ih_category {
  92. IH_ARCH,
  93. IH_COMP,
  94. IH_OS,
  95. IH_TYPE,
  96. IH_COUNT,
  97. };
  98. /*
  99. * Operating System Codes
  100. *
  101. * The following are exposed to uImage header.
  102. * New IDs *MUST* be appended at the end of the list and *NEVER*
  103. * inserted for backward compatibility.
  104. */
  105. enum {
  106. IH_OS_INVALID = 0, /* Invalid OS */
  107. IH_OS_OPENBSD, /* OpenBSD */
  108. IH_OS_NETBSD, /* NetBSD */
  109. IH_OS_FREEBSD, /* FreeBSD */
  110. IH_OS_4_4BSD, /* 4.4BSD */
  111. IH_OS_LINUX, /* Linux */
  112. IH_OS_SVR4, /* SVR4 */
  113. IH_OS_ESIX, /* Esix */
  114. IH_OS_SOLARIS, /* Solaris */
  115. IH_OS_IRIX, /* Irix */
  116. IH_OS_SCO, /* SCO */
  117. IH_OS_DELL, /* Dell */
  118. IH_OS_NCR, /* NCR */
  119. IH_OS_LYNXOS, /* LynxOS */
  120. IH_OS_VXWORKS, /* VxWorks */
  121. IH_OS_PSOS, /* pSOS */
  122. IH_OS_QNX, /* QNX */
  123. IH_OS_U_BOOT, /* Firmware */
  124. IH_OS_RTEMS, /* RTEMS */
  125. IH_OS_ARTOS, /* ARTOS */
  126. IH_OS_UNITY, /* Unity OS */
  127. IH_OS_INTEGRITY, /* INTEGRITY */
  128. IH_OS_OSE, /* OSE */
  129. IH_OS_PLAN9, /* Plan 9 */
  130. IH_OS_OPENRTOS, /* OpenRTOS */
  131. IH_OS_ARM_TRUSTED_FIRMWARE, /* ARM Trusted Firmware */
  132. IH_OS_TEE, /* Trusted Execution Environment */
  133. IH_OS_OPENSBI, /* RISC-V OpenSBI */
  134. IH_OS_EFI, /* EFI Firmware (e.g. GRUB2) */
  135. IH_OS_COUNT,
  136. };
  137. /*
  138. * CPU Architecture Codes (supported by Linux)
  139. *
  140. * The following are exposed to uImage header.
  141. * New IDs *MUST* be appended at the end of the list and *NEVER*
  142. * inserted for backward compatibility.
  143. */
  144. enum {
  145. IH_ARCH_INVALID = 0, /* Invalid CPU */
  146. IH_ARCH_ALPHA, /* Alpha */
  147. IH_ARCH_ARM, /* ARM */
  148. IH_ARCH_I386, /* Intel x86 */
  149. IH_ARCH_IA64, /* IA64 */
  150. IH_ARCH_MIPS, /* MIPS */
  151. IH_ARCH_MIPS64, /* MIPS 64 Bit */
  152. IH_ARCH_PPC, /* PowerPC */
  153. IH_ARCH_S390, /* IBM S390 */
  154. IH_ARCH_SH, /* SuperH */
  155. IH_ARCH_SPARC, /* Sparc */
  156. IH_ARCH_SPARC64, /* Sparc 64 Bit */
  157. IH_ARCH_M68K, /* M68K */
  158. IH_ARCH_NIOS, /* Nios-32 */
  159. IH_ARCH_MICROBLAZE, /* MicroBlaze */
  160. IH_ARCH_NIOS2, /* Nios-II */
  161. IH_ARCH_BLACKFIN, /* Blackfin */
  162. IH_ARCH_AVR32, /* AVR32 */
  163. IH_ARCH_ST200, /* STMicroelectronics ST200 */
  164. IH_ARCH_SANDBOX, /* Sandbox architecture (test only) */
  165. IH_ARCH_NDS32, /* ANDES Technology - NDS32 */
  166. IH_ARCH_OPENRISC, /* OpenRISC 1000 */
  167. IH_ARCH_ARM64, /* ARM64 */
  168. IH_ARCH_ARC, /* Synopsys DesignWare ARC */
  169. IH_ARCH_X86_64, /* AMD x86_64, Intel and Via */
  170. IH_ARCH_XTENSA, /* Xtensa */
  171. IH_ARCH_RISCV, /* RISC-V */
  172. IH_ARCH_COUNT,
  173. };
  174. /*
  175. * Image Types
  176. *
  177. * "Standalone Programs" are directly runnable in the environment
  178. * provided by U-Boot; it is expected that (if they behave
  179. * well) you can continue to work in U-Boot after return from
  180. * the Standalone Program.
  181. * "OS Kernel Images" are usually images of some Embedded OS which
  182. * will take over control completely. Usually these programs
  183. * will install their own set of exception handlers, device
  184. * drivers, set up the MMU, etc. - this means, that you cannot
  185. * expect to re-enter U-Boot except by resetting the CPU.
  186. * "RAMDisk Images" are more or less just data blocks, and their
  187. * parameters (address, size) are passed to an OS kernel that is
  188. * being started.
  189. * "Multi-File Images" contain several images, typically an OS
  190. * (Linux) kernel image and one or more data images like
  191. * RAMDisks. This construct is useful for instance when you want
  192. * to boot over the network using BOOTP etc., where the boot
  193. * server provides just a single image file, but you want to get
  194. * for instance an OS kernel and a RAMDisk image.
  195. *
  196. * "Multi-File Images" start with a list of image sizes, each
  197. * image size (in bytes) specified by an "uint32_t" in network
  198. * byte order. This list is terminated by an "(uint32_t)0".
  199. * Immediately after the terminating 0 follow the images, one by
  200. * one, all aligned on "uint32_t" boundaries (size rounded up to
  201. * a multiple of 4 bytes - except for the last file).
  202. *
  203. * "Firmware Images" are binary images containing firmware (like
  204. * U-Boot or FPGA images) which usually will be programmed to
  205. * flash memory.
  206. *
  207. * "Script files" are command sequences that will be executed by
  208. * U-Boot's command interpreter; this feature is especially
  209. * useful when you configure U-Boot to use a real shell (hush)
  210. * as command interpreter (=> Shell Scripts).
  211. *
  212. * The following are exposed to uImage header.
  213. * New IDs *MUST* be appended at the end of the list and *NEVER*
  214. * inserted for backward compatibility.
  215. */
  216. enum {
  217. IH_TYPE_INVALID = 0, /* Invalid Image */
  218. IH_TYPE_STANDALONE, /* Standalone Program */
  219. IH_TYPE_KERNEL, /* OS Kernel Image */
  220. IH_TYPE_RAMDISK, /* RAMDisk Image */
  221. IH_TYPE_MULTI, /* Multi-File Image */
  222. IH_TYPE_FIRMWARE, /* Firmware Image */
  223. IH_TYPE_SCRIPT, /* Script file */
  224. IH_TYPE_FILESYSTEM, /* Filesystem Image (any type) */
  225. IH_TYPE_FLATDT, /* Binary Flat Device Tree Blob */
  226. IH_TYPE_KWBIMAGE, /* Kirkwood Boot Image */
  227. IH_TYPE_IMXIMAGE, /* Freescale IMXBoot Image */
  228. IH_TYPE_UBLIMAGE, /* Davinci UBL Image */
  229. IH_TYPE_OMAPIMAGE, /* TI OMAP Config Header Image */
  230. IH_TYPE_AISIMAGE, /* TI Davinci AIS Image */
  231. /* OS Kernel Image, can run from any load address */
  232. IH_TYPE_KERNEL_NOLOAD,
  233. IH_TYPE_PBLIMAGE, /* Freescale PBL Boot Image */
  234. IH_TYPE_MXSIMAGE, /* Freescale MXSBoot Image */
  235. IH_TYPE_GPIMAGE, /* TI Keystone GPHeader Image */
  236. IH_TYPE_ATMELIMAGE, /* ATMEL ROM bootable Image */
  237. IH_TYPE_SOCFPGAIMAGE, /* Altera SOCFPGA CV/AV Preloader */
  238. IH_TYPE_X86_SETUP, /* x86 setup.bin Image */
  239. IH_TYPE_LPC32XXIMAGE, /* x86 setup.bin Image */
  240. IH_TYPE_LOADABLE, /* A list of typeless images */
  241. IH_TYPE_RKIMAGE, /* Rockchip Boot Image */
  242. IH_TYPE_RKSD, /* Rockchip SD card */
  243. IH_TYPE_RKSPI, /* Rockchip SPI image */
  244. IH_TYPE_ZYNQIMAGE, /* Xilinx Zynq Boot Image */
  245. IH_TYPE_ZYNQMPIMAGE, /* Xilinx ZynqMP Boot Image */
  246. IH_TYPE_ZYNQMPBIF, /* Xilinx ZynqMP Boot Image (bif) */
  247. IH_TYPE_FPGA, /* FPGA Image */
  248. IH_TYPE_VYBRIDIMAGE, /* VYBRID .vyb Image */
  249. IH_TYPE_TEE, /* Trusted Execution Environment OS Image */
  250. IH_TYPE_FIRMWARE_IVT, /* Firmware Image with HABv4 IVT */
  251. IH_TYPE_PMMC, /* TI Power Management Micro-Controller Firmware */
  252. IH_TYPE_STM32IMAGE, /* STMicroelectronics STM32 Image */
  253. IH_TYPE_SOCFPGAIMAGE_V1, /* Altera SOCFPGA A10 Preloader */
  254. IH_TYPE_MTKIMAGE, /* MediaTek BootROM loadable Image */
  255. IH_TYPE_IMX8MIMAGE, /* Freescale IMX8MBoot Image */
  256. IH_TYPE_IMX8IMAGE, /* Freescale IMX8Boot Image */
  257. IH_TYPE_COPRO, /* Coprocessor Image for remoteproc*/
  258. IH_TYPE_SUNXI_EGON, /* Allwinner eGON Boot Image */
  259. IH_TYPE_COUNT, /* Number of image types */
  260. };
  261. /*
  262. * Compression Types
  263. *
  264. * The following are exposed to uImage header.
  265. * New IDs *MUST* be appended at the end of the list and *NEVER*
  266. * inserted for backward compatibility.
  267. */
  268. enum {
  269. IH_COMP_NONE = 0, /* No Compression Used */
  270. IH_COMP_GZIP, /* gzip Compression Used */
  271. IH_COMP_BZIP2, /* bzip2 Compression Used */
  272. IH_COMP_LZMA, /* lzma Compression Used */
  273. IH_COMP_LZO, /* lzo Compression Used */
  274. IH_COMP_LZ4, /* lz4 Compression Used */
  275. IH_COMP_ZSTD, /* zstd 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. struct bd_info *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. #if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
  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. * Compression type and magic number mapping table.
  396. */
  397. struct comp_magic_map {
  398. int comp_id;
  399. const char *name;
  400. unsigned char magic[2];
  401. };
  402. /*
  403. * get_table_entry_id() scans the translation table trying to find an
  404. * entry that matches the given short name. If a matching entry is
  405. * found, it's id is returned to the caller.
  406. */
  407. int get_table_entry_id(const table_entry_t *table,
  408. const char *table_name, const char *name);
  409. /*
  410. * get_table_entry_name() scans the translation table trying to find
  411. * an entry that matches the given id. If a matching entry is found,
  412. * its long name is returned to the caller.
  413. */
  414. char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
  415. const char *genimg_get_os_name(uint8_t os);
  416. /**
  417. * genimg_get_os_short_name() - get the short name for an OS
  418. *
  419. * @param os OS (IH_OS_...)
  420. * @return OS short name, or "unknown" if unknown
  421. */
  422. const char *genimg_get_os_short_name(uint8_t comp);
  423. const char *genimg_get_arch_name(uint8_t arch);
  424. /**
  425. * genimg_get_arch_short_name() - get the short name for an architecture
  426. *
  427. * @param arch Architecture type (IH_ARCH_...)
  428. * @return architecture short name, or "unknown" if unknown
  429. */
  430. const char *genimg_get_arch_short_name(uint8_t arch);
  431. const char *genimg_get_type_name(uint8_t type);
  432. /**
  433. * genimg_get_type_short_name() - get the short name for an image type
  434. *
  435. * @param type Image type (IH_TYPE_...)
  436. * @return image short name, or "unknown" if unknown
  437. */
  438. const char *genimg_get_type_short_name(uint8_t type);
  439. const char *genimg_get_comp_name(uint8_t comp);
  440. /**
  441. * genimg_get_comp_short_name() - get the short name for a compression method
  442. *
  443. * @param comp compression method (IH_COMP_...)
  444. * @return compression method short name, or "unknown" if unknown
  445. */
  446. const char *genimg_get_comp_short_name(uint8_t comp);
  447. /**
  448. * genimg_get_cat_name() - Get the name of an item in a category
  449. *
  450. * @category: Category of item
  451. * @id: Item ID
  452. * @return name of item, or "Unknown ..." if unknown
  453. */
  454. const char *genimg_get_cat_name(enum ih_category category, uint id);
  455. /**
  456. * genimg_get_cat_short_name() - Get the short name of an item in a category
  457. *
  458. * @category: Category of item
  459. * @id: Item ID
  460. * @return short name of item, or "Unknown ..." if unknown
  461. */
  462. const char *genimg_get_cat_short_name(enum ih_category category, uint id);
  463. /**
  464. * genimg_get_cat_count() - Get the number of items in a category
  465. *
  466. * @category: Category to check
  467. * @return the number of items in the category (IH_xxx_COUNT)
  468. */
  469. int genimg_get_cat_count(enum ih_category category);
  470. /**
  471. * genimg_get_cat_desc() - Get the description of a category
  472. *
  473. * @category: Category to check
  474. * @return the description of a category, e.g. "architecture". This
  475. * effectively converts the enum to a string.
  476. */
  477. const char *genimg_get_cat_desc(enum ih_category category);
  478. /**
  479. * genimg_cat_has_id() - Check whether a category has an item
  480. *
  481. * @category: Category to check
  482. * @id: Item ID
  483. * @return true or false as to whether a category has an item
  484. */
  485. bool genimg_cat_has_id(enum ih_category category, uint id);
  486. int genimg_get_os_id(const char *name);
  487. int genimg_get_arch_id(const char *name);
  488. int genimg_get_type_id(const char *name);
  489. int genimg_get_comp_id(const char *name);
  490. void genimg_print_size(uint32_t size);
  491. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
  492. defined(USE_HOSTCC)
  493. #define IMAGE_ENABLE_TIMESTAMP 1
  494. #else
  495. #define IMAGE_ENABLE_TIMESTAMP 0
  496. #endif
  497. void genimg_print_time(time_t timestamp);
  498. /* What to do with a image load address ('load = <> 'in the FIT) */
  499. enum fit_load_op {
  500. FIT_LOAD_IGNORED, /* Ignore load address */
  501. FIT_LOAD_OPTIONAL, /* Can be provided, but optional */
  502. FIT_LOAD_OPTIONAL_NON_ZERO, /* Optional, a value of 0 is ignored */
  503. FIT_LOAD_REQUIRED, /* Must be provided */
  504. };
  505. int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
  506. ulong *setup_len);
  507. #ifndef USE_HOSTCC
  508. /* Image format types, returned by _get_format() routine */
  509. #define IMAGE_FORMAT_INVALID 0x00
  510. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  511. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  512. #endif
  513. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  514. #define IMAGE_FORMAT_ANDROID 0x03 /* Android boot image */
  515. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  516. const char **fit_uname_config,
  517. const char **fit_uname_kernel);
  518. ulong genimg_get_kernel_addr(char * const img_addr);
  519. int genimg_get_format(const void *img_addr);
  520. int genimg_has_config(bootm_headers_t *images);
  521. int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
  522. uint8_t arch, const ulong *ld_start, ulong * const ld_len);
  523. int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
  524. uint8_t arch, ulong *rd_start, ulong *rd_end);
  525. /**
  526. * boot_get_loadable - routine to load a list of binaries to memory
  527. * @argc: Ignored Argument
  528. * @argv: Ignored Argument
  529. * @images: pointer to the bootm images structure
  530. * @arch: expected architecture for the image
  531. * @ld_start: Ignored Argument
  532. * @ld_len: Ignored Argument
  533. *
  534. * boot_get_loadable() will take the given FIT configuration, and look
  535. * for a field named "loadables". Loadables, is a list of elements in
  536. * the FIT given as strings. exe:
  537. * loadables = "linux_kernel", "fdt-2";
  538. * this function will attempt to parse each string, and load the
  539. * corresponding element from the FIT into memory. Once placed,
  540. * no aditional actions are taken.
  541. *
  542. * @return:
  543. * 0, if only valid images or no images are found
  544. * error code, if an error occurs during fit_image_load
  545. */
  546. int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
  547. uint8_t arch, const ulong *ld_start, ulong *const ld_len);
  548. #endif /* !USE_HOSTCC */
  549. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  550. ulong *setup_start, ulong *setup_len);
  551. /**
  552. * boot_get_fdt_fit() - load a DTB from a FIT file (applying overlays)
  553. *
  554. * This deals with all aspects of loading an DTB from a FIT.
  555. * The correct base image based on configuration will be selected, and
  556. * then any overlays specified will be applied (as present in fit_uname_configp).
  557. *
  558. * @param images Boot images structure
  559. * @param addr Address of FIT in memory
  560. * @param fit_unamep On entry this is the requested image name
  561. * (e.g. "kernel") or NULL to use the default. On exit
  562. * points to the selected image name
  563. * @param fit_uname_configp On entry this is the requested configuration
  564. * name (e.g. "conf-1") or NULL to use the default. On
  565. * exit points to the selected configuration name.
  566. * @param arch Expected architecture (IH_ARCH_...)
  567. * @param datap Returns address of loaded image
  568. * @param lenp Returns length of loaded image
  569. *
  570. * @return node offset of base image, or -ve error code on error
  571. */
  572. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  573. const char **fit_unamep, const char **fit_uname_configp,
  574. int arch, ulong *datap, ulong *lenp);
  575. /**
  576. * fit_image_load() - load an image from a FIT
  577. *
  578. * This deals with all aspects of loading an image from a FIT, including
  579. * selecting the right image based on configuration, verifying it, printing
  580. * out progress messages, checking the type/arch/os and optionally copying it
  581. * to the right load address.
  582. *
  583. * The property to look up is defined by image_type.
  584. *
  585. * @param images Boot images structure
  586. * @param addr Address of FIT in memory
  587. * @param fit_unamep On entry this is the requested image name
  588. * (e.g. "kernel") or NULL to use the default. On exit
  589. * points to the selected image name
  590. * @param fit_uname_configp On entry this is the requested configuration
  591. * name (e.g. "conf-1") or NULL to use the default. On
  592. * exit points to the selected configuration name.
  593. * @param arch Expected architecture (IH_ARCH_...)
  594. * @param image_type Required image type (IH_TYPE_...). If this is
  595. * IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
  596. * also.
  597. * @param bootstage_id ID of starting bootstage to use for progress updates.
  598. * This will be added to the BOOTSTAGE_SUB values when
  599. * calling bootstage_mark()
  600. * @param load_op Decribes what to do with the load address
  601. * @param datap Returns address of loaded image
  602. * @param lenp Returns length of loaded image
  603. * @return node offset of image, or -ve error code on error
  604. */
  605. int fit_image_load(bootm_headers_t *images, ulong addr,
  606. const char **fit_unamep, const char **fit_uname_configp,
  607. int arch, int image_type, int bootstage_id,
  608. enum fit_load_op load_op, ulong *datap, ulong *lenp);
  609. /**
  610. * image_source_script() - Execute a script
  611. *
  612. * Executes a U-Boot script at a particular address in memory. The script should
  613. * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT).
  614. *
  615. * @addr: Address of script
  616. * @fit_uname: FIT subimage name
  617. * @return result code (enum command_ret_t)
  618. */
  619. int image_source_script(ulong addr, const char *fit_uname);
  620. #ifndef USE_HOSTCC
  621. /**
  622. * fit_get_node_from_config() - Look up an image a FIT by type
  623. *
  624. * This looks in the selected conf- node (images->fit_uname_cfg) for a
  625. * particular image type (e.g. "kernel") and then finds the image that is
  626. * referred to.
  627. *
  628. * For example, for something like:
  629. *
  630. * images {
  631. * kernel {
  632. * ...
  633. * };
  634. * };
  635. * configurations {
  636. * conf-1 {
  637. * kernel = "kernel";
  638. * };
  639. * };
  640. *
  641. * the function will return the node offset of the kernel@1 node, assuming
  642. * that conf-1 is the chosen configuration.
  643. *
  644. * @param images Boot images structure
  645. * @param prop_name Property name to look up (FIT_..._PROP)
  646. * @param addr Address of FIT in memory
  647. */
  648. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  649. ulong addr);
  650. int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
  651. bootm_headers_t *images,
  652. char **of_flat_tree, ulong *of_size);
  653. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
  654. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
  655. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  656. ulong *initrd_start, ulong *initrd_end);
  657. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
  658. #ifdef CONFIG_SYS_BOOT_GET_KBD
  659. int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd);
  660. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  661. #endif /* !USE_HOSTCC */
  662. /*******************************************************************/
  663. /* Legacy format specific code (prefixed with image_) */
  664. /*******************************************************************/
  665. static inline uint32_t image_get_header_size(void)
  666. {
  667. return (sizeof(image_header_t));
  668. }
  669. #define image_get_hdr_l(f) \
  670. static inline uint32_t image_get_##f(const image_header_t *hdr) \
  671. { \
  672. return uimage_to_cpu(hdr->ih_##f); \
  673. }
  674. image_get_hdr_l(magic) /* image_get_magic */
  675. image_get_hdr_l(hcrc) /* image_get_hcrc */
  676. image_get_hdr_l(time) /* image_get_time */
  677. image_get_hdr_l(size) /* image_get_size */
  678. image_get_hdr_l(load) /* image_get_load */
  679. image_get_hdr_l(ep) /* image_get_ep */
  680. image_get_hdr_l(dcrc) /* image_get_dcrc */
  681. #define image_get_hdr_b(f) \
  682. static inline uint8_t image_get_##f(const image_header_t *hdr) \
  683. { \
  684. return hdr->ih_##f; \
  685. }
  686. image_get_hdr_b(os) /* image_get_os */
  687. image_get_hdr_b(arch) /* image_get_arch */
  688. image_get_hdr_b(type) /* image_get_type */
  689. image_get_hdr_b(comp) /* image_get_comp */
  690. static inline char *image_get_name(const image_header_t *hdr)
  691. {
  692. return (char *)hdr->ih_name;
  693. }
  694. static inline uint32_t image_get_data_size(const image_header_t *hdr)
  695. {
  696. return image_get_size(hdr);
  697. }
  698. /**
  699. * image_get_data - get image payload start address
  700. * @hdr: image header
  701. *
  702. * image_get_data() returns address of the image payload. For single
  703. * component images it is image data start. For multi component
  704. * images it points to the null terminated table of sub-images sizes.
  705. *
  706. * returns:
  707. * image payload data start address
  708. */
  709. static inline ulong image_get_data(const image_header_t *hdr)
  710. {
  711. return ((ulong)hdr + image_get_header_size());
  712. }
  713. static inline uint32_t image_get_image_size(const image_header_t *hdr)
  714. {
  715. return (image_get_size(hdr) + image_get_header_size());
  716. }
  717. static inline ulong image_get_image_end(const image_header_t *hdr)
  718. {
  719. return ((ulong)hdr + image_get_image_size(hdr));
  720. }
  721. #define image_set_hdr_l(f) \
  722. static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
  723. { \
  724. hdr->ih_##f = cpu_to_uimage(val); \
  725. }
  726. image_set_hdr_l(magic) /* image_set_magic */
  727. image_set_hdr_l(hcrc) /* image_set_hcrc */
  728. image_set_hdr_l(time) /* image_set_time */
  729. image_set_hdr_l(size) /* image_set_size */
  730. image_set_hdr_l(load) /* image_set_load */
  731. image_set_hdr_l(ep) /* image_set_ep */
  732. image_set_hdr_l(dcrc) /* image_set_dcrc */
  733. #define image_set_hdr_b(f) \
  734. static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
  735. { \
  736. hdr->ih_##f = val; \
  737. }
  738. image_set_hdr_b(os) /* image_set_os */
  739. image_set_hdr_b(arch) /* image_set_arch */
  740. image_set_hdr_b(type) /* image_set_type */
  741. image_set_hdr_b(comp) /* image_set_comp */
  742. static inline void image_set_name(image_header_t *hdr, const char *name)
  743. {
  744. strncpy(image_get_name(hdr), name, IH_NMLEN);
  745. }
  746. int image_check_hcrc(const image_header_t *hdr);
  747. int image_check_dcrc(const image_header_t *hdr);
  748. #ifndef USE_HOSTCC
  749. ulong env_get_bootm_low(void);
  750. phys_size_t env_get_bootm_size(void);
  751. phys_size_t env_get_bootm_mapsize(void);
  752. #endif
  753. void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
  754. static inline int image_check_magic(const image_header_t *hdr)
  755. {
  756. return (image_get_magic(hdr) == IH_MAGIC);
  757. }
  758. static inline int image_check_type(const image_header_t *hdr, uint8_t type)
  759. {
  760. return (image_get_type(hdr) == type);
  761. }
  762. static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
  763. {
  764. #ifndef USE_HOSTCC
  765. /* Let's assume that sandbox can load any architecture */
  766. if (IS_ENABLED(CONFIG_SANDBOX))
  767. return true;
  768. #endif
  769. return (image_get_arch(hdr) == arch) ||
  770. (image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
  771. }
  772. static inline int image_check_os(const image_header_t *hdr, uint8_t os)
  773. {
  774. return (image_get_os(hdr) == os);
  775. }
  776. ulong image_multi_count(const image_header_t *hdr);
  777. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  778. ulong *data, ulong *len);
  779. void image_print_contents(const void *hdr);
  780. #ifndef USE_HOSTCC
  781. static inline int image_check_target_arch(const image_header_t *hdr)
  782. {
  783. #ifndef IH_ARCH_DEFAULT
  784. # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
  785. #endif
  786. return image_check_arch(hdr, IH_ARCH_DEFAULT);
  787. }
  788. #endif /* USE_HOSTCC */
  789. /**
  790. * image_decomp_type() - Find out compression type of an image
  791. *
  792. * @buf: Address in U-Boot memory where image is loaded.
  793. * @len: Length of the compressed image.
  794. * @return compression type or IH_COMP_NONE if not compressed.
  795. *
  796. * Note: Only following compression types are supported now.
  797. * lzo, lzma, gzip, bzip2
  798. */
  799. int image_decomp_type(const unsigned char *buf, ulong len);
  800. /**
  801. * image_decomp() - decompress an image
  802. *
  803. * @comp: Compression algorithm that is used (IH_COMP_...)
  804. * @load: Destination load address in U-Boot memory
  805. * @image_start Image start address (where we are decompressing from)
  806. * @type: OS type (IH_OS_...)
  807. * @load_bug: Place to decompress to
  808. * @image_buf: Address to decompress from
  809. * @image_len: Number of bytes in @image_buf to decompress
  810. * @unc_len: Available space for decompression
  811. * @return 0 if OK, -ve on error (BOOTM_ERR_...)
  812. */
  813. int image_decomp(int comp, ulong load, ulong image_start, int type,
  814. void *load_buf, void *image_buf, ulong image_len,
  815. uint unc_len, ulong *load_end);
  816. /**
  817. * Set up properties in the FDT
  818. *
  819. * This sets up properties in the FDT that is to be passed to linux.
  820. *
  821. * @images: Images information
  822. * @blob: FDT to update
  823. * @of_size: Size of the FDT
  824. * @lmb: Points to logical memory block structure
  825. * @return 0 if ok, <0 on failure
  826. */
  827. int image_setup_libfdt(bootm_headers_t *images, void *blob,
  828. int of_size, struct lmb *lmb);
  829. /**
  830. * Set up the FDT to use for booting a kernel
  831. *
  832. * This performs ramdisk setup, sets up the FDT if required, and adds
  833. * paramters to the FDT if libfdt is available.
  834. *
  835. * @param images Images information
  836. * @return 0 if ok, <0 on failure
  837. */
  838. int image_setup_linux(bootm_headers_t *images);
  839. /**
  840. * bootz_setup() - Extract stat and size of a Linux xImage
  841. *
  842. * @image: Address of image
  843. * @start: Returns start address of image
  844. * @end : Returns end address of image
  845. * @return 0 if OK, 1 if the image was not recognised
  846. */
  847. int bootz_setup(ulong image, ulong *start, ulong *end);
  848. /**
  849. * Return the correct start address and size of a Linux aarch64 Image.
  850. *
  851. * @image: Address of image
  852. * @start: Returns start address of image
  853. * @size : Returns size image
  854. * @force_reloc: Ignore image->ep field, always place image to RAM start
  855. * @return 0 if OK, 1 if the image was not recognised
  856. */
  857. int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
  858. bool force_reloc);
  859. /*******************************************************************/
  860. /* New uImage format specific code (prefixed with fit_) */
  861. /*******************************************************************/
  862. #define FIT_IMAGES_PATH "/images"
  863. #define FIT_CONFS_PATH "/configurations"
  864. /* hash/signature/key node */
  865. #define FIT_HASH_NODENAME "hash"
  866. #define FIT_ALGO_PROP "algo"
  867. #define FIT_VALUE_PROP "value"
  868. #define FIT_IGNORE_PROP "uboot-ignore"
  869. #define FIT_SIG_NODENAME "signature"
  870. #define FIT_KEY_REQUIRED "required"
  871. #define FIT_KEY_HINT "key-name-hint"
  872. /* cipher node */
  873. #define FIT_CIPHER_NODENAME "cipher"
  874. #define FIT_ALGO_PROP "algo"
  875. /* image node */
  876. #define FIT_DATA_PROP "data"
  877. #define FIT_DATA_POSITION_PROP "data-position"
  878. #define FIT_DATA_OFFSET_PROP "data-offset"
  879. #define FIT_DATA_SIZE_PROP "data-size"
  880. #define FIT_TIMESTAMP_PROP "timestamp"
  881. #define FIT_DESC_PROP "description"
  882. #define FIT_ARCH_PROP "arch"
  883. #define FIT_TYPE_PROP "type"
  884. #define FIT_OS_PROP "os"
  885. #define FIT_COMP_PROP "compression"
  886. #define FIT_ENTRY_PROP "entry"
  887. #define FIT_LOAD_PROP "load"
  888. /* configuration node */
  889. #define FIT_KERNEL_PROP "kernel"
  890. #define FIT_RAMDISK_PROP "ramdisk"
  891. #define FIT_FDT_PROP "fdt"
  892. #define FIT_LOADABLE_PROP "loadables"
  893. #define FIT_DEFAULT_PROP "default"
  894. #define FIT_SETUP_PROP "setup"
  895. #define FIT_FPGA_PROP "fpga"
  896. #define FIT_FIRMWARE_PROP "firmware"
  897. #define FIT_STANDALONE_PROP "standalone"
  898. #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
  899. #if IMAGE_ENABLE_FIT
  900. /* cmdline argument format parsing */
  901. int fit_parse_conf(const char *spec, ulong addr_curr,
  902. ulong *addr, const char **conf_name);
  903. int fit_parse_subimage(const char *spec, ulong addr_curr,
  904. ulong *addr, const char **image_name);
  905. int fit_get_subimage_count(const void *fit, int images_noffset);
  906. void fit_print_contents(const void *fit);
  907. void fit_image_print(const void *fit, int noffset, const char *p);
  908. /**
  909. * fit_get_end - get FIT image size
  910. * @fit: pointer to the FIT format image header
  911. *
  912. * returns:
  913. * size of the FIT image (blob) in memory
  914. */
  915. static inline ulong fit_get_size(const void *fit)
  916. {
  917. return fdt_totalsize(fit);
  918. }
  919. /**
  920. * fit_get_end - get FIT image end
  921. * @fit: pointer to the FIT format image header
  922. *
  923. * returns:
  924. * end address of the FIT image (blob) in memory
  925. */
  926. ulong fit_get_end(const void *fit);
  927. /**
  928. * fit_get_name - get FIT node name
  929. * @fit: pointer to the FIT format image header
  930. *
  931. * returns:
  932. * NULL, on error
  933. * pointer to node name, on success
  934. */
  935. static inline const char *fit_get_name(const void *fit_hdr,
  936. int noffset, int *len)
  937. {
  938. return fdt_get_name(fit_hdr, noffset, len);
  939. }
  940. int fit_get_desc(const void *fit, int noffset, char **desc);
  941. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
  942. int fit_image_get_node(const void *fit, const char *image_uname);
  943. int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
  944. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
  945. int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
  946. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
  947. int fit_image_get_load(const void *fit, int noffset, ulong *load);
  948. int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
  949. int fit_image_get_data(const void *fit, int noffset,
  950. const void **data, size_t *size);
  951. int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
  952. int fit_image_get_data_position(const void *fit, int noffset,
  953. int *data_position);
  954. int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
  955. int fit_image_get_data_size_unciphered(const void *fit, int noffset,
  956. size_t *data_size);
  957. int fit_image_get_data_and_size(const void *fit, int noffset,
  958. const void **data, size_t *size);
  959. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
  960. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  961. int *value_len);
  962. int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
  963. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  964. const char *comment, int require_keys,
  965. const char *engine_id, const char *cmdname);
  966. /**
  967. * fit_add_verification_data() - add verification data to FIT image nodes
  968. *
  969. * @keydir: Directory containing keys
  970. * @kwydest: FDT blob to write public key information to
  971. * @fit: Pointer to the FIT format image header
  972. * @comment: Comment to add to signature nodes
  973. * @require_keys: Mark all keys as 'required'
  974. * @engine_id: Engine to use for signing
  975. * @cmdname: Command name used when reporting errors
  976. *
  977. * Adds hash values for all component images in the FIT blob.
  978. * Hashes are calculated for all component images which have hash subnodes
  979. * with algorithm property set to one of the supported hash algorithms.
  980. *
  981. * Also add signatures if signature nodes are present.
  982. *
  983. * returns
  984. * 0, on success
  985. * libfdt error code, on failure
  986. */
  987. int fit_add_verification_data(const char *keydir, const char *keyfile,
  988. void *keydest, void *fit, const char *comment,
  989. int require_keys, const char *engine_id,
  990. const char *cmdname);
  991. int fit_image_verify_with_data(const void *fit, int image_noffset,
  992. const void *data, size_t size);
  993. int fit_image_verify(const void *fit, int noffset);
  994. int fit_config_verify(const void *fit, int conf_noffset);
  995. int fit_all_image_verify(const void *fit);
  996. int fit_config_decrypt(const void *fit, int conf_noffset);
  997. int fit_image_check_os(const void *fit, int noffset, uint8_t os);
  998. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
  999. int fit_image_check_type(const void *fit, int noffset, uint8_t type);
  1000. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
  1001. /**
  1002. * fit_check_format() - Check that the FIT is valid
  1003. *
  1004. * This performs various checks on the FIT to make sure it is suitable for
  1005. * use, looking for mandatory properties, nodes, etc.
  1006. *
  1007. * If FIT_FULL_CHECK is enabled, it also runs it through libfdt to make
  1008. * sure that there are no strange tags or broken nodes in the FIT.
  1009. *
  1010. * @fit: pointer to the FIT format image header
  1011. * @return 0 if OK, -ENOEXEC if not an FDT file, -EINVAL if the full FDT check
  1012. * failed (e.g. due to bad structure), -ENOMSG if the description is
  1013. * missing, -EBADMSG if the timestamp is missing, -ENOENT if the /images
  1014. * path is missing
  1015. */
  1016. int fit_check_format(const void *fit, ulong size);
  1017. int fit_conf_find_compat(const void *fit, const void *fdt);
  1018. /**
  1019. * fit_conf_get_node - get node offset for configuration of a given unit name
  1020. * @fit: pointer to the FIT format image header
  1021. * @conf_uname: configuration node unit name (NULL to use default)
  1022. *
  1023. * fit_conf_get_node() finds a configuration (within the '/configurations'
  1024. * parent node) of a provided unit name. If configuration is found its node
  1025. * offset is returned to the caller.
  1026. *
  1027. * When NULL is provided in second argument fit_conf_get_node() will search
  1028. * for a default configuration node instead. Default configuration node unit
  1029. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1030. * node.
  1031. *
  1032. * returns:
  1033. * configuration node offset when found (>=0)
  1034. * negative number on failure (FDT_ERR_* code)
  1035. */
  1036. int fit_conf_get_node(const void *fit, const char *conf_uname);
  1037. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1038. const char *prop_name);
  1039. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1040. const char *prop_name, int index);
  1041. /**
  1042. * fit_conf_get_prop_node() - Get node refered to by a configuration
  1043. * @fit: FIT to check
  1044. * @noffset: Offset of conf@xxx node to check
  1045. * @prop_name: Property to read from the conf node
  1046. *
  1047. * The conf- nodes contain references to other nodes, using properties
  1048. * like 'kernel = "kernel"'. Given such a property name (e.g. "kernel"),
  1049. * return the offset of the node referred to (e.g. offset of node
  1050. * "/images/kernel".
  1051. */
  1052. int fit_conf_get_prop_node(const void *fit, int noffset,
  1053. const char *prop_name);
  1054. int fit_check_ramdisk(const void *fit, int os_noffset,
  1055. uint8_t arch, int verify);
  1056. #endif /* IMAGE_ENABLE_FIT */
  1057. int calculate_hash(const void *data, int data_len, const char *algo,
  1058. uint8_t *value, int *value_len);
  1059. /*
  1060. * At present we only support signing on the host, and verification on the
  1061. * device
  1062. */
  1063. #if defined(USE_HOSTCC)
  1064. # if defined(CONFIG_FIT_SIGNATURE)
  1065. # define IMAGE_ENABLE_SIGN 1
  1066. # define FIT_IMAGE_ENABLE_VERIFY 1
  1067. # include <openssl/evp.h>
  1068. # else
  1069. # define IMAGE_ENABLE_SIGN 0
  1070. # define FIT_IMAGE_ENABLE_VERIFY 0
  1071. # endif
  1072. #else
  1073. # define IMAGE_ENABLE_SIGN 0
  1074. # define FIT_IMAGE_ENABLE_VERIFY CONFIG_IS_ENABLED(FIT_SIGNATURE)
  1075. #endif
  1076. #if IMAGE_ENABLE_FIT
  1077. #ifdef USE_HOSTCC
  1078. void *image_get_host_blob(void);
  1079. void image_set_host_blob(void *host_blob);
  1080. # define gd_fdt_blob() image_get_host_blob()
  1081. #else
  1082. # define gd_fdt_blob() (gd->fdt_blob)
  1083. #endif
  1084. #endif /* IMAGE_ENABLE_FIT */
  1085. /*
  1086. * Information passed to the signing routines
  1087. *
  1088. * Either 'keydir', 'keyname', or 'keyfile' can be NULL. However, either
  1089. * 'keyfile', or both 'keydir' and 'keyname' should have valid values. If
  1090. * neither are valid, some operations might fail with EINVAL.
  1091. */
  1092. struct image_sign_info {
  1093. const char *keydir; /* Directory conaining keys */
  1094. const char *keyname; /* Name of key to use */
  1095. const char *keyfile; /* Filename of private or public key */
  1096. void *fit; /* Pointer to FIT blob */
  1097. int node_offset; /* Offset of signature node */
  1098. const char *name; /* Algorithm name */
  1099. struct checksum_algo *checksum; /* Checksum algorithm information */
  1100. struct padding_algo *padding; /* Padding algorithm information */
  1101. struct crypto_algo *crypto; /* Crypto algorithm information */
  1102. const void *fdt_blob; /* FDT containing public keys */
  1103. int required_keynode; /* Node offset of key to use: -1=any */
  1104. const char *require_keys; /* Value for 'required' property */
  1105. const char *engine_id; /* Engine to use for signing */
  1106. /*
  1107. * Note: the following two fields are always valid even w/o
  1108. * RSA_VERIFY_WITH_PKEY in order to make sure this structure is
  1109. * the same on target and host. Otherwise, vboot test may fail.
  1110. */
  1111. const void *key; /* Pointer to public key in DER */
  1112. int keylen; /* Length of public key */
  1113. };
  1114. /* A part of an image, used for hashing */
  1115. struct image_region {
  1116. const void *data;
  1117. int size;
  1118. };
  1119. #if FIT_IMAGE_ENABLE_VERIFY
  1120. # include <u-boot/hash-checksum.h>
  1121. #endif
  1122. struct checksum_algo {
  1123. const char *name;
  1124. const int checksum_len;
  1125. const int der_len;
  1126. const uint8_t *der_prefix;
  1127. #if IMAGE_ENABLE_SIGN
  1128. const EVP_MD *(*calculate_sign)(void);
  1129. #endif
  1130. int (*calculate)(const char *name,
  1131. const struct image_region region[],
  1132. int region_count, uint8_t *checksum);
  1133. };
  1134. struct crypto_algo {
  1135. const char *name; /* Name of algorithm */
  1136. const int key_len;
  1137. /**
  1138. * sign() - calculate and return signature for given input data
  1139. *
  1140. * @info: Specifies key and FIT information
  1141. * @data: Pointer to the input data
  1142. * @data_len: Data length
  1143. * @sigp: Set to an allocated buffer holding the signature
  1144. * @sig_len: Set to length of the calculated hash
  1145. *
  1146. * This computes input data signature according to selected algorithm.
  1147. * Resulting signature value is placed in an allocated buffer, the
  1148. * pointer is returned as *sigp. The length of the calculated
  1149. * signature is returned via the sig_len pointer argument. The caller
  1150. * should free *sigp.
  1151. *
  1152. * @return: 0, on success, -ve on error
  1153. */
  1154. int (*sign)(struct image_sign_info *info,
  1155. const struct image_region region[],
  1156. int region_count, uint8_t **sigp, uint *sig_len);
  1157. /**
  1158. * add_verify_data() - Add verification information to FDT
  1159. *
  1160. * Add public key information to the FDT node, suitable for
  1161. * verification at run-time. The information added depends on the
  1162. * algorithm being used.
  1163. *
  1164. * @info: Specifies key and FIT information
  1165. * @keydest: Destination FDT blob for public key data
  1166. * @return: 0, on success, -ve on error
  1167. */
  1168. int (*add_verify_data)(struct image_sign_info *info, void *keydest);
  1169. /**
  1170. * verify() - Verify a signature against some data
  1171. *
  1172. * @info: Specifies key and FIT information
  1173. * @data: Pointer to the input data
  1174. * @data_len: Data length
  1175. * @sig: Signature
  1176. * @sig_len: Number of bytes in signature
  1177. * @return 0 if verified, -ve on error
  1178. */
  1179. int (*verify)(struct image_sign_info *info,
  1180. const struct image_region region[], int region_count,
  1181. uint8_t *sig, uint sig_len);
  1182. };
  1183. /* Declare a new U-Boot crypto algorithm handler */
  1184. #define U_BOOT_CRYPTO_ALGO(__name) \
  1185. ll_entry_declare(struct crypto_algo, __name, cryptos)
  1186. struct padding_algo {
  1187. const char *name;
  1188. int (*verify)(struct image_sign_info *info,
  1189. uint8_t *pad, int pad_len,
  1190. const uint8_t *hash, int hash_len);
  1191. };
  1192. /**
  1193. * image_get_checksum_algo() - Look up a checksum algorithm
  1194. *
  1195. * @param full_name Name of algorithm in the form "checksum,crypto"
  1196. * @return pointer to algorithm information, or NULL if not found
  1197. */
  1198. struct checksum_algo *image_get_checksum_algo(const char *full_name);
  1199. /**
  1200. * image_get_crypto_algo() - Look up a cryptosystem algorithm
  1201. *
  1202. * @param full_name Name of algorithm in the form "checksum,crypto"
  1203. * @return pointer to algorithm information, or NULL if not found
  1204. */
  1205. struct crypto_algo *image_get_crypto_algo(const char *full_name);
  1206. /**
  1207. * image_get_padding_algo() - Look up a padding algorithm
  1208. *
  1209. * @param name Name of padding algorithm
  1210. * @return pointer to algorithm information, or NULL if not found
  1211. */
  1212. struct padding_algo *image_get_padding_algo(const char *name);
  1213. #if IMAGE_ENABLE_FIT
  1214. /**
  1215. * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  1216. *
  1217. * @fit: FIT to check
  1218. * @image_noffset: Offset of image node to check
  1219. * @data: Image data to check
  1220. * @size: Size of image data
  1221. * @sig_blob: FDT containing public keys
  1222. * @no_sigsp: Returns 1 if no signatures were required, and
  1223. * therefore nothing was checked. The caller may wish
  1224. * to fall back to other mechanisms, or refuse to
  1225. * boot.
  1226. * @return 0 if all verified ok, <0 on error
  1227. */
  1228. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  1229. const char *data, size_t size, const void *sig_blob,
  1230. int *no_sigsp);
  1231. /**
  1232. * fit_image_check_sig() - Check a single image signature node
  1233. *
  1234. * @fit: FIT to check
  1235. * @noffset: Offset of signature node to check
  1236. * @data: Image data to check
  1237. * @size: Size of image data
  1238. * @required_keynode: Offset in the control FDT of the required key node,
  1239. * if any. If this is given, then the image wil not
  1240. * pass verification unless that key is used. If this is
  1241. * -1 then any signature will do.
  1242. * @err_msgp: In the event of an error, this will be pointed to a
  1243. * help error string to display to the user.
  1244. * @return 0 if all verified ok, <0 on error
  1245. */
  1246. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  1247. size_t size, int required_keynode, char **err_msgp);
  1248. int fit_image_decrypt_data(const void *fit,
  1249. int image_noffset, int cipher_noffset,
  1250. const void *data, size_t size,
  1251. void **data_unciphered, size_t *size_unciphered);
  1252. /**
  1253. * fit_region_make_list() - Make a list of regions to hash
  1254. *
  1255. * Given a list of FIT regions (offset, size) provided by libfdt, create
  1256. * a list of regions (void *, size) for use by the signature creationg
  1257. * and verification code.
  1258. *
  1259. * @fit: FIT image to process
  1260. * @fdt_regions: Regions as returned by libfdt
  1261. * @count: Number of regions returned by libfdt
  1262. * @region: Place to put list of regions (NULL to allocate it)
  1263. * @return pointer to list of regions, or NULL if out of memory
  1264. */
  1265. struct image_region *fit_region_make_list(const void *fit,
  1266. struct fdt_region *fdt_regions, int count,
  1267. struct image_region *region);
  1268. static inline int fit_image_check_target_arch(const void *fdt, int node)
  1269. {
  1270. #ifndef USE_HOSTCC
  1271. return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
  1272. #else
  1273. return 0;
  1274. #endif
  1275. }
  1276. /*
  1277. * At present we only support ciphering on the host, and unciphering on the
  1278. * device
  1279. */
  1280. #if defined(USE_HOSTCC)
  1281. # if defined(CONFIG_FIT_CIPHER)
  1282. # define IMAGE_ENABLE_ENCRYPT 1
  1283. # define IMAGE_ENABLE_DECRYPT 1
  1284. # include <openssl/evp.h>
  1285. # else
  1286. # define IMAGE_ENABLE_ENCRYPT 0
  1287. # define IMAGE_ENABLE_DECRYPT 0
  1288. # endif
  1289. #else
  1290. # define IMAGE_ENABLE_ENCRYPT 0
  1291. # define IMAGE_ENABLE_DECRYPT CONFIG_IS_ENABLED(FIT_CIPHER)
  1292. #endif
  1293. /* Information passed to the ciphering routines */
  1294. struct image_cipher_info {
  1295. const char *keydir; /* Directory containing keys */
  1296. const char *keyname; /* Name of key to use */
  1297. const char *ivname; /* Name of IV to use */
  1298. const void *fit; /* Pointer to FIT blob */
  1299. int node_noffset; /* Offset of the cipher node */
  1300. const char *name; /* Algorithm name */
  1301. struct cipher_algo *cipher; /* Cipher algorithm information */
  1302. const void *fdt_blob; /* FDT containing key and IV */
  1303. const void *key; /* Value of the key */
  1304. const void *iv; /* Value of the IV */
  1305. size_t size_unciphered; /* Size of the unciphered data */
  1306. };
  1307. struct cipher_algo {
  1308. const char *name; /* Name of algorithm */
  1309. int key_len; /* Length of the key */
  1310. int iv_len; /* Length of the IV */
  1311. #if IMAGE_ENABLE_ENCRYPT
  1312. const EVP_CIPHER * (*calculate_type)(void);
  1313. #endif
  1314. int (*encrypt)(struct image_cipher_info *info,
  1315. const unsigned char *data, int data_len,
  1316. unsigned char **cipher, int *cipher_len);
  1317. int (*add_cipher_data)(struct image_cipher_info *info,
  1318. void *keydest, void *fit, int node_noffset);
  1319. int (*decrypt)(struct image_cipher_info *info,
  1320. const void *cipher, size_t cipher_len,
  1321. void **data, size_t *data_len);
  1322. };
  1323. int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
  1324. struct cipher_algo *image_get_cipher_algo(const char *full_name);
  1325. #ifdef CONFIG_FIT_VERBOSE
  1326. #define fit_unsupported(msg) printf("! %s:%d " \
  1327. "FIT images not supported for '%s'\n", \
  1328. __FILE__, __LINE__, (msg))
  1329. #define fit_unsupported_reset(msg) printf("! %s:%d " \
  1330. "FIT images not supported for '%s' " \
  1331. "- must reset board to recover!\n", \
  1332. __FILE__, __LINE__, (msg))
  1333. #else
  1334. #define fit_unsupported(msg)
  1335. #define fit_unsupported_reset(msg)
  1336. #endif /* CONFIG_FIT_VERBOSE */
  1337. #endif /* CONFIG_FIT */
  1338. #if !defined(USE_HOSTCC)
  1339. #if defined(CONFIG_ANDROID_BOOT_IMAGE)
  1340. struct andr_img_hdr;
  1341. int android_image_check_header(const struct andr_img_hdr *hdr);
  1342. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  1343. ulong *os_data, ulong *os_len);
  1344. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  1345. ulong *rd_data, ulong *rd_len);
  1346. int android_image_get_second(const struct andr_img_hdr *hdr,
  1347. ulong *second_data, ulong *second_len);
  1348. bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
  1349. bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
  1350. u32 *size);
  1351. ulong android_image_get_end(const struct andr_img_hdr *hdr);
  1352. ulong android_image_get_kload(const struct andr_img_hdr *hdr);
  1353. ulong android_image_get_kcomp(const struct andr_img_hdr *hdr);
  1354. void android_print_contents(const struct andr_img_hdr *hdr);
  1355. #if !defined(CONFIG_SPL_BUILD)
  1356. bool android_image_print_dtb_contents(ulong hdr_addr);
  1357. #endif
  1358. #endif /* CONFIG_ANDROID_BOOT_IMAGE */
  1359. #endif /* !USE_HOSTCC */
  1360. /**
  1361. * board_fit_config_name_match() - Check for a matching board name
  1362. *
  1363. * This is used when SPL loads a FIT containing multiple device tree files
  1364. * and wants to work out which one to use. The description of each one is
  1365. * passed to this function. The description comes from the 'description' field
  1366. * in each (FDT) image node.
  1367. *
  1368. * @name: Device tree description
  1369. * @return 0 if this device tree should be used, non-zero to try the next
  1370. */
  1371. int board_fit_config_name_match(const char *name);
  1372. /**
  1373. * board_fit_image_post_process() - Do any post-process on FIT binary data
  1374. *
  1375. * This is used to do any sort of image manipulation, verification, decryption
  1376. * etc. in a platform or board specific way. Obviously, anything done here would
  1377. * need to be comprehended in how the images were prepared before being injected
  1378. * into the FIT creation (i.e. the binary blobs would have been pre-processed
  1379. * before being added to the FIT image).
  1380. *
  1381. * @fit: pointer to fit image
  1382. * @node: offset of image node
  1383. * @image: pointer to the image start pointer
  1384. * @size: pointer to the image size
  1385. * @return no return value (failure should be handled internally)
  1386. */
  1387. void board_fit_image_post_process(const void *fit, int node, void **p_image,
  1388. size_t *p_size);
  1389. #define FDT_ERROR ((ulong)(-1))
  1390. ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
  1391. /**
  1392. * fit_find_config_node() - Find the node for the best DTB in a FIT image
  1393. *
  1394. * A FIT image contains one or more DTBs. This function parses the
  1395. * configurations described in the FIT images and returns the node of
  1396. * the first matching DTB. To check if a DTB matches a board, this function
  1397. * calls board_fit_config_name_match(). If no matching DTB is found, it returns
  1398. * the node described by the default configuration if it exists.
  1399. *
  1400. * @fdt: pointer to flat device tree
  1401. * @return the node if found, -ve otherwise
  1402. */
  1403. int fit_find_config_node(const void *fdt);
  1404. /**
  1405. * Mapping of image types to function handlers to be invoked on the associated
  1406. * loaded images
  1407. *
  1408. * @type: Type of image, I.E. IH_TYPE_*
  1409. * @handler: Function to call on loaded image
  1410. */
  1411. struct fit_loadable_tbl {
  1412. int type;
  1413. /**
  1414. * handler() - Process a loaded image
  1415. *
  1416. * @data: Pointer to start of loaded image data
  1417. * @size: Size of loaded image data
  1418. */
  1419. void (*handler)(ulong data, size_t size);
  1420. };
  1421. /*
  1422. * Define a FIT loadable image type handler
  1423. *
  1424. * _type is a valid uimage_type ID as defined in the "Image Type" enum above
  1425. * _handler is the handler function to call after this image type is loaded
  1426. */
  1427. #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
  1428. ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
  1429. .type = _type, \
  1430. .handler = _handler, \
  1431. }
  1432. /**
  1433. * fit_update - update storage with FIT image
  1434. * @fit: Pointer to FIT image
  1435. *
  1436. * Update firmware on storage using FIT image as input.
  1437. * The storage area to be update will be identified by the name
  1438. * in FIT and matching it to "dfu_alt_info" variable.
  1439. *
  1440. * Return: 0 on success, non-zero otherwise
  1441. */
  1442. int fit_update(const void *fit);
  1443. #endif /* __IMAGE_H__ */