image.h 31 KB

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