cmd_bootm.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /*
  2. * (C) Copyright 2000-2009
  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 <u-boot/zlib.h>
  32. #include <bzlib.h>
  33. #include <environment.h>
  34. #include <lmb.h>
  35. #include <linux/ctype.h>
  36. #include <asm/byteorder.h>
  37. #include <linux/compiler.h>
  38. #if defined(CONFIG_CMD_USB)
  39. #include <usb.h>
  40. #endif
  41. #ifdef CONFIG_SYS_HUSH_PARSER
  42. #include <hush.h>
  43. #endif
  44. #if defined(CONFIG_OF_LIBFDT)
  45. #include <fdt.h>
  46. #include <libfdt.h>
  47. #include <fdt_support.h>
  48. #endif
  49. #ifdef CONFIG_LZMA
  50. #include <lzma/LzmaTypes.h>
  51. #include <lzma/LzmaDec.h>
  52. #include <lzma/LzmaTools.h>
  53. #endif /* CONFIG_LZMA */
  54. #ifdef CONFIG_LZO
  55. #include <linux/lzo.h>
  56. #endif /* CONFIG_LZO */
  57. DECLARE_GLOBAL_DATA_PTR;
  58. #ifndef CONFIG_SYS_BOOTM_LEN
  59. #define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
  60. #endif
  61. #ifdef CONFIG_BZIP2
  62. extern void bz_internal_error(int);
  63. #endif
  64. #if defined(CONFIG_CMD_IMI)
  65. static int image_info(unsigned long addr);
  66. #endif
  67. #if defined(CONFIG_CMD_IMLS)
  68. #include <flash.h>
  69. #include <mtd/cfi_flash.h>
  70. extern flash_info_t flash_info[]; /* info for FLASH chips */
  71. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  72. #endif
  73. #ifdef CONFIG_SILENT_CONSOLE
  74. static void fixup_silent_linux(void);
  75. #endif
  76. static image_header_t *image_get_kernel(ulong img_addr, int verify);
  77. #if defined(CONFIG_FIT)
  78. static int fit_check_kernel(const void *fit, int os_noffset, int verify);
  79. #endif
  80. static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  81. char * const argv[], bootm_headers_t *images,
  82. ulong *os_data, ulong *os_len);
  83. /*
  84. * Continue booting an OS image; caller already has:
  85. * - copied image header to global variable `header'
  86. * - checked header magic number, checksums (both header & image),
  87. * - verified image architecture (PPC) and type (KERNEL or MULTI),
  88. * - loaded (first part of) image to header load address,
  89. * - disabled interrupts.
  90. */
  91. typedef int boot_os_fn(int flag, int argc, char * const argv[],
  92. bootm_headers_t *images); /* pointers to os/initrd/fdt */
  93. #ifdef CONFIG_BOOTM_LINUX
  94. extern boot_os_fn do_bootm_linux;
  95. #endif
  96. #ifdef CONFIG_BOOTM_NETBSD
  97. static boot_os_fn do_bootm_netbsd;
  98. #endif
  99. #if defined(CONFIG_LYNXKDI)
  100. static boot_os_fn do_bootm_lynxkdi;
  101. extern void lynxkdi_boot(image_header_t *);
  102. #endif
  103. #ifdef CONFIG_BOOTM_RTEMS
  104. static boot_os_fn do_bootm_rtems;
  105. #endif
  106. #if defined(CONFIG_BOOTM_OSE)
  107. static boot_os_fn do_bootm_ose;
  108. #endif
  109. #if defined(CONFIG_CMD_ELF)
  110. static boot_os_fn do_bootm_vxworks;
  111. static boot_os_fn do_bootm_qnxelf;
  112. int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  113. int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  114. #endif
  115. #if defined(CONFIG_INTEGRITY)
  116. static boot_os_fn do_bootm_integrity;
  117. #endif
  118. static boot_os_fn *boot_os[] = {
  119. #ifdef CONFIG_BOOTM_LINUX
  120. [IH_OS_LINUX] = do_bootm_linux,
  121. #endif
  122. #ifdef CONFIG_BOOTM_NETBSD
  123. [IH_OS_NETBSD] = do_bootm_netbsd,
  124. #endif
  125. #ifdef CONFIG_LYNXKDI
  126. [IH_OS_LYNXOS] = do_bootm_lynxkdi,
  127. #endif
  128. #ifdef CONFIG_BOOTM_RTEMS
  129. [IH_OS_RTEMS] = do_bootm_rtems,
  130. #endif
  131. #if defined(CONFIG_BOOTM_OSE)
  132. [IH_OS_OSE] = do_bootm_ose,
  133. #endif
  134. #if defined(CONFIG_CMD_ELF)
  135. [IH_OS_VXWORKS] = do_bootm_vxworks,
  136. [IH_OS_QNX] = do_bootm_qnxelf,
  137. #endif
  138. #ifdef CONFIG_INTEGRITY
  139. [IH_OS_INTEGRITY] = do_bootm_integrity,
  140. #endif
  141. };
  142. bootm_headers_t images; /* pointers to os/initrd/fdt images */
  143. /* Allow for arch specific config before we boot */
  144. static void __arch_preboot_os(void)
  145. {
  146. /* please define platform specific arch_preboot_os() */
  147. }
  148. void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
  149. #define IH_INITRD_ARCH IH_ARCH_DEFAULT
  150. #ifdef CONFIG_LMB
  151. static void boot_start_lmb(bootm_headers_t *images)
  152. {
  153. ulong mem_start;
  154. phys_size_t mem_size;
  155. lmb_init(&images->lmb);
  156. mem_start = getenv_bootm_low();
  157. mem_size = getenv_bootm_size();
  158. lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
  159. arch_lmb_reserve(&images->lmb);
  160. board_lmb_reserve(&images->lmb);
  161. }
  162. #else
  163. #define lmb_reserve(lmb, base, size)
  164. static inline void boot_start_lmb(bootm_headers_t *images) { }
  165. #endif
  166. static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  167. {
  168. void *os_hdr;
  169. int ret;
  170. memset((void *)&images, 0, sizeof(images));
  171. images.verify = getenv_yesno("verify");
  172. boot_start_lmb(&images);
  173. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
  174. /* get kernel image header, start address and length */
  175. os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
  176. &images, &images.os.image_start, &images.os.image_len);
  177. if (images.os.image_len == 0) {
  178. puts("ERROR: can't get kernel image!\n");
  179. return 1;
  180. }
  181. /* get image parameters */
  182. switch (genimg_get_format(os_hdr)) {
  183. case IMAGE_FORMAT_LEGACY:
  184. images.os.type = image_get_type(os_hdr);
  185. images.os.comp = image_get_comp(os_hdr);
  186. images.os.os = image_get_os(os_hdr);
  187. images.os.end = image_get_image_end(os_hdr);
  188. images.os.load = image_get_load(os_hdr);
  189. break;
  190. #if defined(CONFIG_FIT)
  191. case IMAGE_FORMAT_FIT:
  192. if (fit_image_get_type(images.fit_hdr_os,
  193. images.fit_noffset_os, &images.os.type)) {
  194. puts("Can't get image type!\n");
  195. bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
  196. return 1;
  197. }
  198. if (fit_image_get_comp(images.fit_hdr_os,
  199. images.fit_noffset_os, &images.os.comp)) {
  200. puts("Can't get image compression!\n");
  201. bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
  202. return 1;
  203. }
  204. if (fit_image_get_os(images.fit_hdr_os,
  205. images.fit_noffset_os, &images.os.os)) {
  206. puts("Can't get image OS!\n");
  207. bootstage_error(BOOTSTAGE_ID_FIT_OS);
  208. return 1;
  209. }
  210. images.os.end = fit_get_end(images.fit_hdr_os);
  211. if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
  212. &images.os.load)) {
  213. puts("Can't get image load address!\n");
  214. bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
  215. return 1;
  216. }
  217. break;
  218. #endif
  219. default:
  220. puts("ERROR: unknown image format type!\n");
  221. return 1;
  222. }
  223. /* find kernel entry point */
  224. if (images.legacy_hdr_valid) {
  225. images.ep = image_get_ep(&images.legacy_hdr_os_copy);
  226. #if defined(CONFIG_FIT)
  227. } else if (images.fit_uname_os) {
  228. ret = fit_image_get_entry(images.fit_hdr_os,
  229. images.fit_noffset_os, &images.ep);
  230. if (ret) {
  231. puts("Can't get entry point property!\n");
  232. return 1;
  233. }
  234. #endif
  235. } else {
  236. puts("Could not find kernel entry point!\n");
  237. return 1;
  238. }
  239. if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
  240. images.os.load = images.os.image_start;
  241. images.ep += images.os.load;
  242. }
  243. if (((images.os.type == IH_TYPE_KERNEL) ||
  244. (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
  245. (images.os.type == IH_TYPE_MULTI)) &&
  246. (images.os.os == IH_OS_LINUX)) {
  247. /* find ramdisk */
  248. ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
  249. &images.rd_start, &images.rd_end);
  250. if (ret) {
  251. puts("Ramdisk image is corrupt or invalid\n");
  252. return 1;
  253. }
  254. #if defined(CONFIG_OF_LIBFDT)
  255. /* find flattened device tree */
  256. ret = boot_get_fdt(flag, argc, argv, &images,
  257. &images.ft_addr, &images.ft_len);
  258. if (ret) {
  259. puts("Could not find a valid device tree\n");
  260. return 1;
  261. }
  262. set_working_fdt_addr(images.ft_addr);
  263. #endif
  264. }
  265. images.os.start = (ulong)os_hdr;
  266. images.state = BOOTM_STATE_START;
  267. return 0;
  268. }
  269. #define BOOTM_ERR_RESET -1
  270. #define BOOTM_ERR_OVERLAP -2
  271. #define BOOTM_ERR_UNIMPLEMENTED -3
  272. static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
  273. {
  274. uint8_t comp = os.comp;
  275. ulong load = os.load;
  276. ulong blob_start = os.start;
  277. ulong blob_end = os.end;
  278. ulong image_start = os.image_start;
  279. ulong image_len = os.image_len;
  280. __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
  281. int no_overlap = 0;
  282. #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
  283. int ret;
  284. #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
  285. const char *type_name = genimg_get_type_name(os.type);
  286. switch (comp) {
  287. case IH_COMP_NONE:
  288. if (load == blob_start || load == image_start) {
  289. printf(" XIP %s ... ", type_name);
  290. no_overlap = 1;
  291. } else {
  292. printf(" Loading %s ... ", type_name);
  293. memmove_wd((void *)load, (void *)image_start,
  294. image_len, CHUNKSZ);
  295. }
  296. *load_end = load + image_len;
  297. puts("OK\n");
  298. break;
  299. #ifdef CONFIG_GZIP
  300. case IH_COMP_GZIP:
  301. printf(" Uncompressing %s ... ", type_name);
  302. if (gunzip((void *)load, unc_len,
  303. (uchar *)image_start, &image_len) != 0) {
  304. puts("GUNZIP: uncompress, out-of-mem or overwrite "
  305. "error - must RESET board to recover\n");
  306. if (boot_progress)
  307. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  308. return BOOTM_ERR_RESET;
  309. }
  310. *load_end = load + image_len;
  311. break;
  312. #endif /* CONFIG_GZIP */
  313. #ifdef CONFIG_BZIP2
  314. case IH_COMP_BZIP2:
  315. printf(" Uncompressing %s ... ", type_name);
  316. /*
  317. * If we've got less than 4 MB of malloc() space,
  318. * use slower decompression algorithm which requires
  319. * at most 2300 KB of memory.
  320. */
  321. int i = BZ2_bzBuffToBuffDecompress((char *)load,
  322. &unc_len, (char *)image_start, image_len,
  323. CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
  324. if (i != BZ_OK) {
  325. printf("BUNZIP2: uncompress or overwrite error %d "
  326. "- must RESET board to recover\n", i);
  327. if (boot_progress)
  328. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  329. return BOOTM_ERR_RESET;
  330. }
  331. *load_end = load + unc_len;
  332. break;
  333. #endif /* CONFIG_BZIP2 */
  334. #ifdef CONFIG_LZMA
  335. case IH_COMP_LZMA: {
  336. SizeT lzma_len = unc_len;
  337. printf(" Uncompressing %s ... ", type_name);
  338. ret = lzmaBuffToBuffDecompress(
  339. (unsigned char *)load, &lzma_len,
  340. (unsigned char *)image_start, image_len);
  341. unc_len = lzma_len;
  342. if (ret != SZ_OK) {
  343. printf("LZMA: uncompress or overwrite error %d "
  344. "- must RESET board to recover\n", ret);
  345. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  346. return BOOTM_ERR_RESET;
  347. }
  348. *load_end = load + unc_len;
  349. break;
  350. }
  351. #endif /* CONFIG_LZMA */
  352. #ifdef CONFIG_LZO
  353. case IH_COMP_LZO:
  354. printf(" Uncompressing %s ... ", type_name);
  355. ret = lzop_decompress((const unsigned char *)image_start,
  356. image_len, (unsigned char *)load,
  357. &unc_len);
  358. if (ret != LZO_E_OK) {
  359. printf("LZO: uncompress or overwrite error %d "
  360. "- must RESET board to recover\n", ret);
  361. if (boot_progress)
  362. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  363. return BOOTM_ERR_RESET;
  364. }
  365. *load_end = load + unc_len;
  366. break;
  367. #endif /* CONFIG_LZO */
  368. default:
  369. printf("Unimplemented compression type %d\n", comp);
  370. return BOOTM_ERR_UNIMPLEMENTED;
  371. }
  372. flush_cache(load, (*load_end - load) * sizeof(ulong));
  373. puts("OK\n");
  374. debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
  375. bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
  376. if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
  377. debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
  378. blob_start, blob_end);
  379. debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
  380. *load_end);
  381. return BOOTM_ERR_OVERLAP;
  382. }
  383. return 0;
  384. }
  385. static int bootm_start_standalone(ulong iflag, int argc, char * const argv[])
  386. {
  387. char *s;
  388. int (*appl)(int, char * const []);
  389. /* Don't start if "autostart" is set to "no" */
  390. if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
  391. char buf[32];
  392. sprintf(buf, "%lX", images.os.image_len);
  393. setenv("filesize", buf);
  394. return 0;
  395. }
  396. appl = (int (*)(int, char * const []))(ulong)ntohl(images.ep);
  397. (*appl)(argc-1, &argv[1]);
  398. return 0;
  399. }
  400. /* we overload the cmd field with our state machine info instead of a
  401. * function pointer */
  402. static cmd_tbl_t cmd_bootm_sub[] = {
  403. U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
  404. U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
  405. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  406. U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
  407. #endif
  408. #ifdef CONFIG_OF_LIBFDT
  409. U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
  410. #endif
  411. U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
  412. U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
  413. U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
  414. U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
  415. };
  416. static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
  417. char * const argv[])
  418. {
  419. int ret = 0;
  420. long state;
  421. cmd_tbl_t *c;
  422. boot_os_fn *boot_fn;
  423. c = find_cmd_tbl(argv[1], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
  424. if (c) {
  425. state = (long)c->cmd;
  426. /* treat start special since it resets the state machine */
  427. if (state == BOOTM_STATE_START) {
  428. argc--;
  429. argv++;
  430. return bootm_start(cmdtp, flag, argc, argv);
  431. }
  432. } else {
  433. /* Unrecognized command */
  434. return CMD_RET_USAGE;
  435. }
  436. if (images.state >= state) {
  437. printf("Trying to execute a command out of order\n");
  438. return CMD_RET_USAGE;
  439. }
  440. images.state |= state;
  441. boot_fn = boot_os[images.os.os];
  442. switch (state) {
  443. ulong load_end;
  444. case BOOTM_STATE_START:
  445. /* should never occur */
  446. break;
  447. case BOOTM_STATE_LOADOS:
  448. ret = bootm_load_os(images.os, &load_end, 0);
  449. if (ret)
  450. return ret;
  451. lmb_reserve(&images.lmb, images.os.load,
  452. (load_end - images.os.load));
  453. break;
  454. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  455. case BOOTM_STATE_RAMDISK:
  456. {
  457. ulong rd_len = images.rd_end - images.rd_start;
  458. char str[17];
  459. ret = boot_ramdisk_high(&images.lmb, images.rd_start,
  460. rd_len, &images.initrd_start, &images.initrd_end);
  461. if (ret)
  462. return ret;
  463. sprintf(str, "%lx", images.initrd_start);
  464. setenv("initrd_start", str);
  465. sprintf(str, "%lx", images.initrd_end);
  466. setenv("initrd_end", str);
  467. }
  468. break;
  469. #endif
  470. #if defined(CONFIG_OF_LIBFDT)
  471. case BOOTM_STATE_FDT:
  472. {
  473. boot_fdt_add_mem_rsv_regions(&images.lmb,
  474. images.ft_addr);
  475. ret = boot_relocate_fdt(&images.lmb,
  476. &images.ft_addr, &images.ft_len);
  477. break;
  478. }
  479. #endif
  480. case BOOTM_STATE_OS_CMDLINE:
  481. ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, &images);
  482. if (ret)
  483. printf("cmdline subcommand not supported\n");
  484. break;
  485. case BOOTM_STATE_OS_BD_T:
  486. ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, &images);
  487. if (ret)
  488. printf("bdt subcommand not supported\n");
  489. break;
  490. case BOOTM_STATE_OS_PREP:
  491. ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, &images);
  492. if (ret)
  493. printf("prep subcommand not supported\n");
  494. break;
  495. case BOOTM_STATE_OS_GO:
  496. disable_interrupts();
  497. #ifdef CONFIG_NETCONSOLE
  498. /*
  499. * Stop the ethernet stack if NetConsole could have
  500. * left it up
  501. */
  502. eth_halt();
  503. #endif
  504. arch_preboot_os();
  505. boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
  506. break;
  507. }
  508. return ret;
  509. }
  510. /*******************************************************************/
  511. /* bootm - boot application image from image in memory */
  512. /*******************************************************************/
  513. int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  514. {
  515. ulong iflag;
  516. ulong load_end = 0;
  517. int ret;
  518. boot_os_fn *boot_fn;
  519. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  520. static int relocated = 0;
  521. /* relocate boot function table */
  522. if (!relocated) {
  523. int i;
  524. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  525. if (boot_os[i] != NULL)
  526. boot_os[i] += gd->reloc_off;
  527. relocated = 1;
  528. }
  529. #endif
  530. /* determine if we have a sub command */
  531. if (argc > 1) {
  532. char *endp;
  533. simple_strtoul(argv[1], &endp, 16);
  534. /* endp pointing to NULL means that argv[1] was just a
  535. * valid number, pass it along to the normal bootm processing
  536. *
  537. * If endp is ':' or '#' assume a FIT identifier so pass
  538. * along for normal processing.
  539. *
  540. * Right now we assume the first arg should never be '-'
  541. */
  542. if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
  543. return do_bootm_subcommand(cmdtp, flag, argc, argv);
  544. }
  545. if (bootm_start(cmdtp, flag, argc, argv))
  546. return 1;
  547. /*
  548. * We have reached the point of no return: we are going to
  549. * overwrite all exception vector code, so we cannot easily
  550. * recover from any failures any more...
  551. */
  552. iflag = disable_interrupts();
  553. #ifdef CONFIG_NETCONSOLE
  554. /* Stop the ethernet stack if NetConsole could have left it up */
  555. eth_halt();
  556. #endif
  557. #if defined(CONFIG_CMD_USB)
  558. /*
  559. * turn off USB to prevent the host controller from writing to the
  560. * SDRAM while Linux is booting. This could happen (at least for OHCI
  561. * controller), because the HCCA (Host Controller Communication Area)
  562. * lies within the SDRAM and the host controller writes continously to
  563. * this area (as busmaster!). The HccaFrameNumber is for example
  564. * updated every 1 ms within the HCCA structure in SDRAM! For more
  565. * details see the OpenHCI specification.
  566. */
  567. usb_stop();
  568. #endif
  569. ret = bootm_load_os(images.os, &load_end, 1);
  570. if (ret < 0) {
  571. if (ret == BOOTM_ERR_RESET)
  572. do_reset(cmdtp, flag, argc, argv);
  573. if (ret == BOOTM_ERR_OVERLAP) {
  574. if (images.legacy_hdr_valid) {
  575. image_header_t *hdr;
  576. hdr = &images.legacy_hdr_os_copy;
  577. if (image_get_type(hdr) == IH_TYPE_MULTI)
  578. puts("WARNING: legacy format multi "
  579. "component image "
  580. "overwritten\n");
  581. } else {
  582. puts("ERROR: new format image overwritten - "
  583. "must RESET the board to recover\n");
  584. bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
  585. do_reset(cmdtp, flag, argc, argv);
  586. }
  587. }
  588. if (ret == BOOTM_ERR_UNIMPLEMENTED) {
  589. if (iflag)
  590. enable_interrupts();
  591. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  592. return 1;
  593. }
  594. }
  595. lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
  596. if (images.os.type == IH_TYPE_STANDALONE) {
  597. if (iflag)
  598. enable_interrupts();
  599. /* This may return when 'autostart' is 'no' */
  600. bootm_start_standalone(iflag, argc, argv);
  601. return 0;
  602. }
  603. bootstage_mark(BOOTSTAGE_ID_CHECK_BOOT_OS);
  604. #ifdef CONFIG_SILENT_CONSOLE
  605. if (images.os.os == IH_OS_LINUX)
  606. fixup_silent_linux();
  607. #endif
  608. boot_fn = boot_os[images.os.os];
  609. if (boot_fn == NULL) {
  610. if (iflag)
  611. enable_interrupts();
  612. printf("ERROR: booting os '%s' (%d) is not supported\n",
  613. genimg_get_os_name(images.os.os), images.os.os);
  614. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  615. return 1;
  616. }
  617. arch_preboot_os();
  618. boot_fn(0, argc, argv, &images);
  619. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  620. #ifdef DEBUG
  621. puts("\n## Control returned to monitor - resetting...\n");
  622. #endif
  623. do_reset(cmdtp, flag, argc, argv);
  624. return 1;
  625. }
  626. int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  627. {
  628. const char *ep = getenv("autostart");
  629. if (ep && !strcmp(ep, "yes")) {
  630. char *local_args[2];
  631. local_args[0] = (char *)cmd;
  632. local_args[1] = NULL;
  633. printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
  634. return do_bootm(cmdtp, 0, 1, local_args);
  635. }
  636. return 0;
  637. }
  638. /**
  639. * image_get_kernel - verify legacy format kernel image
  640. * @img_addr: in RAM address of the legacy format image to be verified
  641. * @verify: data CRC verification flag
  642. *
  643. * image_get_kernel() verifies legacy image integrity and returns pointer to
  644. * legacy image header if image verification was completed successfully.
  645. *
  646. * returns:
  647. * pointer to a legacy image header if valid image was found
  648. * otherwise return NULL
  649. */
  650. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  651. {
  652. image_header_t *hdr = (image_header_t *)img_addr;
  653. if (!image_check_magic(hdr)) {
  654. puts("Bad Magic Number\n");
  655. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  656. return NULL;
  657. }
  658. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  659. if (!image_check_hcrc(hdr)) {
  660. puts("Bad Header Checksum\n");
  661. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  662. return NULL;
  663. }
  664. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  665. image_print_contents(hdr);
  666. if (verify) {
  667. puts(" Verifying Checksum ... ");
  668. if (!image_check_dcrc(hdr)) {
  669. printf("Bad Data CRC\n");
  670. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  671. return NULL;
  672. }
  673. puts("OK\n");
  674. }
  675. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  676. if (!image_check_target_arch(hdr)) {
  677. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  678. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  679. return NULL;
  680. }
  681. return hdr;
  682. }
  683. /**
  684. * fit_check_kernel - verify FIT format kernel subimage
  685. * @fit_hdr: pointer to the FIT image header
  686. * os_noffset: kernel subimage node offset within FIT image
  687. * @verify: data CRC verification flag
  688. *
  689. * fit_check_kernel() verifies integrity of the kernel subimage and from
  690. * specified FIT image.
  691. *
  692. * returns:
  693. * 1, on success
  694. * 0, on failure
  695. */
  696. #if defined(CONFIG_FIT)
  697. static int fit_check_kernel(const void *fit, int os_noffset, int verify)
  698. {
  699. fit_image_print(fit, os_noffset, " ");
  700. if (verify) {
  701. puts(" Verifying Hash Integrity ... ");
  702. if (!fit_image_check_hashes(fit, os_noffset)) {
  703. puts("Bad Data Hash\n");
  704. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_HASH);
  705. return 0;
  706. }
  707. puts("OK\n");
  708. }
  709. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_ARCH);
  710. if (!fit_image_check_target_arch(fit, os_noffset)) {
  711. puts("Unsupported Architecture\n");
  712. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_ARCH);
  713. return 0;
  714. }
  715. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
  716. if (!fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL) &&
  717. !fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL_NOLOAD)) {
  718. puts("Not a kernel image\n");
  719. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
  720. return 0;
  721. }
  722. bootstage_mark(BOOTSTAGE_ID_FIT_CHECKED);
  723. return 1;
  724. }
  725. #endif /* CONFIG_FIT */
  726. /**
  727. * boot_get_kernel - find kernel image
  728. * @os_data: pointer to a ulong variable, will hold os data start address
  729. * @os_len: pointer to a ulong variable, will hold os data length
  730. *
  731. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  732. * and locates kernel data.
  733. *
  734. * returns:
  735. * pointer to image header if valid image was found, plus kernel start
  736. * address and length, otherwise NULL
  737. */
  738. static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  739. char * const argv[], bootm_headers_t *images, ulong *os_data,
  740. ulong *os_len)
  741. {
  742. image_header_t *hdr;
  743. ulong img_addr;
  744. #if defined(CONFIG_FIT)
  745. void *fit_hdr;
  746. const char *fit_uname_config = NULL;
  747. const char *fit_uname_kernel = NULL;
  748. const void *data;
  749. size_t len;
  750. int cfg_noffset;
  751. int os_noffset;
  752. #endif
  753. /* find out kernel image address */
  754. if (argc < 2) {
  755. img_addr = load_addr;
  756. debug("* kernel: default image load address = 0x%08lx\n",
  757. load_addr);
  758. #if defined(CONFIG_FIT)
  759. } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
  760. &fit_uname_config)) {
  761. debug("* kernel: config '%s' from image at 0x%08lx\n",
  762. fit_uname_config, img_addr);
  763. } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
  764. &fit_uname_kernel)) {
  765. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  766. fit_uname_kernel, img_addr);
  767. #endif
  768. } else {
  769. img_addr = simple_strtoul(argv[1], NULL, 16);
  770. debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
  771. }
  772. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  773. /* copy from dataflash if needed */
  774. img_addr = genimg_get_image(img_addr);
  775. /* check image type, for FIT images get FIT kernel node */
  776. *os_data = *os_len = 0;
  777. switch (genimg_get_format((void *)img_addr)) {
  778. case IMAGE_FORMAT_LEGACY:
  779. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  780. img_addr);
  781. hdr = image_get_kernel(img_addr, images->verify);
  782. if (!hdr)
  783. return NULL;
  784. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  785. /* get os_data and os_len */
  786. switch (image_get_type(hdr)) {
  787. case IH_TYPE_KERNEL:
  788. case IH_TYPE_KERNEL_NOLOAD:
  789. *os_data = image_get_data(hdr);
  790. *os_len = image_get_data_size(hdr);
  791. break;
  792. case IH_TYPE_MULTI:
  793. image_multi_getimg(hdr, 0, os_data, os_len);
  794. break;
  795. case IH_TYPE_STANDALONE:
  796. *os_data = image_get_data(hdr);
  797. *os_len = image_get_data_size(hdr);
  798. break;
  799. default:
  800. printf("Wrong Image Type for %s command\n",
  801. cmdtp->name);
  802. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  803. return NULL;
  804. }
  805. /*
  806. * copy image header to allow for image overwrites during
  807. * kernel decompression.
  808. */
  809. memmove(&images->legacy_hdr_os_copy, hdr,
  810. sizeof(image_header_t));
  811. /* save pointer to image header */
  812. images->legacy_hdr_os = hdr;
  813. images->legacy_hdr_valid = 1;
  814. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  815. break;
  816. #if defined(CONFIG_FIT)
  817. case IMAGE_FORMAT_FIT:
  818. fit_hdr = (void *)img_addr;
  819. printf("## Booting kernel from FIT Image at %08lx ...\n",
  820. img_addr);
  821. if (!fit_check_format(fit_hdr)) {
  822. puts("Bad FIT kernel image format!\n");
  823. bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
  824. return NULL;
  825. }
  826. bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
  827. if (!fit_uname_kernel) {
  828. /*
  829. * no kernel image node unit name, try to get config
  830. * node first. If config unit node name is NULL
  831. * fit_conf_get_node() will try to find default config
  832. * node
  833. */
  834. bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
  835. #ifdef CONFIG_FIT_BEST_MATCH
  836. if (fit_uname_config)
  837. cfg_noffset =
  838. fit_conf_get_node(fit_hdr,
  839. fit_uname_config);
  840. else
  841. cfg_noffset =
  842. fit_conf_find_compat(fit_hdr,
  843. gd->fdt_blob);
  844. #else
  845. cfg_noffset = fit_conf_get_node(fit_hdr,
  846. fit_uname_config);
  847. #endif
  848. if (cfg_noffset < 0) {
  849. bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
  850. return NULL;
  851. }
  852. /* save configuration uname provided in the first
  853. * bootm argument
  854. */
  855. images->fit_uname_cfg = fdt_get_name(fit_hdr,
  856. cfg_noffset,
  857. NULL);
  858. printf(" Using '%s' configuration\n",
  859. images->fit_uname_cfg);
  860. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  861. os_noffset = fit_conf_get_kernel_node(fit_hdr,
  862. cfg_noffset);
  863. fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
  864. NULL);
  865. } else {
  866. /* get kernel component image node offset */
  867. bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
  868. os_noffset = fit_image_get_node(fit_hdr,
  869. fit_uname_kernel);
  870. }
  871. if (os_noffset < 0) {
  872. bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
  873. return NULL;
  874. }
  875. printf(" Trying '%s' kernel subimage\n", fit_uname_kernel);
  876. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
  877. if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
  878. return NULL;
  879. /* get kernel image data address and length */
  880. if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
  881. puts("Could not find kernel subimage data!\n");
  882. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
  883. return NULL;
  884. }
  885. bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  886. *os_len = len;
  887. *os_data = (ulong)data;
  888. images->fit_hdr_os = fit_hdr;
  889. images->fit_uname_os = fit_uname_kernel;
  890. images->fit_noffset_os = os_noffset;
  891. break;
  892. #endif
  893. default:
  894. printf("Wrong Image Format for %s command\n", cmdtp->name);
  895. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  896. return NULL;
  897. }
  898. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  899. *os_data, *os_len, *os_len);
  900. return (void *)img_addr;
  901. }
  902. #ifdef CONFIG_SYS_LONGHELP
  903. static char bootm_help_text[] =
  904. "[addr [arg ...]]\n - boot application image stored in memory\n"
  905. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  906. "\t'arg' can be the address of an initrd image\n"
  907. #if defined(CONFIG_OF_LIBFDT)
  908. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  909. "\ta third argument is required which is the address of the\n"
  910. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  911. "\tuse a '-' for the second argument. If you do not pass a third\n"
  912. "\ta bd_info struct will be passed instead\n"
  913. #endif
  914. #if defined(CONFIG_FIT)
  915. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  916. "\tmust be extened to include component or configuration unit name:\n"
  917. "\taddr:<subimg_uname> - direct component image specification\n"
  918. "\taddr#<conf_uname> - configuration specification\n"
  919. "\tUse iminfo command to get the list of existing component\n"
  920. "\timages and configurations.\n"
  921. #endif
  922. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  923. "must be\n"
  924. "issued in the order below (it's ok to not issue all sub-commands):\n"
  925. "\tstart [addr [arg ...]]\n"
  926. "\tloados - load OS image\n"
  927. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
  928. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  929. #endif
  930. #if defined(CONFIG_OF_LIBFDT)
  931. "\tfdt - relocate flat device tree\n"
  932. #endif
  933. "\tcmdline - OS specific command line processing/setup\n"
  934. "\tbdt - OS specific bd_t processing\n"
  935. "\tprep - OS specific prep before relocation or go\n"
  936. "\tgo - start OS";
  937. #endif
  938. U_BOOT_CMD(
  939. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  940. "boot application image from memory", bootm_help_text
  941. );
  942. /*******************************************************************/
  943. /* bootd - boot default image */
  944. /*******************************************************************/
  945. #if defined(CONFIG_CMD_BOOTD)
  946. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  947. {
  948. int rcode = 0;
  949. if (run_command(getenv("bootcmd"), flag) < 0)
  950. rcode = 1;
  951. return rcode;
  952. }
  953. U_BOOT_CMD(
  954. boot, 1, 1, do_bootd,
  955. "boot default, i.e., run 'bootcmd'",
  956. ""
  957. );
  958. /* keep old command name "bootd" for backward compatibility */
  959. U_BOOT_CMD(
  960. bootd, 1, 1, do_bootd,
  961. "boot default, i.e., run 'bootcmd'",
  962. ""
  963. );
  964. #endif
  965. /*******************************************************************/
  966. /* iminfo - print header info for a requested image */
  967. /*******************************************************************/
  968. #if defined(CONFIG_CMD_IMI)
  969. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  970. {
  971. int arg;
  972. ulong addr;
  973. int rcode = 0;
  974. if (argc < 2) {
  975. return image_info(load_addr);
  976. }
  977. for (arg = 1; arg < argc; ++arg) {
  978. addr = simple_strtoul(argv[arg], NULL, 16);
  979. if (image_info(addr) != 0)
  980. rcode = 1;
  981. }
  982. return rcode;
  983. }
  984. static int image_info(ulong addr)
  985. {
  986. void *hdr = (void *)addr;
  987. printf("\n## Checking Image at %08lx ...\n", addr);
  988. switch (genimg_get_format(hdr)) {
  989. case IMAGE_FORMAT_LEGACY:
  990. puts(" Legacy image found\n");
  991. if (!image_check_magic(hdr)) {
  992. puts(" Bad Magic Number\n");
  993. return 1;
  994. }
  995. if (!image_check_hcrc(hdr)) {
  996. puts(" Bad Header Checksum\n");
  997. return 1;
  998. }
  999. image_print_contents(hdr);
  1000. puts(" Verifying Checksum ... ");
  1001. if (!image_check_dcrc(hdr)) {
  1002. puts(" Bad Data CRC\n");
  1003. return 1;
  1004. }
  1005. puts("OK\n");
  1006. return 0;
  1007. #if defined(CONFIG_FIT)
  1008. case IMAGE_FORMAT_FIT:
  1009. puts(" FIT image found\n");
  1010. if (!fit_check_format(hdr)) {
  1011. puts("Bad FIT image format!\n");
  1012. return 1;
  1013. }
  1014. fit_print_contents(hdr);
  1015. if (!fit_all_image_check_hashes(hdr)) {
  1016. puts("Bad hash in FIT image!\n");
  1017. return 1;
  1018. }
  1019. return 0;
  1020. #endif
  1021. default:
  1022. puts("Unknown image format!\n");
  1023. break;
  1024. }
  1025. return 1;
  1026. }
  1027. U_BOOT_CMD(
  1028. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  1029. "print header information for application image",
  1030. "addr [addr ...]\n"
  1031. " - print header information for application image starting at\n"
  1032. " address 'addr' in memory; this includes verification of the\n"
  1033. " image contents (magic number, header and payload checksums)"
  1034. );
  1035. #endif
  1036. /*******************************************************************/
  1037. /* imls - list all images found in flash */
  1038. /*******************************************************************/
  1039. #if defined(CONFIG_CMD_IMLS)
  1040. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1041. {
  1042. flash_info_t *info;
  1043. int i, j;
  1044. void *hdr;
  1045. for (i = 0, info = &flash_info[0];
  1046. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  1047. if (info->flash_id == FLASH_UNKNOWN)
  1048. goto next_bank;
  1049. for (j = 0; j < info->sector_count; ++j) {
  1050. hdr = (void *)info->start[j];
  1051. if (!hdr)
  1052. goto next_sector;
  1053. switch (genimg_get_format(hdr)) {
  1054. case IMAGE_FORMAT_LEGACY:
  1055. if (!image_check_hcrc(hdr))
  1056. goto next_sector;
  1057. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  1058. image_print_contents(hdr);
  1059. puts(" Verifying Checksum ... ");
  1060. if (!image_check_dcrc(hdr)) {
  1061. puts("Bad Data CRC\n");
  1062. } else {
  1063. puts("OK\n");
  1064. }
  1065. break;
  1066. #if defined(CONFIG_FIT)
  1067. case IMAGE_FORMAT_FIT:
  1068. if (!fit_check_format(hdr))
  1069. goto next_sector;
  1070. printf("FIT Image at %08lX:\n", (ulong)hdr);
  1071. fit_print_contents(hdr);
  1072. break;
  1073. #endif
  1074. default:
  1075. goto next_sector;
  1076. }
  1077. next_sector: ;
  1078. }
  1079. next_bank: ;
  1080. }
  1081. return (0);
  1082. }
  1083. U_BOOT_CMD(
  1084. imls, 1, 1, do_imls,
  1085. "list all images found in flash",
  1086. "\n"
  1087. " - Prints information about all images found at sector\n"
  1088. " boundaries in flash."
  1089. );
  1090. #endif
  1091. /*******************************************************************/
  1092. /* helper routines */
  1093. /*******************************************************************/
  1094. #ifdef CONFIG_SILENT_CONSOLE
  1095. static void fixup_silent_linux(void)
  1096. {
  1097. char buf[256], *start, *end;
  1098. char *cmdline = getenv("bootargs");
  1099. /* Only fix cmdline when requested */
  1100. if (!(gd->flags & GD_FLG_SILENT))
  1101. return;
  1102. debug("before silent fix-up: %s\n", cmdline);
  1103. if (cmdline) {
  1104. start = strstr(cmdline, "console=");
  1105. if (start) {
  1106. end = strchr(start, ' ');
  1107. strncpy(buf, cmdline, (start - cmdline + 8));
  1108. if (end)
  1109. strcpy(buf + (start - cmdline + 8), end);
  1110. else
  1111. buf[start - cmdline + 8] = '\0';
  1112. } else {
  1113. strcpy(buf, cmdline);
  1114. strcat(buf, " console=");
  1115. }
  1116. } else {
  1117. strcpy(buf, "console=");
  1118. }
  1119. setenv("bootargs", buf);
  1120. debug("after silent fix-up: %s\n", buf);
  1121. }
  1122. #endif /* CONFIG_SILENT_CONSOLE */
  1123. /*******************************************************************/
  1124. /* OS booting routines */
  1125. /*******************************************************************/
  1126. #ifdef CONFIG_BOOTM_NETBSD
  1127. static int do_bootm_netbsd(int flag, int argc, char * const argv[],
  1128. bootm_headers_t *images)
  1129. {
  1130. void (*loader)(bd_t *, image_header_t *, char *, char *);
  1131. image_header_t *os_hdr, *hdr;
  1132. ulong kernel_data, kernel_len;
  1133. char *consdev;
  1134. char *cmdline;
  1135. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1136. return 1;
  1137. #if defined(CONFIG_FIT)
  1138. if (!images->legacy_hdr_valid) {
  1139. fit_unsupported_reset("NetBSD");
  1140. return 1;
  1141. }
  1142. #endif
  1143. hdr = images->legacy_hdr_os;
  1144. /*
  1145. * Booting a (NetBSD) kernel image
  1146. *
  1147. * This process is pretty similar to a standalone application:
  1148. * The (first part of an multi-) image must be a stage-2 loader,
  1149. * which in turn is responsible for loading & invoking the actual
  1150. * kernel. The only differences are the parameters being passed:
  1151. * besides the board info strucure, the loader expects a command
  1152. * line, the name of the console device, and (optionally) the
  1153. * address of the original image header.
  1154. */
  1155. os_hdr = NULL;
  1156. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  1157. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  1158. if (kernel_len)
  1159. os_hdr = hdr;
  1160. }
  1161. consdev = "";
  1162. #if defined(CONFIG_8xx_CONS_SMC1)
  1163. consdev = "smc1";
  1164. #elif defined(CONFIG_8xx_CONS_SMC2)
  1165. consdev = "smc2";
  1166. #elif defined(CONFIG_8xx_CONS_SCC2)
  1167. consdev = "scc2";
  1168. #elif defined(CONFIG_8xx_CONS_SCC3)
  1169. consdev = "scc3";
  1170. #endif
  1171. if (argc > 2) {
  1172. ulong len;
  1173. int i;
  1174. for (i = 2, len = 0; i < argc; i += 1)
  1175. len += strlen(argv[i]) + 1;
  1176. cmdline = malloc(len);
  1177. for (i = 2, len = 0; i < argc; i += 1) {
  1178. if (i > 2)
  1179. cmdline[len++] = ' ';
  1180. strcpy(&cmdline[len], argv[i]);
  1181. len += strlen(argv[i]);
  1182. }
  1183. } else if ((cmdline = getenv("bootargs")) == NULL) {
  1184. cmdline = "";
  1185. }
  1186. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  1187. printf("## Transferring control to NetBSD stage-2 loader "
  1188. "(at address %08lx) ...\n",
  1189. (ulong)loader);
  1190. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1191. /*
  1192. * NetBSD Stage-2 Loader Parameters:
  1193. * r3: ptr to board info data
  1194. * r4: image address
  1195. * r5: console device
  1196. * r6: boot args string
  1197. */
  1198. (*loader)(gd->bd, os_hdr, consdev, cmdline);
  1199. return 1;
  1200. }
  1201. #endif /* CONFIG_BOOTM_NETBSD*/
  1202. #ifdef CONFIG_LYNXKDI
  1203. static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
  1204. bootm_headers_t *images)
  1205. {
  1206. image_header_t *hdr = &images->legacy_hdr_os_copy;
  1207. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1208. return 1;
  1209. #if defined(CONFIG_FIT)
  1210. if (!images->legacy_hdr_valid) {
  1211. fit_unsupported_reset("Lynx");
  1212. return 1;
  1213. }
  1214. #endif
  1215. lynxkdi_boot((image_header_t *)hdr);
  1216. return 1;
  1217. }
  1218. #endif /* CONFIG_LYNXKDI */
  1219. #ifdef CONFIG_BOOTM_RTEMS
  1220. static int do_bootm_rtems(int flag, int argc, char * const argv[],
  1221. bootm_headers_t *images)
  1222. {
  1223. void (*entry_point)(bd_t *);
  1224. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1225. return 1;
  1226. #if defined(CONFIG_FIT)
  1227. if (!images->legacy_hdr_valid) {
  1228. fit_unsupported_reset("RTEMS");
  1229. return 1;
  1230. }
  1231. #endif
  1232. entry_point = (void (*)(bd_t *))images->ep;
  1233. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  1234. (ulong)entry_point);
  1235. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1236. /*
  1237. * RTEMS Parameters:
  1238. * r3: ptr to board info data
  1239. */
  1240. (*entry_point)(gd->bd);
  1241. return 1;
  1242. }
  1243. #endif /* CONFIG_BOOTM_RTEMS */
  1244. #if defined(CONFIG_BOOTM_OSE)
  1245. static int do_bootm_ose(int flag, int argc, char * const argv[],
  1246. bootm_headers_t *images)
  1247. {
  1248. void (*entry_point)(void);
  1249. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1250. return 1;
  1251. #if defined(CONFIG_FIT)
  1252. if (!images->legacy_hdr_valid) {
  1253. fit_unsupported_reset("OSE");
  1254. return 1;
  1255. }
  1256. #endif
  1257. entry_point = (void (*)(void))images->ep;
  1258. printf("## Transferring control to OSE (at address %08lx) ...\n",
  1259. (ulong)entry_point);
  1260. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1261. /*
  1262. * OSE Parameters:
  1263. * None
  1264. */
  1265. (*entry_point)();
  1266. return 1;
  1267. }
  1268. #endif /* CONFIG_BOOTM_OSE */
  1269. #if defined(CONFIG_CMD_ELF)
  1270. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  1271. bootm_headers_t *images)
  1272. {
  1273. char str[80];
  1274. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1275. return 1;
  1276. #if defined(CONFIG_FIT)
  1277. if (!images->legacy_hdr_valid) {
  1278. fit_unsupported_reset("VxWorks");
  1279. return 1;
  1280. }
  1281. #endif
  1282. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1283. setenv("loadaddr", str);
  1284. do_bootvx(NULL, 0, 0, NULL);
  1285. return 1;
  1286. }
  1287. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  1288. bootm_headers_t *images)
  1289. {
  1290. char *local_args[2];
  1291. char str[16];
  1292. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1293. return 1;
  1294. #if defined(CONFIG_FIT)
  1295. if (!images->legacy_hdr_valid) {
  1296. fit_unsupported_reset("QNX");
  1297. return 1;
  1298. }
  1299. #endif
  1300. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1301. local_args[0] = argv[0];
  1302. local_args[1] = str; /* and provide it via the arguments */
  1303. do_bootelf(NULL, 0, 2, local_args);
  1304. return 1;
  1305. }
  1306. #endif
  1307. #ifdef CONFIG_INTEGRITY
  1308. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  1309. bootm_headers_t *images)
  1310. {
  1311. void (*entry_point)(void);
  1312. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1313. return 1;
  1314. #if defined(CONFIG_FIT)
  1315. if (!images->legacy_hdr_valid) {
  1316. fit_unsupported_reset("INTEGRITY");
  1317. return 1;
  1318. }
  1319. #endif
  1320. entry_point = (void (*)(void))images->ep;
  1321. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  1322. (ulong)entry_point);
  1323. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1324. /*
  1325. * INTEGRITY Parameters:
  1326. * None
  1327. */
  1328. (*entry_point)();
  1329. return 1;
  1330. }
  1331. #endif
  1332. #ifdef CONFIG_CMD_BOOTZ
  1333. static int __bootz_setup(void *image, void **start, void **end)
  1334. {
  1335. /* Please define bootz_setup() for your platform */
  1336. puts("Your platform's zImage format isn't supported yet!\n");
  1337. return -1;
  1338. }
  1339. int bootz_setup(void *image, void **start, void **end)
  1340. __attribute__((weak, alias("__bootz_setup")));
  1341. /*
  1342. * zImage booting support
  1343. */
  1344. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  1345. char * const argv[], bootm_headers_t *images)
  1346. {
  1347. int ret;
  1348. void *zi_start, *zi_end;
  1349. memset(images, 0, sizeof(bootm_headers_t));
  1350. boot_start_lmb(images);
  1351. /* Setup Linux kernel zImage entry point */
  1352. if (argc < 2) {
  1353. images->ep = load_addr;
  1354. debug("* kernel: default image load address = 0x%08lx\n",
  1355. load_addr);
  1356. } else {
  1357. images->ep = simple_strtoul(argv[1], NULL, 16);
  1358. debug("* kernel: cmdline image address = 0x%08lx\n",
  1359. images->ep);
  1360. }
  1361. ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
  1362. if (ret != 0)
  1363. return 1;
  1364. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  1365. /* Find ramdisk */
  1366. ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
  1367. &images->rd_start, &images->rd_end);
  1368. if (ret) {
  1369. puts("Ramdisk image is corrupt or invalid\n");
  1370. return 1;
  1371. }
  1372. #if defined(CONFIG_OF_LIBFDT)
  1373. /* find flattened device tree */
  1374. ret = boot_get_fdt(flag, argc, argv, images,
  1375. &images->ft_addr, &images->ft_len);
  1376. if (ret) {
  1377. puts("Could not find a valid device tree\n");
  1378. return 1;
  1379. }
  1380. set_working_fdt_addr(images->ft_addr);
  1381. #endif
  1382. return 0;
  1383. }
  1384. static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1385. {
  1386. bootm_headers_t images;
  1387. if (bootz_start(cmdtp, flag, argc, argv, &images))
  1388. return 1;
  1389. /*
  1390. * We have reached the point of no return: we are going to
  1391. * overwrite all exception vector code, so we cannot easily
  1392. * recover from any failures any more...
  1393. */
  1394. disable_interrupts();
  1395. #ifdef CONFIG_NETCONSOLE
  1396. /* Stop the ethernet stack if NetConsole could have left it up */
  1397. eth_halt();
  1398. #endif
  1399. #if defined(CONFIG_CMD_USB)
  1400. /*
  1401. * turn off USB to prevent the host controller from writing to the
  1402. * SDRAM while Linux is booting. This could happen (at least for OHCI
  1403. * controller), because the HCCA (Host Controller Communication Area)
  1404. * lies within the SDRAM and the host controller writes continously to
  1405. * this area (as busmaster!). The HccaFrameNumber is for example
  1406. * updated every 1 ms within the HCCA structure in SDRAM! For more
  1407. * details see the OpenHCI specification.
  1408. */
  1409. usb_stop();
  1410. #endif
  1411. #ifdef CONFIG_SILENT_CONSOLE
  1412. fixup_silent_linux();
  1413. #endif
  1414. arch_preboot_os();
  1415. do_bootm_linux(0, argc, argv, &images);
  1416. #ifdef DEBUG
  1417. puts("\n## Control returned to monitor - resetting...\n");
  1418. #endif
  1419. do_reset(cmdtp, flag, argc, argv);
  1420. return 1;
  1421. }
  1422. #ifdef CONFIG_SYS_LONGHELP
  1423. static char bootz_help_text[] =
  1424. "[addr [initrd[:size]] [fdt]]\n"
  1425. " - boot Linux zImage stored in memory\n"
  1426. "\tThe argument 'initrd' is optional and specifies the address\n"
  1427. "\tof the initrd in memory. The optional argument ':size' allows\n"
  1428. "\tspecifying the size of RAW initrd.\n"
  1429. #if defined(CONFIG_OF_LIBFDT)
  1430. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  1431. "\ta third argument is required which is the address of the\n"
  1432. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  1433. "\tuse a '-' for the second argument. If you do not pass a third\n"
  1434. "\ta bd_info struct will be passed instead\n"
  1435. #endif
  1436. "";
  1437. #endif
  1438. U_BOOT_CMD(
  1439. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  1440. "boot Linux zImage image from memory", bootz_help_text
  1441. );
  1442. #endif /* CONFIG_CMD_BOOTZ */