image.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  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. #ifndef USE_HOSTCC
  26. # include <common.h>
  27. # include <watchdog.h>
  28. #else
  29. # include "mkimage.h"
  30. #endif
  31. #include <image.h>
  32. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  33. int image_check_hcrc (image_header_t *hdr)
  34. {
  35. ulong hcrc;
  36. ulong len = image_get_header_size ();
  37. image_header_t header;
  38. /* Copy header so we can blank CRC field for re-calculation */
  39. memmove (&header, (char *)hdr, image_get_header_size ());
  40. image_set_hcrc (&header, 0);
  41. hcrc = crc32 (0, (unsigned char *)&header, len);
  42. return (hcrc == image_get_hcrc (hdr));
  43. }
  44. int image_check_dcrc (image_header_t *hdr)
  45. {
  46. ulong data = image_get_data (hdr);
  47. ulong len = image_get_data_size (hdr);
  48. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  49. return (dcrc == image_get_dcrc (hdr));
  50. }
  51. #ifndef USE_HOSTCC
  52. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  53. {
  54. ulong dcrc = 0;
  55. ulong len = image_get_data_size (hdr);
  56. ulong data = image_get_data (hdr);
  57. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  58. ulong cdata = data;
  59. ulong edata = cdata + len;
  60. while (cdata < edata) {
  61. ulong chunk = edata - cdata;
  62. if (chunk > chunksz)
  63. chunk = chunksz;
  64. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  65. cdata += chunk;
  66. WATCHDOG_RESET ();
  67. }
  68. #else
  69. dcrc = crc32 (0, (unsigned char *)data, len);
  70. #endif
  71. return (dcrc == image_get_dcrc (hdr));
  72. }
  73. int getenv_verify (void)
  74. {
  75. char *s = getenv ("verify");
  76. return (s && (*s == 'n')) ? 0 : 1;
  77. }
  78. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  79. {
  80. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  81. while (len > 0) {
  82. size_t tail = (len > chunksz) ? chunksz : len;
  83. WATCHDOG_RESET ();
  84. memmove (to, from, tail);
  85. to += tail;
  86. from += tail;
  87. len -= tail;
  88. }
  89. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  90. memmove (to, from, len);
  91. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  92. }
  93. #endif /* USE_HOSTCC */
  94. /**
  95. * image_multi_count - get component (sub-image) count
  96. * @hdr: pointer to the header of the multi component image
  97. *
  98. * image_multi_count() returns number of components in a multi
  99. * component image.
  100. *
  101. * Note: no checking of the image type is done, caller must pass
  102. * a valid multi component image.
  103. *
  104. * returns:
  105. * number of components
  106. */
  107. ulong image_multi_count (image_header_t *hdr)
  108. {
  109. ulong i, count = 0;
  110. ulong *size;
  111. /* get start of the image payload, which in case of multi
  112. * component images that points to a table of component sizes */
  113. size = (ulong *)image_get_data (hdr);
  114. /* count non empty slots */
  115. for (i = 0; size[i]; ++i)
  116. count++;
  117. return count;
  118. }
  119. /**
  120. * image_multi_getimg - get component data address and size
  121. * @hdr: pointer to the header of the multi component image
  122. * @idx: index of the requested component
  123. * @data: pointer to a ulong variable, will hold component data address
  124. * @len: pointer to a ulong variable, will hold component size
  125. *
  126. * image_multi_getimg() returns size and data address for the requested
  127. * component in a multi component image.
  128. *
  129. * Note: no checking of the image type is done, caller must pass
  130. * a valid multi component image.
  131. *
  132. * returns:
  133. * data address and size of the component, if idx is valid
  134. * 0 in data and len, if idx is out of range
  135. */
  136. void image_multi_getimg (image_header_t *hdr, ulong idx,
  137. ulong *data, ulong *len)
  138. {
  139. int i;
  140. ulong *size;
  141. ulong offset, tail, count, img_data;
  142. /* get number of component */
  143. count = image_multi_count (hdr);
  144. /* get start of the image payload, which in case of multi
  145. * component images that points to a table of component sizes */
  146. size = (ulong *)image_get_data (hdr);
  147. /* get address of the proper component data start, which means
  148. * skipping sizes table (add 1 for last, null entry) */
  149. img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
  150. if (idx < count) {
  151. *len = size[idx];
  152. offset = 0;
  153. tail = 0;
  154. /* go over all indices preceding requested component idx */
  155. for (i = 0; i < idx; i++) {
  156. /* add up i-th component size */
  157. offset += size[i];
  158. /* add up alignment for i-th component */
  159. tail += (4 - size[i] % 4);
  160. }
  161. /* calculate idx-th component data address */
  162. *data = img_data + offset + tail;
  163. } else {
  164. *len = 0;
  165. *data = 0;
  166. }
  167. }
  168. #ifndef USE_HOSTCC
  169. const char* image_get_os_name (uint8_t os)
  170. {
  171. const char *name;
  172. switch (os) {
  173. case IH_OS_INVALID: name = "Invalid OS"; break;
  174. case IH_OS_NETBSD: name = "NetBSD"; break;
  175. case IH_OS_LINUX: name = "Linux"; break;
  176. case IH_OS_VXWORKS: name = "VxWorks"; break;
  177. case IH_OS_QNX: name = "QNX"; break;
  178. case IH_OS_U_BOOT: name = "U-Boot"; break;
  179. case IH_OS_RTEMS: name = "RTEMS"; break;
  180. #ifdef CONFIG_ARTOS
  181. case IH_OS_ARTOS: name = "ARTOS"; break;
  182. #endif
  183. #ifdef CONFIG_LYNXKDI
  184. case IH_OS_LYNXOS: name = "LynxOS"; break;
  185. #endif
  186. default: name = "Unknown OS"; break;
  187. }
  188. return name;
  189. }
  190. const char* image_get_arch_name (uint8_t arch)
  191. {
  192. const char *name;
  193. switch (arch) {
  194. case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
  195. case IH_ARCH_ALPHA: name = "Alpha"; break;
  196. case IH_ARCH_ARM: name = "ARM"; break;
  197. case IH_ARCH_AVR32: name = "AVR32"; break;
  198. case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
  199. case IH_ARCH_I386: name = "Intel x86"; break;
  200. case IH_ARCH_IA64: name = "IA64"; break;
  201. case IH_ARCH_M68K: name = "M68K"; break;
  202. case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
  203. case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
  204. case IH_ARCH_MIPS: name = "MIPS"; break;
  205. case IH_ARCH_NIOS2: name = "Nios-II"; break;
  206. case IH_ARCH_NIOS: name = "Nios"; break;
  207. case IH_ARCH_PPC: name = "PowerPC"; break;
  208. case IH_ARCH_S390: name = "IBM S390"; break;
  209. case IH_ARCH_SH: name = "SuperH"; break;
  210. case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
  211. case IH_ARCH_SPARC: name = "SPARC"; break;
  212. default: name = "Unknown Architecture"; break;
  213. }
  214. return name;
  215. }
  216. const char* image_get_type_name (uint8_t type)
  217. {
  218. const char *name;
  219. switch (type) {
  220. case IH_TYPE_INVALID: name = "Invalid Image"; break;
  221. case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
  222. case IH_TYPE_KERNEL: name = "Kernel Image"; break;
  223. case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
  224. case IH_TYPE_MULTI: name = "Multi-File Image"; break;
  225. case IH_TYPE_FIRMWARE: name = "Firmware"; break;
  226. case IH_TYPE_SCRIPT: name = "Script"; break;
  227. case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
  228. default: name = "Unknown Image"; break;
  229. }
  230. return name;
  231. }
  232. const char* image_get_comp_name (uint8_t comp)
  233. {
  234. const char *name;
  235. switch (comp) {
  236. case IH_COMP_NONE: name = "uncompressed"; break;
  237. case IH_COMP_GZIP: name = "gzip compressed"; break;
  238. case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
  239. default: name = "unknown compression"; break;
  240. }
  241. return name;
  242. }
  243. #endif