cmd_bootm.c 33 KB

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