bootm.c 23 KB

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