bootm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef USE_HOSTCC
  8. #include <common.h>
  9. #include <bootstage.h>
  10. #include <bzlib.h>
  11. #include <errno.h>
  12. #include <fdt_support.h>
  13. #include <lmb.h>
  14. #include <malloc.h>
  15. #include <mapmem.h>
  16. #include <asm/io.h>
  17. #include <linux/lzo.h>
  18. #include <lzma/LzmaTypes.h>
  19. #include <lzma/LzmaDec.h>
  20. #include <lzma/LzmaTools.h>
  21. #if defined(CONFIG_CMD_USB)
  22. #include <usb.h>
  23. #endif
  24. #else
  25. #include "mkimage.h"
  26. #endif
  27. #include <command.h>
  28. #include <bootm.h>
  29. #include <image.h>
  30. #ifndef CONFIG_SYS_BOOTM_LEN
  31. /* use 8MByte as default max gunzip size */
  32. #define CONFIG_SYS_BOOTM_LEN 0x800000
  33. #endif
  34. #define IH_INITRD_ARCH IH_ARCH_DEFAULT
  35. #ifndef USE_HOSTCC
  36. DECLARE_GLOBAL_DATA_PTR;
  37. bootm_headers_t images; /* pointers to os/initrd/fdt images */
  38. static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  39. char * const argv[], bootm_headers_t *images,
  40. ulong *os_data, ulong *os_len);
  41. #ifdef CONFIG_LMB
  42. static void boot_start_lmb(bootm_headers_t *images)
  43. {
  44. ulong mem_start;
  45. phys_size_t mem_size;
  46. lmb_init(&images->lmb);
  47. mem_start = env_get_bootm_low();
  48. mem_size = env_get_bootm_size();
  49. lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
  50. arch_lmb_reserve(&images->lmb);
  51. board_lmb_reserve(&images->lmb);
  52. }
  53. #else
  54. #define lmb_reserve(lmb, base, size)
  55. static inline void boot_start_lmb(bootm_headers_t *images) { }
  56. #endif
  57. static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc,
  58. char * const argv[])
  59. {
  60. memset((void *)&images, 0, sizeof(images));
  61. images.verify = env_get_yesno("verify");
  62. boot_start_lmb(&images);
  63. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
  64. images.state = BOOTM_STATE_START;
  65. return 0;
  66. }
  67. static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
  68. char * const argv[])
  69. {
  70. const void *os_hdr;
  71. bool ep_found = false;
  72. int ret;
  73. /* get kernel image header, start address and length */
  74. os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
  75. &images, &images.os.image_start, &images.os.image_len);
  76. if (images.os.image_len == 0) {
  77. puts("ERROR: can't get kernel image!\n");
  78. return 1;
  79. }
  80. /* get image parameters */
  81. switch (genimg_get_format(os_hdr)) {
  82. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  83. case IMAGE_FORMAT_LEGACY:
  84. images.os.type = image_get_type(os_hdr);
  85. images.os.comp = image_get_comp(os_hdr);
  86. images.os.os = image_get_os(os_hdr);
  87. images.os.end = image_get_image_end(os_hdr);
  88. images.os.load = image_get_load(os_hdr);
  89. images.os.arch = image_get_arch(os_hdr);
  90. break;
  91. #endif
  92. #if IMAGE_ENABLE_FIT
  93. case IMAGE_FORMAT_FIT:
  94. if (fit_image_get_type(images.fit_hdr_os,
  95. images.fit_noffset_os,
  96. &images.os.type)) {
  97. puts("Can't get image type!\n");
  98. bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
  99. return 1;
  100. }
  101. if (fit_image_get_comp(images.fit_hdr_os,
  102. images.fit_noffset_os,
  103. &images.os.comp)) {
  104. puts("Can't get image compression!\n");
  105. bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
  106. return 1;
  107. }
  108. if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
  109. &images.os.os)) {
  110. puts("Can't get image OS!\n");
  111. bootstage_error(BOOTSTAGE_ID_FIT_OS);
  112. return 1;
  113. }
  114. if (fit_image_get_arch(images.fit_hdr_os,
  115. images.fit_noffset_os,
  116. &images.os.arch)) {
  117. puts("Can't get image ARCH!\n");
  118. return 1;
  119. }
  120. images.os.end = fit_get_end(images.fit_hdr_os);
  121. if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
  122. &images.os.load)) {
  123. puts("Can't get image load address!\n");
  124. bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
  125. return 1;
  126. }
  127. break;
  128. #endif
  129. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  130. case IMAGE_FORMAT_ANDROID:
  131. images.os.type = IH_TYPE_KERNEL;
  132. images.os.comp = IH_COMP_NONE;
  133. images.os.os = IH_OS_LINUX;
  134. images.os.end = android_image_get_end(os_hdr);
  135. images.os.load = android_image_get_kload(os_hdr);
  136. images.ep = images.os.load;
  137. ep_found = true;
  138. break;
  139. #endif
  140. default:
  141. puts("ERROR: unknown image format type!\n");
  142. return 1;
  143. }
  144. /* If we have a valid setup.bin, we will use that for entry (x86) */
  145. if (images.os.arch == IH_ARCH_I386 ||
  146. images.os.arch == IH_ARCH_X86_64) {
  147. ulong len;
  148. ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
  149. if (ret < 0 && ret != -ENOENT) {
  150. puts("Could not find a valid setup.bin for x86\n");
  151. return 1;
  152. }
  153. /* Kernel entry point is the setup.bin */
  154. } else if (images.legacy_hdr_valid) {
  155. images.ep = image_get_ep(&images.legacy_hdr_os_copy);
  156. #if IMAGE_ENABLE_FIT
  157. } else if (images.fit_uname_os) {
  158. int ret;
  159. ret = fit_image_get_entry(images.fit_hdr_os,
  160. images.fit_noffset_os, &images.ep);
  161. if (ret) {
  162. puts("Can't get entry point property!\n");
  163. return 1;
  164. }
  165. #endif
  166. } else if (!ep_found) {
  167. puts("Could not find kernel entry point!\n");
  168. return 1;
  169. }
  170. if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
  171. images.os.load = images.os.image_start;
  172. images.ep += images.os.load;
  173. }
  174. images.os.start = map_to_sysmem(os_hdr);
  175. return 0;
  176. }
  177. /**
  178. * bootm_find_images - wrapper to find and locate various images
  179. * @flag: Ignored Argument
  180. * @argc: command argument count
  181. * @argv: command argument list
  182. *
  183. * boot_find_images() will attempt to load an available ramdisk,
  184. * flattened device tree, as well as specifically marked
  185. * "loadable" images (loadables are FIT only)
  186. *
  187. * Note: bootm_find_images will skip an image if it is not found
  188. *
  189. * @return:
  190. * 0, if all existing images were loaded correctly
  191. * 1, if an image is found but corrupted, or invalid
  192. */
  193. int bootm_find_images(int flag, int argc, char * const argv[])
  194. {
  195. int ret;
  196. /* find ramdisk */
  197. ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
  198. &images.rd_start, &images.rd_end);
  199. if (ret) {
  200. puts("Ramdisk image is corrupt or invalid\n");
  201. return 1;
  202. }
  203. #if IMAGE_ENABLE_OF_LIBFDT
  204. /* find flattened device tree */
  205. ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
  206. &images.ft_addr, &images.ft_len);
  207. if (ret) {
  208. puts("Could not find a valid device tree\n");
  209. return 1;
  210. }
  211. set_working_fdt_addr((ulong)images.ft_addr);
  212. #endif
  213. #if IMAGE_ENABLE_FIT
  214. #if defined(CONFIG_FPGA)
  215. /* find bitstreams */
  216. ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
  217. NULL, NULL);
  218. if (ret) {
  219. printf("FPGA image is corrupted or invalid\n");
  220. return 1;
  221. }
  222. #endif
  223. /* find all of the loadables */
  224. ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
  225. NULL, NULL);
  226. if (ret) {
  227. printf("Loadable(s) is corrupt or invalid\n");
  228. return 1;
  229. }
  230. #endif
  231. return 0;
  232. }
  233. static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
  234. char * const argv[])
  235. {
  236. if (((images.os.type == IH_TYPE_KERNEL) ||
  237. (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
  238. (images.os.type == IH_TYPE_MULTI)) &&
  239. (images.os.os == IH_OS_LINUX ||
  240. images.os.os == IH_OS_VXWORKS))
  241. return bootm_find_images(flag, argc, argv);
  242. return 0;
  243. }
  244. #endif /* USE_HOSTC */
  245. /**
  246. * print_decomp_msg() - Print a suitable decompression/loading message
  247. *
  248. * @type: OS type (IH_OS_...)
  249. * @comp_type: Compression type being used (IH_COMP_...)
  250. * @is_xip: true if the load address matches the image start
  251. */
  252. static void print_decomp_msg(int comp_type, int type, bool is_xip)
  253. {
  254. const char *name = genimg_get_type_name(type);
  255. if (comp_type == IH_COMP_NONE)
  256. printf(" %s %s ... ", is_xip ? "XIP" : "Loading", name);
  257. else
  258. printf(" Uncompressing %s ... ", name);
  259. }
  260. /**
  261. * handle_decomp_error() - display a decompression error
  262. *
  263. * This function tries to produce a useful message. In the case where the
  264. * uncompressed size is the same as the available space, we can assume that
  265. * the image is too large for the buffer.
  266. *
  267. * @comp_type: Compression type being used (IH_COMP_...)
  268. * @uncomp_size: Number of bytes uncompressed
  269. * @unc_len: Amount of space available for decompression
  270. * @ret: Error code to report
  271. * @return BOOTM_ERR_RESET, indicating that the board must be reset
  272. */
  273. static int handle_decomp_error(int comp_type, size_t uncomp_size,
  274. size_t unc_len, int ret)
  275. {
  276. const char *name = genimg_get_comp_name(comp_type);
  277. if (uncomp_size >= unc_len)
  278. printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
  279. else
  280. printf("%s: uncompress error %d\n", name, ret);
  281. /*
  282. * The decompression routines are now safe, so will not write beyond
  283. * their bounds. Probably it is not necessary to reset, but maintain
  284. * the current behaviour for now.
  285. */
  286. printf("Must RESET board to recover\n");
  287. #ifndef USE_HOSTCC
  288. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  289. #endif
  290. return BOOTM_ERR_RESET;
  291. }
  292. int bootm_decomp_image(int comp, ulong load, ulong image_start, int type,
  293. void *load_buf, void *image_buf, ulong image_len,
  294. uint unc_len, ulong *load_end)
  295. {
  296. int ret = 0;
  297. *load_end = load;
  298. print_decomp_msg(comp, type, load == image_start);
  299. /*
  300. * Load the image to the right place, decompressing if needed. After
  301. * this, image_len will be set to the number of uncompressed bytes
  302. * loaded, ret will be non-zero on error.
  303. */
  304. switch (comp) {
  305. case IH_COMP_NONE:
  306. if (load == image_start)
  307. break;
  308. if (image_len <= unc_len)
  309. memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
  310. else
  311. ret = 1;
  312. break;
  313. #ifdef CONFIG_GZIP
  314. case IH_COMP_GZIP: {
  315. ret = gunzip(load_buf, unc_len, image_buf, &image_len);
  316. break;
  317. }
  318. #endif /* CONFIG_GZIP */
  319. #ifdef CONFIG_BZIP2
  320. case IH_COMP_BZIP2: {
  321. uint size = unc_len;
  322. /*
  323. * If we've got less than 4 MB of malloc() space,
  324. * use slower decompression algorithm which requires
  325. * at most 2300 KB of memory.
  326. */
  327. ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
  328. image_buf, image_len,
  329. CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
  330. image_len = size;
  331. break;
  332. }
  333. #endif /* CONFIG_BZIP2 */
  334. #ifdef CONFIG_LZMA
  335. case IH_COMP_LZMA: {
  336. SizeT lzma_len = unc_len;
  337. ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
  338. image_buf, image_len);
  339. image_len = lzma_len;
  340. break;
  341. }
  342. #endif /* CONFIG_LZMA */
  343. #ifdef CONFIG_LZO
  344. case IH_COMP_LZO: {
  345. size_t size = unc_len;
  346. ret = lzop_decompress(image_buf, image_len, load_buf, &size);
  347. image_len = size;
  348. break;
  349. }
  350. #endif /* CONFIG_LZO */
  351. #ifdef CONFIG_LZ4
  352. case IH_COMP_LZ4: {
  353. size_t size = unc_len;
  354. ret = ulz4fn(image_buf, image_len, load_buf, &size);
  355. image_len = size;
  356. break;
  357. }
  358. #endif /* CONFIG_LZ4 */
  359. default:
  360. printf("Unimplemented compression type %d\n", comp);
  361. return BOOTM_ERR_UNIMPLEMENTED;
  362. }
  363. if (ret)
  364. return handle_decomp_error(comp, image_len, unc_len, ret);
  365. *load_end = load + image_len;
  366. puts("OK\n");
  367. return 0;
  368. }
  369. #ifndef USE_HOSTCC
  370. static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
  371. int boot_progress)
  372. {
  373. image_info_t os = images->os;
  374. ulong load = os.load;
  375. ulong blob_start = os.start;
  376. ulong blob_end = os.end;
  377. ulong image_start = os.image_start;
  378. ulong image_len = os.image_len;
  379. ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
  380. ulong flush_len = *load_end - load;
  381. bool no_overlap;
  382. void *load_buf, *image_buf;
  383. int err;
  384. load_buf = map_sysmem(load, 0);
  385. image_buf = map_sysmem(os.image_start, image_len);
  386. err = bootm_decomp_image(os.comp, load, os.image_start, os.type,
  387. load_buf, image_buf, image_len,
  388. CONFIG_SYS_BOOTM_LEN, load_end);
  389. if (err) {
  390. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  391. return err;
  392. }
  393. if (flush_start < load)
  394. flush_len += load - flush_start;
  395. flush_cache(flush_start, ALIGN(flush_len, ARCH_DMA_MINALIGN));
  396. debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
  397. bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
  398. no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
  399. if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
  400. debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
  401. blob_start, blob_end);
  402. debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
  403. *load_end);
  404. /* Check what type of image this is. */
  405. if (images->legacy_hdr_valid) {
  406. if (image_get_type(&images->legacy_hdr_os_copy)
  407. == IH_TYPE_MULTI)
  408. puts("WARNING: legacy format multi component image overwritten\n");
  409. return BOOTM_ERR_OVERLAP;
  410. } else {
  411. puts("ERROR: new format image overwritten - must RESET the board to recover\n");
  412. bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
  413. return BOOTM_ERR_RESET;
  414. }
  415. }
  416. return 0;
  417. }
  418. /**
  419. * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
  420. *
  421. * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
  422. * enabled)
  423. */
  424. ulong bootm_disable_interrupts(void)
  425. {
  426. ulong iflag;
  427. /*
  428. * We have reached the point of no return: we are going to
  429. * overwrite all exception vector code, so we cannot easily
  430. * recover from any failures any more...
  431. */
  432. iflag = disable_interrupts();
  433. #ifdef CONFIG_NETCONSOLE
  434. /* Stop the ethernet stack if NetConsole could have left it up */
  435. eth_halt();
  436. # ifndef CONFIG_DM_ETH
  437. eth_unregister(eth_get_dev());
  438. # endif
  439. #endif
  440. #if defined(CONFIG_CMD_USB)
  441. /*
  442. * turn off USB to prevent the host controller from writing to the
  443. * SDRAM while Linux is booting. This could happen (at least for OHCI
  444. * controller), because the HCCA (Host Controller Communication Area)
  445. * lies within the SDRAM and the host controller writes continously to
  446. * this area (as busmaster!). The HccaFrameNumber is for example
  447. * updated every 1 ms within the HCCA structure in SDRAM! For more
  448. * details see the OpenHCI specification.
  449. */
  450. usb_stop();
  451. #endif
  452. return iflag;
  453. }
  454. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  455. #define CONSOLE_ARG "console="
  456. #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
  457. static void fixup_silent_linux(void)
  458. {
  459. char *buf;
  460. const char *env_val;
  461. char *cmdline = env_get("bootargs");
  462. int want_silent;
  463. /*
  464. * Only fix cmdline when requested. The environment variable can be:
  465. *
  466. * no - we never fixup
  467. * yes - we always fixup
  468. * unset - we rely on the console silent flag
  469. */
  470. want_silent = env_get_yesno("silent_linux");
  471. if (want_silent == 0)
  472. return;
  473. else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
  474. return;
  475. debug("before silent fix-up: %s\n", cmdline);
  476. if (cmdline && (cmdline[0] != '\0')) {
  477. char *start = strstr(cmdline, CONSOLE_ARG);
  478. /* Allocate space for maximum possible new command line */
  479. buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
  480. if (!buf) {
  481. debug("%s: out of memory\n", __func__);
  482. return;
  483. }
  484. if (start) {
  485. char *end = strchr(start, ' ');
  486. int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
  487. strncpy(buf, cmdline, num_start_bytes);
  488. if (end)
  489. strcpy(buf + num_start_bytes, end);
  490. else
  491. buf[num_start_bytes] = '\0';
  492. } else {
  493. sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
  494. }
  495. env_val = buf;
  496. } else {
  497. buf = NULL;
  498. env_val = CONSOLE_ARG;
  499. }
  500. env_set("bootargs", env_val);
  501. debug("after silent fix-up: %s\n", env_val);
  502. free(buf);
  503. }
  504. #endif /* CONFIG_SILENT_CONSOLE */
  505. /**
  506. * Execute selected states of the bootm command.
  507. *
  508. * Note the arguments to this state must be the first argument, Any 'bootm'
  509. * or sub-command arguments must have already been taken.
  510. *
  511. * Note that if states contains more than one flag it MUST contain
  512. * BOOTM_STATE_START, since this handles and consumes the command line args.
  513. *
  514. * Also note that aside from boot_os_fn functions and bootm_load_os no other
  515. * functions we store the return value of in 'ret' may use a negative return
  516. * value, without special handling.
  517. *
  518. * @param cmdtp Pointer to bootm command table entry
  519. * @param flag Command flags (CMD_FLAG_...)
  520. * @param argc Number of subcommand arguments (0 = no arguments)
  521. * @param argv Arguments
  522. * @param states Mask containing states to run (BOOTM_STATE_...)
  523. * @param images Image header information
  524. * @param boot_progress 1 to show boot progress, 0 to not do this
  525. * @return 0 if ok, something else on error. Some errors will cause this
  526. * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
  527. * then the intent is to boot an OS, so this function will not return
  528. * unless the image type is standalone.
  529. */
  530. int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  531. int states, bootm_headers_t *images, int boot_progress)
  532. {
  533. boot_os_fn *boot_fn;
  534. ulong iflag = 0;
  535. int ret = 0, need_boot_fn;
  536. images->state |= states;
  537. /*
  538. * Work through the states and see how far we get. We stop on
  539. * any error.
  540. */
  541. if (states & BOOTM_STATE_START)
  542. ret = bootm_start(cmdtp, flag, argc, argv);
  543. if (!ret && (states & BOOTM_STATE_FINDOS))
  544. ret = bootm_find_os(cmdtp, flag, argc, argv);
  545. if (!ret && (states & BOOTM_STATE_FINDOTHER))
  546. ret = bootm_find_other(cmdtp, flag, argc, argv);
  547. /* Load the OS */
  548. if (!ret && (states & BOOTM_STATE_LOADOS)) {
  549. ulong load_end;
  550. iflag = bootm_disable_interrupts();
  551. ret = bootm_load_os(images, &load_end, 0);
  552. if (ret == 0)
  553. lmb_reserve(&images->lmb, images->os.load,
  554. (load_end - images->os.load));
  555. else if (ret && ret != BOOTM_ERR_OVERLAP)
  556. goto err;
  557. else if (ret == BOOTM_ERR_OVERLAP)
  558. ret = 0;
  559. }
  560. /* Relocate the ramdisk */
  561. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  562. if (!ret && (states & BOOTM_STATE_RAMDISK)) {
  563. ulong rd_len = images->rd_end - images->rd_start;
  564. ret = boot_ramdisk_high(&images->lmb, images->rd_start,
  565. rd_len, &images->initrd_start, &images->initrd_end);
  566. if (!ret) {
  567. env_set_hex("initrd_start", images->initrd_start);
  568. env_set_hex("initrd_end", images->initrd_end);
  569. }
  570. }
  571. #endif
  572. #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
  573. if (!ret && (states & BOOTM_STATE_FDT)) {
  574. boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
  575. ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
  576. &images->ft_len);
  577. }
  578. #endif
  579. /* From now on, we need the OS boot function */
  580. if (ret)
  581. return ret;
  582. boot_fn = bootm_os_get_boot_func(images->os.os);
  583. need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
  584. BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
  585. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
  586. if (boot_fn == NULL && need_boot_fn) {
  587. if (iflag)
  588. enable_interrupts();
  589. printf("ERROR: booting os '%s' (%d) is not supported\n",
  590. genimg_get_os_name(images->os.os), images->os.os);
  591. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  592. return 1;
  593. }
  594. /* Call various other states that are not generally used */
  595. if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
  596. ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
  597. if (!ret && (states & BOOTM_STATE_OS_BD_T))
  598. ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
  599. if (!ret && (states & BOOTM_STATE_OS_PREP)) {
  600. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  601. if (images->os.os == IH_OS_LINUX)
  602. fixup_silent_linux();
  603. #endif
  604. ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
  605. }
  606. #ifdef CONFIG_TRACE
  607. /* Pretend to run the OS, then run a user command */
  608. if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
  609. char *cmd_list = env_get("fakegocmd");
  610. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
  611. images, boot_fn);
  612. if (!ret && cmd_list)
  613. ret = run_command_list(cmd_list, -1, flag);
  614. }
  615. #endif
  616. /* Check for unsupported subcommand. */
  617. if (ret) {
  618. puts("subcommand not supported\n");
  619. return ret;
  620. }
  621. /* Now run the OS! We hope this doesn't return */
  622. if (!ret && (states & BOOTM_STATE_OS_GO))
  623. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
  624. images, boot_fn);
  625. /* Deal with any fallout */
  626. err:
  627. if (iflag)
  628. enable_interrupts();
  629. if (ret == BOOTM_ERR_UNIMPLEMENTED)
  630. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  631. else if (ret == BOOTM_ERR_RESET)
  632. do_reset(cmdtp, flag, argc, argv);
  633. return ret;
  634. }
  635. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  636. /**
  637. * image_get_kernel - verify legacy format kernel image
  638. * @img_addr: in RAM address of the legacy format image to be verified
  639. * @verify: data CRC verification flag
  640. *
  641. * image_get_kernel() verifies legacy image integrity and returns pointer to
  642. * legacy image header if image verification was completed successfully.
  643. *
  644. * returns:
  645. * pointer to a legacy image header if valid image was found
  646. * otherwise return NULL
  647. */
  648. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  649. {
  650. image_header_t *hdr = (image_header_t *)img_addr;
  651. if (!image_check_magic(hdr)) {
  652. puts("Bad Magic Number\n");
  653. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  654. return NULL;
  655. }
  656. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  657. if (!image_check_hcrc(hdr)) {
  658. puts("Bad Header Checksum\n");
  659. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  660. return NULL;
  661. }
  662. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  663. image_print_contents(hdr);
  664. if (verify) {
  665. puts(" Verifying Checksum ... ");
  666. if (!image_check_dcrc(hdr)) {
  667. printf("Bad Data CRC\n");
  668. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  669. return NULL;
  670. }
  671. puts("OK\n");
  672. }
  673. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  674. if (!image_check_target_arch(hdr)) {
  675. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  676. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  677. return NULL;
  678. }
  679. return hdr;
  680. }
  681. #endif
  682. /**
  683. * boot_get_kernel - find kernel image
  684. * @os_data: pointer to a ulong variable, will hold os data start address
  685. * @os_len: pointer to a ulong variable, will hold os data length
  686. *
  687. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  688. * and locates kernel data.
  689. *
  690. * returns:
  691. * pointer to image header if valid image was found, plus kernel start
  692. * address and length, otherwise NULL
  693. */
  694. static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  695. char * const argv[], bootm_headers_t *images,
  696. ulong *os_data, ulong *os_len)
  697. {
  698. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  699. image_header_t *hdr;
  700. #endif
  701. ulong img_addr;
  702. const void *buf;
  703. const char *fit_uname_config = NULL;
  704. const char *fit_uname_kernel = NULL;
  705. #if IMAGE_ENABLE_FIT
  706. int os_noffset;
  707. #endif
  708. img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
  709. &fit_uname_config,
  710. &fit_uname_kernel);
  711. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  712. /* check image type, for FIT images get FIT kernel node */
  713. *os_data = *os_len = 0;
  714. buf = map_sysmem(img_addr, 0);
  715. switch (genimg_get_format(buf)) {
  716. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  717. case IMAGE_FORMAT_LEGACY:
  718. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  719. img_addr);
  720. hdr = image_get_kernel(img_addr, images->verify);
  721. if (!hdr)
  722. return NULL;
  723. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  724. /* get os_data and os_len */
  725. switch (image_get_type(hdr)) {
  726. case IH_TYPE_KERNEL:
  727. case IH_TYPE_KERNEL_NOLOAD:
  728. *os_data = image_get_data(hdr);
  729. *os_len = image_get_data_size(hdr);
  730. break;
  731. case IH_TYPE_MULTI:
  732. image_multi_getimg(hdr, 0, os_data, os_len);
  733. break;
  734. case IH_TYPE_STANDALONE:
  735. *os_data = image_get_data(hdr);
  736. *os_len = image_get_data_size(hdr);
  737. break;
  738. default:
  739. printf("Wrong Image Type for %s command\n",
  740. cmdtp->name);
  741. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  742. return NULL;
  743. }
  744. /*
  745. * copy image header to allow for image overwrites during
  746. * kernel decompression.
  747. */
  748. memmove(&images->legacy_hdr_os_copy, hdr,
  749. sizeof(image_header_t));
  750. /* save pointer to image header */
  751. images->legacy_hdr_os = hdr;
  752. images->legacy_hdr_valid = 1;
  753. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  754. break;
  755. #endif
  756. #if IMAGE_ENABLE_FIT
  757. case IMAGE_FORMAT_FIT:
  758. os_noffset = fit_image_load(images, img_addr,
  759. &fit_uname_kernel, &fit_uname_config,
  760. IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
  761. BOOTSTAGE_ID_FIT_KERNEL_START,
  762. FIT_LOAD_IGNORED, os_data, os_len);
  763. if (os_noffset < 0)
  764. return NULL;
  765. images->fit_hdr_os = map_sysmem(img_addr, 0);
  766. images->fit_uname_os = fit_uname_kernel;
  767. images->fit_uname_cfg = fit_uname_config;
  768. images->fit_noffset_os = os_noffset;
  769. break;
  770. #endif
  771. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  772. case IMAGE_FORMAT_ANDROID:
  773. printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
  774. if (android_image_get_kernel(buf, images->verify,
  775. os_data, os_len))
  776. return NULL;
  777. break;
  778. #endif
  779. default:
  780. printf("Wrong Image Format for %s command\n", cmdtp->name);
  781. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  782. return NULL;
  783. }
  784. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  785. *os_data, *os_len, *os_len);
  786. return buf;
  787. }
  788. #else /* USE_HOSTCC */
  789. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  790. {
  791. memmove(to, from, len);
  792. }
  793. static int bootm_host_load_image(const void *fit, int req_image_type)
  794. {
  795. const char *fit_uname_config = NULL;
  796. ulong data, len;
  797. bootm_headers_t images;
  798. int noffset;
  799. ulong load_end;
  800. uint8_t image_type;
  801. uint8_t imape_comp;
  802. void *load_buf;
  803. int ret;
  804. memset(&images, '\0', sizeof(images));
  805. images.verify = 1;
  806. noffset = fit_image_load(&images, (ulong)fit,
  807. NULL, &fit_uname_config,
  808. IH_ARCH_DEFAULT, req_image_type, -1,
  809. FIT_LOAD_IGNORED, &data, &len);
  810. if (noffset < 0)
  811. return noffset;
  812. if (fit_image_get_type(fit, noffset, &image_type)) {
  813. puts("Can't get image type!\n");
  814. return -EINVAL;
  815. }
  816. if (fit_image_get_comp(fit, noffset, &imape_comp)) {
  817. puts("Can't get image compression!\n");
  818. return -EINVAL;
  819. }
  820. /* Allow the image to expand by a factor of 4, should be safe */
  821. load_buf = malloc((1 << 20) + len * 4);
  822. ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf,
  823. (void *)data, len, CONFIG_SYS_BOOTM_LEN,
  824. &load_end);
  825. free(load_buf);
  826. if (ret && ret != BOOTM_ERR_UNIMPLEMENTED)
  827. return ret;
  828. return 0;
  829. }
  830. int bootm_host_load_images(const void *fit, int cfg_noffset)
  831. {
  832. static uint8_t image_types[] = {
  833. IH_TYPE_KERNEL,
  834. IH_TYPE_FLATDT,
  835. IH_TYPE_RAMDISK,
  836. };
  837. int err = 0;
  838. int i;
  839. for (i = 0; i < ARRAY_SIZE(image_types); i++) {
  840. int ret;
  841. ret = bootm_host_load_image(fit, image_types[i]);
  842. if (!err && ret && ret != -ENOENT)
  843. err = ret;
  844. }
  845. /* Return the first error we found */
  846. return err;
  847. }
  848. #endif /* ndef USE_HOSTCC */