bootm.c 24 KB

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