cmd_bootm.c 27 KB

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