image.h 34 KB

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