bootm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef USE_HOSTCC
  8. #include <common.h>
  9. #include <bootstage.h>
  10. #include <bzlib.h>
  11. #include <fdt_support.h>
  12. #include <lmb.h>
  13. #include <malloc.h>
  14. #include <asm/io.h>
  15. #include <linux/lzo.h>
  16. #include <lzma/LzmaTypes.h>
  17. #include <lzma/LzmaDec.h>
  18. #include <lzma/LzmaTools.h>
  19. #if defined(CONFIG_CMD_USB)
  20. #include <usb.h>
  21. #endif
  22. #else
  23. #include "mkimage.h"
  24. #endif
  25. #include <command.h>
  26. #include <bootm.h>
  27. #include <image.h>
  28. #ifndef CONFIG_SYS_BOOTM_LEN
  29. /* use 8MByte as default max gunzip size */
  30. #define CONFIG_SYS_BOOTM_LEN 0x800000
  31. #endif
  32. #define IH_INITRD_ARCH IH_ARCH_DEFAULT
  33. #ifndef USE_HOSTCC
  34. DECLARE_GLOBAL_DATA_PTR;
  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. #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. lmb_init(&images->lmb);
  44. mem_start = getenv_bootm_low();
  45. mem_size = getenv_bootm_size();
  46. lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
  47. arch_lmb_reserve(&images->lmb);
  48. board_lmb_reserve(&images->lmb);
  49. }
  50. #else
  51. #define lmb_reserve(lmb, base, size)
  52. static inline void boot_start_lmb(bootm_headers_t *images) { }
  53. #endif
  54. static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc,
  55. char * const argv[])
  56. {
  57. memset((void *)&images, 0, sizeof(images));
  58. images.verify = getenv_yesno("verify");
  59. boot_start_lmb(&images);
  60. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
  61. images.state = BOOTM_STATE_START;
  62. return 0;
  63. }
  64. static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
  65. char * const argv[])
  66. {
  67. const void *os_hdr;
  68. bool ep_found = false;
  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 defined(CONFIG_IMAGE_FORMAT_LEGACY)
  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. break;
  86. #endif
  87. #if defined(CONFIG_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. images.os.end = fit_get_end(images.fit_hdr_os);
  110. if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
  111. &images.os.load)) {
  112. puts("Can't get image load address!\n");
  113. bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
  114. return 1;
  115. }
  116. break;
  117. #endif
  118. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  119. case IMAGE_FORMAT_ANDROID:
  120. images.os.type = IH_TYPE_KERNEL;
  121. images.os.comp = IH_COMP_NONE;
  122. images.os.os = IH_OS_LINUX;
  123. images.ep = images.os.load;
  124. ep_found = true;
  125. images.os.end = android_image_get_end(os_hdr);
  126. images.os.load = android_image_get_kload(os_hdr);
  127. break;
  128. #endif
  129. default:
  130. puts("ERROR: unknown image format type!\n");
  131. return 1;
  132. }
  133. /* find kernel entry point */
  134. if (images.legacy_hdr_valid) {
  135. images.ep = image_get_ep(&images.legacy_hdr_os_copy);
  136. #if defined(CONFIG_FIT)
  137. } else if (images.fit_uname_os) {
  138. int ret;
  139. ret = fit_image_get_entry(images.fit_hdr_os,
  140. images.fit_noffset_os, &images.ep);
  141. if (ret) {
  142. puts("Can't get entry point property!\n");
  143. return 1;
  144. }
  145. #endif
  146. } else if (!ep_found) {
  147. puts("Could not find kernel entry point!\n");
  148. return 1;
  149. }
  150. if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
  151. images.os.load = images.os.image_start;
  152. images.ep += images.os.load;
  153. }
  154. images.os.start = (ulong)os_hdr;
  155. return 0;
  156. }
  157. static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
  158. {
  159. int ret;
  160. /* find ramdisk */
  161. ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
  162. &images.rd_start, &images.rd_end);
  163. if (ret) {
  164. puts("Ramdisk image is corrupt or invalid\n");
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. #if defined(CONFIG_OF_LIBFDT)
  170. static int bootm_find_fdt(int flag, int argc, char * const argv[])
  171. {
  172. int ret;
  173. /* find flattened device tree */
  174. ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
  175. &images.ft_addr, &images.ft_len);
  176. if (ret) {
  177. puts("Could not find a valid device tree\n");
  178. return 1;
  179. }
  180. set_working_fdt_addr(images.ft_addr);
  181. return 0;
  182. }
  183. #endif
  184. int bootm_find_ramdisk_fdt(int flag, int argc, char * const argv[])
  185. {
  186. if (bootm_find_ramdisk(flag, argc, argv))
  187. return 1;
  188. #if defined(CONFIG_OF_LIBFDT)
  189. if (bootm_find_fdt(flag, argc, argv))
  190. return 1;
  191. #endif
  192. return 0;
  193. }
  194. static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
  195. char * const argv[])
  196. {
  197. if (((images.os.type == IH_TYPE_KERNEL) ||
  198. (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
  199. (images.os.type == IH_TYPE_MULTI)) &&
  200. (images.os.os == IH_OS_LINUX ||
  201. images.os.os == IH_OS_VXWORKS))
  202. return bootm_find_ramdisk_fdt(flag, argc, argv);
  203. return 0;
  204. }
  205. static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
  206. int boot_progress)
  207. {
  208. image_info_t os = images->os;
  209. uint8_t comp = os.comp;
  210. ulong load = os.load;
  211. ulong blob_start = os.start;
  212. ulong blob_end = os.end;
  213. ulong image_start = os.image_start;
  214. ulong image_len = os.image_len;
  215. __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
  216. int no_overlap = 0;
  217. void *load_buf, *image_buf;
  218. #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
  219. int ret;
  220. #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
  221. const char *type_name = genimg_get_type_name(os.type);
  222. load_buf = map_sysmem(load, unc_len);
  223. image_buf = map_sysmem(image_start, image_len);
  224. switch (comp) {
  225. case IH_COMP_NONE:
  226. if (load == image_start) {
  227. printf(" XIP %s ... ", type_name);
  228. no_overlap = 1;
  229. } else {
  230. printf(" Loading %s ... ", type_name);
  231. memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
  232. }
  233. *load_end = load + image_len;
  234. break;
  235. #ifdef CONFIG_GZIP
  236. case IH_COMP_GZIP:
  237. printf(" Uncompressing %s ... ", type_name);
  238. if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
  239. puts("GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover\n");
  240. if (boot_progress)
  241. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  242. return BOOTM_ERR_RESET;
  243. }
  244. *load_end = load + image_len;
  245. break;
  246. #endif /* CONFIG_GZIP */
  247. #ifdef CONFIG_BZIP2
  248. case IH_COMP_BZIP2:
  249. printf(" Uncompressing %s ... ", type_name);
  250. /*
  251. * If we've got less than 4 MB of malloc() space,
  252. * use slower decompression algorithm which requires
  253. * at most 2300 KB of memory.
  254. */
  255. int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
  256. image_buf, image_len,
  257. CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
  258. if (i != BZ_OK) {
  259. printf("BUNZIP2: uncompress or overwrite error %d - must RESET board to recover\n",
  260. i);
  261. if (boot_progress)
  262. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  263. return BOOTM_ERR_RESET;
  264. }
  265. *load_end = load + unc_len;
  266. break;
  267. #endif /* CONFIG_BZIP2 */
  268. #ifdef CONFIG_LZMA
  269. case IH_COMP_LZMA: {
  270. SizeT lzma_len = unc_len;
  271. printf(" Uncompressing %s ... ", type_name);
  272. ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
  273. image_buf, image_len);
  274. unc_len = lzma_len;
  275. if (ret != SZ_OK) {
  276. printf("LZMA: uncompress or overwrite error %d - must RESET board to recover\n",
  277. ret);
  278. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  279. return BOOTM_ERR_RESET;
  280. }
  281. *load_end = load + unc_len;
  282. break;
  283. }
  284. #endif /* CONFIG_LZMA */
  285. #ifdef CONFIG_LZO
  286. case IH_COMP_LZO: {
  287. size_t size = unc_len;
  288. printf(" Uncompressing %s ... ", type_name);
  289. ret = lzop_decompress(image_buf, image_len, load_buf, &size);
  290. if (ret != LZO_E_OK) {
  291. printf("LZO: uncompress or overwrite error %d - must RESET board to recover\n",
  292. ret);
  293. if (boot_progress)
  294. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  295. return BOOTM_ERR_RESET;
  296. }
  297. *load_end = load + size;
  298. break;
  299. }
  300. #endif /* CONFIG_LZO */
  301. default:
  302. printf("Unimplemented compression type %d\n", comp);
  303. return BOOTM_ERR_UNIMPLEMENTED;
  304. }
  305. flush_cache(load, (*load_end - load) * sizeof(ulong));
  306. puts("OK\n");
  307. debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
  308. bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
  309. if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
  310. debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
  311. blob_start, blob_end);
  312. debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
  313. *load_end);
  314. /* Check what type of image this is. */
  315. if (images->legacy_hdr_valid) {
  316. if (image_get_type(&images->legacy_hdr_os_copy)
  317. == IH_TYPE_MULTI)
  318. puts("WARNING: legacy format multi component image overwritten\n");
  319. return BOOTM_ERR_OVERLAP;
  320. } else {
  321. puts("ERROR: new format image overwritten - must RESET the board to recover\n");
  322. bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
  323. return BOOTM_ERR_RESET;
  324. }
  325. }
  326. return 0;
  327. }
  328. /**
  329. * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
  330. *
  331. * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
  332. * enabled)
  333. */
  334. ulong bootm_disable_interrupts(void)
  335. {
  336. ulong iflag;
  337. /*
  338. * We have reached the point of no return: we are going to
  339. * overwrite all exception vector code, so we cannot easily
  340. * recover from any failures any more...
  341. */
  342. iflag = disable_interrupts();
  343. #ifdef CONFIG_NETCONSOLE
  344. /* Stop the ethernet stack if NetConsole could have left it up */
  345. eth_halt();
  346. eth_unregister(eth_get_dev());
  347. #endif
  348. #if defined(CONFIG_CMD_USB)
  349. /*
  350. * turn off USB to prevent the host controller from writing to the
  351. * SDRAM while Linux is booting. This could happen (at least for OHCI
  352. * controller), because the HCCA (Host Controller Communication Area)
  353. * lies within the SDRAM and the host controller writes continously to
  354. * this area (as busmaster!). The HccaFrameNumber is for example
  355. * updated every 1 ms within the HCCA structure in SDRAM! For more
  356. * details see the OpenHCI specification.
  357. */
  358. usb_stop();
  359. #endif
  360. return iflag;
  361. }
  362. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  363. #define CONSOLE_ARG "console="
  364. #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
  365. static void fixup_silent_linux(void)
  366. {
  367. char *buf;
  368. const char *env_val;
  369. char *cmdline = getenv("bootargs");
  370. int want_silent;
  371. /*
  372. * Only fix cmdline when requested. The environment variable can be:
  373. *
  374. * no - we never fixup
  375. * yes - we always fixup
  376. * unset - we rely on the console silent flag
  377. */
  378. want_silent = getenv_yesno("silent_linux");
  379. if (want_silent == 0)
  380. return;
  381. else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
  382. return;
  383. debug("before silent fix-up: %s\n", cmdline);
  384. if (cmdline && (cmdline[0] != '\0')) {
  385. char *start = strstr(cmdline, CONSOLE_ARG);
  386. /* Allocate space for maximum possible new command line */
  387. buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
  388. if (!buf) {
  389. debug("%s: out of memory\n", __func__);
  390. return;
  391. }
  392. if (start) {
  393. char *end = strchr(start, ' ');
  394. int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
  395. strncpy(buf, cmdline, num_start_bytes);
  396. if (end)
  397. strcpy(buf + num_start_bytes, end);
  398. else
  399. buf[num_start_bytes] = '\0';
  400. } else {
  401. sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
  402. }
  403. env_val = buf;
  404. } else {
  405. buf = NULL;
  406. env_val = CONSOLE_ARG;
  407. }
  408. setenv("bootargs", env_val);
  409. debug("after silent fix-up: %s\n", env_val);
  410. free(buf);
  411. }
  412. #endif /* CONFIG_SILENT_CONSOLE */
  413. /**
  414. * Execute selected states of the bootm command.
  415. *
  416. * Note the arguments to this state must be the first argument, Any 'bootm'
  417. * or sub-command arguments must have already been taken.
  418. *
  419. * Note that if states contains more than one flag it MUST contain
  420. * BOOTM_STATE_START, since this handles and consumes the command line args.
  421. *
  422. * Also note that aside from boot_os_fn functions and bootm_load_os no other
  423. * functions we store the return value of in 'ret' may use a negative return
  424. * value, without special handling.
  425. *
  426. * @param cmdtp Pointer to bootm command table entry
  427. * @param flag Command flags (CMD_FLAG_...)
  428. * @param argc Number of subcommand arguments (0 = no arguments)
  429. * @param argv Arguments
  430. * @param states Mask containing states to run (BOOTM_STATE_...)
  431. * @param images Image header information
  432. * @param boot_progress 1 to show boot progress, 0 to not do this
  433. * @return 0 if ok, something else on error. Some errors will cause this
  434. * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
  435. * then the intent is to boot an OS, so this function will not return
  436. * unless the image type is standalone.
  437. */
  438. int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  439. int states, bootm_headers_t *images, int boot_progress)
  440. {
  441. boot_os_fn *boot_fn;
  442. ulong iflag = 0;
  443. int ret = 0, need_boot_fn;
  444. images->state |= states;
  445. /*
  446. * Work through the states and see how far we get. We stop on
  447. * any error.
  448. */
  449. if (states & BOOTM_STATE_START)
  450. ret = bootm_start(cmdtp, flag, argc, argv);
  451. if (!ret && (states & BOOTM_STATE_FINDOS))
  452. ret = bootm_find_os(cmdtp, flag, argc, argv);
  453. if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
  454. ret = bootm_find_other(cmdtp, flag, argc, argv);
  455. argc = 0; /* consume the args */
  456. }
  457. /* Load the OS */
  458. if (!ret && (states & BOOTM_STATE_LOADOS)) {
  459. ulong load_end;
  460. iflag = bootm_disable_interrupts();
  461. ret = bootm_load_os(images, &load_end, 0);
  462. if (ret == 0)
  463. lmb_reserve(&images->lmb, images->os.load,
  464. (load_end - images->os.load));
  465. else if (ret && ret != BOOTM_ERR_OVERLAP)
  466. goto err;
  467. else if (ret == BOOTM_ERR_OVERLAP)
  468. ret = 0;
  469. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  470. if (images->os.os == IH_OS_LINUX)
  471. fixup_silent_linux();
  472. #endif
  473. }
  474. /* Relocate the ramdisk */
  475. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  476. if (!ret && (states & BOOTM_STATE_RAMDISK)) {
  477. ulong rd_len = images->rd_end - images->rd_start;
  478. ret = boot_ramdisk_high(&images->lmb, images->rd_start,
  479. rd_len, &images->initrd_start, &images->initrd_end);
  480. if (!ret) {
  481. setenv_hex("initrd_start", images->initrd_start);
  482. setenv_hex("initrd_end", images->initrd_end);
  483. }
  484. }
  485. #endif
  486. #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
  487. if (!ret && (states & BOOTM_STATE_FDT)) {
  488. boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
  489. ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
  490. &images->ft_len);
  491. }
  492. #endif
  493. /* From now on, we need the OS boot function */
  494. if (ret)
  495. return ret;
  496. boot_fn = bootm_os_get_boot_func(images->os.os);
  497. need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
  498. BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
  499. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
  500. if (boot_fn == NULL && need_boot_fn) {
  501. if (iflag)
  502. enable_interrupts();
  503. printf("ERROR: booting os '%s' (%d) is not supported\n",
  504. genimg_get_os_name(images->os.os), images->os.os);
  505. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  506. return 1;
  507. }
  508. /* Call various other states that are not generally used */
  509. if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
  510. ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
  511. if (!ret && (states & BOOTM_STATE_OS_BD_T))
  512. ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
  513. if (!ret && (states & BOOTM_STATE_OS_PREP))
  514. ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
  515. #ifdef CONFIG_TRACE
  516. /* Pretend to run the OS, then run a user command */
  517. if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
  518. char *cmd_list = getenv("fakegocmd");
  519. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
  520. images, boot_fn);
  521. if (!ret && cmd_list)
  522. ret = run_command_list(cmd_list, -1, flag);
  523. }
  524. #endif
  525. /* Check for unsupported subcommand. */
  526. if (ret) {
  527. puts("subcommand not supported\n");
  528. return ret;
  529. }
  530. /* Now run the OS! We hope this doesn't return */
  531. if (!ret && (states & BOOTM_STATE_OS_GO))
  532. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
  533. images, boot_fn);
  534. /* Deal with any fallout */
  535. err:
  536. if (iflag)
  537. enable_interrupts();
  538. if (ret == BOOTM_ERR_UNIMPLEMENTED)
  539. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  540. else if (ret == BOOTM_ERR_RESET)
  541. do_reset(cmdtp, flag, argc, argv);
  542. return ret;
  543. }
  544. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  545. /**
  546. * image_get_kernel - verify legacy format kernel image
  547. * @img_addr: in RAM address of the legacy format image to be verified
  548. * @verify: data CRC verification flag
  549. *
  550. * image_get_kernel() verifies legacy image integrity and returns pointer to
  551. * legacy image header if image verification was completed successfully.
  552. *
  553. * returns:
  554. * pointer to a legacy image header if valid image was found
  555. * otherwise return NULL
  556. */
  557. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  558. {
  559. image_header_t *hdr = (image_header_t *)img_addr;
  560. if (!image_check_magic(hdr)) {
  561. puts("Bad Magic Number\n");
  562. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  563. return NULL;
  564. }
  565. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  566. if (!image_check_hcrc(hdr)) {
  567. puts("Bad Header Checksum\n");
  568. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  569. return NULL;
  570. }
  571. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  572. image_print_contents(hdr);
  573. if (verify) {
  574. puts(" Verifying Checksum ... ");
  575. if (!image_check_dcrc(hdr)) {
  576. printf("Bad Data CRC\n");
  577. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  578. return NULL;
  579. }
  580. puts("OK\n");
  581. }
  582. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  583. if (!image_check_target_arch(hdr)) {
  584. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  585. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  586. return NULL;
  587. }
  588. return hdr;
  589. }
  590. #endif
  591. /**
  592. * boot_get_kernel - find kernel image
  593. * @os_data: pointer to a ulong variable, will hold os data start address
  594. * @os_len: pointer to a ulong variable, will hold os data length
  595. *
  596. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  597. * and locates kernel data.
  598. *
  599. * returns:
  600. * pointer to image header if valid image was found, plus kernel start
  601. * address and length, otherwise NULL
  602. */
  603. static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  604. char * const argv[], bootm_headers_t *images,
  605. ulong *os_data, ulong *os_len)
  606. {
  607. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  608. image_header_t *hdr;
  609. #endif
  610. ulong img_addr;
  611. const void *buf;
  612. #if defined(CONFIG_FIT)
  613. const char *fit_uname_config = NULL;
  614. const char *fit_uname_kernel = NULL;
  615. int os_noffset;
  616. #endif
  617. /* find out kernel image address */
  618. if (argc < 1) {
  619. img_addr = load_addr;
  620. debug("* kernel: default image load address = 0x%08lx\n",
  621. load_addr);
  622. #if defined(CONFIG_FIT)
  623. } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
  624. &fit_uname_config)) {
  625. debug("* kernel: config '%s' from image at 0x%08lx\n",
  626. fit_uname_config, img_addr);
  627. } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
  628. &fit_uname_kernel)) {
  629. debug("* kernel: subimage '%s' from image at 0x%08lx\n",
  630. fit_uname_kernel, img_addr);
  631. #endif
  632. } else {
  633. img_addr = simple_strtoul(argv[0], NULL, 16);
  634. debug("* kernel: cmdline image address = 0x%08lx\n",
  635. img_addr);
  636. }
  637. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  638. /* copy from dataflash if needed */
  639. img_addr = genimg_get_image(img_addr);
  640. /* check image type, for FIT images get FIT kernel node */
  641. *os_data = *os_len = 0;
  642. buf = map_sysmem(img_addr, 0);
  643. switch (genimg_get_format(buf)) {
  644. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  645. case IMAGE_FORMAT_LEGACY:
  646. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  647. img_addr);
  648. hdr = image_get_kernel(img_addr, images->verify);
  649. if (!hdr)
  650. return NULL;
  651. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  652. /* get os_data and os_len */
  653. switch (image_get_type(hdr)) {
  654. case IH_TYPE_KERNEL:
  655. case IH_TYPE_KERNEL_NOLOAD:
  656. *os_data = image_get_data(hdr);
  657. *os_len = image_get_data_size(hdr);
  658. break;
  659. case IH_TYPE_MULTI:
  660. image_multi_getimg(hdr, 0, os_data, os_len);
  661. break;
  662. case IH_TYPE_STANDALONE:
  663. *os_data = image_get_data(hdr);
  664. *os_len = image_get_data_size(hdr);
  665. break;
  666. default:
  667. printf("Wrong Image Type for %s command\n",
  668. cmdtp->name);
  669. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  670. return NULL;
  671. }
  672. /*
  673. * copy image header to allow for image overwrites during
  674. * kernel decompression.
  675. */
  676. memmove(&images->legacy_hdr_os_copy, hdr,
  677. sizeof(image_header_t));
  678. /* save pointer to image header */
  679. images->legacy_hdr_os = hdr;
  680. images->legacy_hdr_valid = 1;
  681. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  682. break;
  683. #endif
  684. #if defined(CONFIG_FIT)
  685. case IMAGE_FORMAT_FIT:
  686. os_noffset = fit_image_load(images, img_addr,
  687. &fit_uname_kernel, &fit_uname_config,
  688. IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
  689. BOOTSTAGE_ID_FIT_KERNEL_START,
  690. FIT_LOAD_IGNORED, os_data, os_len);
  691. if (os_noffset < 0)
  692. return NULL;
  693. images->fit_hdr_os = map_sysmem(img_addr, 0);
  694. images->fit_uname_os = fit_uname_kernel;
  695. images->fit_uname_cfg = fit_uname_config;
  696. images->fit_noffset_os = os_noffset;
  697. break;
  698. #endif
  699. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  700. case IMAGE_FORMAT_ANDROID:
  701. printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
  702. if (android_image_get_kernel(buf, images->verify,
  703. os_data, os_len))
  704. return NULL;
  705. break;
  706. #endif
  707. default:
  708. printf("Wrong Image Format for %s command\n", cmdtp->name);
  709. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  710. return NULL;
  711. }
  712. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  713. *os_data, *os_len, *os_len);
  714. return buf;
  715. }
  716. #endif /* ndef USE_HOSTCC */