cmd_bootm.c 46 KB

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