cmd_bootm.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
  1203. static void copy_args(char *dest, int argc, char * const argv[], char delim)
  1204. {
  1205. int i;
  1206. for (i = 0; i < argc; i++) {
  1207. if (i > 0)
  1208. *dest++ = delim;
  1209. strcpy(dest, argv[i]);
  1210. dest += strlen(argv[i]);
  1211. }
  1212. }
  1213. #endif
  1214. /*******************************************************************/
  1215. /* OS booting routines */
  1216. /*******************************************************************/
  1217. #ifdef CONFIG_BOOTM_NETBSD
  1218. static int do_bootm_netbsd(int flag, int argc, char * const argv[],
  1219. bootm_headers_t *images)
  1220. {
  1221. void (*loader)(bd_t *, image_header_t *, char *, char *);
  1222. image_header_t *os_hdr, *hdr;
  1223. ulong kernel_data, kernel_len;
  1224. char *consdev;
  1225. char *cmdline;
  1226. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1227. return 1;
  1228. #if defined(CONFIG_FIT)
  1229. if (!images->legacy_hdr_valid) {
  1230. fit_unsupported_reset("NetBSD");
  1231. return 1;
  1232. }
  1233. #endif
  1234. hdr = images->legacy_hdr_os;
  1235. /*
  1236. * Booting a (NetBSD) kernel image
  1237. *
  1238. * This process is pretty similar to a standalone application:
  1239. * The (first part of an multi-) image must be a stage-2 loader,
  1240. * which in turn is responsible for loading & invoking the actual
  1241. * kernel. The only differences are the parameters being passed:
  1242. * besides the board info strucure, the loader expects a command
  1243. * line, the name of the console device, and (optionally) the
  1244. * address of the original image header.
  1245. */
  1246. os_hdr = NULL;
  1247. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  1248. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  1249. if (kernel_len)
  1250. os_hdr = hdr;
  1251. }
  1252. consdev = "";
  1253. #if defined(CONFIG_8xx_CONS_SMC1)
  1254. consdev = "smc1";
  1255. #elif defined(CONFIG_8xx_CONS_SMC2)
  1256. consdev = "smc2";
  1257. #elif defined(CONFIG_8xx_CONS_SCC2)
  1258. consdev = "scc2";
  1259. #elif defined(CONFIG_8xx_CONS_SCC3)
  1260. consdev = "scc3";
  1261. #endif
  1262. if (argc > 0) {
  1263. ulong len;
  1264. int i;
  1265. for (i = 0, len = 0; i < argc; i += 1)
  1266. len += strlen(argv[i]) + 1;
  1267. cmdline = malloc(len);
  1268. copy_args(cmdline, argc, argv, ' ');
  1269. } else if ((cmdline = getenv("bootargs")) == NULL) {
  1270. cmdline = "";
  1271. }
  1272. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  1273. printf("## Transferring control to NetBSD stage-2 loader "
  1274. "(at address %08lx) ...\n",
  1275. (ulong)loader);
  1276. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1277. /*
  1278. * NetBSD Stage-2 Loader Parameters:
  1279. * r3: ptr to board info data
  1280. * r4: image address
  1281. * r5: console device
  1282. * r6: boot args string
  1283. */
  1284. (*loader)(gd->bd, os_hdr, consdev, cmdline);
  1285. return 1;
  1286. }
  1287. #endif /* CONFIG_BOOTM_NETBSD*/
  1288. #ifdef CONFIG_LYNXKDI
  1289. static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
  1290. bootm_headers_t *images)
  1291. {
  1292. image_header_t *hdr = &images->legacy_hdr_os_copy;
  1293. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1294. return 1;
  1295. #if defined(CONFIG_FIT)
  1296. if (!images->legacy_hdr_valid) {
  1297. fit_unsupported_reset("Lynx");
  1298. return 1;
  1299. }
  1300. #endif
  1301. lynxkdi_boot((image_header_t *)hdr);
  1302. return 1;
  1303. }
  1304. #endif /* CONFIG_LYNXKDI */
  1305. #ifdef CONFIG_BOOTM_RTEMS
  1306. static int do_bootm_rtems(int flag, int argc, char * const argv[],
  1307. bootm_headers_t *images)
  1308. {
  1309. void (*entry_point)(bd_t *);
  1310. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1311. return 1;
  1312. #if defined(CONFIG_FIT)
  1313. if (!images->legacy_hdr_valid) {
  1314. fit_unsupported_reset("RTEMS");
  1315. return 1;
  1316. }
  1317. #endif
  1318. entry_point = (void (*)(bd_t *))images->ep;
  1319. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  1320. (ulong)entry_point);
  1321. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1322. /*
  1323. * RTEMS Parameters:
  1324. * r3: ptr to board info data
  1325. */
  1326. (*entry_point)(gd->bd);
  1327. return 1;
  1328. }
  1329. #endif /* CONFIG_BOOTM_RTEMS */
  1330. #if defined(CONFIG_BOOTM_OSE)
  1331. static int do_bootm_ose(int flag, int argc, char * const argv[],
  1332. bootm_headers_t *images)
  1333. {
  1334. void (*entry_point)(void);
  1335. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1336. return 1;
  1337. #if defined(CONFIG_FIT)
  1338. if (!images->legacy_hdr_valid) {
  1339. fit_unsupported_reset("OSE");
  1340. return 1;
  1341. }
  1342. #endif
  1343. entry_point = (void (*)(void))images->ep;
  1344. printf("## Transferring control to OSE (at address %08lx) ...\n",
  1345. (ulong)entry_point);
  1346. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1347. /*
  1348. * OSE Parameters:
  1349. * None
  1350. */
  1351. (*entry_point)();
  1352. return 1;
  1353. }
  1354. #endif /* CONFIG_BOOTM_OSE */
  1355. #if defined(CONFIG_BOOTM_PLAN9)
  1356. static int do_bootm_plan9(int flag, int argc, char * const argv[],
  1357. bootm_headers_t *images)
  1358. {
  1359. void (*entry_point)(void);
  1360. char *s;
  1361. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1362. return 1;
  1363. #if defined(CONFIG_FIT)
  1364. if (!images->legacy_hdr_valid) {
  1365. fit_unsupported_reset("Plan 9");
  1366. return 1;
  1367. }
  1368. #endif
  1369. /* See README.plan9 */
  1370. s = getenv("confaddr");
  1371. if (s != NULL) {
  1372. char *confaddr = (char *)simple_strtoul(s, NULL, 16);
  1373. if (argc > 0) {
  1374. copy_args(confaddr, argc, argv, '\n');
  1375. } else {
  1376. s = getenv("bootargs");
  1377. if (s != NULL)
  1378. strcpy(confaddr, s);
  1379. }
  1380. }
  1381. entry_point = (void (*)(void))images->ep;
  1382. printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
  1383. (ulong)entry_point);
  1384. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1385. /*
  1386. * Plan 9 Parameters:
  1387. * None
  1388. */
  1389. (*entry_point)();
  1390. return 1;
  1391. }
  1392. #endif /* CONFIG_BOOTM_PLAN9 */
  1393. #if defined(CONFIG_CMD_ELF)
  1394. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  1395. bootm_headers_t *images)
  1396. {
  1397. char str[80];
  1398. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1399. return 1;
  1400. #if defined(CONFIG_FIT)
  1401. if (!images->legacy_hdr_valid) {
  1402. fit_unsupported_reset("VxWorks");
  1403. return 1;
  1404. }
  1405. #endif
  1406. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1407. setenv("loadaddr", str);
  1408. do_bootvx(NULL, 0, 0, NULL);
  1409. return 1;
  1410. }
  1411. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  1412. bootm_headers_t *images)
  1413. {
  1414. char *local_args[2];
  1415. char str[16];
  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("QNX");
  1421. return 1;
  1422. }
  1423. #endif
  1424. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1425. local_args[0] = argv[0];
  1426. local_args[1] = str; /* and provide it via the arguments */
  1427. do_bootelf(NULL, 0, 2, local_args);
  1428. return 1;
  1429. }
  1430. #endif
  1431. #ifdef CONFIG_INTEGRITY
  1432. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  1433. bootm_headers_t *images)
  1434. {
  1435. void (*entry_point)(void);
  1436. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1437. return 1;
  1438. #if defined(CONFIG_FIT)
  1439. if (!images->legacy_hdr_valid) {
  1440. fit_unsupported_reset("INTEGRITY");
  1441. return 1;
  1442. }
  1443. #endif
  1444. entry_point = (void (*)(void))images->ep;
  1445. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  1446. (ulong)entry_point);
  1447. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1448. /*
  1449. * INTEGRITY Parameters:
  1450. * None
  1451. */
  1452. (*entry_point)();
  1453. return 1;
  1454. }
  1455. #endif
  1456. #ifdef CONFIG_CMD_BOOTZ
  1457. static int __bootz_setup(void *image, void **start, void **end)
  1458. {
  1459. /* Please define bootz_setup() for your platform */
  1460. puts("Your platform's zImage format isn't supported yet!\n");
  1461. return -1;
  1462. }
  1463. int bootz_setup(void *image, void **start, void **end)
  1464. __attribute__((weak, alias("__bootz_setup")));
  1465. /*
  1466. * zImage booting support
  1467. */
  1468. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  1469. char * const argv[], bootm_headers_t *images)
  1470. {
  1471. int ret;
  1472. void *zi_start, *zi_end;
  1473. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  1474. images, 1);
  1475. /* Setup Linux kernel zImage entry point */
  1476. if (argc < 2) {
  1477. images->ep = load_addr;
  1478. debug("* kernel: default image load address = 0x%08lx\n",
  1479. load_addr);
  1480. } else {
  1481. images->ep = simple_strtoul(argv[1], NULL, 16);
  1482. debug("* kernel: cmdline image address = 0x%08lx\n",
  1483. images->ep);
  1484. }
  1485. ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
  1486. if (ret != 0)
  1487. return 1;
  1488. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  1489. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_FINDOTHER,
  1490. images, 1);
  1491. return ret;
  1492. }
  1493. int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1494. {
  1495. bootm_headers_t images;
  1496. int ret;
  1497. if (bootz_start(cmdtp, flag, argc, argv, &images))
  1498. return 1;
  1499. ret = do_bootm_states(cmdtp, flag, argc, argv,
  1500. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO,
  1501. &images, 1);
  1502. return ret;
  1503. }
  1504. #ifdef CONFIG_SYS_LONGHELP
  1505. static char bootz_help_text[] =
  1506. "[addr [initrd[:size]] [fdt]]\n"
  1507. " - boot Linux zImage stored in memory\n"
  1508. "\tThe argument 'initrd' is optional and specifies the address\n"
  1509. "\tof the initrd in memory. The optional argument ':size' allows\n"
  1510. "\tspecifying the size of RAW initrd.\n"
  1511. #if defined(CONFIG_OF_LIBFDT)
  1512. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  1513. "\ta third argument is required which is the address of the\n"
  1514. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  1515. "\tuse a '-' for the second argument. If you do not pass a third\n"
  1516. "\ta bd_info struct will be passed instead\n"
  1517. #endif
  1518. "";
  1519. #endif
  1520. U_BOOT_CMD(
  1521. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  1522. "boot Linux zImage image from memory", bootz_help_text
  1523. );
  1524. #endif /* CONFIG_CMD_BOOTZ */