bootm.c 29 KB

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