cmd_bootm.c 25 KB

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