cmd_bootm.c 46 KB

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