cmd_bootm.c 30 KB

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