bootm.c 25 KB

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