bootm.c 26 KB

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