bootm.c 25 KB

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