bootm.c 24 KB

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