bootm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. #endif /* USE_HOSTCC */
  206. /**
  207. * decomp_image() - decompress the operating system
  208. *
  209. * @comp: Compression algorithm that is used (IH_COMP_...)
  210. * @load: Destination load address in U-Boot memory
  211. * @image_start Image start address (where we are decompressing from)
  212. * @type: OS type (IH_OS_...)
  213. * @load_bug: Place to decompress to
  214. * @image_buf: Address to decompress from
  215. * @return 0 if OK, -ve on error (BOOTM_ERR_...)
  216. */
  217. static int decomp_image(int comp, ulong load, ulong image_start, int type,
  218. void *load_buf, void *image_buf, ulong image_len,
  219. ulong *load_end)
  220. {
  221. const char *type_name = genimg_get_type_name(type);
  222. __attribute__((unused)) uint unc_len = CONFIG_SYS_BOOTM_LEN;
  223. *load_end = load;
  224. switch (comp) {
  225. case IH_COMP_NONE:
  226. if (load == image_start) {
  227. printf(" XIP %s ... ", type_name);
  228. } else {
  229. printf(" Loading %s ... ", type_name);
  230. memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
  231. }
  232. *load_end = load + image_len;
  233. break;
  234. #ifdef CONFIG_GZIP
  235. case IH_COMP_GZIP:
  236. printf(" Uncompressing %s ... ", type_name);
  237. if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
  238. puts("GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover\n");
  239. return BOOTM_ERR_RESET;
  240. }
  241. *load_end = load + image_len;
  242. break;
  243. #endif /* CONFIG_GZIP */
  244. #ifdef CONFIG_BZIP2
  245. case IH_COMP_BZIP2:
  246. printf(" Uncompressing %s ... ", type_name);
  247. /*
  248. * If we've got less than 4 MB of malloc() space,
  249. * use slower decompression algorithm which requires
  250. * at most 2300 KB of memory.
  251. */
  252. int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
  253. image_buf, image_len,
  254. CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
  255. if (i != BZ_OK) {
  256. printf("BUNZIP2: uncompress or overwrite error %d - must RESET board to recover\n",
  257. i);
  258. return BOOTM_ERR_RESET;
  259. }
  260. *load_end = load + unc_len;
  261. break;
  262. #endif /* CONFIG_BZIP2 */
  263. #ifdef CONFIG_LZMA
  264. case IH_COMP_LZMA: {
  265. SizeT lzma_len = unc_len;
  266. int ret;
  267. printf(" Uncompressing %s ... ", type_name);
  268. ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
  269. image_buf, image_len);
  270. unc_len = lzma_len;
  271. if (ret != SZ_OK) {
  272. printf("LZMA: uncompress or overwrite error %d - must RESET board to recover\n",
  273. ret);
  274. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  275. return BOOTM_ERR_RESET;
  276. }
  277. *load_end = load + unc_len;
  278. break;
  279. }
  280. #endif /* CONFIG_LZMA */
  281. #ifdef CONFIG_LZO
  282. case IH_COMP_LZO: {
  283. size_t size = unc_len;
  284. int ret;
  285. printf(" Uncompressing %s ... ", type_name);
  286. ret = lzop_decompress(image_buf, image_len, load_buf, &size);
  287. if (ret != LZO_E_OK) {
  288. printf("LZO: uncompress or overwrite error %d - must RESET board to recover\n",
  289. ret);
  290. return BOOTM_ERR_RESET;
  291. }
  292. *load_end = load + size;
  293. break;
  294. }
  295. #endif /* CONFIG_LZO */
  296. default:
  297. printf("Unimplemented compression type %d\n", comp);
  298. return BOOTM_ERR_UNIMPLEMENTED;
  299. }
  300. puts("OK\n");
  301. return 0;
  302. }
  303. #ifndef USE_HOSTCC
  304. static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
  305. int boot_progress)
  306. {
  307. image_info_t os = images->os;
  308. ulong load = os.load;
  309. ulong blob_start = os.start;
  310. ulong blob_end = os.end;
  311. ulong image_start = os.image_start;
  312. ulong image_len = os.image_len;
  313. bool no_overlap;
  314. void *load_buf, *image_buf;
  315. int err;
  316. load_buf = map_sysmem(load, 0);
  317. image_buf = map_sysmem(os.image_start, image_len);
  318. err = decomp_image(os.comp, load, os.image_start, os.type, load_buf,
  319. image_buf, image_len, load_end);
  320. if (err) {
  321. bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
  322. return err;
  323. }
  324. flush_cache(load, (*load_end - load) * sizeof(ulong));
  325. debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
  326. bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
  327. no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
  328. if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
  329. debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
  330. blob_start, blob_end);
  331. debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
  332. *load_end);
  333. /* Check what type of image this is. */
  334. if (images->legacy_hdr_valid) {
  335. if (image_get_type(&images->legacy_hdr_os_copy)
  336. == IH_TYPE_MULTI)
  337. puts("WARNING: legacy format multi component image overwritten\n");
  338. return BOOTM_ERR_OVERLAP;
  339. } else {
  340. puts("ERROR: new format image overwritten - must RESET the board to recover\n");
  341. bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
  342. return BOOTM_ERR_RESET;
  343. }
  344. }
  345. return 0;
  346. }
  347. /**
  348. * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
  349. *
  350. * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
  351. * enabled)
  352. */
  353. ulong bootm_disable_interrupts(void)
  354. {
  355. ulong iflag;
  356. /*
  357. * We have reached the point of no return: we are going to
  358. * overwrite all exception vector code, so we cannot easily
  359. * recover from any failures any more...
  360. */
  361. iflag = disable_interrupts();
  362. #ifdef CONFIG_NETCONSOLE
  363. /* Stop the ethernet stack if NetConsole could have left it up */
  364. eth_halt();
  365. eth_unregister(eth_get_dev());
  366. #endif
  367. #if defined(CONFIG_CMD_USB)
  368. /*
  369. * turn off USB to prevent the host controller from writing to the
  370. * SDRAM while Linux is booting. This could happen (at least for OHCI
  371. * controller), because the HCCA (Host Controller Communication Area)
  372. * lies within the SDRAM and the host controller writes continously to
  373. * this area (as busmaster!). The HccaFrameNumber is for example
  374. * updated every 1 ms within the HCCA structure in SDRAM! For more
  375. * details see the OpenHCI specification.
  376. */
  377. usb_stop();
  378. #endif
  379. return iflag;
  380. }
  381. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  382. #define CONSOLE_ARG "console="
  383. #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
  384. static void fixup_silent_linux(void)
  385. {
  386. char *buf;
  387. const char *env_val;
  388. char *cmdline = getenv("bootargs");
  389. int want_silent;
  390. /*
  391. * Only fix cmdline when requested. The environment variable can be:
  392. *
  393. * no - we never fixup
  394. * yes - we always fixup
  395. * unset - we rely on the console silent flag
  396. */
  397. want_silent = getenv_yesno("silent_linux");
  398. if (want_silent == 0)
  399. return;
  400. else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
  401. return;
  402. debug("before silent fix-up: %s\n", cmdline);
  403. if (cmdline && (cmdline[0] != '\0')) {
  404. char *start = strstr(cmdline, CONSOLE_ARG);
  405. /* Allocate space for maximum possible new command line */
  406. buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
  407. if (!buf) {
  408. debug("%s: out of memory\n", __func__);
  409. return;
  410. }
  411. if (start) {
  412. char *end = strchr(start, ' ');
  413. int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
  414. strncpy(buf, cmdline, num_start_bytes);
  415. if (end)
  416. strcpy(buf + num_start_bytes, end);
  417. else
  418. buf[num_start_bytes] = '\0';
  419. } else {
  420. sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
  421. }
  422. env_val = buf;
  423. } else {
  424. buf = NULL;
  425. env_val = CONSOLE_ARG;
  426. }
  427. setenv("bootargs", env_val);
  428. debug("after silent fix-up: %s\n", env_val);
  429. free(buf);
  430. }
  431. #endif /* CONFIG_SILENT_CONSOLE */
  432. /**
  433. * Execute selected states of the bootm command.
  434. *
  435. * Note the arguments to this state must be the first argument, Any 'bootm'
  436. * or sub-command arguments must have already been taken.
  437. *
  438. * Note that if states contains more than one flag it MUST contain
  439. * BOOTM_STATE_START, since this handles and consumes the command line args.
  440. *
  441. * Also note that aside from boot_os_fn functions and bootm_load_os no other
  442. * functions we store the return value of in 'ret' may use a negative return
  443. * value, without special handling.
  444. *
  445. * @param cmdtp Pointer to bootm command table entry
  446. * @param flag Command flags (CMD_FLAG_...)
  447. * @param argc Number of subcommand arguments (0 = no arguments)
  448. * @param argv Arguments
  449. * @param states Mask containing states to run (BOOTM_STATE_...)
  450. * @param images Image header information
  451. * @param boot_progress 1 to show boot progress, 0 to not do this
  452. * @return 0 if ok, something else on error. Some errors will cause this
  453. * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
  454. * then the intent is to boot an OS, so this function will not return
  455. * unless the image type is standalone.
  456. */
  457. int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  458. int states, bootm_headers_t *images, int boot_progress)
  459. {
  460. boot_os_fn *boot_fn;
  461. ulong iflag = 0;
  462. int ret = 0, need_boot_fn;
  463. images->state |= states;
  464. /*
  465. * Work through the states and see how far we get. We stop on
  466. * any error.
  467. */
  468. if (states & BOOTM_STATE_START)
  469. ret = bootm_start(cmdtp, flag, argc, argv);
  470. if (!ret && (states & BOOTM_STATE_FINDOS))
  471. ret = bootm_find_os(cmdtp, flag, argc, argv);
  472. if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
  473. ret = bootm_find_other(cmdtp, flag, argc, argv);
  474. argc = 0; /* consume the args */
  475. }
  476. /* Load the OS */
  477. if (!ret && (states & BOOTM_STATE_LOADOS)) {
  478. ulong load_end;
  479. iflag = bootm_disable_interrupts();
  480. ret = bootm_load_os(images, &load_end, 0);
  481. if (ret == 0)
  482. lmb_reserve(&images->lmb, images->os.load,
  483. (load_end - images->os.load));
  484. else if (ret && ret != BOOTM_ERR_OVERLAP)
  485. goto err;
  486. else if (ret == BOOTM_ERR_OVERLAP)
  487. ret = 0;
  488. #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
  489. if (images->os.os == IH_OS_LINUX)
  490. fixup_silent_linux();
  491. #endif
  492. }
  493. /* Relocate the ramdisk */
  494. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  495. if (!ret && (states & BOOTM_STATE_RAMDISK)) {
  496. ulong rd_len = images->rd_end - images->rd_start;
  497. ret = boot_ramdisk_high(&images->lmb, images->rd_start,
  498. rd_len, &images->initrd_start, &images->initrd_end);
  499. if (!ret) {
  500. setenv_hex("initrd_start", images->initrd_start);
  501. setenv_hex("initrd_end", images->initrd_end);
  502. }
  503. }
  504. #endif
  505. #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
  506. if (!ret && (states & BOOTM_STATE_FDT)) {
  507. boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
  508. ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
  509. &images->ft_len);
  510. }
  511. #endif
  512. /* From now on, we need the OS boot function */
  513. if (ret)
  514. return ret;
  515. boot_fn = bootm_os_get_boot_func(images->os.os);
  516. need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
  517. BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
  518. BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
  519. if (boot_fn == NULL && need_boot_fn) {
  520. if (iflag)
  521. enable_interrupts();
  522. printf("ERROR: booting os '%s' (%d) is not supported\n",
  523. genimg_get_os_name(images->os.os), images->os.os);
  524. bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
  525. return 1;
  526. }
  527. /* Call various other states that are not generally used */
  528. if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
  529. ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
  530. if (!ret && (states & BOOTM_STATE_OS_BD_T))
  531. ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
  532. if (!ret && (states & BOOTM_STATE_OS_PREP))
  533. ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
  534. #ifdef CONFIG_TRACE
  535. /* Pretend to run the OS, then run a user command */
  536. if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
  537. char *cmd_list = getenv("fakegocmd");
  538. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
  539. images, boot_fn);
  540. if (!ret && cmd_list)
  541. ret = run_command_list(cmd_list, -1, flag);
  542. }
  543. #endif
  544. /* Check for unsupported subcommand. */
  545. if (ret) {
  546. puts("subcommand not supported\n");
  547. return ret;
  548. }
  549. /* Now run the OS! We hope this doesn't return */
  550. if (!ret && (states & BOOTM_STATE_OS_GO))
  551. ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
  552. images, boot_fn);
  553. /* Deal with any fallout */
  554. err:
  555. if (iflag)
  556. enable_interrupts();
  557. if (ret == BOOTM_ERR_UNIMPLEMENTED)
  558. bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
  559. else if (ret == BOOTM_ERR_RESET)
  560. do_reset(cmdtp, flag, argc, argv);
  561. return ret;
  562. }
  563. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  564. /**
  565. * image_get_kernel - verify legacy format kernel image
  566. * @img_addr: in RAM address of the legacy format image to be verified
  567. * @verify: data CRC verification flag
  568. *
  569. * image_get_kernel() verifies legacy image integrity and returns pointer to
  570. * legacy image header if image verification was completed successfully.
  571. *
  572. * returns:
  573. * pointer to a legacy image header if valid image was found
  574. * otherwise return NULL
  575. */
  576. static image_header_t *image_get_kernel(ulong img_addr, int verify)
  577. {
  578. image_header_t *hdr = (image_header_t *)img_addr;
  579. if (!image_check_magic(hdr)) {
  580. puts("Bad Magic Number\n");
  581. bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
  582. return NULL;
  583. }
  584. bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
  585. if (!image_check_hcrc(hdr)) {
  586. puts("Bad Header Checksum\n");
  587. bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
  588. return NULL;
  589. }
  590. bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
  591. image_print_contents(hdr);
  592. if (verify) {
  593. puts(" Verifying Checksum ... ");
  594. if (!image_check_dcrc(hdr)) {
  595. printf("Bad Data CRC\n");
  596. bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
  597. return NULL;
  598. }
  599. puts("OK\n");
  600. }
  601. bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
  602. if (!image_check_target_arch(hdr)) {
  603. printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
  604. bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
  605. return NULL;
  606. }
  607. return hdr;
  608. }
  609. #endif
  610. /**
  611. * boot_get_kernel - find kernel image
  612. * @os_data: pointer to a ulong variable, will hold os data start address
  613. * @os_len: pointer to a ulong variable, will hold os data length
  614. *
  615. * boot_get_kernel() tries to find a kernel image, verifies its integrity
  616. * and locates kernel data.
  617. *
  618. * returns:
  619. * pointer to image header if valid image was found, plus kernel start
  620. * address and length, otherwise NULL
  621. */
  622. static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
  623. char * const argv[], bootm_headers_t *images,
  624. ulong *os_data, ulong *os_len)
  625. {
  626. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  627. image_header_t *hdr;
  628. #endif
  629. ulong img_addr;
  630. const void *buf;
  631. const char *fit_uname_config = NULL;
  632. const char *fit_uname_kernel = NULL;
  633. #if defined(CONFIG_FIT)
  634. int os_noffset;
  635. #endif
  636. img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
  637. &fit_uname_config,
  638. &fit_uname_kernel);
  639. bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
  640. /* copy from dataflash if needed */
  641. img_addr = genimg_get_image(img_addr);
  642. /* check image type, for FIT images get FIT kernel node */
  643. *os_data = *os_len = 0;
  644. buf = map_sysmem(img_addr, 0);
  645. switch (genimg_get_format(buf)) {
  646. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  647. case IMAGE_FORMAT_LEGACY:
  648. printf("## Booting kernel from Legacy Image at %08lx ...\n",
  649. img_addr);
  650. hdr = image_get_kernel(img_addr, images->verify);
  651. if (!hdr)
  652. return NULL;
  653. bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  654. /* get os_data and os_len */
  655. switch (image_get_type(hdr)) {
  656. case IH_TYPE_KERNEL:
  657. case IH_TYPE_KERNEL_NOLOAD:
  658. *os_data = image_get_data(hdr);
  659. *os_len = image_get_data_size(hdr);
  660. break;
  661. case IH_TYPE_MULTI:
  662. image_multi_getimg(hdr, 0, os_data, os_len);
  663. break;
  664. case IH_TYPE_STANDALONE:
  665. *os_data = image_get_data(hdr);
  666. *os_len = image_get_data_size(hdr);
  667. break;
  668. default:
  669. printf("Wrong Image Type for %s command\n",
  670. cmdtp->name);
  671. bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
  672. return NULL;
  673. }
  674. /*
  675. * copy image header to allow for image overwrites during
  676. * kernel decompression.
  677. */
  678. memmove(&images->legacy_hdr_os_copy, hdr,
  679. sizeof(image_header_t));
  680. /* save pointer to image header */
  681. images->legacy_hdr_os = hdr;
  682. images->legacy_hdr_valid = 1;
  683. bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
  684. break;
  685. #endif
  686. #if defined(CONFIG_FIT)
  687. case IMAGE_FORMAT_FIT:
  688. os_noffset = fit_image_load(images, img_addr,
  689. &fit_uname_kernel, &fit_uname_config,
  690. IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
  691. BOOTSTAGE_ID_FIT_KERNEL_START,
  692. FIT_LOAD_IGNORED, os_data, os_len);
  693. if (os_noffset < 0)
  694. return NULL;
  695. images->fit_hdr_os = map_sysmem(img_addr, 0);
  696. images->fit_uname_os = fit_uname_kernel;
  697. images->fit_uname_cfg = fit_uname_config;
  698. images->fit_noffset_os = os_noffset;
  699. break;
  700. #endif
  701. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  702. case IMAGE_FORMAT_ANDROID:
  703. printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
  704. if (android_image_get_kernel(buf, images->verify,
  705. os_data, os_len))
  706. return NULL;
  707. break;
  708. #endif
  709. default:
  710. printf("Wrong Image Format for %s command\n", cmdtp->name);
  711. bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
  712. return NULL;
  713. }
  714. debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
  715. *os_data, *os_len, *os_len);
  716. return buf;
  717. }
  718. #else /* USE_HOSTCC */
  719. void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  720. {
  721. memmove(to, from, len);
  722. }
  723. static int bootm_host_load_image(const void *fit, int req_image_type)
  724. {
  725. const char *fit_uname_config = NULL;
  726. ulong data, len;
  727. bootm_headers_t images;
  728. int noffset;
  729. ulong load_end;
  730. uint8_t image_type;
  731. uint8_t imape_comp;
  732. void *load_buf;
  733. int ret;
  734. memset(&images, '\0', sizeof(images));
  735. images.verify = 1;
  736. noffset = fit_image_load(&images, (ulong)fit,
  737. NULL, &fit_uname_config,
  738. IH_ARCH_DEFAULT, req_image_type, -1,
  739. FIT_LOAD_IGNORED, &data, &len);
  740. if (noffset < 0)
  741. return noffset;
  742. if (fit_image_get_type(fit, noffset, &image_type)) {
  743. puts("Can't get image type!\n");
  744. return -EINVAL;
  745. }
  746. if (fit_image_get_comp(fit, noffset, &imape_comp)) {
  747. puts("Can't get image compression!\n");
  748. return -EINVAL;
  749. }
  750. /* Allow the image to expand by a factor of 4, should be safe */
  751. load_buf = malloc((1 << 20) + len * 4);
  752. ret = decomp_image(imape_comp, 0, data, image_type, load_buf,
  753. (void *)data, len, &load_end);
  754. free(load_buf);
  755. if (ret && ret != BOOTM_ERR_UNIMPLEMENTED)
  756. return ret;
  757. return 0;
  758. }
  759. int bootm_host_load_images(const void *fit, int cfg_noffset)
  760. {
  761. static uint8_t image_types[] = {
  762. IH_TYPE_KERNEL,
  763. IH_TYPE_FLATDT,
  764. IH_TYPE_RAMDISK,
  765. };
  766. int err = 0;
  767. int i;
  768. for (i = 0; i < ARRAY_SIZE(image_types); i++) {
  769. int ret;
  770. ret = bootm_host_load_image(fit, image_types[i]);
  771. if (!err && ret && ret != -ENOENT)
  772. err = ret;
  773. }
  774. /* Return the first error we found */
  775. return err;
  776. }
  777. #endif /* ndef USE_HOSTCC */