cmd_bootm.c 31 KB

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