cmd_bootm.c 37 KB

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