image.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. #define DEBUG
  26. #ifndef USE_HOSTCC
  27. #include <common.h>
  28. #include <watchdog.h>
  29. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  30. #include <status_led.h>
  31. #endif
  32. #ifdef CONFIG_HAS_DATAFLASH
  33. #include <dataflash.h>
  34. #endif
  35. #ifdef CONFIG_LOGBUFFER
  36. #include <logbuff.h>
  37. #endif
  38. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  39. #else
  40. #include "mkimage.h"
  41. #endif
  42. #include <image.h>
  43. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  44. int image_check_hcrc (image_header_t *hdr)
  45. {
  46. ulong hcrc;
  47. ulong len = image_get_header_size ();
  48. image_header_t header;
  49. /* Copy header so we can blank CRC field for re-calculation */
  50. memmove (&header, (char *)hdr, image_get_header_size ());
  51. image_set_hcrc (&header, 0);
  52. hcrc = crc32 (0, (unsigned char *)&header, len);
  53. return (hcrc == image_get_hcrc (hdr));
  54. }
  55. int image_check_dcrc (image_header_t *hdr)
  56. {
  57. ulong data = image_get_data (hdr);
  58. ulong len = image_get_data_size (hdr);
  59. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  60. return (dcrc == image_get_dcrc (hdr));
  61. }
  62. #ifndef USE_HOSTCC
  63. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  64. {
  65. ulong dcrc = 0;
  66. ulong len = image_get_data_size (hdr);
  67. ulong data = image_get_data (hdr);
  68. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  69. ulong cdata = data;
  70. ulong edata = cdata + len;
  71. while (cdata < edata) {
  72. ulong chunk = edata - cdata;
  73. if (chunk > chunksz)
  74. chunk = chunksz;
  75. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  76. cdata += chunk;
  77. WATCHDOG_RESET ();
  78. }
  79. #else
  80. dcrc = crc32 (0, (unsigned char *)data, len);
  81. #endif
  82. return (dcrc == image_get_dcrc (hdr));
  83. }
  84. int getenv_verify (void)
  85. {
  86. char *s = getenv ("verify");
  87. return (s && (*s == 'n')) ? 0 : 1;
  88. }
  89. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  90. {
  91. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  92. while (len > 0) {
  93. size_t tail = (len > chunksz) ? chunksz : len;
  94. WATCHDOG_RESET ();
  95. memmove (to, from, tail);
  96. to += tail;
  97. from += tail;
  98. len -= tail;
  99. }
  100. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  101. memmove (to, from, len);
  102. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  103. }
  104. #endif /* USE_HOSTCC */
  105. /**
  106. * image_multi_count - get component (sub-image) count
  107. * @hdr: pointer to the header of the multi component image
  108. *
  109. * image_multi_count() returns number of components in a multi
  110. * component image.
  111. *
  112. * Note: no checking of the image type is done, caller must pass
  113. * a valid multi component image.
  114. *
  115. * returns:
  116. * number of components
  117. */
  118. ulong image_multi_count (image_header_t *hdr)
  119. {
  120. ulong i, count = 0;
  121. ulong *size;
  122. /* get start of the image payload, which in case of multi
  123. * component images that points to a table of component sizes */
  124. size = (ulong *)image_get_data (hdr);
  125. /* count non empty slots */
  126. for (i = 0; size[i]; ++i)
  127. count++;
  128. return count;
  129. }
  130. /**
  131. * image_multi_getimg - get component data address and size
  132. * @hdr: pointer to the header of the multi component image
  133. * @idx: index of the requested component
  134. * @data: pointer to a ulong variable, will hold component data address
  135. * @len: pointer to a ulong variable, will hold component size
  136. *
  137. * image_multi_getimg() returns size and data address for the requested
  138. * component in a multi component image.
  139. *
  140. * Note: no checking of the image type is done, caller must pass
  141. * a valid multi component image.
  142. *
  143. * returns:
  144. * data address and size of the component, if idx is valid
  145. * 0 in data and len, if idx is out of range
  146. */
  147. void image_multi_getimg (image_header_t *hdr, ulong idx,
  148. ulong *data, ulong *len)
  149. {
  150. int i;
  151. ulong *size;
  152. ulong offset, tail, count, img_data;
  153. /* get number of component */
  154. count = image_multi_count (hdr);
  155. /* get start of the image payload, which in case of multi
  156. * component images that points to a table of component sizes */
  157. size = (ulong *)image_get_data (hdr);
  158. /* get address of the proper component data start, which means
  159. * skipping sizes table (add 1 for last, null entry) */
  160. img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
  161. if (idx < count) {
  162. *len = size[idx];
  163. offset = 0;
  164. tail = 0;
  165. /* go over all indices preceding requested component idx */
  166. for (i = 0; i < idx; i++) {
  167. /* add up i-th component size */
  168. offset += size[i];
  169. /* add up alignment for i-th component */
  170. tail += (4 - size[i] % 4);
  171. }
  172. /* calculate idx-th component data address */
  173. *data = img_data + offset + tail;
  174. } else {
  175. *len = 0;
  176. *data = 0;
  177. }
  178. }
  179. #ifndef USE_HOSTCC
  180. const char* image_get_os_name (uint8_t os)
  181. {
  182. const char *name;
  183. switch (os) {
  184. case IH_OS_INVALID: name = "Invalid OS"; break;
  185. case IH_OS_NETBSD: name = "NetBSD"; break;
  186. case IH_OS_LINUX: name = "Linux"; break;
  187. case IH_OS_VXWORKS: name = "VxWorks"; break;
  188. case IH_OS_QNX: name = "QNX"; break;
  189. case IH_OS_U_BOOT: name = "U-Boot"; break;
  190. case IH_OS_RTEMS: name = "RTEMS"; break;
  191. #ifdef CONFIG_ARTOS
  192. case IH_OS_ARTOS: name = "ARTOS"; break;
  193. #endif
  194. #ifdef CONFIG_LYNXKDI
  195. case IH_OS_LYNXOS: name = "LynxOS"; break;
  196. #endif
  197. default: name = "Unknown OS"; break;
  198. }
  199. return name;
  200. }
  201. const char* image_get_arch_name (uint8_t arch)
  202. {
  203. const char *name;
  204. switch (arch) {
  205. case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
  206. case IH_ARCH_ALPHA: name = "Alpha"; break;
  207. case IH_ARCH_ARM: name = "ARM"; break;
  208. case IH_ARCH_AVR32: name = "AVR32"; break;
  209. case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
  210. case IH_ARCH_I386: name = "Intel x86"; break;
  211. case IH_ARCH_IA64: name = "IA64"; break;
  212. case IH_ARCH_M68K: name = "M68K"; break;
  213. case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
  214. case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
  215. case IH_ARCH_MIPS: name = "MIPS"; break;
  216. case IH_ARCH_NIOS2: name = "Nios-II"; break;
  217. case IH_ARCH_NIOS: name = "Nios"; break;
  218. case IH_ARCH_PPC: name = "PowerPC"; break;
  219. case IH_ARCH_S390: name = "IBM S390"; break;
  220. case IH_ARCH_SH: name = "SuperH"; break;
  221. case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
  222. case IH_ARCH_SPARC: name = "SPARC"; break;
  223. default: name = "Unknown Architecture"; break;
  224. }
  225. return name;
  226. }
  227. const char* image_get_type_name (uint8_t type)
  228. {
  229. const char *name;
  230. switch (type) {
  231. case IH_TYPE_INVALID: name = "Invalid Image"; break;
  232. case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
  233. case IH_TYPE_KERNEL: name = "Kernel Image"; break;
  234. case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
  235. case IH_TYPE_MULTI: name = "Multi-File Image"; break;
  236. case IH_TYPE_FIRMWARE: name = "Firmware"; break;
  237. case IH_TYPE_SCRIPT: name = "Script"; break;
  238. case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
  239. default: name = "Unknown Image"; break;
  240. }
  241. return name;
  242. }
  243. const char* image_get_comp_name (uint8_t comp)
  244. {
  245. const char *name;
  246. switch (comp) {
  247. case IH_COMP_NONE: name = "uncompressed"; break;
  248. case IH_COMP_GZIP: name = "gzip compressed"; break;
  249. case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
  250. default: name = "unknown compression"; break;
  251. }
  252. return name;
  253. }
  254. /**
  255. * image_get_ramdisk - get and verify ramdisk image
  256. * @cmdtp: command table pointer
  257. * @flag: command flag
  258. * @argc: command argument count
  259. * @argv: command argument list
  260. * @rd_addr: ramdisk image start address
  261. * @arch: expected ramdisk architecture
  262. * @verify: checksum verification flag
  263. *
  264. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  265. * header. Routine receives image start address and expected architecture
  266. * flag. Verification done covers data and header integrity and os/type/arch
  267. * fields checking.
  268. *
  269. * If dataflash support is enabled routine checks for dataflash addresses
  270. * and handles required dataflash reads.
  271. *
  272. * returns:
  273. * pointer to a ramdisk image header, if image was found and valid
  274. * otherwise, board is reset
  275. */
  276. image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
  277. int argc, char *argv[],
  278. ulong rd_addr, uint8_t arch, int verify)
  279. {
  280. image_header_t *rd_hdr;
  281. show_boot_progress (9);
  282. #ifdef CONFIG_HAS_DATAFLASH
  283. if (addr_dataflash (rd_addr)) {
  284. rd_hdr = (image_header_t *)CFG_LOAD_ADDR;
  285. debug (" Reading Ramdisk image header from dataflash address "
  286. "%08lx to %08lx\n", rd_addr, (ulong)rd_hdr);
  287. read_dataflash (rd_addr, image_get_header_size (),
  288. (char *)rd_hdr);
  289. } else
  290. #endif
  291. rd_hdr = (image_header_t *)rd_addr;
  292. if (!image_check_magic (rd_hdr)) {
  293. puts ("Bad Magic Number\n");
  294. show_boot_progress (-10);
  295. do_reset (cmdtp, flag, argc, argv);
  296. }
  297. if (!image_check_hcrc (rd_hdr)) {
  298. puts ("Bad Header Checksum\n");
  299. show_boot_progress (-11);
  300. do_reset (cmdtp, flag, argc, argv);
  301. }
  302. show_boot_progress (10);
  303. print_image_hdr (rd_hdr);
  304. #ifdef CONFIG_HAS_DATAFLASH
  305. if (addr_dataflash (rd_addr)) {
  306. debug (" Reading Ramdisk image data from dataflash address "
  307. "%08lx to %08lx\n", rd_addr + image_get_header_size,
  308. (ulong)image_get_data (rd_hdr));
  309. read_dataflash (rd_addr + image_get_header_size (),
  310. image_get_data_size (rd_hdr),
  311. (char *)image_get_data (rd_hdr));
  312. }
  313. #endif
  314. if (verify) {
  315. puts(" Verifying Checksum ... ");
  316. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  317. puts ("Bad Data CRC\n");
  318. show_boot_progress (-12);
  319. do_reset (cmdtp, flag, argc, argv);
  320. }
  321. puts("OK\n");
  322. }
  323. show_boot_progress (11);
  324. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  325. !image_check_arch (rd_hdr, arch) ||
  326. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  327. printf ("No Linux %s Ramdisk Image\n",
  328. image_get_arch_name(arch));
  329. show_boot_progress (-13);
  330. do_reset (cmdtp, flag, argc, argv);
  331. }
  332. return rd_hdr;
  333. }
  334. /**
  335. * get_ramdisk - main ramdisk handling routine
  336. * @cmdtp: command table pointer
  337. * @flag: command flag
  338. * @argc: command argument count
  339. * @argv: command argument list
  340. * @hdr: pointer to the posiibly multi componet kernel image
  341. * @verify: checksum verification flag
  342. * @arch: expected ramdisk architecture
  343. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  344. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  345. *
  346. * get_ramdisk() is responsible for finding a valid ramdisk image.
  347. * Curently supported are the following ramdisk sources:
  348. * - multicomponent kernel/ramdisk image,
  349. * - commandline provided address of decicated ramdisk image.
  350. *
  351. * returns:
  352. * rd_start and rd_end are set to ramdisk start/end addresses if
  353. * ramdisk image is found and valid
  354. * rd_start and rd_end are set to 0 if no ramdisk exists
  355. * board is reset if ramdisk image is found but corrupted
  356. */
  357. void get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  358. image_header_t *hdr, int verify, uint8_t arch,
  359. ulong *rd_start, ulong *rd_end)
  360. {
  361. ulong rd_addr;
  362. ulong rd_data, rd_len;
  363. image_header_t *rd_hdr;
  364. if (argc >= 3) {
  365. /*
  366. * Look for a '-' which indicates to ignore the
  367. * ramdisk argument
  368. */
  369. if (strcmp(argv[2], "-") == 0) {
  370. debug ("## Skipping init Ramdisk\n");
  371. rd_len = rd_data = 0;
  372. } else {
  373. /*
  374. * Check if there is an initrd image at the
  375. * address provided in the second bootm argument
  376. */
  377. rd_addr = simple_strtoul (argv[2], NULL, 16);
  378. printf ("## Loading init Ramdisk Image at %08lx ...\n",
  379. rd_addr);
  380. rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
  381. rd_addr, arch, verify);
  382. rd_data = image_get_data (rd_hdr);
  383. rd_len = image_get_data_size (rd_hdr);
  384. #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
  385. /*
  386. *we need to copy the ramdisk to SRAM to let Linux boot
  387. */
  388. memmove ((void *)image_get_load (rd_hdr),
  389. (uchar *)rd_data, rd_len);
  390. rd_data = image_get_load (rd_hdr);
  391. #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
  392. }
  393. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  394. /*
  395. * Now check if we have a multifile image
  396. * Get second entry data start address and len
  397. */
  398. show_boot_progress (13);
  399. printf ("## Loading init Ramdisk from multi component "
  400. "Image at %08lx ...\n", (ulong)hdr);
  401. image_multi_getimg (hdr, 1, &rd_data, &rd_len);
  402. } else {
  403. /*
  404. * no initrd image
  405. */
  406. show_boot_progress (14);
  407. rd_len = rd_data = 0;
  408. }
  409. if (!rd_data) {
  410. debug ("## No init Ramdisk\n");
  411. *rd_start = 0;
  412. *rd_end = 0;
  413. } else {
  414. *rd_start = rd_data;
  415. *rd_end = rd_data + rd_len;
  416. }
  417. debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  418. *rd_start, *rd_end);
  419. }
  420. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  421. /**
  422. * ramdisk_high - relocate init ramdisk
  423. * @rd_data: ramdisk data start address
  424. * @rd_len: ramdisk data length
  425. * @kbd: kernel board info copy (within BOOTMAPSZ boundary)
  426. * @sp_limit: stack pointer limit (including BOOTMAPSZ)
  427. * @sp: current stack pointer
  428. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  429. * start address (after possible relocation)
  430. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  431. * end address (after possible relocation)
  432. *
  433. * ramdisk_high() takes a relocation hint from "initrd_high" environement
  434. * variable and if requested ramdisk data is moved to a specified location.
  435. *
  436. * returns:
  437. * initrd_start and initrd_end are set to final (after relocation) ramdisk
  438. * start/end addresses if ramdisk image start and len were provided
  439. * otherwise set initrd_start and initrd_end to zeros
  440. *
  441. */
  442. void ramdisk_high (ulong rd_data, ulong rd_len, bd_t *kbd, ulong sp_limit,
  443. ulong sp, ulong *initrd_start, ulong *initrd_end)
  444. {
  445. char *s;
  446. ulong initrd_high;
  447. int initrd_copy_to_ram = 1;
  448. if ((s = getenv ("initrd_high")) != NULL) {
  449. /* a value of "no" or a similar string will act like 0,
  450. * turning the "load high" feature off. This is intentional.
  451. */
  452. initrd_high = simple_strtoul (s, NULL, 16);
  453. if (initrd_high == ~0)
  454. initrd_copy_to_ram = 0;
  455. } else {
  456. /* not set, no restrictions to load high */
  457. initrd_high = ~0;
  458. }
  459. #ifdef CONFIG_LOGBUFFER
  460. /* Prevent initrd from overwriting logbuffer */
  461. if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
  462. initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
  463. debug ("## Logbuffer at 0x%08lx ", kbd->bi_memsize - LOGBUFF_LEN);
  464. #endif
  465. debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  466. initrd_high, initrd_copy_to_ram);
  467. if (rd_data) {
  468. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  469. debug (" in-place initrd\n");
  470. *initrd_start = rd_data;
  471. *initrd_end = rd_data + rd_len;
  472. } else {
  473. *initrd_start = (ulong)kbd - rd_len;
  474. *initrd_start &= ~(4096 - 1); /* align on page */
  475. if (initrd_high) {
  476. ulong nsp;
  477. /*
  478. * the inital ramdisk does not need to be within
  479. * CFG_BOOTMAPSZ as it is not accessed until after
  480. * the mm system is initialised.
  481. *
  482. * do the stack bottom calculation again and see if
  483. * the initrd will fit just below the monitor stack
  484. * bottom without overwriting the area allocated
  485. * for command line args and board info.
  486. */
  487. nsp = sp;
  488. nsp -= 2048; /* just to be sure */
  489. nsp &= ~0xF;
  490. if (nsp > initrd_high) /* limit as specified */
  491. nsp = initrd_high;
  492. nsp -= rd_len;
  493. nsp &= ~(4096 - 1); /* align on page */
  494. if (nsp >= sp_limit)
  495. *initrd_start = nsp;
  496. }
  497. show_boot_progress (12);
  498. *initrd_end = *initrd_start + rd_len;
  499. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  500. *initrd_start, *initrd_end);
  501. memmove_wd((void *)*initrd_start,
  502. (void *)rd_data, rd_len, CHUNKSZ);
  503. puts ("OK\n");
  504. }
  505. } else {
  506. *initrd_start = 0;
  507. *initrd_end = 0;
  508. }
  509. debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  510. *initrd_start, *initrd_end);
  511. }
  512. #endif /* CONFIG_PPC || CONFIG_M68K */
  513. #endif /* USE_HOSTCC */