cmd_bootm.c 31 KB

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