cmd_bootm.c 44 KB

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