image.h 34 KB

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