bootm.c 22 KB

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