cmd_bootm.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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. 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. 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. arch_preboot_os();
  498. boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
  499. break;
  500. }
  501. return ret;
  502. }
  503. /*******************************************************************/
  504. /* bootm - boot application image from image in memory */
  505. /*******************************************************************/
  506. int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  507. {
  508. ulong iflag;
  509. ulong load_end = 0;
  510. int ret;
  511. boot_os_fn *boot_fn;
  512. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  513. static int relocated = 0;
  514. /* relocate boot function table */
  515. if (!relocated) {
  516. int i;
  517. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  518. if (boot_os[i] != NULL)
  519. boot_os[i] += gd->reloc_off;
  520. relocated = 1;
  521. }
  522. #endif
  523. /* determine if we have a sub command */
  524. if (argc > 1) {
  525. char *endp;
  526. simple_strtoul(argv[1], &endp, 16);
  527. /* endp pointing to NULL means that argv[1] was just a
  528. * valid number, pass it along to the normal bootm processing
  529. *
  530. * If endp is ':' or '#' assume a FIT identifier so pass
  531. * along for normal processing.
  532. *
  533. * Right now we assume the first arg should never be '-'
  534. */
  535. if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
  536. return do_bootm_subcommand(cmdtp, flag, argc, argv);
  537. }
  538. if (bootm_start(cmdtp, flag, argc, argv))
  539. return 1;
  540. /*
  541. * We have reached the point of no return: we are going to
  542. * overwrite all exception vector code, so we cannot easily
  543. * recover from any failures any more...
  544. */
  545. iflag = disable_interrupts();
  546. #if defined(CONFIG_CMD_USB)
  547. /*
  548. * turn off USB to prevent the host controller from writing to the
  549. * SDRAM while Linux is booting. This could happen (at least for OHCI
  550. * controller), because the HCCA (Host Controller Communication Area)
  551. * lies within the SDRAM and the host controller writes continously to
  552. * this area (as busmaster!). The HccaFrameNumber is for example
  553. * updated every 1 ms within the HCCA structure in SDRAM! For more
  554. * details see the OpenHCI specification.
  555. */
  556. usb_stop();
  557. #endif
  558. ret = bootm_load_os(images.os, &load_end, 1);
  559. if (ret < 0) {
  560. if (ret == BOOTM_ERR_RESET)
  561. do_reset(cmdtp, flag, argc, argv);
  562. if (ret == BOOTM_ERR_OVERLAP) {
  563. if (images.legacy_hdr_valid) {
  564. image_header_t *hdr;
  565. hdr = &images.legacy_hdr_os_copy;
  566. if (image_get_type(hdr) == IH_TYPE_MULTI)
  567. puts("WARNING: legacy format multi "
  568. "component image "
  569. "overwritten\n");
  570. } else {
  571. puts("ERROR: new format image overwritten - "
  572. "must RESET the board to recover\n");
  573. bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
  574. do_reset(cmdtp, flag, argc, argv);
  575. }
  576. }
  577. if (ret == BOOTM_ERR_UNIMPLEMENTED) {
  578. if (iflag)
  579. enable_interrupts();
  580. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  581. return 1;
  582. }
  583. }
  584. lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
  585. if (images.os.type == IH_TYPE_STANDALONE) {
  586. if (iflag)
  587. enable_interrupts();
  588. /* This may return when 'autostart' is 'no' */
  589. bootm_start_standalone(iflag, argc, argv);
  590. return 0;
  591. }
  592. bootstage_mark(BOOTSTAGE_ID_CHECK_BOOT_OS);
  593. #ifdef CONFIG_SILENT_CONSOLE
  594. if (images.os.os == IH_OS_LINUX)
  595. fixup_silent_linux();
  596. #endif
  597. boot_fn = boot_os[images.os.os];
  598. if (boot_fn == NULL) {
  599. if (iflag)
  600. enable_interrupts();
  601. printf("ERROR: booting os '%s' (%d) is not supported\n",
  602. genimg_get_os_name(images.os.os), images.os.os);
  603. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  604. return 1;
  605. }
  606. arch_preboot_os();
  607. boot_fn(0, argc, argv, &images);
  608. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  609. #ifdef DEBUG
  610. puts("\n## Control returned to monitor - resetting...\n");
  611. #endif
  612. do_reset(cmdtp, flag, argc, argv);
  613. return 1;
  614. }
  615. int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  616. {
  617. const char *ep = getenv("autostart");
  618. if (ep && !strcmp(ep, "yes")) {
  619. char *local_args[2];
  620. local_args[0] = (char *)cmd;
  621. local_args[1] = NULL;
  622. printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
  623. return do_bootm(cmdtp, 0, 1, local_args);
  624. }
  625. return 0;
  626. }
  627. /**
  628. * image_get_kernel - verify legacy format kernel image
  629. * @img_addr: in RAM address of the legacy format image to be verified
  630. * @verify: data CRC verification flag
  631. *
  632. * image_get_kernel() verifies legacy image integrity and returns pointer to
  633. * legacy image header if image verification was completed successfully.
  634. *
  635. * returns:
  636. * pointer to a legacy image header if valid image was found
  637. * otherwise return NULL
  638. */
  639. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  640. {
  641. image_header_t *hdr = (image_header_t *)img_addr;
  642. if (!image_check_magic(hdr)) {
  643. puts("Bad Magic Number\n");
  644. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  645. return NULL;
  646. }
  647. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  648. if (!image_check_hcrc(hdr)) {
  649. puts("Bad Header Checksum\n");
  650. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  651. return NULL;
  652. }
  653. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  654. image_print_contents(hdr);
  655. if (verify) {
  656. puts(" Verifying Checksum ... ");
  657. if (!image_check_dcrc(hdr)) {
  658. printf("Bad Data CRC\n");
  659. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  660. return NULL;
  661. }
  662. puts("OK\n");
  663. }
  664. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  665. if (!image_check_target_arch(hdr)) {
  666. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  667. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  668. return NULL;
  669. }
  670. return hdr;
  671. }
  672. /**
  673. * fit_check_kernel - verify FIT format kernel subimage
  674. * @fit_hdr: pointer to the FIT image header
  675. * os_noffset: kernel subimage node offset within FIT image
  676. * @verify: data CRC verification flag
  677. *
  678. * fit_check_kernel() verifies integrity of the kernel subimage and from
  679. * specified FIT image.
  680. *
  681. * returns:
  682. * 1, on success
  683. * 0, on failure
  684. */
  685. #if defined(CONFIG_FIT)
  686. static int fit_check_kernel(const void *fit, int os_noffset, int verify)
  687. {
  688. fit_image_print(fit, os_noffset, " ");
  689. if (verify) {
  690. puts(" Verifying Hash Integrity ... ");
  691. if (!fit_image_check_hashes(fit, os_noffset)) {
  692. puts("Bad Data Hash\n");
  693. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_HASH);
  694. return 0;
  695. }
  696. puts("OK\n");
  697. }
  698. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_ARCH);
  699. if (!fit_image_check_target_arch(fit, os_noffset)) {
  700. puts("Unsupported Architecture\n");
  701. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_ARCH);
  702. return 0;
  703. }
  704. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
  705. if (!fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL) &&
  706. !fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL_NOLOAD)) {
  707. puts("Not a kernel image\n");
  708. bootstage_error(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
  709. return 0;
  710. }
  711. bootstage_mark(BOOTSTAGE_ID_FIT_CHECKED);
  712. return 1;
  713. }
  714. #endif /* CONFIG_FIT */
  715. /**
  716. * boot_get_kernel - find kernel image
  717. * @os_data: pointer to a ulong variable, will hold os data start address
  718. * @os_len: pointer to a ulong variable, will hold os data length
  719. *
  720. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  721. * and locates kernel data.
  722. *
  723. * returns:
  724. * pointer to image header if valid image was found, plus kernel start
  725. * address and length, otherwise NULL
  726. */
  727. static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  728. char * const argv[], bootm_headers_t *images, ulong *os_data,
  729. ulong *os_len)
  730. {
  731. image_header_t *hdr;
  732. ulong img_addr;
  733. #if defined(CONFIG_FIT)
  734. void *fit_hdr;
  735. const char *fit_uname_config = NULL;
  736. const char *fit_uname_kernel = NULL;
  737. const void *data;
  738. size_t len;
  739. int cfg_noffset;
  740. int os_noffset;
  741. #endif
  742. /* find out kernel image address */
  743. if (argc < 2) {
  744. img_addr = load_addr;
  745. debug("* kernel: default image load address = 0x%08lx\n",
  746. load_addr);
  747. #if defined(CONFIG_FIT)
  748. } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
  749. &fit_uname_config)) {
  750. debug("* kernel: config '%s' from image at 0x%08lx\n",
  751. fit_uname_config, img_addr);
  752. } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
  753. &fit_uname_kernel)) {
  754. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  755. fit_uname_kernel, img_addr);
  756. #endif
  757. } else {
  758. img_addr = simple_strtoul(argv[1], NULL, 16);
  759. debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
  760. }
  761. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  762. /* copy from dataflash if needed */
  763. img_addr = genimg_get_image(img_addr);
  764. /* check image type, for FIT images get FIT kernel node */
  765. *os_data = *os_len = 0;
  766. switch (genimg_get_format((void *)img_addr)) {
  767. case IMAGE_FORMAT_LEGACY:
  768. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  769. img_addr);
  770. hdr = image_get_kernel(img_addr, images->verify);
  771. if (!hdr)
  772. return NULL;
  773. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  774. /* get os_data and os_len */
  775. switch (image_get_type(hdr)) {
  776. case IH_TYPE_KERNEL:
  777. case IH_TYPE_KERNEL_NOLOAD:
  778. *os_data = image_get_data(hdr);
  779. *os_len = image_get_data_size(hdr);
  780. break;
  781. case IH_TYPE_MULTI:
  782. image_multi_getimg(hdr, 0, os_data, os_len);
  783. break;
  784. case IH_TYPE_STANDALONE:
  785. *os_data = image_get_data(hdr);
  786. *os_len = image_get_data_size(hdr);
  787. break;
  788. default:
  789. printf("Wrong Image Type for %s command\n",
  790. cmdtp->name);
  791. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  792. return NULL;
  793. }
  794. /*
  795. * copy image header to allow for image overwrites during
  796. * kernel decompression.
  797. */
  798. memmove(&images->legacy_hdr_os_copy, hdr,
  799. sizeof(image_header_t));
  800. /* save pointer to image header */
  801. images->legacy_hdr_os = hdr;
  802. images->legacy_hdr_valid = 1;
  803. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  804. break;
  805. #if defined(CONFIG_FIT)
  806. case IMAGE_FORMAT_FIT:
  807. fit_hdr = (void *)img_addr;
  808. printf("## Booting kernel from FIT Image at %08lx ...\n",
  809. img_addr);
  810. if (!fit_check_format(fit_hdr)) {
  811. puts("Bad FIT kernel image format!\n");
  812. bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
  813. return NULL;
  814. }
  815. bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
  816. if (!fit_uname_kernel) {
  817. /*
  818. * no kernel image node unit name, try to get config
  819. * node first. If config unit node name is NULL
  820. * fit_conf_get_node() will try to find default config
  821. * node
  822. */
  823. bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
  824. cfg_noffset = fit_conf_get_node(fit_hdr,
  825. fit_uname_config);
  826. if (cfg_noffset < 0) {
  827. bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
  828. return NULL;
  829. }
  830. /* save configuration uname provided in the first
  831. * bootm argument
  832. */
  833. images->fit_uname_cfg = fdt_get_name(fit_hdr,
  834. cfg_noffset,
  835. NULL);
  836. printf(" Using '%s' configuration\n",
  837. images->fit_uname_cfg);
  838. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  839. os_noffset = fit_conf_get_kernel_node(fit_hdr,
  840. cfg_noffset);
  841. fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
  842. NULL);
  843. } else {
  844. /* get kernel component image node offset */
  845. bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
  846. os_noffset = fit_image_get_node(fit_hdr,
  847. fit_uname_kernel);
  848. }
  849. if (os_noffset < 0) {
  850. bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
  851. return NULL;
  852. }
  853. printf(" Trying '%s' kernel subimage\n", fit_uname_kernel);
  854. bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
  855. if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
  856. return NULL;
  857. /* get kernel image data address and length */
  858. if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
  859. puts("Could not find kernel subimage data!\n");
  860. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
  861. return NULL;
  862. }
  863. bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  864. *os_len = len;
  865. *os_data = (ulong)data;
  866. images->fit_hdr_os = fit_hdr;
  867. images->fit_uname_os = fit_uname_kernel;
  868. images->fit_noffset_os = os_noffset;
  869. break;
  870. #endif
  871. default:
  872. printf("Wrong Image Format for %s command\n", cmdtp->name);
  873. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  874. return NULL;
  875. }
  876. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  877. *os_data, *os_len, *os_len);
  878. return (void *)img_addr;
  879. }
  880. U_BOOT_CMD(
  881. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  882. "boot application image from memory",
  883. "[addr [arg ...]]\n - boot application image stored in memory\n"
  884. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  885. "\t'arg' can be the address of an initrd image\n"
  886. #if defined(CONFIG_OF_LIBFDT)
  887. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  888. "\ta third argument is required which is the address of the\n"
  889. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  890. "\tuse a '-' for the second argument. If you do not pass a third\n"
  891. "\ta bd_info struct will be passed instead\n"
  892. #endif
  893. #if defined(CONFIG_FIT)
  894. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  895. "\tmust be extened to include component or configuration unit name:\n"
  896. "\taddr:<subimg_uname> - direct component image specification\n"
  897. "\taddr#<conf_uname> - configuration specification\n"
  898. "\tUse iminfo command to get the list of existing component\n"
  899. "\timages and configurations.\n"
  900. #endif
  901. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  902. "must be\n"
  903. "issued in the order below (it's ok to not issue all sub-commands):\n"
  904. "\tstart [addr [arg ...]]\n"
  905. "\tloados - load OS image\n"
  906. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
  907. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  908. #endif
  909. #if defined(CONFIG_OF_LIBFDT)
  910. "\tfdt - relocate flat device tree\n"
  911. #endif
  912. "\tcmdline - OS specific command line processing/setup\n"
  913. "\tbdt - OS specific bd_t processing\n"
  914. "\tprep - OS specific prep before relocation or go\n"
  915. "\tgo - start OS"
  916. );
  917. /*******************************************************************/
  918. /* bootd - boot default image */
  919. /*******************************************************************/
  920. #if defined(CONFIG_CMD_BOOTD)
  921. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  922. {
  923. int rcode = 0;
  924. if (run_command(getenv("bootcmd"), flag) < 0)
  925. rcode = 1;
  926. return rcode;
  927. }
  928. U_BOOT_CMD(
  929. boot, 1, 1, do_bootd,
  930. "boot default, i.e., run 'bootcmd'",
  931. ""
  932. );
  933. /* keep old command name "bootd" for backward compatibility */
  934. U_BOOT_CMD(
  935. bootd, 1, 1, do_bootd,
  936. "boot default, i.e., run 'bootcmd'",
  937. ""
  938. );
  939. #endif
  940. /*******************************************************************/
  941. /* iminfo - print header info for a requested image */
  942. /*******************************************************************/
  943. #if defined(CONFIG_CMD_IMI)
  944. int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  945. {
  946. int arg;
  947. ulong addr;
  948. int rcode = 0;
  949. if (argc < 2) {
  950. return image_info(load_addr);
  951. }
  952. for (arg = 1; arg < argc; ++arg) {
  953. addr = simple_strtoul(argv[arg], NULL, 16);
  954. if (image_info(addr) != 0)
  955. rcode = 1;
  956. }
  957. return rcode;
  958. }
  959. static int image_info(ulong addr)
  960. {
  961. void *hdr = (void *)addr;
  962. printf("\n## Checking Image at %08lx ...\n", addr);
  963. switch (genimg_get_format(hdr)) {
  964. case IMAGE_FORMAT_LEGACY:
  965. puts(" Legacy image found\n");
  966. if (!image_check_magic(hdr)) {
  967. puts(" Bad Magic Number\n");
  968. return 1;
  969. }
  970. if (!image_check_hcrc(hdr)) {
  971. puts(" Bad Header Checksum\n");
  972. return 1;
  973. }
  974. image_print_contents(hdr);
  975. puts(" Verifying Checksum ... ");
  976. if (!image_check_dcrc(hdr)) {
  977. puts(" Bad Data CRC\n");
  978. return 1;
  979. }
  980. puts("OK\n");
  981. return 0;
  982. #if defined(CONFIG_FIT)
  983. case IMAGE_FORMAT_FIT:
  984. puts(" FIT image found\n");
  985. if (!fit_check_format(hdr)) {
  986. puts("Bad FIT image format!\n");
  987. return 1;
  988. }
  989. fit_print_contents(hdr);
  990. if (!fit_all_image_check_hashes(hdr)) {
  991. puts("Bad hash in FIT image!\n");
  992. return 1;
  993. }
  994. return 0;
  995. #endif
  996. default:
  997. puts("Unknown image format!\n");
  998. break;
  999. }
  1000. return 1;
  1001. }
  1002. U_BOOT_CMD(
  1003. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  1004. "print header information for application image",
  1005. "addr [addr ...]\n"
  1006. " - print header information for application image starting at\n"
  1007. " address 'addr' in memory; this includes verification of the\n"
  1008. " image contents (magic number, header and payload checksums)"
  1009. );
  1010. #endif
  1011. /*******************************************************************/
  1012. /* imls - list all images found in flash */
  1013. /*******************************************************************/
  1014. #if defined(CONFIG_CMD_IMLS)
  1015. int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1016. {
  1017. flash_info_t *info;
  1018. int i, j;
  1019. void *hdr;
  1020. for (i = 0, info = &flash_info[0];
  1021. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  1022. if (info->flash_id == FLASH_UNKNOWN)
  1023. goto next_bank;
  1024. for (j = 0; j < info->sector_count; ++j) {
  1025. hdr = (void *)info->start[j];
  1026. if (!hdr)
  1027. goto next_sector;
  1028. switch (genimg_get_format(hdr)) {
  1029. case IMAGE_FORMAT_LEGACY:
  1030. if (!image_check_hcrc(hdr))
  1031. goto next_sector;
  1032. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  1033. image_print_contents(hdr);
  1034. puts(" Verifying Checksum ... ");
  1035. if (!image_check_dcrc(hdr)) {
  1036. puts("Bad Data CRC\n");
  1037. } else {
  1038. puts("OK\n");
  1039. }
  1040. break;
  1041. #if defined(CONFIG_FIT)
  1042. case IMAGE_FORMAT_FIT:
  1043. if (!fit_check_format(hdr))
  1044. goto next_sector;
  1045. printf("FIT Image at %08lX:\n", (ulong)hdr);
  1046. fit_print_contents(hdr);
  1047. break;
  1048. #endif
  1049. default:
  1050. goto next_sector;
  1051. }
  1052. next_sector: ;
  1053. }
  1054. next_bank: ;
  1055. }
  1056. return (0);
  1057. }
  1058. U_BOOT_CMD(
  1059. imls, 1, 1, do_imls,
  1060. "list all images found in flash",
  1061. "\n"
  1062. " - Prints information about all images found at sector\n"
  1063. " boundaries in flash."
  1064. );
  1065. #endif
  1066. /*******************************************************************/
  1067. /* helper routines */
  1068. /*******************************************************************/
  1069. #ifdef CONFIG_SILENT_CONSOLE
  1070. static void fixup_silent_linux(void)
  1071. {
  1072. char buf[256], *start, *end;
  1073. char *cmdline = getenv("bootargs");
  1074. /* Only fix cmdline when requested */
  1075. if (!(gd->flags & GD_FLG_SILENT))
  1076. return;
  1077. debug("before silent fix-up: %s\n", cmdline);
  1078. if (cmdline) {
  1079. start = strstr(cmdline, "console=");
  1080. if (start) {
  1081. end = strchr(start, ' ');
  1082. strncpy(buf, cmdline, (start - cmdline + 8));
  1083. if (end)
  1084. strcpy(buf + (start - cmdline + 8), end);
  1085. else
  1086. buf[start - cmdline + 8] = '\0';
  1087. } else {
  1088. strcpy(buf, cmdline);
  1089. strcat(buf, " console=");
  1090. }
  1091. } else {
  1092. strcpy(buf, "console=");
  1093. }
  1094. setenv("bootargs", buf);
  1095. debug("after silent fix-up: %s\n", buf);
  1096. }
  1097. #endif /* CONFIG_SILENT_CONSOLE */
  1098. /*******************************************************************/
  1099. /* OS booting routines */
  1100. /*******************************************************************/
  1101. #ifdef CONFIG_BOOTM_NETBSD
  1102. static int do_bootm_netbsd(int flag, int argc, char * const argv[],
  1103. bootm_headers_t *images)
  1104. {
  1105. void (*loader)(bd_t *, image_header_t *, char *, char *);
  1106. image_header_t *os_hdr, *hdr;
  1107. ulong kernel_data, kernel_len;
  1108. char *consdev;
  1109. char *cmdline;
  1110. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1111. return 1;
  1112. #if defined(CONFIG_FIT)
  1113. if (!images->legacy_hdr_valid) {
  1114. fit_unsupported_reset("NetBSD");
  1115. return 1;
  1116. }
  1117. #endif
  1118. hdr = images->legacy_hdr_os;
  1119. /*
  1120. * Booting a (NetBSD) kernel image
  1121. *
  1122. * This process is pretty similar to a standalone application:
  1123. * The (first part of an multi-) image must be a stage-2 loader,
  1124. * which in turn is responsible for loading & invoking the actual
  1125. * kernel. The only differences are the parameters being passed:
  1126. * besides the board info strucure, the loader expects a command
  1127. * line, the name of the console device, and (optionally) the
  1128. * address of the original image header.
  1129. */
  1130. os_hdr = NULL;
  1131. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  1132. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  1133. if (kernel_len)
  1134. os_hdr = hdr;
  1135. }
  1136. consdev = "";
  1137. #if defined(CONFIG_8xx_CONS_SMC1)
  1138. consdev = "smc1";
  1139. #elif defined(CONFIG_8xx_CONS_SMC2)
  1140. consdev = "smc2";
  1141. #elif defined(CONFIG_8xx_CONS_SCC2)
  1142. consdev = "scc2";
  1143. #elif defined(CONFIG_8xx_CONS_SCC3)
  1144. consdev = "scc3";
  1145. #endif
  1146. if (argc > 2) {
  1147. ulong len;
  1148. int i;
  1149. for (i = 2, len = 0; i < argc; i += 1)
  1150. len += strlen(argv[i]) + 1;
  1151. cmdline = malloc(len);
  1152. for (i = 2, len = 0; i < argc; i += 1) {
  1153. if (i > 2)
  1154. cmdline[len++] = ' ';
  1155. strcpy(&cmdline[len], argv[i]);
  1156. len += strlen(argv[i]);
  1157. }
  1158. } else if ((cmdline = getenv("bootargs")) == NULL) {
  1159. cmdline = "";
  1160. }
  1161. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  1162. printf("## Transferring control to NetBSD stage-2 loader "
  1163. "(at address %08lx) ...\n",
  1164. (ulong)loader);
  1165. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1166. /*
  1167. * NetBSD Stage-2 Loader Parameters:
  1168. * r3: ptr to board info data
  1169. * r4: image address
  1170. * r5: console device
  1171. * r6: boot args string
  1172. */
  1173. (*loader)(gd->bd, os_hdr, consdev, cmdline);
  1174. return 1;
  1175. }
  1176. #endif /* CONFIG_BOOTM_NETBSD*/
  1177. #ifdef CONFIG_LYNXKDI
  1178. static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
  1179. bootm_headers_t *images)
  1180. {
  1181. image_header_t *hdr = &images->legacy_hdr_os_copy;
  1182. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1183. return 1;
  1184. #if defined(CONFIG_FIT)
  1185. if (!images->legacy_hdr_valid) {
  1186. fit_unsupported_reset("Lynx");
  1187. return 1;
  1188. }
  1189. #endif
  1190. lynxkdi_boot((image_header_t *)hdr);
  1191. return 1;
  1192. }
  1193. #endif /* CONFIG_LYNXKDI */
  1194. #ifdef CONFIG_BOOTM_RTEMS
  1195. static int do_bootm_rtems(int flag, int argc, char * const argv[],
  1196. bootm_headers_t *images)
  1197. {
  1198. void (*entry_point)(bd_t *);
  1199. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1200. return 1;
  1201. #if defined(CONFIG_FIT)
  1202. if (!images->legacy_hdr_valid) {
  1203. fit_unsupported_reset("RTEMS");
  1204. return 1;
  1205. }
  1206. #endif
  1207. entry_point = (void (*)(bd_t *))images->ep;
  1208. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  1209. (ulong)entry_point);
  1210. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1211. /*
  1212. * RTEMS Parameters:
  1213. * r3: ptr to board info data
  1214. */
  1215. (*entry_point)(gd->bd);
  1216. return 1;
  1217. }
  1218. #endif /* CONFIG_BOOTM_RTEMS */
  1219. #if defined(CONFIG_BOOTM_OSE)
  1220. static int do_bootm_ose(int flag, int argc, char * const argv[],
  1221. bootm_headers_t *images)
  1222. {
  1223. void (*entry_point)(void);
  1224. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1225. return 1;
  1226. #if defined(CONFIG_FIT)
  1227. if (!images->legacy_hdr_valid) {
  1228. fit_unsupported_reset("OSE");
  1229. return 1;
  1230. }
  1231. #endif
  1232. entry_point = (void (*)(void))images->ep;
  1233. printf("## Transferring control to OSE (at address %08lx) ...\n",
  1234. (ulong)entry_point);
  1235. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1236. /*
  1237. * OSE Parameters:
  1238. * None
  1239. */
  1240. (*entry_point)();
  1241. return 1;
  1242. }
  1243. #endif /* CONFIG_BOOTM_OSE */
  1244. #if defined(CONFIG_CMD_ELF)
  1245. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  1246. bootm_headers_t *images)
  1247. {
  1248. char str[80];
  1249. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1250. return 1;
  1251. #if defined(CONFIG_FIT)
  1252. if (!images->legacy_hdr_valid) {
  1253. fit_unsupported_reset("VxWorks");
  1254. return 1;
  1255. }
  1256. #endif
  1257. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1258. setenv("loadaddr", str);
  1259. do_bootvx(NULL, 0, 0, NULL);
  1260. return 1;
  1261. }
  1262. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  1263. bootm_headers_t *images)
  1264. {
  1265. char *local_args[2];
  1266. char str[16];
  1267. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1268. return 1;
  1269. #if defined(CONFIG_FIT)
  1270. if (!images->legacy_hdr_valid) {
  1271. fit_unsupported_reset("QNX");
  1272. return 1;
  1273. }
  1274. #endif
  1275. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1276. local_args[0] = argv[0];
  1277. local_args[1] = str; /* and provide it via the arguments */
  1278. do_bootelf(NULL, 0, 2, local_args);
  1279. return 1;
  1280. }
  1281. #endif
  1282. #ifdef CONFIG_INTEGRITY
  1283. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  1284. bootm_headers_t *images)
  1285. {
  1286. void (*entry_point)(void);
  1287. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1288. return 1;
  1289. #if defined(CONFIG_FIT)
  1290. if (!images->legacy_hdr_valid) {
  1291. fit_unsupported_reset("INTEGRITY");
  1292. return 1;
  1293. }
  1294. #endif
  1295. entry_point = (void (*)(void))images->ep;
  1296. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  1297. (ulong)entry_point);
  1298. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1299. /*
  1300. * INTEGRITY Parameters:
  1301. * None
  1302. */
  1303. (*entry_point)();
  1304. return 1;
  1305. }
  1306. #endif
  1307. #ifdef CONFIG_CMD_BOOTZ
  1308. static int __bootz_setup(void *image, void **start, void **end)
  1309. {
  1310. /* Please define bootz_setup() for your platform */
  1311. puts("Your platform's zImage format isn't supported yet!\n");
  1312. return -1;
  1313. }
  1314. int bootz_setup(void *image, void **start, void **end)
  1315. __attribute__((weak, alias("__bootz_setup")));
  1316. /*
  1317. * zImage booting support
  1318. */
  1319. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  1320. char * const argv[], bootm_headers_t *images)
  1321. {
  1322. int ret;
  1323. void *zi_start, *zi_end;
  1324. memset(images, 0, sizeof(bootm_headers_t));
  1325. boot_start_lmb(images);
  1326. /* Setup Linux kernel zImage entry point */
  1327. if (argc < 2) {
  1328. images->ep = load_addr;
  1329. debug("* kernel: default image load address = 0x%08lx\n",
  1330. load_addr);
  1331. } else {
  1332. images->ep = simple_strtoul(argv[1], NULL, 16);
  1333. debug("* kernel: cmdline image address = 0x%08lx\n",
  1334. images->ep);
  1335. }
  1336. ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
  1337. if (ret != 0)
  1338. return 1;
  1339. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  1340. /* Find ramdisk */
  1341. ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
  1342. &images->rd_start, &images->rd_end);
  1343. if (ret) {
  1344. puts("Ramdisk image is corrupt or invalid\n");
  1345. return 1;
  1346. }
  1347. #if defined(CONFIG_OF_LIBFDT)
  1348. /* find flattened device tree */
  1349. ret = boot_get_fdt(flag, argc, argv, images,
  1350. &images->ft_addr, &images->ft_len);
  1351. if (ret) {
  1352. puts("Could not find a valid device tree\n");
  1353. return 1;
  1354. }
  1355. set_working_fdt_addr(images->ft_addr);
  1356. #endif
  1357. return 0;
  1358. }
  1359. static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1360. {
  1361. bootm_headers_t images;
  1362. if (bootz_start(cmdtp, flag, argc, argv, &images))
  1363. return 1;
  1364. /*
  1365. * We have reached the point of no return: we are going to
  1366. * overwrite all exception vector code, so we cannot easily
  1367. * recover from any failures any more...
  1368. */
  1369. disable_interrupts();
  1370. #if defined(CONFIG_CMD_USB)
  1371. /*
  1372. * turn off USB to prevent the host controller from writing to the
  1373. * SDRAM while Linux is booting. This could happen (at least for OHCI
  1374. * controller), because the HCCA (Host Controller Communication Area)
  1375. * lies within the SDRAM and the host controller writes continously to
  1376. * this area (as busmaster!). The HccaFrameNumber is for example
  1377. * updated every 1 ms within the HCCA structure in SDRAM! For more
  1378. * details see the OpenHCI specification.
  1379. */
  1380. usb_stop();
  1381. #endif
  1382. #ifdef CONFIG_SILENT_CONSOLE
  1383. fixup_silent_linux();
  1384. #endif
  1385. arch_preboot_os();
  1386. do_bootm_linux(0, argc, argv, &images);
  1387. #ifdef DEBUG
  1388. puts("\n## Control returned to monitor - resetting...\n");
  1389. #endif
  1390. do_reset(cmdtp, flag, argc, argv);
  1391. return 1;
  1392. }
  1393. U_BOOT_CMD(
  1394. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  1395. "boot Linux zImage image from memory",
  1396. "[addr [initrd[:size]] [fdt]]\n"
  1397. " - boot Linux zImage stored in memory\n"
  1398. "\tThe argument 'initrd' is optional and specifies the address\n"
  1399. "\tof the initrd in memory. The optional argument ':size' allows\n"
  1400. "\tspecifying the size of RAW initrd.\n"
  1401. #if defined(CONFIG_OF_LIBFDT)
  1402. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  1403. "\ta third argument is required which is the address of the\n"
  1404. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  1405. "\tuse a '-' for the second argument. If you do not pass a third\n"
  1406. "\ta bd_info struct will be passed instead\n"
  1407. #endif
  1408. );
  1409. #endif /* CONFIG_CMD_BOOTZ */