image.h 33 KB

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