image.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  29. #include <status_led.h>
  30. #endif
  31. #ifdef CONFIG_HAS_DATAFLASH
  32. #include <dataflash.h>
  33. #endif
  34. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  35. #else
  36. #include "mkimage.h"
  37. #endif
  38. #include <image.h>
  39. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  40. int image_check_hcrc (image_header_t *hdr)
  41. {
  42. ulong hcrc;
  43. ulong len = image_get_header_size ();
  44. image_header_t header;
  45. /* Copy header so we can blank CRC field for re-calculation */
  46. memmove (&header, (char *)hdr, image_get_header_size ());
  47. image_set_hcrc (&header, 0);
  48. hcrc = crc32 (0, (unsigned char *)&header, len);
  49. return (hcrc == image_get_hcrc (hdr));
  50. }
  51. int image_check_dcrc (image_header_t *hdr)
  52. {
  53. ulong data = image_get_data (hdr);
  54. ulong len = image_get_data_size (hdr);
  55. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  56. return (dcrc == image_get_dcrc (hdr));
  57. }
  58. #ifndef USE_HOSTCC
  59. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  60. {
  61. ulong dcrc = 0;
  62. ulong len = image_get_data_size (hdr);
  63. ulong data = image_get_data (hdr);
  64. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  65. ulong cdata = data;
  66. ulong edata = cdata + len;
  67. while (cdata < edata) {
  68. ulong chunk = edata - cdata;
  69. if (chunk > chunksz)
  70. chunk = chunksz;
  71. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  72. cdata += chunk;
  73. WATCHDOG_RESET ();
  74. }
  75. #else
  76. dcrc = crc32 (0, (unsigned char *)data, len);
  77. #endif
  78. return (dcrc == image_get_dcrc (hdr));
  79. }
  80. int getenv_verify (void)
  81. {
  82. char *s = getenv ("verify");
  83. return (s && (*s == 'n')) ? 0 : 1;
  84. }
  85. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  86. {
  87. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  88. while (len > 0) {
  89. size_t tail = (len > chunksz) ? chunksz : len;
  90. WATCHDOG_RESET ();
  91. memmove (to, from, tail);
  92. to += tail;
  93. from += tail;
  94. len -= tail;
  95. }
  96. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  97. memmove (to, from, len);
  98. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  99. }
  100. #endif /* USE_HOSTCC */
  101. /**
  102. * image_multi_count - get component (sub-image) count
  103. * @hdr: pointer to the header of the multi component image
  104. *
  105. * image_multi_count() returns number of components in a multi
  106. * component image.
  107. *
  108. * Note: no checking of the image type is done, caller must pass
  109. * a valid multi component image.
  110. *
  111. * returns:
  112. * number of components
  113. */
  114. ulong image_multi_count (image_header_t *hdr)
  115. {
  116. ulong i, count = 0;
  117. ulong *size;
  118. /* get start of the image payload, which in case of multi
  119. * component images that points to a table of component sizes */
  120. size = (ulong *)image_get_data (hdr);
  121. /* count non empty slots */
  122. for (i = 0; size[i]; ++i)
  123. count++;
  124. return count;
  125. }
  126. /**
  127. * image_multi_getimg - get component data address and size
  128. * @hdr: pointer to the header of the multi component image
  129. * @idx: index of the requested component
  130. * @data: pointer to a ulong variable, will hold component data address
  131. * @len: pointer to a ulong variable, will hold component size
  132. *
  133. * image_multi_getimg() returns size and data address for the requested
  134. * component in a multi component image.
  135. *
  136. * Note: no checking of the image type is done, caller must pass
  137. * a valid multi component image.
  138. *
  139. * returns:
  140. * data address and size of the component, if idx is valid
  141. * 0 in data and len, if idx is out of range
  142. */
  143. void image_multi_getimg (image_header_t *hdr, ulong idx,
  144. ulong *data, ulong *len)
  145. {
  146. int i;
  147. ulong *size;
  148. ulong offset, tail, count, img_data;
  149. /* get number of component */
  150. count = image_multi_count (hdr);
  151. /* get start of the image payload, which in case of multi
  152. * component images that points to a table of component sizes */
  153. size = (ulong *)image_get_data (hdr);
  154. /* get address of the proper component data start, which means
  155. * skipping sizes table (add 1 for last, null entry) */
  156. img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
  157. if (idx < count) {
  158. *len = size[idx];
  159. offset = 0;
  160. tail = 0;
  161. /* go over all indices preceding requested component idx */
  162. for (i = 0; i < idx; i++) {
  163. /* add up i-th component size */
  164. offset += size[i];
  165. /* add up alignment for i-th component */
  166. tail += (4 - size[i] % 4);
  167. }
  168. /* calculate idx-th component data address */
  169. *data = img_data + offset + tail;
  170. } else {
  171. *len = 0;
  172. *data = 0;
  173. }
  174. }
  175. #ifndef USE_HOSTCC
  176. const char* image_get_os_name (uint8_t os)
  177. {
  178. const char *name;
  179. switch (os) {
  180. case IH_OS_INVALID: name = "Invalid OS"; break;
  181. case IH_OS_NETBSD: name = "NetBSD"; break;
  182. case IH_OS_LINUX: name = "Linux"; break;
  183. case IH_OS_VXWORKS: name = "VxWorks"; break;
  184. case IH_OS_QNX: name = "QNX"; break;
  185. case IH_OS_U_BOOT: name = "U-Boot"; break;
  186. case IH_OS_RTEMS: name = "RTEMS"; break;
  187. #ifdef CONFIG_ARTOS
  188. case IH_OS_ARTOS: name = "ARTOS"; break;
  189. #endif
  190. #ifdef CONFIG_LYNXKDI
  191. case IH_OS_LYNXOS: name = "LynxOS"; break;
  192. #endif
  193. default: name = "Unknown OS"; break;
  194. }
  195. return name;
  196. }
  197. const char* image_get_arch_name (uint8_t arch)
  198. {
  199. const char *name;
  200. switch (arch) {
  201. case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
  202. case IH_ARCH_ALPHA: name = "Alpha"; break;
  203. case IH_ARCH_ARM: name = "ARM"; break;
  204. case IH_ARCH_AVR32: name = "AVR32"; break;
  205. case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
  206. case IH_ARCH_I386: name = "Intel x86"; break;
  207. case IH_ARCH_IA64: name = "IA64"; break;
  208. case IH_ARCH_M68K: name = "M68K"; break;
  209. case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
  210. case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
  211. case IH_ARCH_MIPS: name = "MIPS"; break;
  212. case IH_ARCH_NIOS2: name = "Nios-II"; break;
  213. case IH_ARCH_NIOS: name = "Nios"; break;
  214. case IH_ARCH_PPC: name = "PowerPC"; break;
  215. case IH_ARCH_S390: name = "IBM S390"; break;
  216. case IH_ARCH_SH: name = "SuperH"; break;
  217. case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
  218. case IH_ARCH_SPARC: name = "SPARC"; break;
  219. default: name = "Unknown Architecture"; break;
  220. }
  221. return name;
  222. }
  223. const char* image_get_type_name (uint8_t type)
  224. {
  225. const char *name;
  226. switch (type) {
  227. case IH_TYPE_INVALID: name = "Invalid Image"; break;
  228. case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
  229. case IH_TYPE_KERNEL: name = "Kernel Image"; break;
  230. case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
  231. case IH_TYPE_MULTI: name = "Multi-File Image"; break;
  232. case IH_TYPE_FIRMWARE: name = "Firmware"; break;
  233. case IH_TYPE_SCRIPT: name = "Script"; break;
  234. case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
  235. default: name = "Unknown Image"; break;
  236. }
  237. return name;
  238. }
  239. const char* image_get_comp_name (uint8_t comp)
  240. {
  241. const char *name;
  242. switch (comp) {
  243. case IH_COMP_NONE: name = "uncompressed"; break;
  244. case IH_COMP_GZIP: name = "gzip compressed"; break;
  245. case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
  246. default: name = "unknown compression"; break;
  247. }
  248. return name;
  249. }
  250. /**
  251. * image_get_ramdisk - get and verify ramdisk image
  252. * @cmdtp: command table pointer
  253. * @flag: command flag
  254. * @argc: command argument count
  255. * @argv: command argument list
  256. * @rd_addr: ramdisk image start address
  257. * @arch: expected ramdisk architecture
  258. * @verify: checksum verification flag
  259. *
  260. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  261. * header. Routine receives image start address and expected architecture
  262. * flag. Verification done covers data and header integrity and os/type/arch
  263. * fields checking.
  264. *
  265. * If dataflash support is enabled routine checks for dataflash addresses
  266. * and handles required dataflash reads.
  267. *
  268. * returns:
  269. * pointer to a ramdisk image header, if image was found and valid
  270. * otherwise, board is reset
  271. */
  272. image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
  273. int argc, char *argv[],
  274. ulong rd_addr, uint8_t arch, int verify)
  275. {
  276. image_header_t *rd_hdr;
  277. show_boot_progress (9);
  278. #ifdef CONFIG_HAS_DATAFLASH
  279. if (addr_dataflash (rd_addr)) {
  280. rd_hdr = (image_header_t *)CFG_LOAD_ADDR;
  281. debug (" Reading Ramdisk image header from dataflash address "
  282. "%08lx to %08lx\n", rd_addr, (ulong)rd_hdr);
  283. read_dataflash (rd_addr, image_get_header_size (),
  284. (char *)rd_hdr);
  285. } else
  286. #endif
  287. rd_hdr = (image_header_t *)rd_addr;
  288. if (!image_check_magic (rd_hdr)) {
  289. puts ("Bad Magic Number\n");
  290. show_boot_progress (-10);
  291. do_reset (cmdtp, flag, argc, argv);
  292. }
  293. if (!image_check_hcrc (rd_hdr)) {
  294. puts ("Bad Header Checksum\n");
  295. show_boot_progress (-11);
  296. do_reset (cmdtp, flag, argc, argv);
  297. }
  298. show_boot_progress (10);
  299. print_image_hdr (rd_hdr);
  300. #ifdef CONFIG_HAS_DATAFLASH
  301. if (addr_dataflash (rd_addr)) {
  302. debug (" Reading Ramdisk image data from dataflash address "
  303. "%08lx to %08lx\n", rd_addr + image_get_header_size,
  304. (ulong)image_get_data (rd_hdr));
  305. read_dataflash (rd_addr + image_get_header_size (),
  306. image_get_data_size (rd_hdr),
  307. (char *)image_get_data (rd_hdr));
  308. }
  309. #endif
  310. if (verify) {
  311. puts(" Verifying Checksum ... ");
  312. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  313. puts ("Bad Data CRC\n");
  314. show_boot_progress (-12);
  315. do_reset (cmdtp, flag, argc, argv);
  316. }
  317. puts("OK\n");
  318. }
  319. show_boot_progress (11);
  320. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  321. !image_check_arch (rd_hdr, arch) ||
  322. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  323. printf ("No Linux %s Ramdisk Image\n",
  324. image_get_arch_name(arch));
  325. show_boot_progress (-13);
  326. do_reset (cmdtp, flag, argc, argv);
  327. }
  328. return rd_hdr;
  329. }
  330. /**
  331. * get_ramdisk - main ramdisk handling routine
  332. * @cmdtp: command table pointer
  333. * @flag: command flag
  334. * @argc: command argument count
  335. * @argv: command argument list
  336. * @hdr: pointer to the posiibly multi componet kernel image
  337. * @verify: checksum verification flag
  338. * @arch: expected ramdisk architecture
  339. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  340. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  341. *
  342. * get_ramdisk() is responsible for finding a valid ramdisk image.
  343. * Curently supported are the following ramdisk sources:
  344. * - multicomponent kernel/ramdisk image,
  345. * - commandline provided address of decicated ramdisk image.
  346. *
  347. * returns:
  348. * rd_start and rd_end are set to ramdisk start/end addresses if
  349. * ramdisk image is found and valid
  350. * rd_start and rd_end are set to 0 if no ramdisk exists
  351. * board is reset if ramdisk image is found but corrupted
  352. */
  353. void get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  354. image_header_t *hdr, int verify, uint8_t arch,
  355. ulong *rd_start, ulong *rd_end)
  356. {
  357. ulong rd_addr;
  358. ulong rd_data, rd_len;
  359. image_header_t *rd_hdr;
  360. if (argc >= 3) {
  361. /*
  362. * Look for a '-' which indicates to ignore the
  363. * ramdisk argument
  364. */
  365. if (strcmp(argv[2], "-") == 0) {
  366. debug ("## Skipping init Ramdisk\n");
  367. rd_len = rd_data = 0;
  368. } else {
  369. /*
  370. * Check if there is an initrd image at the
  371. * address provided in the second bootm argument
  372. */
  373. rd_addr = simple_strtoul (argv[2], NULL, 16);
  374. printf ("## Loading init Ramdisk Image at %08lx ...\n",
  375. rd_addr);
  376. rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
  377. rd_addr, arch, verify);
  378. rd_data = image_get_data (rd_hdr);
  379. rd_len = image_get_data_size (rd_hdr);
  380. #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
  381. /*
  382. *we need to copy the ramdisk to SRAM to let Linux boot
  383. */
  384. memmove ((void *)image_get_load (rd_hdr),
  385. (uchar *)rd_data, rd_len);
  386. rd_data = image_get_load (rd_hdr);
  387. #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
  388. }
  389. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  390. /*
  391. * Now check if we have a multifile image
  392. * Get second entry data start address and len
  393. */
  394. show_boot_progress (13);
  395. printf ("## Loading init Ramdisk from multi component "
  396. "Image at %08lx ...\n", (ulong)hdr);
  397. image_multi_getimg (hdr, 1, &rd_data, &rd_len);
  398. } else {
  399. /*
  400. * no initrd image
  401. */
  402. show_boot_progress (14);
  403. rd_len = rd_data = 0;
  404. }
  405. if (!rd_data) {
  406. debug ("## No init Ramdisk\n");
  407. *rd_start = 0;
  408. *rd_end = 0;
  409. } else {
  410. *rd_start = rd_data;
  411. *rd_end = rd_data + rd_len;
  412. }
  413. debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  414. *rd_start, *rd_end);
  415. }
  416. #endif /* USE_HOSTCC */