image.h 52 KB

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