image.h 44 KB

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