cmd_bootm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 <lmb.h>
  35. #include <asm/byteorder.h>
  36. #if defined(CONFIG_CMD_USB)
  37. #include <usb.h>
  38. #endif
  39. #ifdef CFG_HUSH_PARSER
  40. #include <hush.h>
  41. #endif
  42. DECLARE_GLOBAL_DATA_PTR;
  43. extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
  44. #ifndef CFG_BOOTM_LEN
  45. #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
  46. #endif
  47. #ifdef CONFIG_BZIP2
  48. extern void bz_internal_error(int);
  49. #endif
  50. #if defined(CONFIG_CMD_IMI)
  51. static int image_info (unsigned long addr);
  52. #endif
  53. #if defined(CONFIG_CMD_IMLS)
  54. #include <flash.h>
  55. extern flash_info_t flash_info[]; /* info for FLASH chips */
  56. static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  57. #endif
  58. #ifdef CONFIG_SILENT_CONSOLE
  59. static void fixup_silent_linux (void);
  60. #endif
  61. static image_header_t *image_get_kernel (ulong img_addr, int verify);
  62. #if defined(CONFIG_FIT)
  63. static int fit_check_kernel (const void *fit, int os_noffset, int verify);
  64. #endif
  65. static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
  66. bootm_headers_t *images, ulong *os_data, ulong *os_len);
  67. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  68. /*
  69. * Continue booting an OS image; caller already has:
  70. * - copied image header to global variable `header'
  71. * - checked header magic number, checksums (both header & image),
  72. * - verified image architecture (PPC) and type (KERNEL or MULTI),
  73. * - loaded (first part of) image to header load address,
  74. * - disabled interrupts.
  75. */
  76. typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
  77. int argc, char *argv[],
  78. bootm_headers_t *images); /* pointers to os/initrd/fdt */
  79. extern boot_os_fn do_bootm_linux;
  80. static boot_os_fn do_bootm_netbsd;
  81. #if defined(CONFIG_LYNXKDI)
  82. static boot_os_fn do_bootm_lynxkdi;
  83. extern void lynxkdi_boot (image_header_t *);
  84. #endif
  85. static boot_os_fn do_bootm_rtems;
  86. #if defined(CONFIG_CMD_ELF)
  87. static boot_os_fn do_bootm_vxworks;
  88. static boot_os_fn do_bootm_qnxelf;
  89. int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  90. int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  91. #endif
  92. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  93. static boot_os_fn do_bootm_artos;
  94. #endif
  95. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
  96. static bootm_headers_t images; /* pointers to os/initrd/fdt images */
  97. void __board_lmb_reserve(struct lmb *lmb)
  98. {
  99. /* please define platform specific board_lmb_reserve() */
  100. }
  101. void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
  102. /*******************************************************************/
  103. /* bootm - boot application image from image in memory */
  104. /*******************************************************************/
  105. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  106. {
  107. ulong iflag;
  108. const char *type_name;
  109. uint unc_len = CFG_BOOTM_LEN;
  110. uint8_t comp, type, os;
  111. void *os_hdr;
  112. ulong os_data, os_len;
  113. ulong image_start, image_end;
  114. ulong load_start, load_end;
  115. ulong mem_start;
  116. phys_size_t mem_size;
  117. struct lmb lmb;
  118. memset ((void *)&images, 0, sizeof (images));
  119. images.verify = getenv_yesno ("verify");
  120. images.lmb = &lmb;
  121. lmb_init(&lmb);
  122. mem_start = getenv_bootm_low();
  123. mem_size = getenv_bootm_size();
  124. lmb_add(&lmb, (phys_addr_t)mem_start, mem_size);
  125. board_lmb_reserve(&lmb);
  126. /* get kernel image header, start address and length */
  127. os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
  128. &images, &os_data, &os_len);
  129. if (os_len == 0) {
  130. puts ("ERROR: can't get kernel image!\n");
  131. return 1;
  132. }
  133. /* get image parameters */
  134. switch (genimg_get_format (os_hdr)) {
  135. case IMAGE_FORMAT_LEGACY:
  136. type = image_get_type (os_hdr);
  137. comp = image_get_comp (os_hdr);
  138. os = image_get_os (os_hdr);
  139. image_end = image_get_image_end (os_hdr);
  140. load_start = image_get_load (os_hdr);
  141. break;
  142. #if defined(CONFIG_FIT)
  143. case IMAGE_FORMAT_FIT:
  144. if (fit_image_get_type (images.fit_hdr_os,
  145. images.fit_noffset_os, &type)) {
  146. puts ("Can't get image type!\n");
  147. show_boot_progress (-109);
  148. return 1;
  149. }
  150. if (fit_image_get_comp (images.fit_hdr_os,
  151. images.fit_noffset_os, &comp)) {
  152. puts ("Can't get image compression!\n");
  153. show_boot_progress (-110);
  154. return 1;
  155. }
  156. if (fit_image_get_os (images.fit_hdr_os,
  157. images.fit_noffset_os, &os)) {
  158. puts ("Can't get image OS!\n");
  159. show_boot_progress (-111);
  160. return 1;
  161. }
  162. image_end = fit_get_end (images.fit_hdr_os);
  163. if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
  164. &load_start)) {
  165. puts ("Can't get image load address!\n");
  166. show_boot_progress (-112);
  167. return 1;
  168. }
  169. break;
  170. #endif
  171. default:
  172. puts ("ERROR: unknown image format type!\n");
  173. return 1;
  174. }
  175. image_start = (ulong)os_hdr;
  176. load_end = 0;
  177. type_name = genimg_get_type_name (type);
  178. /*
  179. * We have reached the point of no return: we are going to
  180. * overwrite all exception vector code, so we cannot easily
  181. * recover from any failures any more...
  182. */
  183. iflag = disable_interrupts();
  184. #if defined(CONFIG_CMD_USB)
  185. /*
  186. * turn off USB to prevent the host controller from writing to the
  187. * SDRAM while Linux is booting. This could happen (at least for OHCI
  188. * controller), because the HCCA (Host Controller Communication Area)
  189. * lies within the SDRAM and the host controller writes continously to
  190. * this area (as busmaster!). The HccaFrameNumber is for example
  191. * updated every 1 ms within the HCCA structure in SDRAM! For more
  192. * details see the OpenHCI specification.
  193. */
  194. usb_stop();
  195. #endif
  196. #ifdef CONFIG_AMIGAONEG3SE
  197. /*
  198. * We've possible left the caches enabled during
  199. * bios emulation, so turn them off again
  200. */
  201. icache_disable();
  202. invalidate_l1_instruction_cache();
  203. flush_data_cache();
  204. dcache_disable();
  205. #endif
  206. switch (comp) {
  207. case IH_COMP_NONE:
  208. if (load_start == (ulong)os_hdr) {
  209. printf (" XIP %s ... ", type_name);
  210. } else {
  211. printf (" Loading %s ... ", type_name);
  212. memmove_wd ((void *)load_start,
  213. (void *)os_data, os_len, CHUNKSZ);
  214. }
  215. load_end = load_start + os_len;
  216. puts("OK\n");
  217. break;
  218. case IH_COMP_GZIP:
  219. printf (" Uncompressing %s ... ", type_name);
  220. if (gunzip ((void *)load_start, unc_len,
  221. (uchar *)os_data, &os_len) != 0) {
  222. puts ("GUNZIP: uncompress or overwrite error "
  223. "- must RESET board to recover\n");
  224. show_boot_progress (-6);
  225. do_reset (cmdtp, flag, argc, argv);
  226. }
  227. load_end = load_start + os_len;
  228. break;
  229. #ifdef CONFIG_BZIP2
  230. case IH_COMP_BZIP2:
  231. printf (" Uncompressing %s ... ", type_name);
  232. /*
  233. * If we've got less than 4 MB of malloc() space,
  234. * use slower decompression algorithm which requires
  235. * at most 2300 KB of memory.
  236. */
  237. int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
  238. &unc_len, (char *)os_data, os_len,
  239. CFG_MALLOC_LEN < (4096 * 1024), 0);
  240. if (i != BZ_OK) {
  241. printf ("BUNZIP2: uncompress or overwrite error %d "
  242. "- must RESET board to recover\n", i);
  243. show_boot_progress (-6);
  244. do_reset (cmdtp, flag, argc, argv);
  245. }
  246. load_end = load_start + unc_len;
  247. break;
  248. #endif /* CONFIG_BZIP2 */
  249. default:
  250. if (iflag)
  251. enable_interrupts();
  252. printf ("Unimplemented compression type %d\n", comp);
  253. show_boot_progress (-7);
  254. return 1;
  255. }
  256. puts ("OK\n");
  257. debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
  258. show_boot_progress (7);
  259. if ((load_start < image_end) && (load_end > image_start)) {
  260. debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
  261. debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
  262. if (images.legacy_hdr_valid) {
  263. if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
  264. puts ("WARNING: legacy format multi component "
  265. "image overwritten\n");
  266. } else {
  267. puts ("ERROR: new format image overwritten - "
  268. "must RESET the board to recover\n");
  269. show_boot_progress (-113);
  270. do_reset (cmdtp, flag, argc, argv);
  271. }
  272. }
  273. show_boot_progress (8);
  274. lmb_reserve(&lmb, load_start, (load_end - load_start));
  275. switch (os) {
  276. default: /* handled by (original) Linux case */
  277. case IH_OS_LINUX:
  278. #ifdef CONFIG_SILENT_CONSOLE
  279. fixup_silent_linux();
  280. #endif
  281. do_bootm_linux (cmdtp, flag, argc, argv, &images);
  282. break;
  283. case IH_OS_NETBSD:
  284. do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
  285. break;
  286. #ifdef CONFIG_LYNXKDI
  287. case IH_OS_LYNXOS:
  288. do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
  289. break;
  290. #endif
  291. case IH_OS_RTEMS:
  292. do_bootm_rtems (cmdtp, flag, argc, argv, &images);
  293. break;
  294. #if defined(CONFIG_CMD_ELF)
  295. case IH_OS_VXWORKS:
  296. do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
  297. break;
  298. case IH_OS_QNX:
  299. do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
  300. break;
  301. #endif
  302. #ifdef CONFIG_ARTOS
  303. case IH_OS_ARTOS:
  304. do_bootm_artos (cmdtp, flag, argc, argv, &images);
  305. break;
  306. #endif
  307. }
  308. show_boot_progress (-9);
  309. #ifdef DEBUG
  310. puts ("\n## Control returned to monitor - resetting...\n");
  311. do_reset (cmdtp, flag, argc, argv);
  312. #endif
  313. if (iflag)
  314. enable_interrupts();
  315. return 1;
  316. }
  317. /**
  318. * image_get_kernel - verify legacy format kernel image
  319. * @img_addr: in RAM address of the legacy format image to be verified
  320. * @verify: data CRC verification flag
  321. *
  322. * image_get_kernel() verifies legacy image integrity and returns pointer to
  323. * legacy image header if image verification was completed successfully.
  324. *
  325. * returns:
  326. * pointer to a legacy image header if valid image was found
  327. * otherwise return NULL
  328. */
  329. static image_header_t *image_get_kernel (ulong img_addr, int verify)
  330. {
  331. image_header_t *hdr = (image_header_t *)img_addr;
  332. if (!image_check_magic(hdr)) {
  333. puts ("Bad Magic Number\n");
  334. show_boot_progress (-1);
  335. return NULL;
  336. }
  337. show_boot_progress (2);
  338. if (!image_check_hcrc (hdr)) {
  339. puts ("Bad Header Checksum\n");
  340. show_boot_progress (-2);
  341. return NULL;
  342. }
  343. show_boot_progress (3);
  344. image_print_contents (hdr);
  345. if (verify) {
  346. puts (" Verifying Checksum ... ");
  347. if (!image_check_dcrc (hdr)) {
  348. printf ("Bad Data CRC\n");
  349. show_boot_progress (-3);
  350. return NULL;
  351. }
  352. puts ("OK\n");
  353. }
  354. show_boot_progress (4);
  355. if (!image_check_target_arch (hdr)) {
  356. printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
  357. show_boot_progress (-4);
  358. return NULL;
  359. }
  360. return hdr;
  361. }
  362. /**
  363. * fit_check_kernel - verify FIT format kernel subimage
  364. * @fit_hdr: pointer to the FIT image header
  365. * os_noffset: kernel subimage node offset within FIT image
  366. * @verify: data CRC verification flag
  367. *
  368. * fit_check_kernel() verifies integrity of the kernel subimage and from
  369. * specified FIT image.
  370. *
  371. * returns:
  372. * 1, on success
  373. * 0, on failure
  374. */
  375. #if defined (CONFIG_FIT)
  376. static int fit_check_kernel (const void *fit, int os_noffset, int verify)
  377. {
  378. fit_image_print (fit, os_noffset, " ");
  379. if (verify) {
  380. puts (" Verifying Hash Integrity ... ");
  381. if (!fit_image_check_hashes (fit, os_noffset)) {
  382. puts ("Bad Data Hash\n");
  383. show_boot_progress (-104);
  384. return 0;
  385. }
  386. puts ("OK\n");
  387. }
  388. show_boot_progress (105);
  389. if (!fit_image_check_target_arch (fit, os_noffset)) {
  390. puts ("Unsupported Architecture\n");
  391. show_boot_progress (-105);
  392. return 0;
  393. }
  394. show_boot_progress (106);
  395. if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
  396. puts ("Not a kernel image\n");
  397. show_boot_progress (-106);
  398. return 0;
  399. }
  400. show_boot_progress (107);
  401. return 1;
  402. }
  403. #endif /* CONFIG_FIT */
  404. /**
  405. * boot_get_kernel - find kernel image
  406. * @os_data: pointer to a ulong variable, will hold os data start address
  407. * @os_len: pointer to a ulong variable, will hold os data length
  408. *
  409. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  410. * and locates kernel data.
  411. *
  412. * returns:
  413. * pointer to image header if valid image was found, plus kernel start
  414. * address and length, otherwise NULL
  415. */
  416. static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  417. bootm_headers_t *images, ulong *os_data, ulong *os_len)
  418. {
  419. image_header_t *hdr;
  420. ulong img_addr;
  421. #if defined(CONFIG_FIT)
  422. void *fit_hdr;
  423. const char *fit_uname_config = NULL;
  424. const char *fit_uname_kernel = NULL;
  425. const void *data;
  426. size_t len;
  427. int cfg_noffset;
  428. int os_noffset;
  429. #endif
  430. /* find out kernel image address */
  431. if (argc < 2) {
  432. img_addr = load_addr;
  433. debug ("* kernel: default image load address = 0x%08lx\n",
  434. load_addr);
  435. #if defined(CONFIG_FIT)
  436. } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
  437. &fit_uname_config)) {
  438. debug ("* kernel: config '%s' from image at 0x%08lx\n",
  439. fit_uname_config, img_addr);
  440. } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
  441. &fit_uname_kernel)) {
  442. debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
  443. fit_uname_kernel, img_addr);
  444. #endif
  445. } else {
  446. img_addr = simple_strtoul(argv[1], NULL, 16);
  447. debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
  448. }
  449. show_boot_progress (1);
  450. /* copy from dataflash if needed */
  451. img_addr = genimg_get_image (img_addr);
  452. /* check image type, for FIT images get FIT kernel node */
  453. *os_data = *os_len = 0;
  454. switch (genimg_get_format ((void *)img_addr)) {
  455. case IMAGE_FORMAT_LEGACY:
  456. printf ("## Booting kernel from Legacy Image at %08lx ...\n",
  457. img_addr);
  458. hdr = image_get_kernel (img_addr, images->verify);
  459. if (!hdr)
  460. return NULL;
  461. show_boot_progress (5);
  462. /* get os_data and os_len */
  463. switch (image_get_type (hdr)) {
  464. case IH_TYPE_KERNEL:
  465. *os_data = image_get_data (hdr);
  466. *os_len = image_get_data_size (hdr);
  467. break;
  468. case IH_TYPE_MULTI:
  469. image_multi_getimg (hdr, 0, os_data, os_len);
  470. break;
  471. default:
  472. printf ("Wrong Image Type for %s command\n", cmdtp->name);
  473. show_boot_progress (-5);
  474. return NULL;
  475. }
  476. /*
  477. * copy image header to allow for image overwrites during kernel
  478. * decompression.
  479. */
  480. memmove (&images->legacy_hdr_os_copy, hdr, sizeof(image_header_t));
  481. /* save pointer to image header */
  482. images->legacy_hdr_os = hdr;
  483. images->legacy_hdr_valid = 1;
  484. show_boot_progress (6);
  485. break;
  486. #if defined(CONFIG_FIT)
  487. case IMAGE_FORMAT_FIT:
  488. fit_hdr = (void *)img_addr;
  489. printf ("## Booting kernel from FIT Image at %08lx ...\n",
  490. img_addr);
  491. if (!fit_check_format (fit_hdr)) {
  492. puts ("Bad FIT kernel image format!\n");
  493. show_boot_progress (-100);
  494. return NULL;
  495. }
  496. show_boot_progress (100);
  497. if (!fit_uname_kernel) {
  498. /*
  499. * no kernel image node unit name, try to get config
  500. * node first. If config unit node name is NULL
  501. * fit_conf_get_node() will try to find default config node
  502. */
  503. show_boot_progress (101);
  504. cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
  505. if (cfg_noffset < 0) {
  506. show_boot_progress (-101);
  507. return NULL;
  508. }
  509. /* save configuration uname provided in the first
  510. * bootm argument
  511. */
  512. images->fit_uname_cfg = fdt_get_name (fit_hdr, cfg_noffset, NULL);
  513. printf (" Using '%s' configuration\n", images->fit_uname_cfg);
  514. show_boot_progress (103);
  515. os_noffset = fit_conf_get_kernel_node (fit_hdr, cfg_noffset);
  516. fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
  517. } else {
  518. /* get kernel component image node offset */
  519. show_boot_progress (102);
  520. os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
  521. }
  522. if (os_noffset < 0) {
  523. show_boot_progress (-103);
  524. return NULL;
  525. }
  526. printf (" Trying '%s' kernel subimage\n", fit_uname_kernel);
  527. show_boot_progress (104);
  528. if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
  529. return NULL;
  530. /* get kernel image data address and length */
  531. if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
  532. puts ("Could not find kernel subimage data!\n");
  533. show_boot_progress (-107);
  534. return NULL;
  535. }
  536. show_boot_progress (108);
  537. *os_len = len;
  538. *os_data = (ulong)data;
  539. images->fit_hdr_os = fit_hdr;
  540. images->fit_uname_os = fit_uname_kernel;
  541. images->fit_noffset_os = os_noffset;
  542. break;
  543. #endif
  544. default:
  545. printf ("Wrong Image Format for %s command\n", cmdtp->name);
  546. show_boot_progress (-108);
  547. return NULL;
  548. }
  549. debug (" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  550. *os_data, *os_len, *os_len);
  551. return (void *)img_addr;
  552. }
  553. U_BOOT_CMD(
  554. bootm, CFG_MAXARGS, 1, do_bootm,
  555. "bootm - boot application image from memory\n",
  556. "[addr [arg ...]]\n - boot application image stored in memory\n"
  557. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  558. "\t'arg' can be the address of an initrd image\n"
  559. #if defined(CONFIG_OF_LIBFDT)
  560. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  561. "\ta third argument is required which is the address of the\n"
  562. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  563. "\tuse a '-' for the second argument. If you do not pass a third\n"
  564. "\ta bd_info struct will be passed instead\n"
  565. #endif
  566. #if defined(CONFIG_FIT)
  567. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  568. "\tmust be extened to include component or configuration unit name:\n"
  569. "\taddr:<subimg_uname> - direct component image specification\n"
  570. "\taddr#<conf_uname> - configuration specification\n"
  571. "\tUse iminfo command to get the list of existing component\n"
  572. "\timages and configurations.\n"
  573. #endif
  574. );
  575. /*******************************************************************/
  576. /* bootd - boot default image */
  577. /*******************************************************************/
  578. #if defined(CONFIG_CMD_BOOTD)
  579. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  580. {
  581. int rcode = 0;
  582. #ifndef CFG_HUSH_PARSER
  583. if (run_command (getenv ("bootcmd"), flag) < 0)
  584. rcode = 1;
  585. #else
  586. if (parse_string_outer (getenv ("bootcmd"),
  587. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  588. rcode = 1;
  589. #endif
  590. return rcode;
  591. }
  592. U_BOOT_CMD(
  593. boot, 1, 1, do_bootd,
  594. "boot - boot default, i.e., run 'bootcmd'\n",
  595. NULL
  596. );
  597. /* keep old command name "bootd" for backward compatibility */
  598. U_BOOT_CMD(
  599. bootd, 1, 1, do_bootd,
  600. "bootd - boot default, i.e., run 'bootcmd'\n",
  601. NULL
  602. );
  603. #endif
  604. /*******************************************************************/
  605. /* iminfo - print header info for a requested image */
  606. /*******************************************************************/
  607. #if defined(CONFIG_CMD_IMI)
  608. int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  609. {
  610. int arg;
  611. ulong addr;
  612. int rcode = 0;
  613. if (argc < 2) {
  614. return image_info (load_addr);
  615. }
  616. for (arg = 1; arg < argc; ++arg) {
  617. addr = simple_strtoul (argv[arg], NULL, 16);
  618. if (image_info (addr) != 0)
  619. rcode = 1;
  620. }
  621. return rcode;
  622. }
  623. static int image_info (ulong addr)
  624. {
  625. void *hdr = (void *)addr;
  626. printf ("\n## Checking Image at %08lx ...\n", addr);
  627. switch (genimg_get_format (hdr)) {
  628. case IMAGE_FORMAT_LEGACY:
  629. puts (" Legacy image found\n");
  630. if (!image_check_magic (hdr)) {
  631. puts (" Bad Magic Number\n");
  632. return 1;
  633. }
  634. if (!image_check_hcrc (hdr)) {
  635. puts (" Bad Header Checksum\n");
  636. return 1;
  637. }
  638. image_print_contents (hdr);
  639. puts (" Verifying Checksum ... ");
  640. if (!image_check_dcrc (hdr)) {
  641. puts (" Bad Data CRC\n");
  642. return 1;
  643. }
  644. puts ("OK\n");
  645. return 0;
  646. #if defined(CONFIG_FIT)
  647. case IMAGE_FORMAT_FIT:
  648. puts (" FIT image found\n");
  649. if (!fit_check_format (hdr)) {
  650. puts ("Bad FIT image format!\n");
  651. return 1;
  652. }
  653. fit_print_contents (hdr);
  654. return 0;
  655. #endif
  656. default:
  657. puts ("Unknown image format!\n");
  658. break;
  659. }
  660. return 1;
  661. }
  662. U_BOOT_CMD(
  663. iminfo, CFG_MAXARGS, 1, do_iminfo,
  664. "iminfo - print header information for application image\n",
  665. "addr [addr ...]\n"
  666. " - print header information for application image starting at\n"
  667. " address 'addr' in memory; this includes verification of the\n"
  668. " image contents (magic number, header and payload checksums)\n"
  669. );
  670. #endif
  671. /*******************************************************************/
  672. /* imls - list all images found in flash */
  673. /*******************************************************************/
  674. #if defined(CONFIG_CMD_IMLS)
  675. int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  676. {
  677. flash_info_t *info;
  678. int i, j;
  679. void *hdr;
  680. for (i = 0, info = &flash_info[0];
  681. i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
  682. if (info->flash_id == FLASH_UNKNOWN)
  683. goto next_bank;
  684. for (j = 0; j < info->sector_count; ++j) {
  685. hdr = (void *)info->start[j];
  686. if (!hdr)
  687. goto next_sector;
  688. switch (genimg_get_format (hdr)) {
  689. case IMAGE_FORMAT_LEGACY:
  690. if (!image_check_hcrc (hdr))
  691. goto next_sector;
  692. printf ("Legacy Image at %08lX:\n", (ulong)hdr);
  693. image_print_contents (hdr);
  694. puts (" Verifying Checksum ... ");
  695. if (!image_check_dcrc (hdr)) {
  696. puts ("Bad Data CRC\n");
  697. } else {
  698. puts ("OK\n");
  699. }
  700. break;
  701. #if defined(CONFIG_FIT)
  702. case IMAGE_FORMAT_FIT:
  703. if (!fit_check_format (hdr))
  704. goto next_sector;
  705. printf ("FIT Image at %08lX:\n", (ulong)hdr);
  706. fit_print_contents (hdr);
  707. break;
  708. #endif
  709. default:
  710. goto next_sector;
  711. }
  712. next_sector: ;
  713. }
  714. next_bank: ;
  715. }
  716. return (0);
  717. }
  718. U_BOOT_CMD(
  719. imls, 1, 1, do_imls,
  720. "imls - list all images found in flash\n",
  721. "\n"
  722. " - Prints information about all images found at sector\n"
  723. " boundaries in flash.\n"
  724. );
  725. #endif
  726. /*******************************************************************/
  727. /* helper routines */
  728. /*******************************************************************/
  729. #ifdef CONFIG_SILENT_CONSOLE
  730. static void fixup_silent_linux ()
  731. {
  732. char buf[256], *start, *end;
  733. char *cmdline = getenv ("bootargs");
  734. /* Only fix cmdline when requested */
  735. if (!(gd->flags & GD_FLG_SILENT))
  736. return;
  737. debug ("before silent fix-up: %s\n", cmdline);
  738. if (cmdline) {
  739. if ((start = strstr (cmdline, "console=")) != NULL) {
  740. end = strchr (start, ' ');
  741. strncpy (buf, cmdline, (start - cmdline + 8));
  742. if (end)
  743. strcpy (buf + (start - cmdline + 8), end);
  744. else
  745. buf[start - cmdline + 8] = '\0';
  746. } else {
  747. strcpy (buf, cmdline);
  748. strcat (buf, " console=");
  749. }
  750. } else {
  751. strcpy (buf, "console=");
  752. }
  753. setenv ("bootargs", buf);
  754. debug ("after silent fix-up: %s\n", buf);
  755. }
  756. #endif /* CONFIG_SILENT_CONSOLE */
  757. /*******************************************************************/
  758. /* OS booting routines */
  759. /*******************************************************************/
  760. static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
  761. int argc, char *argv[],
  762. bootm_headers_t *images)
  763. {
  764. void (*loader)(bd_t *, image_header_t *, char *, char *);
  765. image_header_t *os_hdr, *hdr;
  766. ulong kernel_data, kernel_len;
  767. char *consdev;
  768. char *cmdline;
  769. #if defined(CONFIG_FIT)
  770. if (!images->legacy_hdr_valid) {
  771. fit_unsupported_reset ("NetBSD");
  772. do_reset (cmdtp, flag, argc, argv);
  773. }
  774. #endif
  775. hdr = images->legacy_hdr_os;
  776. /*
  777. * Booting a (NetBSD) kernel image
  778. *
  779. * This process is pretty similar to a standalone application:
  780. * The (first part of an multi-) image must be a stage-2 loader,
  781. * which in turn is responsible for loading & invoking the actual
  782. * kernel. The only differences are the parameters being passed:
  783. * besides the board info strucure, the loader expects a command
  784. * line, the name of the console device, and (optionally) the
  785. * address of the original image header.
  786. */
  787. os_hdr = NULL;
  788. if (image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  789. image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
  790. if (kernel_len)
  791. os_hdr = hdr;
  792. }
  793. consdev = "";
  794. #if defined (CONFIG_8xx_CONS_SMC1)
  795. consdev = "smc1";
  796. #elif defined (CONFIG_8xx_CONS_SMC2)
  797. consdev = "smc2";
  798. #elif defined (CONFIG_8xx_CONS_SCC2)
  799. consdev = "scc2";
  800. #elif defined (CONFIG_8xx_CONS_SCC3)
  801. consdev = "scc3";
  802. #endif
  803. if (argc > 2) {
  804. ulong len;
  805. int i;
  806. for (i = 2, len = 0; i < argc; i += 1)
  807. len += strlen (argv[i]) + 1;
  808. cmdline = malloc (len);
  809. for (i = 2, len = 0; i < argc; i += 1) {
  810. if (i > 2)
  811. cmdline[len++] = ' ';
  812. strcpy (&cmdline[len], argv[i]);
  813. len += strlen (argv[i]);
  814. }
  815. } else if ((cmdline = getenv ("bootargs")) == NULL) {
  816. cmdline = "";
  817. }
  818. loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
  819. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  820. (ulong)loader);
  821. show_boot_progress (15);
  822. /*
  823. * NetBSD Stage-2 Loader Parameters:
  824. * r3: ptr to board info data
  825. * r4: image address
  826. * r5: console device
  827. * r6: boot args string
  828. */
  829. (*loader) (gd->bd, os_hdr, consdev, cmdline);
  830. }
  831. #ifdef CONFIG_LYNXKDI
  832. static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
  833. int argc, char *argv[],
  834. bootm_headers_t *images)
  835. {
  836. image_header_t *hdr = &images->legacy_hdr_os_copy;
  837. #if defined(CONFIG_FIT)
  838. if (!images->legacy_hdr_valid) {
  839. fit_unsupported_reset ("Lynx");
  840. do_reset (cmdtp, flag, argc, argv);
  841. }
  842. #endif
  843. lynxkdi_boot ((image_header_t *)hdr);
  844. }
  845. #endif /* CONFIG_LYNXKDI */
  846. static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
  847. int argc, char *argv[],
  848. bootm_headers_t *images)
  849. {
  850. image_header_t *hdr = &images->legacy_hdr_os_copy;
  851. void (*entry_point)(bd_t *);
  852. #if defined(CONFIG_FIT)
  853. if (!images->legacy_hdr_valid) {
  854. fit_unsupported_reset ("RTEMS");
  855. do_reset (cmdtp, flag, argc, argv);
  856. }
  857. #endif
  858. entry_point = (void (*)(bd_t *))image_get_ep (hdr);
  859. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  860. (ulong)entry_point);
  861. show_boot_progress (15);
  862. /*
  863. * RTEMS Parameters:
  864. * r3: ptr to board info data
  865. */
  866. (*entry_point)(gd->bd);
  867. }
  868. #if defined(CONFIG_CMD_ELF)
  869. static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
  870. int argc, char *argv[],
  871. bootm_headers_t *images)
  872. {
  873. char str[80];
  874. image_header_t *hdr = &images->legacy_hdr_os_copy;
  875. #if defined(CONFIG_FIT)
  876. if (!images->legacy_hdr_valid) {
  877. fit_unsupported_reset ("VxWorks");
  878. do_reset (cmdtp, flag, argc, argv);
  879. }
  880. #endif
  881. sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
  882. setenv("loadaddr", str);
  883. do_bootvx(cmdtp, 0, 0, NULL);
  884. }
  885. static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
  886. int argc, char *argv[],
  887. bootm_headers_t *images)
  888. {
  889. char *local_args[2];
  890. char str[16];
  891. image_header_t *hdr = &images->legacy_hdr_os_copy;
  892. #if defined(CONFIG_FIT)
  893. if (!images->legacy_hdr_valid) {
  894. fit_unsupported_reset ("QNX");
  895. do_reset (cmdtp, flag, argc, argv);
  896. }
  897. #endif
  898. sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
  899. local_args[0] = argv[0];
  900. local_args[1] = str; /* and provide it via the arguments */
  901. do_bootelf(cmdtp, 0, 2, local_args);
  902. }
  903. #endif
  904. #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
  905. static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
  906. int argc, char *argv[],
  907. bootm_headers_t *images)
  908. {
  909. ulong top;
  910. char *s, *cmdline;
  911. char **fwenv, **ss;
  912. int i, j, nxt, len, envno, envsz;
  913. bd_t *kbd;
  914. void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
  915. image_header_t *hdr = &images->legacy_hdr_os_copy;
  916. #if defined(CONFIG_FIT)
  917. if (!images->legacy_hdr_valid) {
  918. fit_unsupported_reset ("ARTOS");
  919. do_reset (cmdtp, flag, argc, argv);
  920. }
  921. #endif
  922. /*
  923. * Booting an ARTOS kernel image + application
  924. */
  925. /* this used to be the top of memory, but was wrong... */
  926. #ifdef CONFIG_PPC
  927. /* get stack pointer */
  928. asm volatile ("mr %0,1" : "=r"(top) );
  929. #endif
  930. debug ("## Current stack ends at 0x%08lX ", top);
  931. top -= 2048; /* just to be sure */
  932. if (top > CFG_BOOTMAPSZ)
  933. top = CFG_BOOTMAPSZ;
  934. top &= ~0xF;
  935. debug ("=> set upper limit to 0x%08lX\n", top);
  936. /* first check the artos specific boot args, then the linux args*/
  937. if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
  938. s = "";
  939. /* get length of cmdline, and place it */
  940. len = strlen (s);
  941. top = (top - (len + 1)) & ~0xF;
  942. cmdline = (char *)top;
  943. debug ("## cmdline at 0x%08lX ", top);
  944. strcpy (cmdline, s);
  945. /* copy bdinfo */
  946. top = (top - sizeof (bd_t)) & ~0xF;
  947. debug ("## bd at 0x%08lX ", top);
  948. kbd = (bd_t *)top;
  949. memcpy (kbd, gd->bd, sizeof (bd_t));
  950. /* first find number of env entries, and their size */
  951. envno = 0;
  952. envsz = 0;
  953. for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
  954. for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
  955. ;
  956. envno++;
  957. envsz += (nxt - i) + 1; /* plus trailing zero */
  958. }
  959. envno++; /* plus the terminating zero */
  960. debug ("## %u envvars total size %u ", envno, envsz);
  961. top = (top - sizeof (char **) * envno) & ~0xF;
  962. fwenv = (char **)top;
  963. debug ("## fwenv at 0x%08lX ", top);
  964. top = (top - envsz) & ~0xF;
  965. s = (char *)top;
  966. ss = fwenv;
  967. /* now copy them */
  968. for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
  969. for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
  970. ;
  971. *ss++ = s;
  972. for (j = i; j < nxt; ++j)
  973. *s++ = env_get_char (j);
  974. *s++ = '\0';
  975. }
  976. *ss++ = NULL; /* terminate */
  977. entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
  978. (*entry) (kbd, cmdline, fwenv, top);
  979. }
  980. #endif