cmd_bootm.c 33 KB

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