cmd_bootm.c 28 KB

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