cmd_bootm.c 35 KB

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