cmd_bootm.c 31 KB

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