bootm.c 24 KB

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