cmd_bootm.c 24 KB

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