image.h 13 KB

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