cmd_bootm.c 45 KB

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