image.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 <asm/byteorder.h>
  35. #include <command.h>
  36. #ifndef USE_HOSTCC
  37. #include <lmb.h>
  38. #include <linux/string.h>
  39. #include <asm/u-boot.h>
  40. /* new uImage format support enabled by default */
  41. #define CONFIG_FIT 1
  42. #define CONFIG_OF_LIBFDT 1
  43. /* enable fit_format_error(), fit_format_warning() */
  44. #define CONFIG_FIT_VERBOSE 1
  45. #if defined(CONFIG_FIT) && !defined(CONFIG_OF_LIBFDT)
  46. #error "CONFIG_OF_LIBFDT not enabled, required by CONFIG_FIT!"
  47. #endif
  48. #endif /* USE_HOSTCC */
  49. /*
  50. * Operating System Codes
  51. */
  52. #define IH_OS_INVALID 0 /* Invalid OS */
  53. #define IH_OS_OPENBSD 1 /* OpenBSD */
  54. #define IH_OS_NETBSD 2 /* NetBSD */
  55. #define IH_OS_FREEBSD 3 /* FreeBSD */
  56. #define IH_OS_4_4BSD 4 /* 4.4BSD */
  57. #define IH_OS_LINUX 5 /* Linux */
  58. #define IH_OS_SVR4 6 /* SVR4 */
  59. #define IH_OS_ESIX 7 /* Esix */
  60. #define IH_OS_SOLARIS 8 /* Solaris */
  61. #define IH_OS_IRIX 9 /* Irix */
  62. #define IH_OS_SCO 10 /* SCO */
  63. #define IH_OS_DELL 11 /* Dell */
  64. #define IH_OS_NCR 12 /* NCR */
  65. #define IH_OS_LYNXOS 13 /* LynxOS */
  66. #define IH_OS_VXWORKS 14 /* VxWorks */
  67. #define IH_OS_PSOS 15 /* pSOS */
  68. #define IH_OS_QNX 16 /* QNX */
  69. #define IH_OS_U_BOOT 17 /* Firmware */
  70. #define IH_OS_RTEMS 18 /* RTEMS */
  71. #define IH_OS_ARTOS 19 /* ARTOS */
  72. #define IH_OS_UNITY 20 /* Unity OS */
  73. /*
  74. * CPU Architecture Codes (supported by Linux)
  75. */
  76. #define IH_ARCH_INVALID 0 /* Invalid CPU */
  77. #define IH_ARCH_ALPHA 1 /* Alpha */
  78. #define IH_ARCH_ARM 2 /* ARM */
  79. #define IH_ARCH_I386 3 /* Intel x86 */
  80. #define IH_ARCH_IA64 4 /* IA64 */
  81. #define IH_ARCH_MIPS 5 /* MIPS */
  82. #define IH_ARCH_MIPS64 6 /* MIPS 64 Bit */
  83. #define IH_ARCH_PPC 7 /* PowerPC */
  84. #define IH_ARCH_S390 8 /* IBM S390 */
  85. #define IH_ARCH_SH 9 /* SuperH */
  86. #define IH_ARCH_SPARC 10 /* Sparc */
  87. #define IH_ARCH_SPARC64 11 /* Sparc 64 Bit */
  88. #define IH_ARCH_M68K 12 /* M68K */
  89. #define IH_ARCH_NIOS 13 /* Nios-32 */
  90. #define IH_ARCH_MICROBLAZE 14 /* MicroBlaze */
  91. #define IH_ARCH_NIOS2 15 /* Nios-II */
  92. #define IH_ARCH_BLACKFIN 16 /* Blackfin */
  93. #define IH_ARCH_AVR32 17 /* AVR32 */
  94. #define IH_ARCH_ST200 18 /* STMicroelectronics ST200 */
  95. /*
  96. * Image Types
  97. *
  98. * "Standalone Programs" are directly runnable in the environment
  99. * provided by U-Boot; it is expected that (if they behave
  100. * well) you can continue to work in U-Boot after return from
  101. * the Standalone Program.
  102. * "OS Kernel Images" are usually images of some Embedded OS which
  103. * will take over control completely. Usually these programs
  104. * will install their own set of exception handlers, device
  105. * drivers, set up the MMU, etc. - this means, that you cannot
  106. * expect to re-enter U-Boot except by resetting the CPU.
  107. * "RAMDisk Images" are more or less just data blocks, and their
  108. * parameters (address, size) are passed to an OS kernel that is
  109. * being started.
  110. * "Multi-File Images" contain several images, typically an OS
  111. * (Linux) kernel image and one or more data images like
  112. * RAMDisks. This construct is useful for instance when you want
  113. * to boot over the network using BOOTP etc., where the boot
  114. * server provides just a single image file, but you want to get
  115. * for instance an OS kernel and a RAMDisk image.
  116. *
  117. * "Multi-File Images" start with a list of image sizes, each
  118. * image size (in bytes) specified by an "uint32_t" in network
  119. * byte order. This list is terminated by an "(uint32_t)0".
  120. * Immediately after the terminating 0 follow the images, one by
  121. * one, all aligned on "uint32_t" boundaries (size rounded up to
  122. * a multiple of 4 bytes - except for the last file).
  123. *
  124. * "Firmware Images" are binary images containing firmware (like
  125. * U-Boot or FPGA images) which usually will be programmed to
  126. * flash memory.
  127. *
  128. * "Script files" are command sequences that will be executed by
  129. * U-Boot's command interpreter; this feature is especially
  130. * useful when you configure U-Boot to use a real shell (hush)
  131. * as command interpreter (=> Shell Scripts).
  132. */
  133. #define IH_TYPE_INVALID 0 /* Invalid Image */
  134. #define IH_TYPE_STANDALONE 1 /* Standalone Program */
  135. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  136. #define IH_TYPE_RAMDISK 3 /* RAMDisk Image */
  137. #define IH_TYPE_MULTI 4 /* Multi-File Image */
  138. #define IH_TYPE_FIRMWARE 5 /* Firmware Image */
  139. #define IH_TYPE_SCRIPT 6 /* Script file */
  140. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
  141. #define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
  142. /*
  143. * Compression Types
  144. */
  145. #define IH_COMP_NONE 0 /* No Compression Used */
  146. #define IH_COMP_GZIP 1 /* gzip Compression Used */
  147. #define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
  148. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  149. #define IH_NMLEN 32 /* Image Name Length */
  150. /*
  151. * Legacy format image header,
  152. * all data in network byte order (aka natural aka bigendian).
  153. */
  154. typedef struct image_header {
  155. uint32_t ih_magic; /* Image Header Magic Number */
  156. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  157. uint32_t ih_time; /* Image Creation Timestamp */
  158. uint32_t ih_size; /* Image Data Size */
  159. uint32_t ih_load; /* Data Load Address */
  160. uint32_t ih_ep; /* Entry Point Address */
  161. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  162. uint8_t ih_os; /* Operating System */
  163. uint8_t ih_arch; /* CPU architecture */
  164. uint8_t ih_type; /* Image Type */
  165. uint8_t ih_comp; /* Compression Type */
  166. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  167. } image_header_t;
  168. /*
  169. * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
  170. * routines.
  171. */
  172. typedef struct bootm_headers {
  173. /*
  174. * Legacy os image header, if it is a multi component image
  175. * then boot_get_ramdisk() and get_fdt() will attempt to get
  176. * data from second and third component accordingly.
  177. */
  178. image_header_t *legacy_hdr_os;
  179. ulong legacy_hdr_valid;
  180. #if defined(CONFIG_FIT)
  181. void *fit_hdr_os; /* os FIT image header */
  182. char *fit_uname_os; /* os subimage node unit name */
  183. void *fit_hdr_rd; /* init ramdisk FIT image header */
  184. char *fit_uname_rd; /* init ramdisk node unit name */
  185. #if defined(CONFIG_PPC)
  186. void *fit_hdr_fdt; /* FDT blob FIT image header */
  187. char *fit_uname_fdt; /* FDT blob node unit name */
  188. #endif
  189. int verify; /* getenv("verify")[0] != 'n' */
  190. int autostart; /* getenv("autostart")[0] != 'n' */
  191. struct lmb *lmb; /* for memory mgmt */
  192. #endif
  193. } bootm_headers_t;
  194. /*
  195. * Some systems (for example LWMON) have very short watchdog periods;
  196. * we must make sure to split long operations like memmove() or
  197. * crc32() into reasonable chunks.
  198. */
  199. #define CHUNKSZ (64 * 1024)
  200. #define uimage_to_cpu(x) ntohl(x)
  201. #define cpu_to_uimage(x) htonl(x)
  202. #ifndef USE_HOSTCC
  203. /* Image format types, returned by _get_format() routine */
  204. #define IMAGE_FORMAT_INVALID 0x00
  205. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  206. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  207. int genimg_get_format (void *img_addr);
  208. ulong genimg_get_image (ulong img_addr);
  209. const char* genimg_get_os_name (uint8_t os);
  210. const char* genimg_get_arch_name (uint8_t arch);
  211. const char* genimg_get_type_name (uint8_t type);
  212. const char* genimg_get_comp_name (uint8_t comp);
  213. int boot_get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  214. bootm_headers_t *images, uint8_t arch,
  215. ulong *rd_start, ulong *rd_end);
  216. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  217. int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
  218. ulong *initrd_start, ulong *initrd_end);
  219. int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
  220. ulong bootmap_base);
  221. int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base);
  222. #endif /* CONFIG_PPC || CONFIG_M68K */
  223. #endif /* USE_HOSTCC */
  224. /*******************************************************************/
  225. /* Legacy format specific code (prefixed with image_) */
  226. /*******************************************************************/
  227. static inline uint32_t image_get_header_size (void)
  228. {
  229. return (sizeof (image_header_t));
  230. }
  231. #define image_get_hdr_l(f) \
  232. static inline uint32_t image_get_##f(image_header_t *hdr) \
  233. { \
  234. return uimage_to_cpu (hdr->ih_##f); \
  235. }
  236. image_get_hdr_l (magic);
  237. image_get_hdr_l (hcrc);
  238. image_get_hdr_l (time);
  239. image_get_hdr_l (size);
  240. image_get_hdr_l (load);
  241. image_get_hdr_l (ep);
  242. image_get_hdr_l (dcrc);
  243. #define image_get_hdr_b(f) \
  244. static inline uint8_t image_get_##f(image_header_t *hdr) \
  245. { \
  246. return hdr->ih_##f; \
  247. }
  248. image_get_hdr_b (os);
  249. image_get_hdr_b (arch);
  250. image_get_hdr_b (type);
  251. image_get_hdr_b (comp);
  252. static inline char *image_get_name (image_header_t *hdr)
  253. {
  254. return (char *)hdr->ih_name;
  255. }
  256. static inline uint32_t image_get_data_size (image_header_t *hdr)
  257. {
  258. return image_get_size (hdr);
  259. }
  260. /**
  261. * image_get_data - get image payload start address
  262. * @hdr: image header
  263. *
  264. * image_get_data() returns address of the image payload. For single
  265. * component images it is image data start. For multi component
  266. * images it points to the null terminated table of sub-images sizes.
  267. *
  268. * returns:
  269. * image payload data start address
  270. */
  271. static inline ulong image_get_data (image_header_t *hdr)
  272. {
  273. return ((ulong)hdr + image_get_header_size ());
  274. }
  275. static inline uint32_t image_get_image_size (image_header_t *hdr)
  276. {
  277. return (image_get_size (hdr) + image_get_header_size ());
  278. }
  279. static inline ulong image_get_image_end (image_header_t *hdr)
  280. {
  281. return ((ulong)hdr + image_get_image_size (hdr));
  282. }
  283. #define image_set_hdr_l(f) \
  284. static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
  285. { \
  286. hdr->ih_##f = cpu_to_uimage (val); \
  287. }
  288. image_set_hdr_l (magic);
  289. image_set_hdr_l (hcrc);
  290. image_set_hdr_l (time);
  291. image_set_hdr_l (size);
  292. image_set_hdr_l (load);
  293. image_set_hdr_l (ep);
  294. image_set_hdr_l (dcrc);
  295. #define image_set_hdr_b(f) \
  296. static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
  297. { \
  298. hdr->ih_##f = val; \
  299. }
  300. image_set_hdr_b (os);
  301. image_set_hdr_b (arch);
  302. image_set_hdr_b (type);
  303. image_set_hdr_b (comp);
  304. static inline void image_set_name (image_header_t *hdr, const char *name)
  305. {
  306. strncpy (image_get_name (hdr), name, IH_NMLEN);
  307. }
  308. int image_check_hcrc (image_header_t *hdr);
  309. int image_check_dcrc (image_header_t *hdr);
  310. #ifndef USE_HOSTCC
  311. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksize);
  312. int getenv_verify (void);
  313. int getenv_autostart (void);
  314. ulong getenv_bootm_low(void);
  315. ulong getenv_bootm_size(void);
  316. void memmove_wd (void *to, void *from, size_t len, ulong chunksz);
  317. #endif
  318. static inline int image_check_magic (image_header_t *hdr)
  319. {
  320. return (image_get_magic (hdr) == IH_MAGIC);
  321. }
  322. static inline int image_check_type (image_header_t *hdr, uint8_t type)
  323. {
  324. return (image_get_type (hdr) == type);
  325. }
  326. static inline int image_check_arch (image_header_t *hdr, uint8_t arch)
  327. {
  328. return (image_get_arch (hdr) == arch);
  329. }
  330. static inline int image_check_os (image_header_t *hdr, uint8_t os)
  331. {
  332. return (image_get_os (hdr) == os);
  333. }
  334. ulong image_multi_count (image_header_t *hdr);
  335. void image_multi_getimg (image_header_t *hdr, ulong idx,
  336. ulong *data, ulong *len);
  337. #ifndef USE_HOSTCC
  338. static inline int image_check_target_arch (image_header_t *hdr)
  339. {
  340. #if defined(__ARM__)
  341. if (!image_check_arch (hdr, IH_ARCH_ARM))
  342. #elif defined(__avr32__)
  343. if (!image_check_arch (hdr, IH_ARCH_AVR32))
  344. #elif defined(__bfin__)
  345. if (!image_check_arch (hdr, IH_ARCH_BLACKFIN))
  346. #elif defined(__I386__)
  347. if (!image_check_arch (hdr, IH_ARCH_I386))
  348. #elif defined(__M68K__)
  349. if (!image_check_arch (hdr, IH_ARCH_M68K))
  350. #elif defined(__microblaze__)
  351. if (!image_check_arch (hdr, IH_ARCH_MICROBLAZE))
  352. #elif defined(__mips__)
  353. if (!image_check_arch (hdr, IH_ARCH_MIPS))
  354. #elif defined(__nios__)
  355. if (!image_check_arch (hdr, IH_ARCH_NIOS))
  356. #elif defined(__nios2__)
  357. if (!image_check_arch (hdr, IH_ARCH_NIOS2))
  358. #elif defined(__PPC__)
  359. if (!image_check_arch (hdr, IH_ARCH_PPC))
  360. #elif defined(__sh__)
  361. if (!image_check_arch (hdr, IH_ARCH_SH))
  362. #else
  363. # error Unknown CPU type
  364. #endif
  365. return 0;
  366. return 1;
  367. }
  368. void image_print_contents (image_header_t *hdr);
  369. /*******************************************************************/
  370. /* New uImage format specific code (prefixed with fit_) */
  371. /*******************************************************************/
  372. #if defined(CONFIG_FIT)
  373. inline int fit_parse_conf (const char *spec, ulong addr_curr,
  374. ulong *addr, const char **conf_name);
  375. inline int fit_parse_subimage (const char *spec, ulong addr_curr,
  376. ulong *addr, const char **image_name);
  377. #ifdef CONFIG_FIT_VERBOSE
  378. #define fit_unsupported(msg) printf ("! %s:%d " \
  379. "FIT images not supported for '%s'\n", \
  380. __FILE__, __LINE__, (msg))
  381. #define fit_unsupported_reset(msg) printf ("! %s:%d " \
  382. "FIT images not supported for '%s' " \
  383. "- must reset board to recover!\n", \
  384. __FILE__, __LINE__, (msg))
  385. #else
  386. #define fit_unsupported(msg)
  387. #define fit_unsupported_reset(msg)
  388. #endif /* CONFIG_FIT_VERBOSE */
  389. #endif /* CONFIG_FIT */
  390. #endif /* USE_HOSTCC */
  391. #endif /* __IMAGE_H__ */