cmd_bootm.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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. * Execute selected states of the bootm command.
  476. *
  477. * Note the arguments to this state must be the first argument, Any 'bootm'
  478. * or sub-command arguments must have already been taken.
  479. *
  480. * Note that if states contains more than one flag it MUST contain
  481. * BOOTM_STATE_START, since this handles and consumes the command line args.
  482. *
  483. * Also note that aside from boot_os_fn functions and bootm_load_os no other
  484. * functions we store the return value of in 'ret' may use a negative return
  485. * value, without special handling.
  486. *
  487. * @param cmdtp Pointer to bootm command table entry
  488. * @param flag Command flags (CMD_FLAG_...)
  489. * @param argc Number of subcommand arguments (0 = no arguments)
  490. * @param argv Arguments
  491. * @param states Mask containing states to run (BOOTM_STATE_...)
  492. * @param images Image header information
  493. * @param boot_progress 1 to show boot progress, 0 to not do this
  494. * @return 0 if ok, something else on error. Some errors will cause this
  495. * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
  496. * then the intent is to boot an OS, so this function will not return
  497. * unless the image type is standalone.
  498. */
  499. static int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc,
  500. char * const argv[], int states, bootm_headers_t *images,
  501. int boot_progress)
  502. {
  503. boot_os_fn *boot_fn;
  504. ulong iflag = 0;
  505. int ret = 0;
  506. images->state |= states;
  507. /*
  508. * Work through the states and see how far we get. We stop on
  509. * any error.
  510. */
  511. if (states & BOOTM_STATE_START)
  512. ret = bootm_start(cmdtp, flag, argc, argv);
  513. if (!ret && (states & BOOTM_STATE_FINDOS))
  514. ret = bootm_find_os(cmdtp, flag, argc, argv);
  515. if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
  516. ret = bootm_find_other(cmdtp, flag, argc, argv);
  517. argc = 0; /* consume the args */
  518. }
  519. /*
  520. * We have reached the point of no return: we are going to
  521. * overwrite all exception vector code, so we cannot easily
  522. * recover from any failures any more...
  523. */
  524. iflag = disable_interrupts();
  525. #ifdef CONFIG_NETCONSOLE
  526. /* Stop the ethernet stack if NetConsole could have left it up */
  527. eth_halt();
  528. #endif
  529. #if defined(CONFIG_CMD_USB)
  530. /*
  531. * turn off USB to prevent the host controller from writing to the
  532. * SDRAM while Linux is booting. This could happen (at least for OHCI
  533. * controller), because the HCCA (Host Controller Communication Area)
  534. * lies within the SDRAM and the host controller writes continously to
  535. * this area (as busmaster!). The HccaFrameNumber is for example
  536. * updated every 1 ms within the HCCA structure in SDRAM! For more
  537. * details see the OpenHCI specification.
  538. */
  539. usb_stop();
  540. #endif
  541. /* Load the OS */
  542. if (!ret && (states & BOOTM_STATE_LOADOS)) {
  543. ulong load_end;
  544. ret = bootm_load_os(images, &load_end, 0);
  545. if (ret && ret != BOOTM_ERR_OVERLAP)
  546. goto err;
  547. if (ret == 0)
  548. lmb_reserve(&images->lmb, images->os.load,
  549. (load_end - images->os.load));
  550. else if (ret == BOOTM_ERR_OVERLAP)
  551. ret = 0;
  552. }
  553. /* Relocate the ramdisk */
  554. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  555. if (!ret && (states & BOOTM_STATE_RAMDISK)) {
  556. ulong rd_len = images->rd_end - images->rd_start;
  557. ret = boot_ramdisk_high(&images->lmb, images->rd_start,
  558. rd_len, &images->initrd_start, &images->initrd_end);
  559. if (!ret) {
  560. setenv_hex("initrd_start", images->initrd_start);
  561. setenv_hex("initrd_end", images->initrd_end);
  562. }
  563. }
  564. #endif
  565. #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
  566. if (!ret && (states & BOOTM_STATE_FDT)) {
  567. boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
  568. ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
  569. &images->ft_len);
  570. }
  571. #endif
  572. /* From now on, we need the OS boot function */
  573. if (ret)
  574. return ret;
  575. boot_fn = boot_os[images->os.os];
  576. if (boot_fn == NULL) {
  577. if (iflag)
  578. enable_interrupts();
  579. printf("ERROR: booting os '%s' (%d) is not supported\n",
  580. genimg_get_os_name(images->os.os), images->os.os);
  581. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  582. return 1;
  583. }
  584. /* Call various other states that are not generally used */
  585. if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
  586. ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
  587. if (!ret && (states & BOOTM_STATE_OS_BD_T))
  588. ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
  589. if (!ret && (states & BOOTM_STATE_OS_PREP))
  590. ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
  591. /* Check for unsupported subcommand. */
  592. if (ret) {
  593. puts("subcommand not supported\n");
  594. return ret;
  595. }
  596. #ifdef CONFIG_TRACE
  597. /* Pretend to run the OS, then run a user command */
  598. if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
  599. char *cmd_list = getenv("fakegocmd");
  600. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
  601. images, boot_fn);
  602. if (!ret && cmd_list)
  603. ret = run_command_list(cmd_list, -1, flag);
  604. }
  605. #endif
  606. /* Now run the OS! We hope this doesn't return */
  607. if (!ret && (states & BOOTM_STATE_OS_GO)) {
  608. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
  609. images, boot_fn);
  610. if (ret)
  611. goto err;
  612. }
  613. return ret;
  614. /* Deal with any fallout */
  615. err:
  616. if (iflag)
  617. enable_interrupts();
  618. if (ret == BOOTM_ERR_UNIMPLEMENTED)
  619. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  620. else if (ret == BOOTM_ERR_RESET)
  621. do_reset(cmdtp, flag, argc, argv);
  622. return ret;
  623. }
  624. static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
  625. char * const argv[])
  626. {
  627. int ret = 0;
  628. long state;
  629. cmd_tbl_t *c;
  630. c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
  631. argc--; argv++;
  632. if (c) {
  633. state = (long)c->cmd;
  634. if (state == BOOTM_STATE_START)
  635. state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
  636. } else {
  637. /* Unrecognized command */
  638. return CMD_RET_USAGE;
  639. }
  640. if (state != BOOTM_STATE_START && images.state >= state) {
  641. printf("Trying to execute a command out of order\n");
  642. return CMD_RET_USAGE;
  643. }
  644. ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
  645. return ret;
  646. }
  647. /*******************************************************************/
  648. /* bootm - boot application image from image in memory */
  649. /*******************************************************************/
  650. int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  651. {
  652. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  653. static int relocated = 0;
  654. if (!relocated) {
  655. int i;
  656. /* relocate boot function table */
  657. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  658. if (boot_os[i] != NULL)
  659. boot_os[i] += gd->reloc_off;
  660. /* relocate names of sub-command table */
  661. for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
  662. cmd_bootm_sub[i].name += gd->reloc_off;
  663. relocated = 1;
  664. }
  665. #endif
  666. /* determine if we have a sub command */
  667. argc--; argv++;
  668. if (argc > 0) {
  669. char *endp;
  670. simple_strtoul(argv[0], &endp, 16);
  671. /* endp pointing to NULL means that argv[0] was just a
  672. * valid number, pass it along to the normal bootm processing
  673. *
  674. * If endp is ':' or '#' assume a FIT identifier so pass
  675. * along for normal processing.
  676. *
  677. * Right now we assume the first arg should never be '-'
  678. */
  679. if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
  680. return do_bootm_subcommand(cmdtp, flag, argc, argv);
  681. }
  682. return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
  683. BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
  684. BOOTM_STATE_LOADOS | BOOTM_STATE_OS_PREP |
  685. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO, &images, 1);
  686. }
  687. int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  688. {
  689. const char *ep = getenv("autostart");
  690. if (ep && !strcmp(ep, "yes")) {
  691. char *local_args[2];
  692. local_args[0] = (char *)cmd;
  693. local_args[1] = NULL;
  694. printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
  695. return do_bootm(cmdtp, 0, 1, local_args);
  696. }
  697. return 0;
  698. }
  699. /**
  700. * image_get_kernel - verify legacy format kernel image
  701. * @img_addr: in RAM address of the legacy format image to be verified
  702. * @verify: data CRC verification flag
  703. *
  704. * image_get_kernel() verifies legacy image integrity and returns pointer to
  705. * legacy image header if image verification was completed successfully.
  706. *
  707. * returns:
  708. * pointer to a legacy image header if valid image was found
  709. * otherwise return NULL
  710. */
  711. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  712. {
  713. image_header_t *hdr = (image_header_t *)img_addr;
  714. if (!image_check_magic(hdr)) {
  715. puts("Bad Magic Number\n");
  716. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  717. return NULL;
  718. }
  719. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  720. if (!image_check_hcrc(hdr)) {
  721. puts("Bad Header Checksum\n");
  722. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  723. return NULL;
  724. }
  725. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  726. image_print_contents(hdr);
  727. if (verify) {
  728. puts(" Verifying Checksum ... ");
  729. if (!image_check_dcrc(hdr)) {
  730. printf("Bad Data CRC\n");
  731. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  732. return NULL;
  733. }
  734. puts("OK\n");
  735. }
  736. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  737. if (!image_check_target_arch(hdr)) {
  738. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  739. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  740. return NULL;
  741. }
  742. return hdr;
  743. }
  744. /**
  745. * boot_get_kernel - find kernel image
  746. * @os_data: pointer to a ulong variable, will hold os data start address
  747. * @os_len: pointer to a ulong variable, will hold os data length
  748. *
  749. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  750. * and locates kernel data.
  751. *
  752. * returns:
  753. * pointer to image header if valid image was found, plus kernel start
  754. * address and length, otherwise NULL
  755. */
  756. static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  757. char * const argv[], bootm_headers_t *images, ulong *os_data,
  758. ulong *os_len)
  759. {
  760. image_header_t *hdr;
  761. ulong img_addr;
  762. const void *buf;
  763. #if defined(CONFIG_FIT)
  764. const char *fit_uname_config = NULL;
  765. const char *fit_uname_kernel = NULL;
  766. int os_noffset;
  767. #endif
  768. /* find out kernel image address */
  769. if (argc < 1) {
  770. img_addr = load_addr;
  771. debug("* kernel: default image load address = 0x%08lx\n",
  772. load_addr);
  773. #if defined(CONFIG_FIT)
  774. } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
  775. &fit_uname_config)) {
  776. debug("* kernel: config '%s' from image at 0x%08lx\n",
  777. fit_uname_config, img_addr);
  778. } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
  779. &fit_uname_kernel)) {
  780. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  781. fit_uname_kernel, img_addr);
  782. #endif
  783. } else {
  784. img_addr = simple_strtoul(argv[0], NULL, 16);
  785. debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
  786. }
  787. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  788. /* copy from dataflash if needed */
  789. img_addr = genimg_get_image(img_addr);
  790. /* check image type, for FIT images get FIT kernel node */
  791. *os_data = *os_len = 0;
  792. buf = map_sysmem(img_addr, 0);
  793. switch (genimg_get_format(buf)) {
  794. case IMAGE_FORMAT_LEGACY:
  795. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  796. img_addr);
  797. hdr = image_get_kernel(img_addr, images->verify);
  798. if (!hdr)
  799. return NULL;
  800. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  801. /* get os_data and os_len */
  802. switch (image_get_type(hdr)) {
  803. case IH_TYPE_KERNEL:
  804. case IH_TYPE_KERNEL_NOLOAD:
  805. *os_data = image_get_data(hdr);
  806. *os_len = image_get_data_size(hdr);
  807. break;
  808. case IH_TYPE_MULTI:
  809. image_multi_getimg(hdr, 0, os_data, os_len);
  810. break;
  811. case IH_TYPE_STANDALONE:
  812. *os_data = image_get_data(hdr);
  813. *os_len = image_get_data_size(hdr);
  814. break;
  815. default:
  816. printf("Wrong Image Type for %s command\n",
  817. cmdtp->name);
  818. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  819. return NULL;
  820. }
  821. /*
  822. * copy image header to allow for image overwrites during
  823. * kernel decompression.
  824. */
  825. memmove(&images->legacy_hdr_os_copy, hdr,
  826. sizeof(image_header_t));
  827. /* save pointer to image header */
  828. images->legacy_hdr_os = hdr;
  829. images->legacy_hdr_valid = 1;
  830. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  831. break;
  832. #if defined(CONFIG_FIT)
  833. case IMAGE_FORMAT_FIT:
  834. os_noffset = fit_image_load(images, FIT_KERNEL_PROP,
  835. img_addr,
  836. &fit_uname_kernel, fit_uname_config,
  837. IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
  838. BOOTSTAGE_ID_FIT_KERNEL_START,
  839. FIT_LOAD_IGNORED, os_data, os_len);
  840. if (os_noffset < 0)
  841. return NULL;
  842. images->fit_hdr_os = map_sysmem(img_addr, 0);
  843. images->fit_uname_os = fit_uname_kernel;
  844. images->fit_noffset_os = os_noffset;
  845. break;
  846. #endif
  847. default:
  848. printf("Wrong Image Format for %s command\n", cmdtp->name);
  849. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  850. return NULL;
  851. }
  852. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  853. *os_data, *os_len, *os_len);
  854. return buf;
  855. }
  856. #ifdef CONFIG_SYS_LONGHELP
  857. static char bootm_help_text[] =
  858. "[addr [arg ...]]\n - boot application image stored in memory\n"
  859. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  860. "\t'arg' can be the address of an initrd image\n"
  861. #if defined(CONFIG_OF_LIBFDT)
  862. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  863. "\ta third argument is required which is the address of the\n"
  864. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  865. "\tuse a '-' for the second argument. If you do not pass a third\n"
  866. "\ta bd_info struct will be passed instead\n"
  867. #endif
  868. #if defined(CONFIG_FIT)
  869. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  870. "\tmust be extened to include component or configuration unit name:\n"
  871. "\taddr:<subimg_uname> - direct component image specification\n"
  872. "\taddr#<conf_uname> - configuration specification\n"
  873. "\tUse iminfo command to get the list of existing component\n"
  874. "\timages and configurations.\n"
  875. #endif
  876. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  877. "must be\n"
  878. "issued in the order below (it's ok to not issue all sub-commands):\n"
  879. "\tstart [addr [arg ...]]\n"
  880. "\tloados - load OS image\n"
  881. #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
  882. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  883. #endif
  884. #if defined(CONFIG_OF_LIBFDT)
  885. "\tfdt - relocate flat device tree\n"
  886. #endif
  887. "\tcmdline - OS specific command line processing/setup\n"
  888. "\tbdt - OS specific bd_t processing\n"
  889. "\tprep - OS specific prep before relocation or go\n"
  890. "\tgo - start OS";
  891. #endif
  892. U_BOOT_CMD(
  893. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  894. "boot application image from memory", bootm_help_text
  895. );
  896. /*******************************************************************/
  897. /* bootd - boot default image */
  898. /*******************************************************************/
  899. #if defined(CONFIG_CMD_BOOTD)
  900. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  901. {
  902. int rcode = 0;
  903. if (run_command(getenv("bootcmd"), flag) < 0)
  904. rcode = 1;
  905. return rcode;
  906. }
  907. U_BOOT_CMD(
  908. boot, 1, 1, do_bootd,
  909. "boot default, i.e., run 'bootcmd'",
  910. ""
  911. );
  912. /* keep old command name "bootd" for backward compatibility */
  913. U_BOOT_CMD(
  914. bootd, 1, 1, do_bootd,
  915. "boot default, i.e., run 'bootcmd'",
  916. ""
  917. );
  918. #endif
  919. /*******************************************************************/
  920. /* iminfo - print header info for a requested image */
  921. /*******************************************************************/
  922. #if defined(CONFIG_CMD_IMI)
  923. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  924. {
  925. int arg;
  926. ulong addr;
  927. int rcode = 0;
  928. if (argc < 2) {
  929. return image_info(load_addr);
  930. }
  931. for (arg = 1; arg < argc; ++arg) {
  932. addr = simple_strtoul(argv[arg], NULL, 16);
  933. if (image_info(addr) != 0)
  934. rcode = 1;
  935. }
  936. return rcode;
  937. }
  938. static int image_info(ulong addr)
  939. {
  940. void *hdr = (void *)addr;
  941. printf("\n## Checking Image at %08lx ...\n", addr);
  942. switch (genimg_get_format(hdr)) {
  943. case IMAGE_FORMAT_LEGACY:
  944. puts(" Legacy image found\n");
  945. if (!image_check_magic(hdr)) {
  946. puts(" Bad Magic Number\n");
  947. return 1;
  948. }
  949. if (!image_check_hcrc(hdr)) {
  950. puts(" Bad Header Checksum\n");
  951. return 1;
  952. }
  953. image_print_contents(hdr);
  954. puts(" Verifying Checksum ... ");
  955. if (!image_check_dcrc(hdr)) {
  956. puts(" Bad Data CRC\n");
  957. return 1;
  958. }
  959. puts("OK\n");
  960. return 0;
  961. #if defined(CONFIG_FIT)
  962. case IMAGE_FORMAT_FIT:
  963. puts(" FIT image found\n");
  964. if (!fit_check_format(hdr)) {
  965. puts("Bad FIT image format!\n");
  966. return 1;
  967. }
  968. fit_print_contents(hdr);
  969. if (!fit_all_image_verify(hdr)) {
  970. puts("Bad hash in FIT image!\n");
  971. return 1;
  972. }
  973. return 0;
  974. #endif
  975. default:
  976. puts("Unknown image format!\n");
  977. break;
  978. }
  979. return 1;
  980. }
  981. U_BOOT_CMD(
  982. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  983. "print header information for application image",
  984. "addr [addr ...]\n"
  985. " - print header information for application image starting at\n"
  986. " address 'addr' in memory; this includes verification of the\n"
  987. " image contents (magic number, header and payload checksums)"
  988. );
  989. #endif
  990. /*******************************************************************/
  991. /* imls - list all images found in flash */
  992. /*******************************************************************/
  993. #if defined(CONFIG_CMD_IMLS)
  994. static int do_imls_nor(void)
  995. {
  996. flash_info_t *info;
  997. int i, j;
  998. void *hdr;
  999. for (i = 0, info = &flash_info[0];
  1000. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  1001. if (info->flash_id == FLASH_UNKNOWN)
  1002. goto next_bank;
  1003. for (j = 0; j < info->sector_count; ++j) {
  1004. hdr = (void *)info->start[j];
  1005. if (!hdr)
  1006. goto next_sector;
  1007. switch (genimg_get_format(hdr)) {
  1008. case IMAGE_FORMAT_LEGACY:
  1009. if (!image_check_hcrc(hdr))
  1010. goto next_sector;
  1011. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  1012. image_print_contents(hdr);
  1013. puts(" Verifying Checksum ... ");
  1014. if (!image_check_dcrc(hdr)) {
  1015. puts("Bad Data CRC\n");
  1016. } else {
  1017. puts("OK\n");
  1018. }
  1019. break;
  1020. #if defined(CONFIG_FIT)
  1021. case IMAGE_FORMAT_FIT:
  1022. if (!fit_check_format(hdr))
  1023. goto next_sector;
  1024. printf("FIT Image at %08lX:\n", (ulong)hdr);
  1025. fit_print_contents(hdr);
  1026. break;
  1027. #endif
  1028. default:
  1029. goto next_sector;
  1030. }
  1031. next_sector: ;
  1032. }
  1033. next_bank: ;
  1034. }
  1035. return 0;
  1036. }
  1037. #endif
  1038. #if defined(CONFIG_CMD_IMLS_NAND)
  1039. static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
  1040. size_t len)
  1041. {
  1042. void *imgdata;
  1043. int ret;
  1044. imgdata = malloc(len);
  1045. if (!imgdata) {
  1046. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  1047. nand_dev, off);
  1048. printf(" Low memory(cannot allocate memory for image)\n");
  1049. return -ENOMEM;
  1050. }
  1051. ret = nand_read_skip_bad(nand, off, &len,
  1052. imgdata);
  1053. if (ret < 0 && ret != -EUCLEAN) {
  1054. free(imgdata);
  1055. return ret;
  1056. }
  1057. if (!image_check_hcrc(imgdata)) {
  1058. free(imgdata);
  1059. return 0;
  1060. }
  1061. printf("Legacy Image at NAND device %d offset %08llX:\n",
  1062. nand_dev, off);
  1063. image_print_contents(imgdata);
  1064. puts(" Verifying Checksum ... ");
  1065. if (!image_check_dcrc(imgdata))
  1066. puts("Bad Data CRC\n");
  1067. else
  1068. puts("OK\n");
  1069. free(imgdata);
  1070. return 0;
  1071. }
  1072. static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
  1073. size_t len)
  1074. {
  1075. void *imgdata;
  1076. int ret;
  1077. imgdata = malloc(len);
  1078. if (!imgdata) {
  1079. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  1080. nand_dev, off);
  1081. printf(" Low memory(cannot allocate memory for image)\n");
  1082. return -ENOMEM;
  1083. }
  1084. ret = nand_read_skip_bad(nand, off, &len,
  1085. imgdata);
  1086. if (ret < 0 && ret != -EUCLEAN) {
  1087. free(imgdata);
  1088. return ret;
  1089. }
  1090. if (!fit_check_format(imgdata)) {
  1091. free(imgdata);
  1092. return 0;
  1093. }
  1094. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  1095. fit_print_contents(imgdata);
  1096. free(imgdata);
  1097. return 0;
  1098. }
  1099. static int do_imls_nand(void)
  1100. {
  1101. nand_info_t *nand;
  1102. int nand_dev = nand_curr_device;
  1103. size_t len;
  1104. loff_t off;
  1105. u32 buffer[16];
  1106. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  1107. puts("\nNo NAND devices available\n");
  1108. return -ENODEV;
  1109. }
  1110. printf("\n");
  1111. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  1112. nand = &nand_info[nand_dev];
  1113. if (!nand->name || !nand->size)
  1114. continue;
  1115. for (off = 0; off < nand->size; off += nand->erasesize) {
  1116. const image_header_t *header;
  1117. int ret;
  1118. if (nand_block_isbad(nand, off))
  1119. continue;
  1120. len = sizeof(buffer);
  1121. ret = nand_read(nand, off, &len, (u8 *)buffer);
  1122. if (ret < 0 && ret != -EUCLEAN) {
  1123. printf("NAND read error %d at offset %08llX\n",
  1124. ret, off);
  1125. continue;
  1126. }
  1127. switch (genimg_get_format(buffer)) {
  1128. case IMAGE_FORMAT_LEGACY:
  1129. header = (const image_header_t *)buffer;
  1130. len = image_get_image_size(header);
  1131. nand_imls_legacyimage(nand, nand_dev, off, len);
  1132. break;
  1133. #if defined(CONFIG_FIT)
  1134. case IMAGE_FORMAT_FIT:
  1135. len = fit_get_size(buffer);
  1136. nand_imls_fitimage(nand, nand_dev, off, len);
  1137. break;
  1138. #endif
  1139. }
  1140. }
  1141. }
  1142. return 0;
  1143. }
  1144. #endif
  1145. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  1146. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1147. {
  1148. int ret_nor = 0, ret_nand = 0;
  1149. #if defined(CONFIG_CMD_IMLS)
  1150. ret_nor = do_imls_nor();
  1151. #endif
  1152. #if defined(CONFIG_CMD_IMLS_NAND)
  1153. ret_nand = do_imls_nand();
  1154. #endif
  1155. if (ret_nor)
  1156. return ret_nor;
  1157. if (ret_nand)
  1158. return ret_nand;
  1159. return (0);
  1160. }
  1161. U_BOOT_CMD(
  1162. imls, 1, 1, do_imls,
  1163. "list all images found in flash",
  1164. "\n"
  1165. " - Prints information about all images found at sector/block\n"
  1166. " boundaries in nor/nand flash."
  1167. );
  1168. #endif
  1169. /*******************************************************************/
  1170. /* helper routines */
  1171. /*******************************************************************/
  1172. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  1173. #define CONSOLE_ARG "console="
  1174. #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
  1175. static void fixup_silent_linux(void)
  1176. {
  1177. char *buf;
  1178. const char *env_val;
  1179. char *cmdline = getenv("bootargs");
  1180. /* Only fix cmdline when requested */
  1181. if (!(gd->flags & GD_FLG_SILENT))
  1182. return;
  1183. debug("before silent fix-up: %s\n", cmdline);
  1184. if (cmdline && (cmdline[0] != '\0')) {
  1185. char *start = strstr(cmdline, CONSOLE_ARG);
  1186. /* Allocate space for maximum possible new command line */
  1187. buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
  1188. if (!buf) {
  1189. debug("%s: out of memory\n", __func__);
  1190. return;
  1191. }
  1192. if (start) {
  1193. char *end = strchr(start, ' ');
  1194. int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
  1195. strncpy(buf, cmdline, num_start_bytes);
  1196. if (end)
  1197. strcpy(buf + num_start_bytes, end);
  1198. else
  1199. buf[num_start_bytes] = '\0';
  1200. } else {
  1201. sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
  1202. }
  1203. env_val = buf;
  1204. } else {
  1205. buf = NULL;
  1206. env_val = CONSOLE_ARG;
  1207. }
  1208. setenv("bootargs", env_val);
  1209. debug("after silent fix-up: %s\n", env_val);
  1210. free(buf);
  1211. }
  1212. #endif /* CONFIG_SILENT_CONSOLE */
  1213. #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
  1214. static void copy_args(char *dest, int argc, char * const argv[], char delim)
  1215. {
  1216. int i;
  1217. for (i = 0; i < argc; i++) {
  1218. if (i > 0)
  1219. *dest++ = delim;
  1220. strcpy(dest, argv[i]);
  1221. dest += strlen(argv[i]);
  1222. }
  1223. }
  1224. #endif
  1225. /*******************************************************************/
  1226. /* OS booting routines */
  1227. /*******************************************************************/
  1228. #ifdef CONFIG_BOOTM_NETBSD
  1229. static int do_bootm_netbsd(int flag, int argc, char * const argv[],
  1230. bootm_headers_t *images)
  1231. {
  1232. void (*loader)(bd_t *, image_header_t *, char *, char *);
  1233. image_header_t *os_hdr, *hdr;
  1234. ulong kernel_data, kernel_len;
  1235. char *consdev;
  1236. char *cmdline;
  1237. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1238. return 1;
  1239. #if defined(CONFIG_FIT)
  1240. if (!images->legacy_hdr_valid) {
  1241. fit_unsupported_reset("NetBSD");
  1242. return 1;
  1243. }
  1244. #endif
  1245. hdr = images->legacy_hdr_os;
  1246. /*
  1247. * Booting a (NetBSD) kernel image
  1248. *
  1249. * This process is pretty similar to a standalone application:
  1250. * The (first part of an multi-) image must be a stage-2 loader,
  1251. * which in turn is responsible for loading & invoking the actual
  1252. * kernel. The only differences are the parameters being passed:
  1253. * besides the board info strucure, the loader expects a command
  1254. * line, the name of the console device, and (optionally) the
  1255. * address of the original image header.
  1256. */
  1257. os_hdr = NULL;
  1258. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  1259. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  1260. if (kernel_len)
  1261. os_hdr = hdr;
  1262. }
  1263. consdev = "";
  1264. #if defined(CONFIG_8xx_CONS_SMC1)
  1265. consdev = "smc1";
  1266. #elif defined(CONFIG_8xx_CONS_SMC2)
  1267. consdev = "smc2";
  1268. #elif defined(CONFIG_8xx_CONS_SCC2)
  1269. consdev = "scc2";
  1270. #elif defined(CONFIG_8xx_CONS_SCC3)
  1271. consdev = "scc3";
  1272. #endif
  1273. if (argc > 0) {
  1274. ulong len;
  1275. int i;
  1276. for (i = 0, len = 0; i < argc; i += 1)
  1277. len += strlen(argv[i]) + 1;
  1278. cmdline = malloc(len);
  1279. copy_args(cmdline, argc, argv, ' ');
  1280. } else if ((cmdline = getenv("bootargs")) == NULL) {
  1281. cmdline = "";
  1282. }
  1283. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  1284. printf("## Transferring control to NetBSD stage-2 loader "
  1285. "(at address %08lx) ...\n",
  1286. (ulong)loader);
  1287. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1288. /*
  1289. * NetBSD Stage-2 Loader Parameters:
  1290. * r3: ptr to board info data
  1291. * r4: image address
  1292. * r5: console device
  1293. * r6: boot args string
  1294. */
  1295. (*loader)(gd->bd, os_hdr, consdev, cmdline);
  1296. return 1;
  1297. }
  1298. #endif /* CONFIG_BOOTM_NETBSD*/
  1299. #ifdef CONFIG_LYNXKDI
  1300. static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
  1301. bootm_headers_t *images)
  1302. {
  1303. image_header_t *hdr = &images->legacy_hdr_os_copy;
  1304. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1305. return 1;
  1306. #if defined(CONFIG_FIT)
  1307. if (!images->legacy_hdr_valid) {
  1308. fit_unsupported_reset("Lynx");
  1309. return 1;
  1310. }
  1311. #endif
  1312. lynxkdi_boot((image_header_t *)hdr);
  1313. return 1;
  1314. }
  1315. #endif /* CONFIG_LYNXKDI */
  1316. #ifdef CONFIG_BOOTM_RTEMS
  1317. static int do_bootm_rtems(int flag, int argc, char * const argv[],
  1318. bootm_headers_t *images)
  1319. {
  1320. void (*entry_point)(bd_t *);
  1321. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1322. return 1;
  1323. #if defined(CONFIG_FIT)
  1324. if (!images->legacy_hdr_valid) {
  1325. fit_unsupported_reset("RTEMS");
  1326. return 1;
  1327. }
  1328. #endif
  1329. entry_point = (void (*)(bd_t *))images->ep;
  1330. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  1331. (ulong)entry_point);
  1332. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1333. /*
  1334. * RTEMS Parameters:
  1335. * r3: ptr to board info data
  1336. */
  1337. (*entry_point)(gd->bd);
  1338. return 1;
  1339. }
  1340. #endif /* CONFIG_BOOTM_RTEMS */
  1341. #if defined(CONFIG_BOOTM_OSE)
  1342. static int do_bootm_ose(int flag, int argc, char * const argv[],
  1343. bootm_headers_t *images)
  1344. {
  1345. void (*entry_point)(void);
  1346. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1347. return 1;
  1348. #if defined(CONFIG_FIT)
  1349. if (!images->legacy_hdr_valid) {
  1350. fit_unsupported_reset("OSE");
  1351. return 1;
  1352. }
  1353. #endif
  1354. entry_point = (void (*)(void))images->ep;
  1355. printf("## Transferring control to OSE (at address %08lx) ...\n",
  1356. (ulong)entry_point);
  1357. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1358. /*
  1359. * OSE Parameters:
  1360. * None
  1361. */
  1362. (*entry_point)();
  1363. return 1;
  1364. }
  1365. #endif /* CONFIG_BOOTM_OSE */
  1366. #if defined(CONFIG_BOOTM_PLAN9)
  1367. static int do_bootm_plan9(int flag, int argc, char * const argv[],
  1368. bootm_headers_t *images)
  1369. {
  1370. void (*entry_point)(void);
  1371. char *s;
  1372. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1373. return 1;
  1374. #if defined(CONFIG_FIT)
  1375. if (!images->legacy_hdr_valid) {
  1376. fit_unsupported_reset("Plan 9");
  1377. return 1;
  1378. }
  1379. #endif
  1380. /* See README.plan9 */
  1381. s = getenv("confaddr");
  1382. if (s != NULL) {
  1383. char *confaddr = (char *)simple_strtoul(s, NULL, 16);
  1384. if (argc > 0) {
  1385. copy_args(confaddr, argc, argv, '\n');
  1386. } else {
  1387. s = getenv("bootargs");
  1388. if (s != NULL)
  1389. strcpy(confaddr, s);
  1390. }
  1391. }
  1392. entry_point = (void (*)(void))images->ep;
  1393. printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
  1394. (ulong)entry_point);
  1395. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1396. /*
  1397. * Plan 9 Parameters:
  1398. * None
  1399. */
  1400. (*entry_point)();
  1401. return 1;
  1402. }
  1403. #endif /* CONFIG_BOOTM_PLAN9 */
  1404. #if defined(CONFIG_CMD_ELF)
  1405. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  1406. bootm_headers_t *images)
  1407. {
  1408. char str[80];
  1409. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1410. return 1;
  1411. #if defined(CONFIG_FIT)
  1412. if (!images->legacy_hdr_valid) {
  1413. fit_unsupported_reset("VxWorks");
  1414. return 1;
  1415. }
  1416. #endif
  1417. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1418. setenv("loadaddr", str);
  1419. do_bootvx(NULL, 0, 0, NULL);
  1420. return 1;
  1421. }
  1422. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  1423. bootm_headers_t *images)
  1424. {
  1425. char *local_args[2];
  1426. char str[16];
  1427. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1428. return 1;
  1429. #if defined(CONFIG_FIT)
  1430. if (!images->legacy_hdr_valid) {
  1431. fit_unsupported_reset("QNX");
  1432. return 1;
  1433. }
  1434. #endif
  1435. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  1436. local_args[0] = argv[0];
  1437. local_args[1] = str; /* and provide it via the arguments */
  1438. do_bootelf(NULL, 0, 2, local_args);
  1439. return 1;
  1440. }
  1441. #endif
  1442. #ifdef CONFIG_INTEGRITY
  1443. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  1444. bootm_headers_t *images)
  1445. {
  1446. void (*entry_point)(void);
  1447. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  1448. return 1;
  1449. #if defined(CONFIG_FIT)
  1450. if (!images->legacy_hdr_valid) {
  1451. fit_unsupported_reset("INTEGRITY");
  1452. return 1;
  1453. }
  1454. #endif
  1455. entry_point = (void (*)(void))images->ep;
  1456. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  1457. (ulong)entry_point);
  1458. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  1459. /*
  1460. * INTEGRITY Parameters:
  1461. * None
  1462. */
  1463. (*entry_point)();
  1464. return 1;
  1465. }
  1466. #endif
  1467. #ifdef CONFIG_CMD_BOOTZ
  1468. static int __bootz_setup(void *image, void **start, void **end)
  1469. {
  1470. /* Please define bootz_setup() for your platform */
  1471. puts("Your platform's zImage format isn't supported yet!\n");
  1472. return -1;
  1473. }
  1474. int bootz_setup(void *image, void **start, void **end)
  1475. __attribute__((weak, alias("__bootz_setup")));
  1476. /*
  1477. * zImage booting support
  1478. */
  1479. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  1480. char * const argv[], bootm_headers_t *images)
  1481. {
  1482. int ret;
  1483. void *zi_start, *zi_end;
  1484. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  1485. images, 1);
  1486. /* Setup Linux kernel zImage entry point */
  1487. if (argc < 2) {
  1488. images->ep = load_addr;
  1489. debug("* kernel: default image load address = 0x%08lx\n",
  1490. load_addr);
  1491. } else {
  1492. images->ep = simple_strtoul(argv[1], NULL, 16);
  1493. debug("* kernel: cmdline image address = 0x%08lx\n",
  1494. images->ep);
  1495. }
  1496. ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
  1497. if (ret != 0)
  1498. return 1;
  1499. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  1500. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_FINDOTHER,
  1501. images, 1);
  1502. return ret;
  1503. }
  1504. int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1505. {
  1506. bootm_headers_t images;
  1507. int ret;
  1508. if (bootz_start(cmdtp, flag, argc, argv, &images))
  1509. return 1;
  1510. ret = do_bootm_states(cmdtp, flag, argc, argv,
  1511. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO,
  1512. &images, 1);
  1513. return ret;
  1514. }
  1515. #ifdef CONFIG_SYS_LONGHELP
  1516. static char bootz_help_text[] =
  1517. "[addr [initrd[:size]] [fdt]]\n"
  1518. " - boot Linux zImage stored in memory\n"
  1519. "\tThe argument 'initrd' is optional and specifies the address\n"
  1520. "\tof the initrd in memory. The optional argument ':size' allows\n"
  1521. "\tspecifying the size of RAW initrd.\n"
  1522. #if defined(CONFIG_OF_LIBFDT)
  1523. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  1524. "\ta third argument is required which is the address of the\n"
  1525. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  1526. "\tuse a '-' for the second argument. If you do not pass a third\n"
  1527. "\ta bd_info struct will be passed instead\n"
  1528. #endif
  1529. "";
  1530. #endif
  1531. U_BOOT_CMD(
  1532. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  1533. "boot Linux zImage image from memory", bootz_help_text
  1534. );
  1535. #endif /* CONFIG_CMD_BOOTZ */