cmd_bootm.c 46 KB

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