cmd_bootm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Boot support
  25. */
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #include <command.h>
  29. #include <cmd_boot.h>
  30. #include <image.h>
  31. #include <malloc.h>
  32. #include <zlib.h>
  33. #include <asm/byteorder.h>
  34. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  35. #include <rtc.h>
  36. #endif
  37. #ifdef CFG_HUSH_PARSER
  38. #include <hush.h>
  39. #endif
  40. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  41. # include <status_led.h>
  42. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  43. #else
  44. # define SHOW_BOOT_PROGRESS(arg)
  45. #endif
  46. #ifdef CFG_INIT_RAM_LOCK
  47. #include <asm/cache.h>
  48. #endif
  49. /*
  50. * Some systems (for example LWMON) have very short watchdog periods;
  51. * we must make sure to split long operations like memmove() or
  52. * crc32() into reasonable chunks.
  53. */
  54. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  55. # define CHUNKSZ (64 * 1024)
  56. #endif
  57. int gunzip (void *, int, unsigned char *, int *);
  58. static void *zalloc(void *, unsigned, unsigned);
  59. static void zfree(void *, void *, unsigned);
  60. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  61. static int image_info (unsigned long addr);
  62. #endif
  63. static void print_type (image_header_t *hdr);
  64. /*
  65. * Continue booting an OS image; caller already has:
  66. * - copied image header to global variable `header'
  67. * - checked header magic number, checksums (both header & image),
  68. * - verified image architecture (PPC) and type (KERNEL or MULTI),
  69. * - loaded (first part of) image to header load address,
  70. * - disabled interrupts.
  71. */
  72. typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
  73. int argc, char *argv[],
  74. ulong addr, /* of image to boot */
  75. ulong *len_ptr, /* multi-file image length table */
  76. int verify); /* getenv("verify")[0] != 'n' */
  77. #ifndef CONFIG_ARM
  78. static boot_os_Fcn do_bootm_linux;
  79. #else
  80. extern boot_os_Fcn do_bootm_linux;
  81. #endif
  82. static boot_os_Fcn do_bootm_netbsd;
  83. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  84. static boot_os_Fcn do_bootm_vxworks;
  85. static boot_os_Fcn do_bootm_qnxelf;
  86. int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  87. int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
  88. #endif /* CFG_CMD_ELF */
  89. image_header_t header;
  90. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
  91. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  92. {
  93. ulong iflag;
  94. ulong addr;
  95. ulong data, len, checksum;
  96. ulong *len_ptr;
  97. int i, verify;
  98. char *name, *s;
  99. int (*appl)(cmd_tbl_t *, int, int, char *[]);
  100. image_header_t *hdr = &header;
  101. s = getenv ("verify");
  102. verify = (s && (*s == 'n')) ? 0 : 1;
  103. if (argc < 2) {
  104. addr = load_addr;
  105. } else {
  106. addr = simple_strtoul(argv[1], NULL, 16);
  107. }
  108. SHOW_BOOT_PROGRESS (1);
  109. printf ("## Booting image at %08lx ...\n", addr);
  110. /* Copy header so we can blank CRC field for re-calculation */
  111. memmove (&header, (char *)addr, sizeof(image_header_t));
  112. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  113. printf ("Bad Magic Number\n");
  114. SHOW_BOOT_PROGRESS (-1);
  115. return 1;
  116. }
  117. SHOW_BOOT_PROGRESS (2);
  118. data = (ulong)&header;
  119. len = sizeof(image_header_t);
  120. checksum = ntohl(hdr->ih_hcrc);
  121. hdr->ih_hcrc = 0;
  122. if (crc32 (0, (char *)data, len) != checksum) {
  123. printf ("Bad Header Checksum\n");
  124. SHOW_BOOT_PROGRESS (-2);
  125. return 1;
  126. }
  127. SHOW_BOOT_PROGRESS (3);
  128. /* for multi-file images we need the data part, too */
  129. print_image_hdr ((image_header_t *)addr);
  130. data = addr + sizeof(image_header_t);
  131. len = ntohl(hdr->ih_size);
  132. if (verify) {
  133. printf (" Verifying Checksum ... ");
  134. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  135. printf ("Bad Data CRC\n");
  136. SHOW_BOOT_PROGRESS (-3);
  137. return 1;
  138. }
  139. printf ("OK\n");
  140. }
  141. SHOW_BOOT_PROGRESS (4);
  142. len_ptr = (ulong *)data;
  143. if (hdr->ih_arch != IH_CPU_PPC && hdr->ih_arch != IH_CPU_ARM) {
  144. printf ("Unsupported Architecture\n");
  145. SHOW_BOOT_PROGRESS (-4);
  146. return 1;
  147. }
  148. SHOW_BOOT_PROGRESS (5);
  149. switch (hdr->ih_type) {
  150. case IH_TYPE_STANDALONE: name = "Standalone Application";
  151. break;
  152. case IH_TYPE_KERNEL: name = "Kernel Image";
  153. break;
  154. case IH_TYPE_MULTI: name = "Multi-File Image";
  155. len = ntohl(len_ptr[0]);
  156. /* OS kernel is always the first image */
  157. data += 8; /* kernel_len + terminator */
  158. for (i=1; len_ptr[i]; ++i)
  159. data += 4;
  160. break;
  161. default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
  162. SHOW_BOOT_PROGRESS (-5);
  163. return 1;
  164. }
  165. SHOW_BOOT_PROGRESS (6);
  166. /*
  167. * We have reached the point of no return: we are going to
  168. * overwrite all exception vector code, so we cannot easily
  169. * recover from any failures any more...
  170. */
  171. iflag = disable_interrupts();
  172. switch (hdr->ih_comp) {
  173. case IH_COMP_NONE:
  174. if(hdr->ih_load == addr) {
  175. printf (" XIP %s ... ", name);
  176. } else {
  177. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  178. size_t l = len;
  179. void *to = (void *)ntohl(hdr->ih_load);
  180. void *from = (void *)data;
  181. printf (" Loading %s ... ", name);
  182. while (l > 0) {
  183. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  184. WATCHDOG_RESET();
  185. memmove (to, from, tail);
  186. to += tail;
  187. from += tail;
  188. l -= tail;
  189. }
  190. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  191. memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
  192. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  193. }
  194. break;
  195. case IH_COMP_GZIP:
  196. printf (" Uncompressing %s ... ", name);
  197. if (gunzip ((void *)ntohl(hdr->ih_load), 0x400000,
  198. (uchar *)data, (int *)&len) != 0) {
  199. printf ("GUNZIP ERROR - must RESET board to recover\n");
  200. SHOW_BOOT_PROGRESS (-6);
  201. do_reset (cmdtp, flag, argc, argv);
  202. }
  203. break;
  204. default:
  205. if (iflag)
  206. enable_interrupts();
  207. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  208. SHOW_BOOT_PROGRESS (-7);
  209. return 1;
  210. }
  211. printf ("OK\n");
  212. SHOW_BOOT_PROGRESS (7);
  213. switch (hdr->ih_type) {
  214. case IH_TYPE_STANDALONE:
  215. appl = (int (*)(cmd_tbl_t *, int, int, char *[]))ntohl(hdr->ih_ep);
  216. if (iflag)
  217. enable_interrupts();
  218. (*appl)(cmdtp, flag, argc-1, &argv[1]);
  219. break;
  220. case IH_TYPE_KERNEL:
  221. case IH_TYPE_MULTI:
  222. /* handled below */
  223. break;
  224. default:
  225. if (iflag)
  226. enable_interrupts();
  227. printf ("Can't boot image type %d\n", hdr->ih_type);
  228. SHOW_BOOT_PROGRESS (-8);
  229. return 1;
  230. }
  231. SHOW_BOOT_PROGRESS (8);
  232. switch (hdr->ih_os) {
  233. default: /* handled by (original) Linux case */
  234. case IH_OS_LINUX:
  235. do_bootm_linux (cmdtp, flag, argc, argv,
  236. addr, len_ptr, verify);
  237. break;
  238. case IH_OS_NETBSD:
  239. do_bootm_netbsd (cmdtp, flag, argc, argv,
  240. addr, len_ptr, verify);
  241. break;
  242. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  243. case IH_OS_VXWORKS:
  244. do_bootm_vxworks (cmdtp, flag, argc, argv,
  245. addr, len_ptr, verify);
  246. break;
  247. case IH_OS_QNX:
  248. do_bootm_qnxelf (cmdtp, flag, argc, argv,
  249. addr, len_ptr, verify);
  250. break;
  251. #endif /* CFG_CMD_ELF */
  252. }
  253. SHOW_BOOT_PROGRESS (-9);
  254. #ifdef DEBUG
  255. printf ("\n## Control returned to monitor - resetting...\n");
  256. do_reset (cmdtp, flag, argc, argv);
  257. #endif
  258. return 1;
  259. }
  260. #ifndef CONFIG_ARM
  261. static void
  262. do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
  263. int argc, char *argv[],
  264. ulong addr,
  265. ulong *len_ptr,
  266. int verify)
  267. {
  268. DECLARE_GLOBAL_DATA_PTR;
  269. ulong sp;
  270. ulong len, checksum;
  271. ulong initrd_start, initrd_end;
  272. ulong cmd_start, cmd_end;
  273. ulong initrd_high;
  274. ulong data;
  275. char *cmdline;
  276. char *s;
  277. bd_t *kbd;
  278. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  279. image_header_t *hdr = &header;
  280. if ((s = getenv ("initrd_high")) != NULL) {
  281. /* a value of "no" or a similar string will act like 0,
  282. * turning the "load high" feature off. This is intentional.
  283. */
  284. initrd_high = simple_strtoul(s, NULL, 16);
  285. } else { /* not set, no restrictions to load high */
  286. initrd_high = ~0;
  287. }
  288. /*
  289. * Booting a (Linux) kernel image
  290. *
  291. * Allocate space for command line and board info - the
  292. * address should be as high as possible within the reach of
  293. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  294. * memory, which means far enough below the current stack
  295. * pointer.
  296. */
  297. asm( "mr %0,1": "=r"(sp) : );
  298. #ifdef DEBUG
  299. printf ("## Current stack ends at 0x%08lX ", sp);
  300. #endif
  301. sp -= 2048; /* just to be sure */
  302. if (sp > CFG_BOOTMAPSZ)
  303. sp = CFG_BOOTMAPSZ;
  304. sp &= ~0xF;
  305. #ifdef DEBUG
  306. printf ("=> set upper limit to 0x%08lX\n", sp);
  307. #endif
  308. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  309. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  310. if ((s = getenv("bootargs")) == NULL)
  311. s = "";
  312. strcpy (cmdline, s);
  313. cmd_start = (ulong)&cmdline[0];
  314. cmd_end = cmd_start + strlen(cmdline);
  315. *kbd = *(gd->bd);
  316. #ifdef DEBUG
  317. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  318. do_bdinfo (NULL, 0, 0, NULL);
  319. #endif
  320. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  321. /* convert all clock information to MHz */
  322. kbd->bi_intfreq /= 1000000L;
  323. kbd->bi_busfreq /= 1000000L;
  324. #if defined(CONFIG_8260)
  325. kbd->bi_cpmfreq /= 1000000L;
  326. kbd->bi_brgfreq /= 1000000L;
  327. kbd->bi_sccfreq /= 1000000L;
  328. kbd->bi_vco /= 1000000L;
  329. #endif /* CONFIG_8260 */
  330. }
  331. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
  332. /*
  333. * Check if there is an initrd image
  334. */
  335. if (argc >= 3) {
  336. SHOW_BOOT_PROGRESS (9);
  337. addr = simple_strtoul(argv[2], NULL, 16);
  338. printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
  339. /* Copy header so we can blank CRC field for re-calculation */
  340. memmove (&header, (char *)addr, sizeof(image_header_t));
  341. if (hdr->ih_magic != IH_MAGIC) {
  342. printf ("Bad Magic Number\n");
  343. SHOW_BOOT_PROGRESS (-10);
  344. do_reset (cmdtp, flag, argc, argv);
  345. }
  346. data = (ulong)&header;
  347. len = sizeof(image_header_t);
  348. checksum = hdr->ih_hcrc;
  349. hdr->ih_hcrc = 0;
  350. if (crc32 (0, (char *)data, len) != checksum) {
  351. printf ("Bad Header Checksum\n");
  352. SHOW_BOOT_PROGRESS (-11);
  353. do_reset (cmdtp, flag, argc, argv);
  354. }
  355. SHOW_BOOT_PROGRESS (10);
  356. print_image_hdr (hdr);
  357. data = addr + sizeof(image_header_t);
  358. len = hdr->ih_size;
  359. if (verify) {
  360. ulong csum = 0;
  361. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  362. ulong cdata = data, edata = cdata + len;
  363. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  364. printf (" Verifying Checksum ... ");
  365. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  366. while (cdata < edata) {
  367. ulong chunk = edata - cdata;
  368. if (chunk > CHUNKSZ)
  369. chunk = CHUNKSZ;
  370. csum = crc32 (csum, (char *)cdata, chunk);
  371. cdata += chunk;
  372. WATCHDOG_RESET();
  373. }
  374. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  375. csum = crc32 (0, (char *)data, len);
  376. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  377. if (csum != hdr->ih_dcrc) {
  378. printf ("Bad Data CRC\n");
  379. SHOW_BOOT_PROGRESS (-12);
  380. do_reset (cmdtp, flag, argc, argv);
  381. }
  382. printf ("OK\n");
  383. }
  384. SHOW_BOOT_PROGRESS (11);
  385. if ((hdr->ih_os != IH_OS_LINUX) ||
  386. (hdr->ih_arch != IH_CPU_PPC) ||
  387. (hdr->ih_type != IH_TYPE_RAMDISK) ) {
  388. printf ("No Linux PPC Ramdisk Image\n");
  389. SHOW_BOOT_PROGRESS (-13);
  390. do_reset (cmdtp, flag, argc, argv);
  391. }
  392. /*
  393. * Now check if we have a multifile image
  394. */
  395. } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
  396. u_long tail = ntohl(len_ptr[0]) % 4;
  397. int i;
  398. SHOW_BOOT_PROGRESS (13);
  399. /* skip kernel length and terminator */
  400. data = (ulong)(&len_ptr[2]);
  401. /* skip any additional image length fields */
  402. for (i=1; len_ptr[i]; ++i)
  403. data += 4;
  404. /* add kernel length, and align */
  405. data += ntohl(len_ptr[0]);
  406. if (tail) {
  407. data += 4 - tail;
  408. }
  409. len = ntohl(len_ptr[1]);
  410. } else {
  411. /*
  412. * no initrd image
  413. */
  414. SHOW_BOOT_PROGRESS (14);
  415. len = data = 0;
  416. }
  417. #ifdef DEBUG
  418. if (!data) {
  419. printf ("No initrd\n");
  420. }
  421. #endif
  422. if (data) {
  423. initrd_start = (ulong)kbd - len;
  424. initrd_start &= ~(4096 - 1); /* align on page */
  425. if (initrd_high) {
  426. ulong nsp;
  427. /*
  428. * the inital ramdisk does not need to be within
  429. * CFG_BOOTMAPSZ as it is not accessed until after
  430. * the mm system is initialised.
  431. *
  432. * do the stack bottom calculation again and see if
  433. * the initrd will fit just below the monitor stack
  434. * bottom without overwriting the area allocated
  435. * above for command line args and board info.
  436. */
  437. asm( "mr %0,1": "=r"(nsp) : );
  438. nsp -= 2048; /* just to be sure */
  439. nsp &= ~0xF;
  440. if (nsp > initrd_high) /* limit as specified */
  441. nsp = initrd_high;
  442. nsp -= len;
  443. nsp &= ~(4096 - 1); /* align on page */
  444. if (nsp >= sp)
  445. initrd_start = nsp;
  446. }
  447. SHOW_BOOT_PROGRESS (12);
  448. #ifdef DEBUG
  449. printf ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  450. data, data + len - 1, len, len);
  451. #endif
  452. initrd_end = initrd_start + len;
  453. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  454. initrd_start, initrd_end);
  455. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  456. {
  457. size_t l = len;
  458. void *to = (void *)initrd_start;
  459. void *from = (void *)data;
  460. while (l > 0) {
  461. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  462. WATCHDOG_RESET();
  463. memmove (to, from, tail);
  464. to += tail;
  465. from += tail;
  466. l -= tail;
  467. }
  468. }
  469. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  470. memmove ((void *)initrd_start, (void *)data, len);
  471. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  472. printf ("OK\n");
  473. } else {
  474. initrd_start = 0;
  475. initrd_end = 0;
  476. }
  477. #ifdef DEBUG
  478. printf ("## Transferring control to Linux (at address %08lx) ...\n",
  479. (ulong)kernel);
  480. #endif
  481. SHOW_BOOT_PROGRESS (15);
  482. #ifdef CFG_INIT_RAM_LOCK
  483. unlock_ram_in_cache();
  484. #endif
  485. /*
  486. * Linux Kernel Parameters:
  487. * r3: ptr to board info data
  488. * r4: initrd_start or 0 if no initrd
  489. * r5: initrd_end - unused if r4 is 0
  490. * r6: Start of command line string
  491. * r7: End of command line string
  492. */
  493. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  494. }
  495. #endif /* CONFIG_ARM */
  496. static void
  497. do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  498. int argc, char *argv[],
  499. ulong addr,
  500. ulong *len_ptr,
  501. int verify)
  502. {
  503. DECLARE_GLOBAL_DATA_PTR;
  504. image_header_t *hdr = &header;
  505. void (*loader)(bd_t *, image_header_t *, char *, char *);
  506. image_header_t *img_addr;
  507. char *consdev;
  508. char *cmdline;
  509. /*
  510. * Booting a (NetBSD) kernel image
  511. *
  512. * This process is pretty similar to a standalone application:
  513. * The (first part of an multi-) image must be a stage-2 loader,
  514. * which in turn is responsible for loading & invoking the actual
  515. * kernel. The only differences are the parameters being passed:
  516. * besides the board info strucure, the loader expects a command
  517. * line, the name of the console device, and (optionally) the
  518. * address of the original image header.
  519. */
  520. img_addr = 0;
  521. if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
  522. img_addr = (image_header_t *) addr;
  523. consdev = "";
  524. #if defined (CONFIG_8xx_CONS_SMC1)
  525. consdev = "smc1";
  526. #elif defined (CONFIG_8xx_CONS_SMC2)
  527. consdev = "smc2";
  528. #elif defined (CONFIG_8xx_CONS_SCC2)
  529. consdev = "scc2";
  530. #elif defined (CONFIG_8xx_CONS_SCC3)
  531. consdev = "scc3";
  532. #endif
  533. if (argc > 2) {
  534. ulong len;
  535. int i;
  536. for (i=2, len=0 ; i<argc ; i+=1)
  537. len += strlen (argv[i]) + 1;
  538. cmdline = malloc (len);
  539. for (i=2, len=0 ; i<argc ; i+=1) {
  540. if (i > 2)
  541. cmdline[len++] = ' ';
  542. strcpy (&cmdline[len], argv[i]);
  543. len += strlen (argv[i]);
  544. }
  545. } else if ((cmdline = getenv("bootargs")) == NULL) {
  546. cmdline = "";
  547. }
  548. loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
  549. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  550. (ulong)loader);
  551. SHOW_BOOT_PROGRESS (15);
  552. /*
  553. * NetBSD Stage-2 Loader Parameters:
  554. * r3: ptr to board info data
  555. * r4: image address
  556. * r5: console device
  557. * r6: boot args string
  558. */
  559. (*loader) (gd->bd, img_addr, consdev, cmdline);
  560. }
  561. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  562. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  563. {
  564. int rcode = 0;
  565. #ifndef CFG_HUSH_PARSER
  566. if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
  567. #else
  568. if (parse_string_outer(getenv("bootcmd"),
  569. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
  570. #endif
  571. return rcode;
  572. }
  573. #endif
  574. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  575. int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  576. {
  577. int arg;
  578. ulong addr;
  579. int rcode=0;
  580. if (argc < 2) {
  581. return image_info (load_addr);
  582. }
  583. for (arg=1; arg <argc; ++arg) {
  584. addr = simple_strtoul(argv[arg], NULL, 16);
  585. if (image_info (addr) != 0) rcode = 1;
  586. }
  587. return rcode;
  588. }
  589. static int image_info (ulong addr)
  590. {
  591. ulong data, len, checksum;
  592. image_header_t *hdr = &header;
  593. printf ("\n## Checking Image at %08lx ...\n", addr);
  594. /* Copy header so we can blank CRC field for re-calculation */
  595. memmove (&header, (char *)addr, sizeof(image_header_t));
  596. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  597. printf (" Bad Magic Number\n");
  598. return 1;
  599. }
  600. data = (ulong)&header;
  601. len = sizeof(image_header_t);
  602. checksum = ntohl(hdr->ih_hcrc);
  603. hdr->ih_hcrc = 0;
  604. if (crc32 (0, (char *)data, len) != checksum) {
  605. printf (" Bad Header Checksum\n");
  606. return 1;
  607. }
  608. /* for multi-file images we need the data part, too */
  609. print_image_hdr ((image_header_t *)addr);
  610. data = addr + sizeof(image_header_t);
  611. len = ntohl(hdr->ih_size);
  612. printf (" Verifying Checksum ... ");
  613. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  614. printf (" Bad Data CRC\n");
  615. return 1;
  616. }
  617. printf ("OK\n");
  618. return 0;
  619. }
  620. #endif /* CFG_CMD_IMI */
  621. void
  622. print_image_hdr (image_header_t *hdr)
  623. {
  624. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  625. time_t timestamp = (time_t)ntohl(hdr->ih_time);
  626. struct rtc_time tm;
  627. #endif
  628. printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  629. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  630. to_tm (timestamp, &tm);
  631. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  632. tm.tm_year, tm.tm_mon, tm.tm_mday,
  633. tm.tm_hour, tm.tm_min, tm.tm_sec);
  634. #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
  635. printf (" Image Type: "); print_type(hdr); printf ("\n");
  636. printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
  637. print_size (ntohl(hdr->ih_size), "\n");
  638. printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
  639. printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
  640. if (hdr->ih_type == IH_TYPE_MULTI) {
  641. int i;
  642. ulong len;
  643. ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
  644. printf (" Contents:\n");
  645. for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
  646. printf (" Image %d: %8ld Bytes = ", i, len);
  647. print_size (len, "\n");
  648. }
  649. }
  650. }
  651. static void
  652. print_type (image_header_t *hdr)
  653. {
  654. char *os, *arch, *type, *comp;
  655. switch (hdr->ih_os) {
  656. case IH_OS_INVALID: os = "Invalid OS"; break;
  657. case IH_OS_NETBSD: os = "NetBSD"; break;
  658. case IH_OS_LINUX: os = "Linux"; break;
  659. case IH_OS_VXWORKS: os = "VxWorks"; break;
  660. case IH_OS_QNX: os = "QNX"; break;
  661. case IH_OS_U_BOOT: os = "U-Boot"; break;
  662. default: os = "Unknown OS"; break;
  663. }
  664. switch (hdr->ih_arch) {
  665. case IH_CPU_INVALID: arch = "Invalid CPU"; break;
  666. case IH_CPU_ALPHA: arch = "Alpha"; break;
  667. case IH_CPU_ARM: arch = "ARM"; break;
  668. case IH_CPU_I386: arch = "Intel x86"; break;
  669. case IH_CPU_IA64: arch = "IA64"; break;
  670. case IH_CPU_MIPS: arch = "MIPS"; break;
  671. case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
  672. case IH_CPU_PPC: arch = "PowerPC"; break;
  673. case IH_CPU_S390: arch = "IBM S390"; break;
  674. case IH_CPU_SH: arch = "SuperH"; break;
  675. case IH_CPU_SPARC: arch = "SPARC"; break;
  676. case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
  677. default: arch = "Unknown Architecture"; break;
  678. }
  679. switch (hdr->ih_type) {
  680. case IH_TYPE_INVALID: type = "Invalid Image"; break;
  681. case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
  682. case IH_TYPE_KERNEL: type = "Kernel Image"; break;
  683. case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
  684. case IH_TYPE_MULTI: type = "Multi-File Image"; break;
  685. case IH_TYPE_FIRMWARE: type = "Firmware"; break;
  686. case IH_TYPE_SCRIPT: type = "Script"; break;
  687. default: type = "Unknown Image"; break;
  688. }
  689. switch (hdr->ih_comp) {
  690. case IH_COMP_NONE: comp = "uncompressed"; break;
  691. case IH_COMP_GZIP: comp = "gzip compressed"; break;
  692. case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
  693. default: comp = "unknown compression"; break;
  694. }
  695. printf ("%s %s %s (%s)", arch, os, type, comp);
  696. }
  697. #define ZALLOC_ALIGNMENT 16
  698. static void *zalloc(void *x, unsigned items, unsigned size)
  699. {
  700. void *p;
  701. size *= items;
  702. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  703. p = malloc (size);
  704. return (p);
  705. }
  706. static void zfree(void *x, void *addr, unsigned nb)
  707. {
  708. free (addr);
  709. }
  710. #define HEAD_CRC 2
  711. #define EXTRA_FIELD 4
  712. #define ORIG_NAME 8
  713. #define COMMENT 0x10
  714. #define RESERVED 0xe0
  715. #define DEFLATED 8
  716. int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
  717. {
  718. z_stream s;
  719. int r, i, flags;
  720. /* skip header */
  721. i = 10;
  722. flags = src[3];
  723. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  724. printf ("Error: Bad gzipped data\n");
  725. return (-1);
  726. }
  727. if ((flags & EXTRA_FIELD) != 0)
  728. i = 12 + src[10] + (src[11] << 8);
  729. if ((flags & ORIG_NAME) != 0)
  730. while (src[i++] != 0)
  731. ;
  732. if ((flags & COMMENT) != 0)
  733. while (src[i++] != 0)
  734. ;
  735. if ((flags & HEAD_CRC) != 0)
  736. i += 2;
  737. if (i >= *lenp) {
  738. printf ("Error: gunzip out of data in header\n");
  739. return (-1);
  740. }
  741. s.zalloc = zalloc;
  742. s.zfree = zfree;
  743. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  744. s.outcb = (cb_func)WATCHDOG_RESET;
  745. #else
  746. s.outcb = Z_NULL;
  747. #endif /* CONFIG_HW_WATCHDOG */
  748. r = inflateInit2(&s, -MAX_WBITS);
  749. if (r != Z_OK) {
  750. printf ("Error: inflateInit2() returned %d\n", r);
  751. return (-1);
  752. }
  753. s.next_in = src + i;
  754. s.avail_in = *lenp - i;
  755. s.next_out = dst;
  756. s.avail_out = dstlen;
  757. r = inflate(&s, Z_FINISH);
  758. if (r != Z_OK && r != Z_STREAM_END) {
  759. printf ("Error: inflate() returned %d\n", r);
  760. return (-1);
  761. }
  762. *lenp = s.next_out - (unsigned char *) dst;
  763. inflateEnd(&s);
  764. return (0);
  765. }
  766. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  767. static void
  768. do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  769. ulong addr, ulong *len_ptr, int verify)
  770. {
  771. image_header_t *hdr = &header;
  772. char str[80];
  773. sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
  774. setenv("loadaddr", str);
  775. do_bootvx(cmdtp, 0, 0, NULL);
  776. }
  777. static void
  778. do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  779. ulong addr, ulong *len_ptr, int verify)
  780. {
  781. image_header_t *hdr = &header;
  782. char *local_args[2];
  783. char str[16];
  784. sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
  785. local_args[0] = argv[0];
  786. local_args[1] = str; /* and provide it via the arguments */
  787. do_bootelf(cmdtp, 0, 2, local_args);
  788. }
  789. #endif /* CFG_CMD_ELF */