image.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. /* new uImage format support enabled on host */
  23. #define CONFIG_FIT 1
  24. #define CONFIG_OF_LIBFDT 1
  25. #define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */
  26. #define IMAGE_ENABLE_IGNORE 0
  27. #define IMAGE_INDENT_STRING ""
  28. #else
  29. #include <lmb.h>
  30. #include <asm/u-boot.h>
  31. #include <command.h>
  32. /* Take notice of the 'ignore' property for hashes */
  33. #define IMAGE_ENABLE_IGNORE 1
  34. #define IMAGE_INDENT_STRING " "
  35. #endif /* USE_HOSTCC */
  36. #if defined(CONFIG_FIT)
  37. #include <hash.h>
  38. #include <libfdt.h>
  39. #include <fdt_support.h>
  40. # ifdef CONFIG_SPL_BUILD
  41. # ifdef CONFIG_SPL_CRC32_SUPPORT
  42. # define IMAGE_ENABLE_CRC32 1
  43. # endif
  44. # ifdef CONFIG_SPL_MD5_SUPPORT
  45. # define IMAGE_ENABLE_MD5 1
  46. # endif
  47. # ifdef CONFIG_SPL_SHA1_SUPPORT
  48. # define IMAGE_ENABLE_SHA1 1
  49. # endif
  50. # ifdef CONFIG_SPL_SHA256_SUPPORT
  51. # define IMAGE_ENABLE_SHA256 1
  52. # endif
  53. # else
  54. # define CONFIG_CRC32 /* FIT images need CRC32 support */
  55. # define CONFIG_MD5 /* and MD5 */
  56. # define CONFIG_SHA1 /* and SHA1 */
  57. # define CONFIG_SHA256 /* and SHA256 */
  58. # define IMAGE_ENABLE_CRC32 1
  59. # define IMAGE_ENABLE_MD5 1
  60. # define IMAGE_ENABLE_SHA1 1
  61. # define IMAGE_ENABLE_SHA256 1
  62. # endif
  63. #ifdef CONFIG_FIT_DISABLE_SHA256
  64. #undef CONFIG_SHA256
  65. #undef IMAGE_ENABLE_SHA256
  66. #endif
  67. #ifndef IMAGE_ENABLE_CRC32
  68. #define IMAGE_ENABLE_CRC32 0
  69. #endif
  70. #ifndef IMAGE_ENABLE_MD5
  71. #define IMAGE_ENABLE_MD5 0
  72. #endif
  73. #ifndef IMAGE_ENABLE_SHA1
  74. #define IMAGE_ENABLE_SHA1 0
  75. #endif
  76. #ifndef IMAGE_ENABLE_SHA256
  77. #define IMAGE_ENABLE_SHA256 0
  78. #endif
  79. #endif /* CONFIG_FIT */
  80. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  81. # define IMAGE_ENABLE_RAMDISK_HIGH 1
  82. #else
  83. # define IMAGE_ENABLE_RAMDISK_HIGH 0
  84. #endif
  85. #ifdef CONFIG_OF_LIBFDT
  86. # define IMAGE_ENABLE_OF_LIBFDT 1
  87. #else
  88. # define IMAGE_ENABLE_OF_LIBFDT 0
  89. #endif
  90. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  91. # define IMAGE_BOOT_GET_CMDLINE 1
  92. #else
  93. # define IMAGE_BOOT_GET_CMDLINE 0
  94. #endif
  95. #ifdef CONFIG_OF_BOARD_SETUP
  96. # define IMAGE_OF_BOARD_SETUP 1
  97. #else
  98. # define IMAGE_OF_BOARD_SETUP 0
  99. #endif
  100. /*
  101. * Operating System Codes
  102. */
  103. #define IH_OS_INVALID 0 /* Invalid OS */
  104. #define IH_OS_OPENBSD 1 /* OpenBSD */
  105. #define IH_OS_NETBSD 2 /* NetBSD */
  106. #define IH_OS_FREEBSD 3 /* FreeBSD */
  107. #define IH_OS_4_4BSD 4 /* 4.4BSD */
  108. #define IH_OS_LINUX 5 /* Linux */
  109. #define IH_OS_SVR4 6 /* SVR4 */
  110. #define IH_OS_ESIX 7 /* Esix */
  111. #define IH_OS_SOLARIS 8 /* Solaris */
  112. #define IH_OS_IRIX 9 /* Irix */
  113. #define IH_OS_SCO 10 /* SCO */
  114. #define IH_OS_DELL 11 /* Dell */
  115. #define IH_OS_NCR 12 /* NCR */
  116. #define IH_OS_LYNXOS 13 /* LynxOS */
  117. #define IH_OS_VXWORKS 14 /* VxWorks */
  118. #define IH_OS_PSOS 15 /* pSOS */
  119. #define IH_OS_QNX 16 /* QNX */
  120. #define IH_OS_U_BOOT 17 /* Firmware */
  121. #define IH_OS_RTEMS 18 /* RTEMS */
  122. #define IH_OS_ARTOS 19 /* ARTOS */
  123. #define IH_OS_UNITY 20 /* Unity OS */
  124. #define IH_OS_INTEGRITY 21 /* INTEGRITY */
  125. #define IH_OS_OSE 22 /* OSE */
  126. #define IH_OS_PLAN9 23 /* Plan 9 */
  127. /*
  128. * CPU Architecture Codes (supported by Linux)
  129. */
  130. #define IH_ARCH_INVALID 0 /* Invalid CPU */
  131. #define IH_ARCH_ALPHA 1 /* Alpha */
  132. #define IH_ARCH_ARM 2 /* ARM */
  133. #define IH_ARCH_I386 3 /* Intel x86 */
  134. #define IH_ARCH_IA64 4 /* IA64 */
  135. #define IH_ARCH_MIPS 5 /* MIPS */
  136. #define IH_ARCH_MIPS64 6 /* MIPS 64 Bit */
  137. #define IH_ARCH_PPC 7 /* PowerPC */
  138. #define IH_ARCH_S390 8 /* IBM S390 */
  139. #define IH_ARCH_SH 9 /* SuperH */
  140. #define IH_ARCH_SPARC 10 /* Sparc */
  141. #define IH_ARCH_SPARC64 11 /* Sparc 64 Bit */
  142. #define IH_ARCH_M68K 12 /* M68K */
  143. #define IH_ARCH_MICROBLAZE 14 /* MicroBlaze */
  144. #define IH_ARCH_NIOS2 15 /* Nios-II */
  145. #define IH_ARCH_BLACKFIN 16 /* Blackfin */
  146. #define IH_ARCH_AVR32 17 /* AVR32 */
  147. #define IH_ARCH_ST200 18 /* STMicroelectronics ST200 */
  148. #define IH_ARCH_SANDBOX 19 /* Sandbox architecture (test only) */
  149. #define IH_ARCH_NDS32 20 /* ANDES Technology - NDS32 */
  150. #define IH_ARCH_OPENRISC 21 /* OpenRISC 1000 */
  151. #define IH_ARCH_ARM64 22 /* ARM64 */
  152. #define IH_ARCH_ARC 23 /* Synopsys DesignWare ARC */
  153. /*
  154. * Image Types
  155. *
  156. * "Standalone Programs" are directly runnable in the environment
  157. * provided by U-Boot; it is expected that (if they behave
  158. * well) you can continue to work in U-Boot after return from
  159. * the Standalone Program.
  160. * "OS Kernel Images" are usually images of some Embedded OS which
  161. * will take over control completely. Usually these programs
  162. * will install their own set of exception handlers, device
  163. * drivers, set up the MMU, etc. - this means, that you cannot
  164. * expect to re-enter U-Boot except by resetting the CPU.
  165. * "RAMDisk Images" are more or less just data blocks, and their
  166. * parameters (address, size) are passed to an OS kernel that is
  167. * being started.
  168. * "Multi-File Images" contain several images, typically an OS
  169. * (Linux) kernel image and one or more data images like
  170. * RAMDisks. This construct is useful for instance when you want
  171. * to boot over the network using BOOTP etc., where the boot
  172. * server provides just a single image file, but you want to get
  173. * for instance an OS kernel and a RAMDisk image.
  174. *
  175. * "Multi-File Images" start with a list of image sizes, each
  176. * image size (in bytes) specified by an "uint32_t" in network
  177. * byte order. This list is terminated by an "(uint32_t)0".
  178. * Immediately after the terminating 0 follow the images, one by
  179. * one, all aligned on "uint32_t" boundaries (size rounded up to
  180. * a multiple of 4 bytes - except for the last file).
  181. *
  182. * "Firmware Images" are binary images containing firmware (like
  183. * U-Boot or FPGA images) which usually will be programmed to
  184. * flash memory.
  185. *
  186. * "Script files" are command sequences that will be executed by
  187. * U-Boot's command interpreter; this feature is especially
  188. * useful when you configure U-Boot to use a real shell (hush)
  189. * as command interpreter (=> Shell Scripts).
  190. */
  191. #define IH_TYPE_INVALID 0 /* Invalid Image */
  192. #define IH_TYPE_STANDALONE 1 /* Standalone Program */
  193. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  194. #define IH_TYPE_RAMDISK 3 /* RAMDisk Image */
  195. #define IH_TYPE_MULTI 4 /* Multi-File Image */
  196. #define IH_TYPE_FIRMWARE 5 /* Firmware Image */
  197. #define IH_TYPE_SCRIPT 6 /* Script file */
  198. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
  199. #define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
  200. #define IH_TYPE_KWBIMAGE 9 /* Kirkwood Boot Image */
  201. #define IH_TYPE_IMXIMAGE 10 /* Freescale IMXBoot Image */
  202. #define IH_TYPE_UBLIMAGE 11 /* Davinci UBL Image */
  203. #define IH_TYPE_OMAPIMAGE 12 /* TI OMAP Config Header Image */
  204. #define IH_TYPE_AISIMAGE 13 /* TI Davinci AIS Image */
  205. #define IH_TYPE_KERNEL_NOLOAD 14 /* OS Kernel Image, can run from any load address */
  206. #define IH_TYPE_PBLIMAGE 15 /* Freescale PBL Boot Image */
  207. #define IH_TYPE_MXSIMAGE 16 /* Freescale MXSBoot Image */
  208. #define IH_TYPE_GPIMAGE 17 /* TI Keystone GPHeader Image */
  209. #define IH_TYPE_ATMELIMAGE 18 /* ATMEL ROM bootable Image */
  210. #define IH_TYPE_SOCFPGAIMAGE 19 /* Altera SOCFPGA Preloader */
  211. #define IH_TYPE_X86_SETUP 20 /* x86 setup.bin Image */
  212. /*
  213. * Compression Types
  214. */
  215. #define IH_COMP_NONE 0 /* No Compression Used */
  216. #define IH_COMP_GZIP 1 /* gzip Compression Used */
  217. #define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
  218. #define IH_COMP_LZMA 3 /* lzma Compression Used */
  219. #define IH_COMP_LZO 4 /* lzo Compression Used */
  220. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  221. #define IH_NMLEN 32 /* Image Name Length */
  222. /* Reused from common.h */
  223. #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
  224. /*
  225. * Legacy format image header,
  226. * all data in network byte order (aka natural aka bigendian).
  227. */
  228. typedef struct image_header {
  229. __be32 ih_magic; /* Image Header Magic Number */
  230. __be32 ih_hcrc; /* Image Header CRC Checksum */
  231. __be32 ih_time; /* Image Creation Timestamp */
  232. __be32 ih_size; /* Image Data Size */
  233. __be32 ih_load; /* Data Load Address */
  234. __be32 ih_ep; /* Entry Point Address */
  235. __be32 ih_dcrc; /* Image Data CRC Checksum */
  236. uint8_t ih_os; /* Operating System */
  237. uint8_t ih_arch; /* CPU architecture */
  238. uint8_t ih_type; /* Image Type */
  239. uint8_t ih_comp; /* Compression Type */
  240. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  241. } image_header_t;
  242. typedef struct image_info {
  243. ulong start, end; /* start/end of blob */
  244. ulong image_start, image_len; /* start of image within blob, len of image */
  245. ulong load; /* load addr for the image */
  246. uint8_t comp, type, os; /* compression, type of image, os type */
  247. uint8_t arch; /* CPU architecture */
  248. } image_info_t;
  249. /*
  250. * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
  251. * routines.
  252. */
  253. typedef struct bootm_headers {
  254. /*
  255. * Legacy os image header, if it is a multi component image
  256. * then boot_get_ramdisk() and get_fdt() will attempt to get
  257. * data from second and third component accordingly.
  258. */
  259. image_header_t *legacy_hdr_os; /* image header pointer */
  260. image_header_t legacy_hdr_os_copy; /* header copy */
  261. ulong legacy_hdr_valid;
  262. #if defined(CONFIG_FIT)
  263. const char *fit_uname_cfg; /* configuration node unit name */
  264. void *fit_hdr_os; /* os FIT image header */
  265. const char *fit_uname_os; /* os subimage node unit name */
  266. int fit_noffset_os; /* os subimage node offset */
  267. void *fit_hdr_rd; /* init ramdisk FIT image header */
  268. const char *fit_uname_rd; /* init ramdisk subimage node unit name */
  269. int fit_noffset_rd; /* init ramdisk subimage node offset */
  270. void *fit_hdr_fdt; /* FDT blob FIT image header */
  271. const char *fit_uname_fdt; /* FDT blob subimage node unit name */
  272. int fit_noffset_fdt;/* FDT blob subimage node offset */
  273. void *fit_hdr_setup; /* x86 setup FIT image header */
  274. const char *fit_uname_setup; /* x86 setup subimage node name */
  275. int fit_noffset_setup;/* x86 setup subimage node offset */
  276. #endif
  277. #ifndef USE_HOSTCC
  278. image_info_t os; /* os image info */
  279. ulong ep; /* entry point of OS */
  280. ulong rd_start, rd_end;/* ramdisk start/end */
  281. char *ft_addr; /* flat dev tree address */
  282. ulong ft_len; /* length of flat device tree */
  283. ulong initrd_start;
  284. ulong initrd_end;
  285. ulong cmdline_start;
  286. ulong cmdline_end;
  287. bd_t *kbd;
  288. #endif
  289. int verify; /* getenv("verify")[0] != 'n' */
  290. #define BOOTM_STATE_START (0x00000001)
  291. #define BOOTM_STATE_FINDOS (0x00000002)
  292. #define BOOTM_STATE_FINDOTHER (0x00000004)
  293. #define BOOTM_STATE_LOADOS (0x00000008)
  294. #define BOOTM_STATE_RAMDISK (0x00000010)
  295. #define BOOTM_STATE_FDT (0x00000020)
  296. #define BOOTM_STATE_OS_CMDLINE (0x00000040)
  297. #define BOOTM_STATE_OS_BD_T (0x00000080)
  298. #define BOOTM_STATE_OS_PREP (0x00000100)
  299. #define BOOTM_STATE_OS_FAKE_GO (0x00000200) /* 'Almost' run the OS */
  300. #define BOOTM_STATE_OS_GO (0x00000400)
  301. int state;
  302. #ifdef CONFIG_LMB
  303. struct lmb lmb; /* for memory mgmt */
  304. #endif
  305. } bootm_headers_t;
  306. extern bootm_headers_t images;
  307. /*
  308. * Some systems (for example LWMON) have very short watchdog periods;
  309. * we must make sure to split long operations like memmove() or
  310. * checksum calculations into reasonable chunks.
  311. */
  312. #ifndef CHUNKSZ
  313. #define CHUNKSZ (64 * 1024)
  314. #endif
  315. #ifndef CHUNKSZ_CRC32
  316. #define CHUNKSZ_CRC32 (64 * 1024)
  317. #endif
  318. #ifndef CHUNKSZ_MD5
  319. #define CHUNKSZ_MD5 (64 * 1024)
  320. #endif
  321. #ifndef CHUNKSZ_SHA1
  322. #define CHUNKSZ_SHA1 (64 * 1024)
  323. #endif
  324. #define uimage_to_cpu(x) be32_to_cpu(x)
  325. #define cpu_to_uimage(x) cpu_to_be32(x)
  326. /*
  327. * Translation table for entries of a specific type; used by
  328. * get_table_entry_id() and get_table_entry_name().
  329. */
  330. typedef struct table_entry {
  331. int id;
  332. char *sname; /* short (input) name to find table entry */
  333. char *lname; /* long (output) name to print for messages */
  334. } table_entry_t;
  335. /*
  336. * get_table_entry_id() scans the translation table trying to find an
  337. * entry that matches the given short name. If a matching entry is
  338. * found, it's id is returned to the caller.
  339. */
  340. int get_table_entry_id(const table_entry_t *table,
  341. const char *table_name, const char *name);
  342. /*
  343. * get_table_entry_name() scans the translation table trying to find
  344. * an entry that matches the given id. If a matching entry is found,
  345. * its long name is returned to the caller.
  346. */
  347. char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
  348. const char *genimg_get_os_name(uint8_t os);
  349. const char *genimg_get_arch_name(uint8_t arch);
  350. const char *genimg_get_type_name(uint8_t type);
  351. const char *genimg_get_comp_name(uint8_t comp);
  352. int genimg_get_os_id(const char *name);
  353. int genimg_get_arch_id(const char *name);
  354. int genimg_get_type_id(const char *name);
  355. int genimg_get_comp_id(const char *name);
  356. void genimg_print_size(uint32_t size);
  357. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
  358. defined(USE_HOSTCC)
  359. #define IMAGE_ENABLE_TIMESTAMP 1
  360. #else
  361. #define IMAGE_ENABLE_TIMESTAMP 0
  362. #endif
  363. void genimg_print_time(time_t timestamp);
  364. /* What to do with a image load address ('load = <> 'in the FIT) */
  365. enum fit_load_op {
  366. FIT_LOAD_IGNORED, /* Ignore load address */
  367. FIT_LOAD_OPTIONAL, /* Can be provided, but optional */
  368. FIT_LOAD_OPTIONAL_NON_ZERO, /* Optional, a value of 0 is ignored */
  369. FIT_LOAD_REQUIRED, /* Must be provided */
  370. };
  371. int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
  372. ulong *setup_len);
  373. #ifndef USE_HOSTCC
  374. /* Image format types, returned by _get_format() routine */
  375. #define IMAGE_FORMAT_INVALID 0x00
  376. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  377. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  378. #endif
  379. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  380. #define IMAGE_FORMAT_ANDROID 0x03 /* Android boot image */
  381. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  382. const char **fit_uname_config,
  383. const char **fit_uname_kernel);
  384. ulong genimg_get_kernel_addr(char * const img_addr);
  385. int genimg_get_format(const void *img_addr);
  386. int genimg_has_config(bootm_headers_t *images);
  387. ulong genimg_get_image(ulong img_addr);
  388. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  389. uint8_t arch, ulong *rd_start, ulong *rd_end);
  390. #endif
  391. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  392. ulong *setup_start, ulong *setup_len);
  393. /**
  394. * fit_image_load() - load an image from a FIT
  395. *
  396. * This deals with all aspects of loading an image from a FIT, including
  397. * selecting the right image based on configuration, verifying it, printing
  398. * out progress messages, checking the type/arch/os and optionally copying it
  399. * to the right load address.
  400. *
  401. * The property to look up is defined by image_type.
  402. *
  403. * @param images Boot images structure
  404. * @param addr Address of FIT in memory
  405. * @param fit_unamep On entry this is the requested image name
  406. * (e.g. "kernel@1") or NULL to use the default. On exit
  407. * points to the selected image name
  408. * @param fit_uname_configp On entry this is the requested configuration
  409. * name (e.g. "conf@1") or NULL to use the default. On
  410. * exit points to the selected configuration name.
  411. * @param arch Expected architecture (IH_ARCH_...)
  412. * @param image_type Required image type (IH_TYPE_...). If this is
  413. * IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
  414. * also.
  415. * @param bootstage_id ID of starting bootstage to use for progress updates.
  416. * This will be added to the BOOTSTAGE_SUB values when
  417. * calling bootstage_mark()
  418. * @param load_op Decribes what to do with the load address
  419. * @param datap Returns address of loaded image
  420. * @param lenp Returns length of loaded image
  421. * @return node offset of image, or -ve error code on error
  422. */
  423. int fit_image_load(bootm_headers_t *images, ulong addr,
  424. const char **fit_unamep, const char **fit_uname_configp,
  425. int arch, int image_type, int bootstage_id,
  426. enum fit_load_op load_op, ulong *datap, ulong *lenp);
  427. #ifndef USE_HOSTCC
  428. /**
  429. * fit_get_node_from_config() - Look up an image a FIT by type
  430. *
  431. * This looks in the selected conf@ node (images->fit_uname_cfg) for a
  432. * particular image type (e.g. "kernel") and then finds the image that is
  433. * referred to.
  434. *
  435. * For example, for something like:
  436. *
  437. * images {
  438. * kernel@1 {
  439. * ...
  440. * };
  441. * };
  442. * configurations {
  443. * conf@1 {
  444. * kernel = "kernel@1";
  445. * };
  446. * };
  447. *
  448. * the function will return the node offset of the kernel@1 node, assuming
  449. * that conf@1 is the chosen configuration.
  450. *
  451. * @param images Boot images structure
  452. * @param prop_name Property name to look up (FIT_..._PROP)
  453. * @param addr Address of FIT in memory
  454. */
  455. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  456. ulong addr);
  457. int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
  458. bootm_headers_t *images,
  459. char **of_flat_tree, ulong *of_size);
  460. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
  461. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
  462. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  463. ulong *initrd_start, ulong *initrd_end);
  464. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
  465. #ifdef CONFIG_SYS_BOOT_GET_KBD
  466. int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
  467. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  468. #endif /* !USE_HOSTCC */
  469. /*******************************************************************/
  470. /* Legacy format specific code (prefixed with image_) */
  471. /*******************************************************************/
  472. static inline uint32_t image_get_header_size(void)
  473. {
  474. return (sizeof(image_header_t));
  475. }
  476. #define image_get_hdr_l(f) \
  477. static inline uint32_t image_get_##f(const image_header_t *hdr) \
  478. { \
  479. return uimage_to_cpu(hdr->ih_##f); \
  480. }
  481. image_get_hdr_l(magic) /* image_get_magic */
  482. image_get_hdr_l(hcrc) /* image_get_hcrc */
  483. image_get_hdr_l(time) /* image_get_time */
  484. image_get_hdr_l(size) /* image_get_size */
  485. image_get_hdr_l(load) /* image_get_load */
  486. image_get_hdr_l(ep) /* image_get_ep */
  487. image_get_hdr_l(dcrc) /* image_get_dcrc */
  488. #define image_get_hdr_b(f) \
  489. static inline uint8_t image_get_##f(const image_header_t *hdr) \
  490. { \
  491. return hdr->ih_##f; \
  492. }
  493. image_get_hdr_b(os) /* image_get_os */
  494. image_get_hdr_b(arch) /* image_get_arch */
  495. image_get_hdr_b(type) /* image_get_type */
  496. image_get_hdr_b(comp) /* image_get_comp */
  497. static inline char *image_get_name(const image_header_t *hdr)
  498. {
  499. return (char *)hdr->ih_name;
  500. }
  501. static inline uint32_t image_get_data_size(const image_header_t *hdr)
  502. {
  503. return image_get_size(hdr);
  504. }
  505. /**
  506. * image_get_data - get image payload start address
  507. * @hdr: image header
  508. *
  509. * image_get_data() returns address of the image payload. For single
  510. * component images it is image data start. For multi component
  511. * images it points to the null terminated table of sub-images sizes.
  512. *
  513. * returns:
  514. * image payload data start address
  515. */
  516. static inline ulong image_get_data(const image_header_t *hdr)
  517. {
  518. return ((ulong)hdr + image_get_header_size());
  519. }
  520. static inline uint32_t image_get_image_size(const image_header_t *hdr)
  521. {
  522. return (image_get_size(hdr) + image_get_header_size());
  523. }
  524. static inline ulong image_get_image_end(const image_header_t *hdr)
  525. {
  526. return ((ulong)hdr + image_get_image_size(hdr));
  527. }
  528. #define image_set_hdr_l(f) \
  529. static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
  530. { \
  531. hdr->ih_##f = cpu_to_uimage(val); \
  532. }
  533. image_set_hdr_l(magic) /* image_set_magic */
  534. image_set_hdr_l(hcrc) /* image_set_hcrc */
  535. image_set_hdr_l(time) /* image_set_time */
  536. image_set_hdr_l(size) /* image_set_size */
  537. image_set_hdr_l(load) /* image_set_load */
  538. image_set_hdr_l(ep) /* image_set_ep */
  539. image_set_hdr_l(dcrc) /* image_set_dcrc */
  540. #define image_set_hdr_b(f) \
  541. static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
  542. { \
  543. hdr->ih_##f = val; \
  544. }
  545. image_set_hdr_b(os) /* image_set_os */
  546. image_set_hdr_b(arch) /* image_set_arch */
  547. image_set_hdr_b(type) /* image_set_type */
  548. image_set_hdr_b(comp) /* image_set_comp */
  549. static inline void image_set_name(image_header_t *hdr, const char *name)
  550. {
  551. strncpy(image_get_name(hdr), name, IH_NMLEN);
  552. }
  553. int image_check_hcrc(const image_header_t *hdr);
  554. int image_check_dcrc(const image_header_t *hdr);
  555. #ifndef USE_HOSTCC
  556. ulong getenv_bootm_low(void);
  557. phys_size_t getenv_bootm_size(void);
  558. phys_size_t getenv_bootm_mapsize(void);
  559. #endif
  560. void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
  561. static inline int image_check_magic(const image_header_t *hdr)
  562. {
  563. return (image_get_magic(hdr) == IH_MAGIC);
  564. }
  565. static inline int image_check_type(const image_header_t *hdr, uint8_t type)
  566. {
  567. return (image_get_type(hdr) == type);
  568. }
  569. static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
  570. {
  571. return (image_get_arch(hdr) == arch);
  572. }
  573. static inline int image_check_os(const image_header_t *hdr, uint8_t os)
  574. {
  575. return (image_get_os(hdr) == os);
  576. }
  577. ulong image_multi_count(const image_header_t *hdr);
  578. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  579. ulong *data, ulong *len);
  580. void image_print_contents(const void *hdr);
  581. #ifndef USE_HOSTCC
  582. static inline int image_check_target_arch(const image_header_t *hdr)
  583. {
  584. #ifndef IH_ARCH_DEFAULT
  585. # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
  586. #endif
  587. return image_check_arch(hdr, IH_ARCH_DEFAULT);
  588. }
  589. #endif /* USE_HOSTCC */
  590. /**
  591. * Set up properties in the FDT
  592. *
  593. * This sets up properties in the FDT that is to be passed to linux.
  594. *
  595. * @images: Images information
  596. * @blob: FDT to update
  597. * @of_size: Size of the FDT
  598. * @lmb: Points to logical memory block structure
  599. * @return 0 if ok, <0 on failure
  600. */
  601. int image_setup_libfdt(bootm_headers_t *images, void *blob,
  602. int of_size, struct lmb *lmb);
  603. /**
  604. * Set up the FDT to use for booting a kernel
  605. *
  606. * This performs ramdisk setup, sets up the FDT if required, and adds
  607. * paramters to the FDT if libfdt is available.
  608. *
  609. * @param images Images information
  610. * @return 0 if ok, <0 on failure
  611. */
  612. int image_setup_linux(bootm_headers_t *images);
  613. /**
  614. * bootz_setup() - Extract stat and size of a Linux xImage
  615. *
  616. * @image: Address of image
  617. * @start: Returns start address of image
  618. * @end : Returns end address of image
  619. * @return 0 if OK, 1 if the image was not recognised
  620. */
  621. int bootz_setup(ulong image, ulong *start, ulong *end);
  622. /*******************************************************************/
  623. /* New uImage format specific code (prefixed with fit_) */
  624. /*******************************************************************/
  625. #if defined(CONFIG_FIT)
  626. #define FIT_IMAGES_PATH "/images"
  627. #define FIT_CONFS_PATH "/configurations"
  628. /* hash/signature node */
  629. #define FIT_HASH_NODENAME "hash"
  630. #define FIT_ALGO_PROP "algo"
  631. #define FIT_VALUE_PROP "value"
  632. #define FIT_IGNORE_PROP "uboot-ignore"
  633. #define FIT_SIG_NODENAME "signature"
  634. /* image node */
  635. #define FIT_DATA_PROP "data"
  636. #define FIT_TIMESTAMP_PROP "timestamp"
  637. #define FIT_DESC_PROP "description"
  638. #define FIT_ARCH_PROP "arch"
  639. #define FIT_TYPE_PROP "type"
  640. #define FIT_OS_PROP "os"
  641. #define FIT_COMP_PROP "compression"
  642. #define FIT_ENTRY_PROP "entry"
  643. #define FIT_LOAD_PROP "load"
  644. /* configuration node */
  645. #define FIT_KERNEL_PROP "kernel"
  646. #define FIT_RAMDISK_PROP "ramdisk"
  647. #define FIT_FDT_PROP "fdt"
  648. #define FIT_DEFAULT_PROP "default"
  649. #define FIT_SETUP_PROP "setup"
  650. #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
  651. /* cmdline argument format parsing */
  652. int fit_parse_conf(const char *spec, ulong addr_curr,
  653. ulong *addr, const char **conf_name);
  654. int fit_parse_subimage(const char *spec, ulong addr_curr,
  655. ulong *addr, const char **image_name);
  656. void fit_print_contents(const void *fit);
  657. void fit_image_print(const void *fit, int noffset, const char *p);
  658. /**
  659. * fit_get_end - get FIT image size
  660. * @fit: pointer to the FIT format image header
  661. *
  662. * returns:
  663. * size of the FIT image (blob) in memory
  664. */
  665. static inline ulong fit_get_size(const void *fit)
  666. {
  667. return fdt_totalsize(fit);
  668. }
  669. /**
  670. * fit_get_end - get FIT image end
  671. * @fit: pointer to the FIT format image header
  672. *
  673. * returns:
  674. * end address of the FIT image (blob) in memory
  675. */
  676. static inline ulong fit_get_end(const void *fit)
  677. {
  678. return (ulong)fit + fdt_totalsize(fit);
  679. }
  680. /**
  681. * fit_get_name - get FIT node name
  682. * @fit: pointer to the FIT format image header
  683. *
  684. * returns:
  685. * NULL, on error
  686. * pointer to node name, on success
  687. */
  688. static inline const char *fit_get_name(const void *fit_hdr,
  689. int noffset, int *len)
  690. {
  691. return fdt_get_name(fit_hdr, noffset, len);
  692. }
  693. int fit_get_desc(const void *fit, int noffset, char **desc);
  694. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
  695. int fit_image_get_node(const void *fit, const char *image_uname);
  696. int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
  697. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
  698. int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
  699. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
  700. int fit_image_get_load(const void *fit, int noffset, ulong *load);
  701. int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
  702. int fit_image_get_data(const void *fit, int noffset,
  703. const void **data, size_t *size);
  704. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
  705. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  706. int *value_len);
  707. int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
  708. /**
  709. * fit_add_verification_data() - add verification data to FIT image nodes
  710. *
  711. * @keydir: Directory containing keys
  712. * @kwydest: FDT blob to write public key information to
  713. * @fit: Pointer to the FIT format image header
  714. * @comment: Comment to add to signature nodes
  715. * @require_keys: Mark all keys as 'required'
  716. *
  717. * Adds hash values for all component images in the FIT blob.
  718. * Hashes are calculated for all component images which have hash subnodes
  719. * with algorithm property set to one of the supported hash algorithms.
  720. *
  721. * Also add signatures if signature nodes are present.
  722. *
  723. * returns
  724. * 0, on success
  725. * libfdt error code, on failure
  726. */
  727. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  728. const char *comment, int require_keys);
  729. int fit_image_verify(const void *fit, int noffset);
  730. int fit_config_verify(const void *fit, int conf_noffset);
  731. int fit_all_image_verify(const void *fit);
  732. int fit_image_check_os(const void *fit, int noffset, uint8_t os);
  733. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
  734. int fit_image_check_type(const void *fit, int noffset, uint8_t type);
  735. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
  736. int fit_check_format(const void *fit);
  737. int fit_conf_find_compat(const void *fit, const void *fdt);
  738. int fit_conf_get_node(const void *fit, const char *conf_uname);
  739. /**
  740. * fit_conf_get_prop_node() - Get node refered to by a configuration
  741. * @fit: FIT to check
  742. * @noffset: Offset of conf@xxx node to check
  743. * @prop_name: Property to read from the conf node
  744. *
  745. * The conf@ nodes contain references to other nodes, using properties
  746. * like 'kernel = "kernel@1"'. Given such a property name (e.g. "kernel"),
  747. * return the offset of the node referred to (e.g. offset of node
  748. * "/images/kernel@1".
  749. */
  750. int fit_conf_get_prop_node(const void *fit, int noffset,
  751. const char *prop_name);
  752. void fit_conf_print(const void *fit, int noffset, const char *p);
  753. int fit_check_ramdisk(const void *fit, int os_noffset,
  754. uint8_t arch, int verify);
  755. int calculate_hash(const void *data, int data_len, const char *algo,
  756. uint8_t *value, int *value_len);
  757. /*
  758. * At present we only support signing on the host, and verification on the
  759. * device
  760. */
  761. #if defined(CONFIG_FIT_SIGNATURE)
  762. # ifdef USE_HOSTCC
  763. # define IMAGE_ENABLE_SIGN 1
  764. # define IMAGE_ENABLE_VERIFY 1
  765. # include <openssl/evp.h>
  766. #else
  767. # define IMAGE_ENABLE_SIGN 0
  768. # define IMAGE_ENABLE_VERIFY 1
  769. # endif
  770. #else
  771. # define IMAGE_ENABLE_SIGN 0
  772. # define IMAGE_ENABLE_VERIFY 0
  773. #endif
  774. #ifdef USE_HOSTCC
  775. void *image_get_host_blob(void);
  776. void image_set_host_blob(void *host_blob);
  777. # define gd_fdt_blob() image_get_host_blob()
  778. #else
  779. # define gd_fdt_blob() (gd->fdt_blob)
  780. #endif
  781. #ifdef CONFIG_FIT_BEST_MATCH
  782. #define IMAGE_ENABLE_BEST_MATCH 1
  783. #else
  784. #define IMAGE_ENABLE_BEST_MATCH 0
  785. #endif
  786. /* Information passed to the signing routines */
  787. struct image_sign_info {
  788. const char *keydir; /* Directory conaining keys */
  789. const char *keyname; /* Name of key to use */
  790. void *fit; /* Pointer to FIT blob */
  791. int node_offset; /* Offset of signature node */
  792. struct image_sig_algo *algo; /* Algorithm information */
  793. const void *fdt_blob; /* FDT containing public keys */
  794. int required_keynode; /* Node offset of key to use: -1=any */
  795. const char *require_keys; /* Value for 'required' property */
  796. };
  797. /* A part of an image, used for hashing */
  798. struct image_region {
  799. const void *data;
  800. int size;
  801. };
  802. #if IMAGE_ENABLE_VERIFY
  803. # include <u-boot/rsa-checksum.h>
  804. #endif
  805. struct checksum_algo {
  806. const char *name;
  807. const int checksum_len;
  808. const int pad_len;
  809. #if IMAGE_ENABLE_SIGN
  810. const EVP_MD *(*calculate_sign)(void);
  811. #endif
  812. void (*calculate)(const struct image_region region[],
  813. int region_count, uint8_t *checksum);
  814. const uint8_t *rsa_padding;
  815. };
  816. struct image_sig_algo {
  817. const char *name; /* Name of algorithm */
  818. /**
  819. * sign() - calculate and return signature for given input data
  820. *
  821. * @info: Specifies key and FIT information
  822. * @data: Pointer to the input data
  823. * @data_len: Data length
  824. * @sigp: Set to an allocated buffer holding the signature
  825. * @sig_len: Set to length of the calculated hash
  826. *
  827. * This computes input data signature according to selected algorithm.
  828. * Resulting signature value is placed in an allocated buffer, the
  829. * pointer is returned as *sigp. The length of the calculated
  830. * signature is returned via the sig_len pointer argument. The caller
  831. * should free *sigp.
  832. *
  833. * @return: 0, on success, -ve on error
  834. */
  835. int (*sign)(struct image_sign_info *info,
  836. const struct image_region region[],
  837. int region_count, uint8_t **sigp, uint *sig_len);
  838. /**
  839. * add_verify_data() - Add verification information to FDT
  840. *
  841. * Add public key information to the FDT node, suitable for
  842. * verification at run-time. The information added depends on the
  843. * algorithm being used.
  844. *
  845. * @info: Specifies key and FIT information
  846. * @keydest: Destination FDT blob for public key data
  847. * @return: 0, on success, -ve on error
  848. */
  849. int (*add_verify_data)(struct image_sign_info *info, void *keydest);
  850. /**
  851. * verify() - Verify a signature against some data
  852. *
  853. * @info: Specifies key and FIT information
  854. * @data: Pointer to the input data
  855. * @data_len: Data length
  856. * @sig: Signature
  857. * @sig_len: Number of bytes in signature
  858. * @return 0 if verified, -ve on error
  859. */
  860. int (*verify)(struct image_sign_info *info,
  861. const struct image_region region[], int region_count,
  862. uint8_t *sig, uint sig_len);
  863. /* pointer to checksum algorithm */
  864. struct checksum_algo *checksum;
  865. };
  866. /**
  867. * image_get_sig_algo() - Look up a signature algortihm
  868. *
  869. * @param name Name of algorithm
  870. * @return pointer to algorithm information, or NULL if not found
  871. */
  872. struct image_sig_algo *image_get_sig_algo(const char *name);
  873. /**
  874. * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  875. *
  876. * @fit: FIT to check
  877. * @image_noffset: Offset of image node to check
  878. * @data: Image data to check
  879. * @size: Size of image data
  880. * @sig_blob: FDT containing public keys
  881. * @no_sigsp: Returns 1 if no signatures were required, and
  882. * therefore nothing was checked. The caller may wish
  883. * to fall back to other mechanisms, or refuse to
  884. * boot.
  885. * @return 0 if all verified ok, <0 on error
  886. */
  887. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  888. const char *data, size_t size, const void *sig_blob,
  889. int *no_sigsp);
  890. /**
  891. * fit_image_check_sig() - Check a single image signature node
  892. *
  893. * @fit: FIT to check
  894. * @noffset: Offset of signature node to check
  895. * @data: Image data to check
  896. * @size: Size of image data
  897. * @required_keynode: Offset in the control FDT of the required key node,
  898. * if any. If this is given, then the image wil not
  899. * pass verification unless that key is used. If this is
  900. * -1 then any signature will do.
  901. * @err_msgp: In the event of an error, this will be pointed to a
  902. * help error string to display to the user.
  903. * @return 0 if all verified ok, <0 on error
  904. */
  905. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  906. size_t size, int required_keynode, char **err_msgp);
  907. /**
  908. * fit_region_make_list() - Make a list of regions to hash
  909. *
  910. * Given a list of FIT regions (offset, size) provided by libfdt, create
  911. * a list of regions (void *, size) for use by the signature creationg
  912. * and verification code.
  913. *
  914. * @fit: FIT image to process
  915. * @fdt_regions: Regions as returned by libfdt
  916. * @count: Number of regions returned by libfdt
  917. * @region: Place to put list of regions (NULL to allocate it)
  918. * @return pointer to list of regions, or NULL if out of memory
  919. */
  920. struct image_region *fit_region_make_list(const void *fit,
  921. struct fdt_region *fdt_regions, int count,
  922. struct image_region *region);
  923. static inline int fit_image_check_target_arch(const void *fdt, int node)
  924. {
  925. #ifndef USE_HOSTCC
  926. return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
  927. #else
  928. return 0;
  929. #endif
  930. }
  931. #ifdef CONFIG_FIT_VERBOSE
  932. #define fit_unsupported(msg) printf("! %s:%d " \
  933. "FIT images not supported for '%s'\n", \
  934. __FILE__, __LINE__, (msg))
  935. #define fit_unsupported_reset(msg) printf("! %s:%d " \
  936. "FIT images not supported for '%s' " \
  937. "- must reset board to recover!\n", \
  938. __FILE__, __LINE__, (msg))
  939. #else
  940. #define fit_unsupported(msg)
  941. #define fit_unsupported_reset(msg)
  942. #endif /* CONFIG_FIT_VERBOSE */
  943. #endif /* CONFIG_FIT */
  944. #if defined(CONFIG_ANDROID_BOOT_IMAGE)
  945. struct andr_img_hdr;
  946. int android_image_check_header(const struct andr_img_hdr *hdr);
  947. int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  948. ulong *os_data, ulong *os_len);
  949. int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  950. ulong *rd_data, ulong *rd_len);
  951. ulong android_image_get_end(const struct andr_img_hdr *hdr);
  952. ulong android_image_get_kload(const struct andr_img_hdr *hdr);
  953. #endif /* CONFIG_ANDROID_BOOT_IMAGE */
  954. #endif /* __IMAGE_H__ */