cmd_bootm.c 31 KB

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