cmd_bootm.c 33 KB

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