cmd_bootm.c 24 KB

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