cmd_bootm.c 42 KB

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