cmd_bootm.c 46 KB

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