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