cmd_bootm.c 29 KB

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