cmd_bootm.c 35 KB

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