cmd_bootm.c 45 KB

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