bootm.c 23 KB

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