cmd_bootm.c 31 KB

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