image.h 11 KB

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