cmd_bootm.c 46 KB

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