image.h 43 KB

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