cmd_bootm.c 34 KB

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