cmd_bootm.c 43 KB

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