cmd_bootm.c 31 KB

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