cmd_bootm.c 31 KB

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