cmd_bootm.c 24 KB

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