image.h 52 KB

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