cmd_bootm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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. /*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. #ifndef CFG_BOOTM_LEN
  124. #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
  125. #endif
  126. image_header_t header;
  127. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
  128. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  129. {
  130. ulong iflag;
  131. ulong addr;
  132. ulong data, len, checksum;
  133. ulong *len_ptr;
  134. uint unc_len = CFG_BOOTM_LEN;
  135. int i, verify;
  136. char *name, *s;
  137. int (*appl)(int, char *[]);
  138. image_header_t *hdr = &header;
  139. s = getenv ("verify");
  140. verify = (s && (*s == 'n')) ? 0 : 1;
  141. if (argc < 2) {
  142. addr = load_addr;
  143. } else {
  144. addr = simple_strtoul(argv[1], NULL, 16);
  145. }
  146. SHOW_BOOT_PROGRESS (1);
  147. printf ("## Booting image at %08lx ...\n", addr);
  148. /* Copy header so we can blank CRC field for re-calculation */
  149. #ifdef CONFIG_HAS_DATAFLASH
  150. if (addr_dataflash(addr)){
  151. read_dataflash(addr, sizeof(image_header_t), (char *)&header);
  152. } else
  153. #endif
  154. memmove (&header, (char *)addr, sizeof(image_header_t));
  155. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  156. #ifdef __I386__ /* correct image format not implemented yet - fake it */
  157. if (fake_header(hdr, (void*)addr, -1) != NULL) {
  158. /* to compensate for the addition below */
  159. addr -= sizeof(image_header_t);
  160. /* turnof verify,
  161. * fake_header() does not fake the data crc
  162. */
  163. verify = 0;
  164. } else
  165. #endif /* __I386__ */
  166. {
  167. puts ("Bad Magic Number\n");
  168. SHOW_BOOT_PROGRESS (-1);
  169. return 1;
  170. }
  171. }
  172. SHOW_BOOT_PROGRESS (2);
  173. data = (ulong)&header;
  174. len = sizeof(image_header_t);
  175. checksum = ntohl(hdr->ih_hcrc);
  176. hdr->ih_hcrc = 0;
  177. if (crc32 (0, (uchar *)data, len) != checksum) {
  178. puts ("Bad Header Checksum\n");
  179. SHOW_BOOT_PROGRESS (-2);
  180. return 1;
  181. }
  182. SHOW_BOOT_PROGRESS (3);
  183. #ifdef CONFIG_HAS_DATAFLASH
  184. if (addr_dataflash(addr)){
  185. len = ntohl(hdr->ih_size) + sizeof(image_header_t);
  186. read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
  187. addr = CFG_LOAD_ADDR;
  188. }
  189. #endif
  190. /* for multi-file images we need the data part, too */
  191. print_image_hdr ((image_header_t *)addr);
  192. data = addr + sizeof(image_header_t);
  193. len = ntohl(hdr->ih_size);
  194. if (verify) {
  195. puts (" Verifying Checksum ... ");
  196. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  197. printf ("Bad Data CRC\n");
  198. SHOW_BOOT_PROGRESS (-3);
  199. return 1;
  200. }
  201. puts ("OK\n");
  202. }
  203. SHOW_BOOT_PROGRESS (4);
  204. len_ptr = (ulong *)data;
  205. #if defined(__PPC__)
  206. if (hdr->ih_arch != IH_CPU_PPC)
  207. #elif defined(__ARM__)
  208. if (hdr->ih_arch != IH_CPU_ARM)
  209. #elif defined(__I386__)
  210. if (hdr->ih_arch != IH_CPU_I386)
  211. #elif defined(__mips__)
  212. if (hdr->ih_arch != IH_CPU_MIPS)
  213. #elif defined(__nios__)
  214. if (hdr->ih_arch != IH_CPU_NIOS)
  215. #elif defined(__M68K__)
  216. if (hdr->ih_arch != IH_CPU_M68K)
  217. #elif defined(__microblaze__)
  218. if (hdr->ih_arch != IH_CPU_MICROBLAZE)
  219. #elif defined(__nios2__)
  220. if (hdr->ih_arch != IH_CPU_NIOS2)
  221. #elif defined(__blackfin__)
  222. if (hdr->ih_arch != IH_CPU_BLACKFIN)
  223. #else
  224. # error Unknown CPU type
  225. #endif
  226. {
  227. printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
  228. SHOW_BOOT_PROGRESS (-4);
  229. return 1;
  230. }
  231. SHOW_BOOT_PROGRESS (5);
  232. switch (hdr->ih_type) {
  233. case IH_TYPE_STANDALONE:
  234. name = "Standalone Application";
  235. /* A second argument overwrites the load address */
  236. if (argc > 2) {
  237. hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
  238. }
  239. break;
  240. case IH_TYPE_KERNEL:
  241. name = "Kernel Image";
  242. break;
  243. case IH_TYPE_MULTI:
  244. name = "Multi-File Image";
  245. len = ntohl(len_ptr[0]);
  246. /* OS kernel is always the first image */
  247. data += 8; /* kernel_len + terminator */
  248. for (i=1; len_ptr[i]; ++i)
  249. data += 4;
  250. break;
  251. default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
  252. SHOW_BOOT_PROGRESS (-5);
  253. return 1;
  254. }
  255. SHOW_BOOT_PROGRESS (6);
  256. /*
  257. * We have reached the point of no return: we are going to
  258. * overwrite all exception vector code, so we cannot easily
  259. * recover from any failures any more...
  260. */
  261. iflag = disable_interrupts();
  262. #ifdef CONFIG_AMIGAONEG3SE
  263. /*
  264. * We've possible left the caches enabled during
  265. * bios emulation, so turn them off again
  266. */
  267. icache_disable();
  268. invalidate_l1_instruction_cache();
  269. flush_data_cache();
  270. dcache_disable();
  271. #endif
  272. switch (hdr->ih_comp) {
  273. case IH_COMP_NONE:
  274. if(ntohl(hdr->ih_load) == addr) {
  275. printf (" XIP %s ... ", name);
  276. } else {
  277. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  278. size_t l = len;
  279. void *to = (void *)ntohl(hdr->ih_load);
  280. void *from = (void *)data;
  281. printf (" Loading %s ... ", name);
  282. while (l > 0) {
  283. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  284. WATCHDOG_RESET();
  285. memmove (to, from, tail);
  286. to += tail;
  287. from += tail;
  288. l -= tail;
  289. }
  290. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  291. memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
  292. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  293. }
  294. break;
  295. case IH_COMP_GZIP:
  296. printf (" Uncompressing %s ... ", name);
  297. if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
  298. (uchar *)data, &len) != 0) {
  299. puts ("GUNZIP ERROR - must RESET board to recover\n");
  300. SHOW_BOOT_PROGRESS (-6);
  301. do_reset (cmdtp, flag, argc, argv);
  302. }
  303. break;
  304. #ifdef CONFIG_BZIP2
  305. case IH_COMP_BZIP2:
  306. printf (" Uncompressing %s ... ", name);
  307. /*
  308. * If we've got less than 4 MB of malloc() space,
  309. * use slower decompression algorithm which requires
  310. * at most 2300 KB of memory.
  311. */
  312. i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
  313. &unc_len, (char *)data, len,
  314. CFG_MALLOC_LEN < (4096 * 1024), 0);
  315. if (i != BZ_OK) {
  316. printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
  317. SHOW_BOOT_PROGRESS (-6);
  318. udelay(100000);
  319. do_reset (cmdtp, flag, argc, argv);
  320. }
  321. break;
  322. #endif /* CONFIG_BZIP2 */
  323. default:
  324. if (iflag)
  325. enable_interrupts();
  326. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  327. SHOW_BOOT_PROGRESS (-7);
  328. return 1;
  329. }
  330. puts ("OK\n");
  331. SHOW_BOOT_PROGRESS (7);
  332. switch (hdr->ih_type) {
  333. case IH_TYPE_STANDALONE:
  334. if (iflag)
  335. enable_interrupts();
  336. /* load (and uncompress), but don't start if "autostart"
  337. * is set to "no"
  338. */
  339. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
  340. char buf[32];
  341. sprintf(buf, "%lX", len);
  342. setenv("filesize", buf);
  343. return 0;
  344. }
  345. appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
  346. (*appl)(argc-1, &argv[1]);
  347. return 0;
  348. case IH_TYPE_KERNEL:
  349. case IH_TYPE_MULTI:
  350. /* handled below */
  351. break;
  352. default:
  353. if (iflag)
  354. enable_interrupts();
  355. printf ("Can't boot image type %d\n", hdr->ih_type);
  356. SHOW_BOOT_PROGRESS (-8);
  357. return 1;
  358. }
  359. SHOW_BOOT_PROGRESS (8);
  360. switch (hdr->ih_os) {
  361. default: /* handled by (original) Linux case */
  362. case IH_OS_LINUX:
  363. #ifdef CONFIG_SILENT_CONSOLE
  364. fixup_silent_linux();
  365. #endif
  366. do_bootm_linux (cmdtp, flag, argc, argv,
  367. addr, len_ptr, verify);
  368. break;
  369. case IH_OS_NETBSD:
  370. do_bootm_netbsd (cmdtp, flag, argc, argv,
  371. addr, len_ptr, verify);
  372. break;
  373. #ifdef CONFIG_LYNXKDI
  374. case IH_OS_LYNXOS:
  375. do_bootm_lynxkdi (cmdtp, flag, argc, argv,
  376. addr, len_ptr, verify);
  377. break;
  378. #endif
  379. case IH_OS_RTEMS:
  380. do_bootm_rtems (cmdtp, flag, argc, argv,
  381. addr, len_ptr, verify);
  382. break;
  383. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  384. case IH_OS_VXWORKS:
  385. do_bootm_vxworks (cmdtp, flag, argc, argv,
  386. addr, len_ptr, verify);
  387. break;
  388. case IH_OS_QNX:
  389. do_bootm_qnxelf (cmdtp, flag, argc, argv,
  390. addr, len_ptr, verify);
  391. break;
  392. #endif /* CFG_CMD_ELF */
  393. #ifdef CONFIG_ARTOS
  394. case IH_OS_ARTOS:
  395. do_bootm_artos (cmdtp, flag, argc, argv,
  396. addr, len_ptr, verify);
  397. break;
  398. #endif
  399. }
  400. SHOW_BOOT_PROGRESS (-9);
  401. #ifdef DEBUG
  402. puts ("\n## Control returned to monitor - resetting...\n");
  403. do_reset (cmdtp, flag, argc, argv);
  404. #endif
  405. return 1;
  406. }
  407. U_BOOT_CMD(
  408. bootm, CFG_MAXARGS, 1, do_bootm,
  409. "bootm - boot application image from memory\n",
  410. "[addr [arg ...]]\n - boot application image stored in memory\n"
  411. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  412. "\t'arg' can be the address of an initrd image\n"
  413. );
  414. #ifdef CONFIG_SILENT_CONSOLE
  415. static void
  416. fixup_silent_linux ()
  417. {
  418. DECLARE_GLOBAL_DATA_PTR;
  419. char buf[256], *start, *end;
  420. char *cmdline = getenv ("bootargs");
  421. /* Only fix cmdline when requested */
  422. if (!(gd->flags & GD_FLG_SILENT))
  423. return;
  424. debug ("before silent fix-up: %s\n", cmdline);
  425. if (cmdline) {
  426. if ((start = strstr (cmdline, "console=")) != NULL) {
  427. end = strchr (start, ' ');
  428. strncpy (buf, cmdline, (start - cmdline + 8));
  429. if (end)
  430. strcpy (buf + (start - cmdline + 8), end);
  431. else
  432. buf[start - cmdline + 8] = '\0';
  433. } else {
  434. strcpy (buf, cmdline);
  435. strcat (buf, " console=");
  436. }
  437. } else {
  438. strcpy (buf, "console=");
  439. }
  440. setenv ("bootargs", buf);
  441. debug ("after silent fix-up: %s\n", buf);
  442. }
  443. #endif /* CONFIG_SILENT_CONSOLE */
  444. #ifdef CONFIG_OF_FLAT_TREE
  445. extern const unsigned char oftree_dtb[];
  446. extern const unsigned int oftree_dtb_len;
  447. #endif
  448. #ifdef CONFIG_PPC
  449. static void
  450. do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
  451. int argc, char *argv[],
  452. ulong addr,
  453. ulong *len_ptr,
  454. int verify)
  455. {
  456. DECLARE_GLOBAL_DATA_PTR;
  457. ulong sp;
  458. ulong len, checksum;
  459. ulong initrd_start, initrd_end;
  460. ulong cmd_start, cmd_end;
  461. ulong initrd_high;
  462. ulong data;
  463. int initrd_copy_to_ram = 1;
  464. char *cmdline;
  465. char *s;
  466. bd_t *kbd;
  467. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  468. image_header_t *hdr = &header;
  469. #ifdef CONFIG_OF_FLAT_TREE
  470. char *of_flat_tree;
  471. #endif
  472. if ((s = getenv ("initrd_high")) != NULL) {
  473. /* a value of "no" or a similar string will act like 0,
  474. * turning the "load high" feature off. This is intentional.
  475. */
  476. initrd_high = simple_strtoul(s, NULL, 16);
  477. if (initrd_high == ~0)
  478. initrd_copy_to_ram = 0;
  479. } else { /* not set, no restrictions to load high */
  480. initrd_high = ~0;
  481. }
  482. #ifdef CONFIG_LOGBUFFER
  483. kbd=gd->bd;
  484. /* Prevent initrd from overwriting logbuffer */
  485. if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
  486. initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
  487. debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
  488. #endif
  489. /*
  490. * Booting a (Linux) kernel image
  491. *
  492. * Allocate space for command line and board info - the
  493. * address should be as high as possible within the reach of
  494. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  495. * memory, which means far enough below the current stack
  496. * pointer.
  497. */
  498. asm( "mr %0,1": "=r"(sp) : );
  499. debug ("## Current stack ends at 0x%08lX ", sp);
  500. sp -= 2048; /* just to be sure */
  501. if (sp > CFG_BOOTMAPSZ)
  502. sp = CFG_BOOTMAPSZ;
  503. sp &= ~0xF;
  504. debug ("=> set upper limit to 0x%08lX\n", sp);
  505. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  506. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  507. if ((s = getenv("bootargs")) == NULL)
  508. s = "";
  509. strcpy (cmdline, s);
  510. cmd_start = (ulong)&cmdline[0];
  511. cmd_end = cmd_start + strlen(cmdline);
  512. *kbd = *(gd->bd);
  513. #ifdef DEBUG
  514. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  515. do_bdinfo (NULL, 0, 0, NULL);
  516. #endif
  517. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  518. /* convert all clock information to MHz */
  519. kbd->bi_intfreq /= 1000000L;
  520. kbd->bi_busfreq /= 1000000L;
  521. #if defined(CONFIG_MPC8220)
  522. kbd->bi_inpfreq /= 1000000L;
  523. kbd->bi_pcifreq /= 1000000L;
  524. kbd->bi_pevfreq /= 1000000L;
  525. kbd->bi_flbfreq /= 1000000L;
  526. kbd->bi_vcofreq /= 1000000L;
  527. #endif
  528. #if defined(CONFIG_CPM2)
  529. kbd->bi_cpmfreq /= 1000000L;
  530. kbd->bi_brgfreq /= 1000000L;
  531. kbd->bi_sccfreq /= 1000000L;
  532. kbd->bi_vco /= 1000000L;
  533. #endif
  534. #if defined(CONFIG_MPC5xxx)
  535. kbd->bi_ipbfreq /= 1000000L;
  536. kbd->bi_pcifreq /= 1000000L;
  537. #endif /* CONFIG_MPC5xxx */
  538. }
  539. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
  540. /*
  541. * Check if there is an initrd image
  542. */
  543. if (argc >= 3) {
  544. SHOW_BOOT_PROGRESS (9);
  545. addr = simple_strtoul(argv[2], NULL, 16);
  546. printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
  547. /* Copy header so we can blank CRC field for re-calculation */
  548. memmove (&header, (char *)addr, sizeof(image_header_t));
  549. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  550. puts ("Bad Magic Number\n");
  551. SHOW_BOOT_PROGRESS (-10);
  552. do_reset (cmdtp, flag, argc, argv);
  553. }
  554. data = (ulong)&header;
  555. len = sizeof(image_header_t);
  556. checksum = ntohl(hdr->ih_hcrc);
  557. hdr->ih_hcrc = 0;
  558. if (crc32 (0, (uchar *)data, len) != checksum) {
  559. puts ("Bad Header Checksum\n");
  560. SHOW_BOOT_PROGRESS (-11);
  561. do_reset (cmdtp, flag, argc, argv);
  562. }
  563. SHOW_BOOT_PROGRESS (10);
  564. print_image_hdr (hdr);
  565. data = addr + sizeof(image_header_t);
  566. len = ntohl(hdr->ih_size);
  567. if (verify) {
  568. ulong csum = 0;
  569. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  570. ulong cdata = data, edata = cdata + len;
  571. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  572. puts (" Verifying Checksum ... ");
  573. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  574. while (cdata < edata) {
  575. ulong chunk = edata - cdata;
  576. if (chunk > CHUNKSZ)
  577. chunk = CHUNKSZ;
  578. csum = crc32 (csum, (uchar *)cdata, chunk);
  579. cdata += chunk;
  580. WATCHDOG_RESET();
  581. }
  582. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  583. csum = crc32 (0, (uchar *)data, len);
  584. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  585. if (csum != ntohl(hdr->ih_dcrc)) {
  586. puts ("Bad Data CRC\n");
  587. SHOW_BOOT_PROGRESS (-12);
  588. do_reset (cmdtp, flag, argc, argv);
  589. }
  590. puts ("OK\n");
  591. }
  592. SHOW_BOOT_PROGRESS (11);
  593. if ((hdr->ih_os != IH_OS_LINUX) ||
  594. (hdr->ih_arch != IH_CPU_PPC) ||
  595. (hdr->ih_type != IH_TYPE_RAMDISK) ) {
  596. puts ("No Linux PPC Ramdisk Image\n");
  597. SHOW_BOOT_PROGRESS (-13);
  598. do_reset (cmdtp, flag, argc, argv);
  599. }
  600. /*
  601. * Now check if we have a multifile image
  602. */
  603. } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
  604. u_long tail = ntohl(len_ptr[0]) % 4;
  605. int i;
  606. SHOW_BOOT_PROGRESS (13);
  607. /* skip kernel length and terminator */
  608. data = (ulong)(&len_ptr[2]);
  609. /* skip any additional image length fields */
  610. for (i=1; len_ptr[i]; ++i)
  611. data += 4;
  612. /* add kernel length, and align */
  613. data += ntohl(len_ptr[0]);
  614. if (tail) {
  615. data += 4 - tail;
  616. }
  617. len = ntohl(len_ptr[1]);
  618. } else {
  619. /*
  620. * no initrd image
  621. */
  622. SHOW_BOOT_PROGRESS (14);
  623. len = data = 0;
  624. }
  625. if (!data) {
  626. debug ("No initrd\n");
  627. }
  628. if (data) {
  629. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  630. initrd_start = data;
  631. initrd_end = initrd_start + len;
  632. } else {
  633. initrd_start = (ulong)kbd - len;
  634. initrd_start &= ~(4096 - 1); /* align on page */
  635. if (initrd_high) {
  636. ulong nsp;
  637. /*
  638. * the inital ramdisk does not need to be within
  639. * CFG_BOOTMAPSZ as it is not accessed until after
  640. * the mm system is initialised.
  641. *
  642. * do the stack bottom calculation again and see if
  643. * the initrd will fit just below the monitor stack
  644. * bottom without overwriting the area allocated
  645. * above for command line args and board info.
  646. */
  647. asm( "mr %0,1": "=r"(nsp) : );
  648. nsp -= 2048; /* just to be sure */
  649. nsp &= ~0xF;
  650. if (nsp > initrd_high) /* limit as specified */
  651. nsp = initrd_high;
  652. nsp -= len;
  653. nsp &= ~(4096 - 1); /* align on page */
  654. if (nsp >= sp)
  655. initrd_start = nsp;
  656. }
  657. SHOW_BOOT_PROGRESS (12);
  658. debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  659. data, data + len - 1, len, len);
  660. initrd_end = initrd_start + len;
  661. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  662. initrd_start, initrd_end);
  663. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  664. {
  665. size_t l = len;
  666. void *to = (void *)initrd_start;
  667. void *from = (void *)data;
  668. while (l > 0) {
  669. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  670. WATCHDOG_RESET();
  671. memmove (to, from, tail);
  672. to += tail;
  673. from += tail;
  674. l -= tail;
  675. }
  676. }
  677. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  678. memmove ((void *)initrd_start, (void *)data, len);
  679. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  680. puts ("OK\n");
  681. }
  682. } else {
  683. initrd_start = 0;
  684. initrd_end = 0;
  685. }
  686. #ifdef CONFIG_OF_FLAT_TREE
  687. if (initrd_start == 0)
  688. of_flat_tree = (char *)(((ulong)kbd - OF_FLAT_TREE_MAX_SIZE -
  689. sizeof(bd_t)) & ~0xF);
  690. else
  691. of_flat_tree = (char *)((initrd_start - OF_FLAT_TREE_MAX_SIZE -
  692. sizeof(bd_t)) & ~0xF);
  693. #endif
  694. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  695. (ulong)kernel);
  696. SHOW_BOOT_PROGRESS (15);
  697. #ifndef CONFIG_OF_FLAT_TREE
  698. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  699. unlock_ram_in_cache();
  700. #endif
  701. /*
  702. * Linux Kernel Parameters:
  703. * r3: ptr to board info data
  704. * r4: initrd_start or 0 if no initrd
  705. * r5: initrd_end - unused if r4 is 0
  706. * r6: Start of command line string
  707. * r7: End of command line string
  708. */
  709. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  710. #else
  711. ft_setup(of_flat_tree, OF_FLAT_TREE_MAX_SIZE, kbd, initrd_start, initrd_end);
  712. /* ft_dump_blob(of_flat_tree); */
  713. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  714. unlock_ram_in_cache();
  715. #endif
  716. /*
  717. * Linux Kernel Parameters:
  718. * r3: ptr to OF flat tree, followed by the board info data
  719. * r4: physical pointer to the kernel itself
  720. * r5: NULL
  721. * r6: NULL
  722. * r7: NULL
  723. */
  724. if (getenv("disable_of") != NULL)
  725. (*kernel) ((bd_t *)of_flat_tree, initrd_start, initrd_end,
  726. cmd_start, cmd_end);
  727. else
  728. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  729. #endif
  730. }
  731. #endif /* CONFIG_PPC */
  732. static void
  733. do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  734. int argc, char *argv[],
  735. ulong addr,
  736. ulong *len_ptr,
  737. int verify)
  738. {
  739. DECLARE_GLOBAL_DATA_PTR;
  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. DECLARE_GLOBAL_DATA_PTR;
  808. ulong top;
  809. char *s, *cmdline;
  810. char **fwenv, **ss;
  811. int i, j, nxt, len, envno, envsz;
  812. bd_t *kbd;
  813. void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
  814. image_header_t *hdr = &header;
  815. /*
  816. * Booting an ARTOS kernel image + application
  817. */
  818. /* this used to be the top of memory, but was wrong... */
  819. #ifdef CONFIG_PPC
  820. /* get stack pointer */
  821. asm volatile ("mr %0,1" : "=r"(top) );
  822. #endif
  823. debug ("## Current stack ends at 0x%08lX ", top);
  824. top -= 2048; /* just to be sure */
  825. if (top > CFG_BOOTMAPSZ)
  826. top = CFG_BOOTMAPSZ;
  827. top &= ~0xF;
  828. debug ("=> set upper limit to 0x%08lX\n", top);
  829. /* first check the artos specific boot args, then the linux args*/
  830. if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
  831. s = "";
  832. /* get length of cmdline, and place it */
  833. len = strlen(s);
  834. top = (top - (len + 1)) & ~0xF;
  835. cmdline = (char *)top;
  836. debug ("## cmdline at 0x%08lX ", top);
  837. strcpy(cmdline, s);
  838. /* copy bdinfo */
  839. top = (top - sizeof(bd_t)) & ~0xF;
  840. debug ("## bd at 0x%08lX ", top);
  841. kbd = (bd_t *)top;
  842. memcpy(kbd, gd->bd, sizeof(bd_t));
  843. /* first find number of env entries, and their size */
  844. envno = 0;
  845. envsz = 0;
  846. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  847. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  848. ;
  849. envno++;
  850. envsz += (nxt - i) + 1; /* plus trailing zero */
  851. }
  852. envno++; /* plus the terminating zero */
  853. debug ("## %u envvars total size %u ", envno, envsz);
  854. top = (top - sizeof(char **)*envno) & ~0xF;
  855. fwenv = (char **)top;
  856. debug ("## fwenv at 0x%08lX ", top);
  857. top = (top - envsz) & ~0xF;
  858. s = (char *)top;
  859. ss = fwenv;
  860. /* now copy them */
  861. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  862. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  863. ;
  864. *ss++ = s;
  865. for (j = i; j < nxt; ++j)
  866. *s++ = env_get_char(j);
  867. *s++ = '\0';
  868. }
  869. *ss++ = NULL; /* terminate */
  870. entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
  871. (*entry)(kbd, cmdline, fwenv, top);
  872. }
  873. #endif
  874. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  875. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  876. {
  877. int rcode = 0;
  878. #ifndef CFG_HUSH_PARSER
  879. if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
  880. #else
  881. if (parse_string_outer(getenv("bootcmd"),
  882. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
  883. #endif
  884. return rcode;
  885. }
  886. U_BOOT_CMD(
  887. boot, 1, 1, do_bootd,
  888. "boot - boot default, i.e., run 'bootcmd'\n",
  889. NULL
  890. );
  891. /* keep old command name "bootd" for backward compatibility */
  892. U_BOOT_CMD(
  893. bootd, 1, 1, do_bootd,
  894. "bootd - boot default, i.e., run 'bootcmd'\n",
  895. NULL
  896. );
  897. #endif
  898. #if (CONFIG_COMMANDS & CFG_CMD_IMI)
  899. int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  900. {
  901. int arg;
  902. ulong addr;
  903. int rcode=0;
  904. if (argc < 2) {
  905. return image_info (load_addr);
  906. }
  907. for (arg=1; arg <argc; ++arg) {
  908. addr = simple_strtoul(argv[arg], NULL, 16);
  909. if (image_info (addr) != 0) rcode = 1;
  910. }
  911. return rcode;
  912. }
  913. static int image_info (ulong addr)
  914. {
  915. ulong data, len, checksum;
  916. image_header_t *hdr = &header;
  917. printf ("\n## Checking Image at %08lx ...\n", addr);
  918. /* Copy header so we can blank CRC field for re-calculation */
  919. memmove (&header, (char *)addr, sizeof(image_header_t));
  920. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  921. puts (" Bad Magic Number\n");
  922. return 1;
  923. }
  924. data = (ulong)&header;
  925. len = sizeof(image_header_t);
  926. checksum = ntohl(hdr->ih_hcrc);
  927. hdr->ih_hcrc = 0;
  928. if (crc32 (0, (uchar *)data, len) != checksum) {
  929. puts (" Bad Header Checksum\n");
  930. return 1;
  931. }
  932. /* for multi-file images we need the data part, too */
  933. print_image_hdr ((image_header_t *)addr);
  934. data = addr + sizeof(image_header_t);
  935. len = ntohl(hdr->ih_size);
  936. puts (" Verifying Checksum ... ");
  937. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  938. puts (" Bad Data CRC\n");
  939. return 1;
  940. }
  941. puts ("OK\n");
  942. return 0;
  943. }
  944. U_BOOT_CMD(
  945. iminfo, CFG_MAXARGS, 1, do_iminfo,
  946. "iminfo - print header information for application image\n",
  947. "addr [addr ...]\n"
  948. " - print header information for application image starting at\n"
  949. " address 'addr' in memory; this includes verification of the\n"
  950. " image contents (magic number, header and payload checksums)\n"
  951. );
  952. #endif /* CFG_CMD_IMI */
  953. #if (CONFIG_COMMANDS & CFG_CMD_IMLS)
  954. /*-----------------------------------------------------------------------
  955. * List all images found in flash.
  956. */
  957. int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  958. {
  959. flash_info_t *info;
  960. int i, j;
  961. image_header_t *hdr;
  962. ulong data, len, checksum;
  963. for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
  964. if (info->flash_id == FLASH_UNKNOWN)
  965. goto next_bank;
  966. for (j=0; j<info->sector_count; ++j) {
  967. if (!(hdr=(image_header_t *)info->start[j]) ||
  968. (ntohl(hdr->ih_magic) != IH_MAGIC))
  969. goto next_sector;
  970. /* Copy header so we can blank CRC field for re-calculation */
  971. memmove (&header, (char *)hdr, sizeof(image_header_t));
  972. checksum = ntohl(header.ih_hcrc);
  973. header.ih_hcrc = 0;
  974. if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
  975. != checksum)
  976. goto next_sector;
  977. printf ("Image at %08lX:\n", (ulong)hdr);
  978. print_image_hdr( hdr );
  979. data = (ulong)hdr + sizeof(image_header_t);
  980. len = ntohl(hdr->ih_size);
  981. puts (" Verifying Checksum ... ");
  982. if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
  983. puts (" Bad Data CRC\n");
  984. }
  985. puts ("OK\n");
  986. next_sector: ;
  987. }
  988. next_bank: ;
  989. }
  990. return (0);
  991. }
  992. U_BOOT_CMD(
  993. imls, 1, 1, do_imls,
  994. "imls - list all images found in flash\n",
  995. "\n"
  996. " - Prints information about all images found at sector\n"
  997. " boundaries in flash.\n"
  998. );
  999. #endif /* CFG_CMD_IMLS */
  1000. void
  1001. print_image_hdr (image_header_t *hdr)
  1002. {
  1003. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  1004. time_t timestamp = (time_t)ntohl(hdr->ih_time);
  1005. struct rtc_time tm;
  1006. #endif
  1007. printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
  1008. #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  1009. to_tm (timestamp, &tm);
  1010. printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  1011. tm.tm_year, tm.tm_mon, tm.tm_mday,
  1012. tm.tm_hour, tm.tm_min, tm.tm_sec);
  1013. #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
  1014. puts (" Image Type: "); print_type(hdr);
  1015. printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
  1016. print_size (ntohl(hdr->ih_size), "\n");
  1017. printf (" Load Address: %08x\n"
  1018. " Entry Point: %08x\n",
  1019. ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
  1020. if (hdr->ih_type == IH_TYPE_MULTI) {
  1021. int i;
  1022. ulong len;
  1023. ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
  1024. puts (" Contents:\n");
  1025. for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
  1026. printf (" Image %d: %8ld Bytes = ", i, len);
  1027. print_size (len, "\n");
  1028. }
  1029. }
  1030. }
  1031. static void
  1032. print_type (image_header_t *hdr)
  1033. {
  1034. char *os, *arch, *type, *comp;
  1035. switch (hdr->ih_os) {
  1036. case IH_OS_INVALID: os = "Invalid OS"; break;
  1037. case IH_OS_NETBSD: os = "NetBSD"; break;
  1038. case IH_OS_LINUX: os = "Linux"; break;
  1039. case IH_OS_VXWORKS: os = "VxWorks"; break;
  1040. case IH_OS_QNX: os = "QNX"; break;
  1041. case IH_OS_U_BOOT: os = "U-Boot"; break;
  1042. case IH_OS_RTEMS: os = "RTEMS"; break;
  1043. #ifdef CONFIG_ARTOS
  1044. case IH_OS_ARTOS: os = "ARTOS"; break;
  1045. #endif
  1046. #ifdef CONFIG_LYNXKDI
  1047. case IH_OS_LYNXOS: os = "LynxOS"; break;
  1048. #endif
  1049. default: os = "Unknown OS"; break;
  1050. }
  1051. switch (hdr->ih_arch) {
  1052. case IH_CPU_INVALID: arch = "Invalid CPU"; break;
  1053. case IH_CPU_ALPHA: arch = "Alpha"; break;
  1054. case IH_CPU_ARM: arch = "ARM"; 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. DECLARE_GLOBAL_DATA_PTR;
  1168. image_header_t *hdr = &header;
  1169. void (*entry_point)(bd_t *);
  1170. entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
  1171. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  1172. (ulong)entry_point);
  1173. SHOW_BOOT_PROGRESS (15);
  1174. /*
  1175. * RTEMS Parameters:
  1176. * r3: ptr to board info data
  1177. */
  1178. (*entry_point ) ( gd->bd );
  1179. }
  1180. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  1181. static void
  1182. do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1183. ulong addr, ulong *len_ptr, int verify)
  1184. {
  1185. image_header_t *hdr = &header;
  1186. char str[80];
  1187. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1188. setenv("loadaddr", str);
  1189. do_bootvx(cmdtp, 0, 0, NULL);
  1190. }
  1191. static void
  1192. do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  1193. ulong addr, ulong *len_ptr, int verify)
  1194. {
  1195. image_header_t *hdr = &header;
  1196. char *local_args[2];
  1197. char str[16];
  1198. sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
  1199. local_args[0] = argv[0];
  1200. local_args[1] = str; /* and provide it via the arguments */
  1201. do_bootelf(cmdtp, 0, 2, local_args);
  1202. }
  1203. #endif /* CFG_CMD_ELF */
  1204. #ifdef CONFIG_LYNXKDI
  1205. static void
  1206. do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
  1207. int argc, char *argv[],
  1208. ulong addr,
  1209. ulong *len_ptr,
  1210. int verify)
  1211. {
  1212. lynxkdi_boot( &header );
  1213. }
  1214. #endif /* CONFIG_LYNXKDI */