cmd_bootm.c 24 KB

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