cmd_bootm.c 40 KB

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