image.h 32 KB

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