image.h 43 KB

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