cmd_bootm.c 30 KB

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