cmd_bootm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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_INTEGRITY)
  97. static boot_os_fn do_bootm_integrity;
  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%8p\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%p\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_INTEGRITY
  396. case IH_OS_INTEGRITY:
  397. do_bootm_integrity (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. if (!fit_all_image_check_hashes (hdr)) {
  746. puts ("Bad hash in FIT image!\n");
  747. return 1;
  748. }
  749. return 0;
  750. #endif
  751. default:
  752. puts ("Unknown image format!\n");
  753. break;
  754. }
  755. return 1;
  756. }
  757. U_BOOT_CMD(
  758. iminfo, CFG_MAXARGS, 1, do_iminfo,
  759. "iminfo - print header information for application image\n",
  760. "addr [addr ...]\n"
  761. " - print header information for application image starting at\n"
  762. " address 'addr' in memory; this includes verification of the\n"
  763. " image contents (magic number, header and payload checksums)\n"
  764. );
  765. #endif
  766. /*******************************************************************/
  767. /* imls - list all images found in flash */
  768. /*******************************************************************/
  769. #if defined(CONFIG_CMD_IMLS)
  770. int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  771. {
  772. flash_info_t *info;
  773. int i, j;
  774. void *hdr;
  775. for (i = 0, info = &flash_info[0];
  776. i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
  777. if (info->flash_id == FLASH_UNKNOWN)
  778. goto next_bank;
  779. for (j = 0; j < info->sector_count; ++j) {
  780. hdr = (void *)info->start[j];
  781. if (!hdr)
  782. goto next_sector;
  783. switch (genimg_get_format (hdr)) {
  784. case IMAGE_FORMAT_LEGACY:
  785. if (!image_check_hcrc (hdr))
  786. goto next_sector;
  787. printf ("Legacy Image at %08lX:\n", (ulong)hdr);
  788. image_print_contents (hdr);
  789. puts (" Verifying Checksum ... ");
  790. if (!image_check_dcrc (hdr)) {
  791. puts ("Bad Data CRC\n");
  792. } else {
  793. puts ("OK\n");
  794. }
  795. break;
  796. #if defined(CONFIG_FIT)
  797. case IMAGE_FORMAT_FIT:
  798. if (!fit_check_format (hdr))
  799. goto next_sector;
  800. printf ("FIT Image at %08lX:\n", (ulong)hdr);
  801. fit_print_contents (hdr);
  802. break;
  803. #endif
  804. default:
  805. goto next_sector;
  806. }
  807. next_sector: ;
  808. }
  809. next_bank: ;
  810. }
  811. return (0);
  812. }
  813. U_BOOT_CMD(
  814. imls, 1, 1, do_imls,
  815. "imls - list all images found in flash\n",
  816. "\n"
  817. " - Prints information about all images found at sector\n"
  818. " boundaries in flash.\n"
  819. );
  820. #endif
  821. /*******************************************************************/
  822. /* helper routines */
  823. /*******************************************************************/
  824. #ifdef CONFIG_SILENT_CONSOLE
  825. static void fixup_silent_linux ()
  826. {
  827. char buf[256], *start, *end;
  828. char *cmdline = getenv ("bootargs");
  829. /* Only fix cmdline when requested */
  830. if (!(gd->flags & GD_FLG_SILENT))
  831. return;
  832. debug ("before silent fix-up: %s\n", cmdline);
  833. if (cmdline) {
  834. if ((start = strstr (cmdline, "console=")) != NULL) {
  835. end = strchr (start, ' ');
  836. strncpy (buf, cmdline, (start - cmdline + 8));
  837. if (end)
  838. strcpy (buf + (start - cmdline + 8), end);
  839. else
  840. buf[start - cmdline + 8] = '\0';
  841. } else {
  842. strcpy (buf, cmdline);
  843. strcat (buf, " console=");
  844. }
  845. } else {
  846. strcpy (buf, "console=");
  847. }
  848. setenv ("bootargs", buf);
  849. debug ("after silent fix-up: %s\n", buf);
  850. }
  851. #endif /* CONFIG_SILENT_CONSOLE */
  852. /*******************************************************************/
  853. /* OS booting routines */
  854. /*******************************************************************/
  855. static int do_bootm_netbsd (int flag, int argc, char *argv[],
  856. bootm_headers_t *images)
  857. {
  858. void (*loader)(bd_t *, image_header_t *, char *, char *);
  859. image_header_t *os_hdr, *hdr;
  860. ulong kernel_data, kernel_len;
  861. char *consdev;
  862. char *cmdline;
  863. #if defined(CONFIG_FIT)
  864. if (!images->legacy_hdr_valid) {
  865. fit_unsupported_reset ("NetBSD");
  866. return 1;
  867. }
  868. #endif
  869. hdr = images->legacy_hdr_os;
  870. /*
  871. * Booting a (NetBSD) kernel image
  872. *
  873. * This process is pretty similar to a standalone application:
  874. * The (first part of an multi-) image must be a stage-2 loader,
  875. * which in turn is responsible for loading & invoking the actual
  876. * kernel. The only differences are the parameters being passed:
  877. * besides the board info strucure, the loader expects a command
  878. * line, the name of the console device, and (optionally) the
  879. * address of the original image header.
  880. */
  881. os_hdr = NULL;
  882. if (image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  883. image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
  884. if (kernel_len)
  885. os_hdr = hdr;
  886. }
  887. consdev = "";
  888. #if defined (CONFIG_8xx_CONS_SMC1)
  889. consdev = "smc1";
  890. #elif defined (CONFIG_8xx_CONS_SMC2)
  891. consdev = "smc2";
  892. #elif defined (CONFIG_8xx_CONS_SCC2)
  893. consdev = "scc2";
  894. #elif defined (CONFIG_8xx_CONS_SCC3)
  895. consdev = "scc3";
  896. #endif
  897. if (argc > 2) {
  898. ulong len;
  899. int i;
  900. for (i = 2, len = 0; i < argc; i += 1)
  901. len += strlen (argv[i]) + 1;
  902. cmdline = malloc (len);
  903. for (i = 2, len = 0; i < argc; i += 1) {
  904. if (i > 2)
  905. cmdline[len++] = ' ';
  906. strcpy (&cmdline[len], argv[i]);
  907. len += strlen (argv[i]);
  908. }
  909. } else if ((cmdline = getenv ("bootargs")) == NULL) {
  910. cmdline = "";
  911. }
  912. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  913. printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  914. (ulong)loader);
  915. show_boot_progress (15);
  916. /*
  917. * NetBSD Stage-2 Loader Parameters:
  918. * r3: ptr to board info data
  919. * r4: image address
  920. * r5: console device
  921. * r6: boot args string
  922. */
  923. (*loader) (gd->bd, os_hdr, consdev, cmdline);
  924. return 1;
  925. }
  926. #ifdef CONFIG_LYNXKDI
  927. static int do_bootm_lynxkdi (int flag, int argc, char *argv[],
  928. bootm_headers_t *images)
  929. {
  930. image_header_t *hdr = &images->legacy_hdr_os_copy;
  931. #if defined(CONFIG_FIT)
  932. if (!images->legacy_hdr_valid) {
  933. fit_unsupported_reset ("Lynx");
  934. return 1;
  935. }
  936. #endif
  937. lynxkdi_boot ((image_header_t *)hdr);
  938. return 1;
  939. }
  940. #endif /* CONFIG_LYNXKDI */
  941. static int do_bootm_rtems (int flag, int argc, char *argv[],
  942. bootm_headers_t *images)
  943. {
  944. void (*entry_point)(bd_t *);
  945. #if defined(CONFIG_FIT)
  946. if (!images->legacy_hdr_valid) {
  947. fit_unsupported_reset ("RTEMS");
  948. return 1;
  949. }
  950. #endif
  951. entry_point = (void (*)(bd_t *))images->ep;
  952. printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
  953. (ulong)entry_point);
  954. show_boot_progress (15);
  955. /*
  956. * RTEMS Parameters:
  957. * r3: ptr to board info data
  958. */
  959. (*entry_point)(gd->bd);
  960. return 1;
  961. }
  962. #if defined(CONFIG_CMD_ELF)
  963. static int do_bootm_vxworks (int flag, int argc, char *argv[],
  964. bootm_headers_t *images)
  965. {
  966. char str[80];
  967. #if defined(CONFIG_FIT)
  968. if (!images->legacy_hdr_valid) {
  969. fit_unsupported_reset ("VxWorks");
  970. return 1;
  971. }
  972. #endif
  973. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  974. setenv("loadaddr", str);
  975. do_bootvx(NULL, 0, 0, NULL);
  976. return 1;
  977. }
  978. static int do_bootm_qnxelf(int flag, int argc, char *argv[],
  979. bootm_headers_t *images)
  980. {
  981. char *local_args[2];
  982. char str[16];
  983. #if defined(CONFIG_FIT)
  984. if (!images->legacy_hdr_valid) {
  985. fit_unsupported_reset ("QNX");
  986. return 1;
  987. }
  988. #endif
  989. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  990. local_args[0] = argv[0];
  991. local_args[1] = str; /* and provide it via the arguments */
  992. do_bootelf(NULL, 0, 2, local_args);
  993. return 1;
  994. }
  995. #endif
  996. #ifdef CONFIG_INTEGRITY
  997. static int do_bootm_integrity (int flag, int argc, char *argv[],
  998. bootm_headers_t *images)
  999. {
  1000. void (*entry_point)(void);
  1001. #if defined(CONFIG_FIT)
  1002. if (!images->legacy_hdr_valid) {
  1003. fit_unsupported_reset ("INTEGRITY");
  1004. return 1;
  1005. }
  1006. #endif
  1007. entry_point = (void (*)(void))images->ep;
  1008. printf ("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  1009. (ulong)entry_point);
  1010. show_boot_progress (15);
  1011. /*
  1012. * INTEGRITY Parameters:
  1013. * None
  1014. */
  1015. (*entry_point)();
  1016. return 1;
  1017. }
  1018. #endif