bootm.c 27 KB

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