spl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010
  4. * Texas Instruments, <www.ti.com>
  5. *
  6. * Aneesh V <aneesh@ti.com>
  7. */
  8. #include <common.h>
  9. #include <binman_sym.h>
  10. #include <dm.h>
  11. #include <spl.h>
  12. #include <asm/u-boot.h>
  13. #include <nand.h>
  14. #include <fat.h>
  15. #include <version.h>
  16. #include <image.h>
  17. #include <malloc.h>
  18. #include <dm/root.h>
  19. #include <linux/compiler.h>
  20. #include <fdt_support.h>
  21. #include <bootcount.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #ifndef CONFIG_SYS_UBOOT_START
  24. #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
  25. #endif
  26. #ifndef CONFIG_SYS_MONITOR_LEN
  27. /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
  28. #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
  29. #endif
  30. u32 *boot_params_ptr = NULL;
  31. /* See spl.h for information about this */
  32. binman_sym_declare(ulong, u_boot_any, image_pos);
  33. /* Define board data structure */
  34. static bd_t bdata __attribute__ ((section(".data")));
  35. /*
  36. * Board-specific Platform code can reimplement show_boot_progress () if needed
  37. */
  38. __weak void show_boot_progress(int val) {}
  39. /*
  40. * Default function to determine if u-boot or the OS should
  41. * be started. This implementation always returns 1.
  42. *
  43. * Please implement your own board specific funcion to do this.
  44. *
  45. * RETURN
  46. * 0 to not start u-boot
  47. * positive if u-boot should start
  48. */
  49. #ifdef CONFIG_SPL_OS_BOOT
  50. __weak int spl_start_uboot(void)
  51. {
  52. puts("SPL: Please implement spl_start_uboot() for your board\n");
  53. puts("SPL: Direct Linux boot not active!\n");
  54. return 1;
  55. }
  56. /* weak default platform specific function to initialize
  57. * dram banks
  58. */
  59. __weak int dram_init_banksize(void)
  60. {
  61. return 0;
  62. }
  63. /*
  64. * Weak default function for arch specific zImage check. Return zero
  65. * and fill start and end address if image is recognized.
  66. */
  67. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  68. {
  69. return 1;
  70. }
  71. #endif
  72. /* Weak default function for arch/board-specific fixups to the spl_image_info */
  73. void __weak spl_perform_fixups(struct spl_image_info *spl_image)
  74. {
  75. }
  76. void spl_fixup_fdt(void)
  77. {
  78. #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
  79. void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  80. int err;
  81. err = fdt_check_header(fdt_blob);
  82. if (err < 0) {
  83. printf("fdt_root: %s\n", fdt_strerror(err));
  84. return;
  85. }
  86. /* fixup the memory dt node */
  87. err = fdt_shrink_to_minimum(fdt_blob, 0);
  88. if (err == 0) {
  89. printf("spl: fdt_shrink_to_minimum err - %d\n", err);
  90. return;
  91. }
  92. err = arch_fixup_fdt(fdt_blob);
  93. if (err) {
  94. printf("spl: arch_fixup_fdt err - %d\n", err);
  95. return;
  96. }
  97. #endif
  98. }
  99. /*
  100. * Weak default function for board specific cleanup/preparation before
  101. * Linux boot. Some boards/platforms might not need it, so just provide
  102. * an empty stub here.
  103. */
  104. __weak void spl_board_prepare_for_linux(void)
  105. {
  106. /* Nothing to do! */
  107. }
  108. __weak void spl_board_prepare_for_boot(void)
  109. {
  110. /* Nothing to do! */
  111. }
  112. __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
  113. {
  114. return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
  115. }
  116. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  117. {
  118. ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
  119. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  120. /*
  121. * Binman error cases: address of the end of the previous region or the
  122. * start of the image's entry area (usually 0) if there is no previous
  123. * region.
  124. */
  125. if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
  126. /* Binman does not support separated entry addresses */
  127. spl_image->entry_point = u_boot_pos;
  128. spl_image->load_addr = u_boot_pos;
  129. } else {
  130. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  131. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  132. }
  133. spl_image->os = IH_OS_U_BOOT;
  134. spl_image->name = "U-Boot";
  135. }
  136. #ifdef CONFIG_SPL_LOAD_FIT_FULL
  137. /* Parse and load full fitImage in SPL */
  138. static int spl_load_fit_image(struct spl_image_info *spl_image,
  139. const struct image_header *header)
  140. {
  141. bootm_headers_t images;
  142. const char *fit_uname_config = NULL;
  143. const char *fit_uname_fdt = FIT_FDT_PROP;
  144. const char *uname;
  145. ulong fw_data = 0, dt_data = 0, img_data = 0;
  146. ulong fw_len = 0, dt_len = 0, img_len = 0;
  147. int idx, conf_noffset;
  148. int ret;
  149. #ifdef CONFIG_SPL_FIT_SIGNATURE
  150. images.verify = 1;
  151. #endif
  152. ret = fit_image_load(&images, (ulong)header,
  153. NULL, &fit_uname_config,
  154. IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
  155. FIT_LOAD_REQUIRED, &fw_data, &fw_len);
  156. if (ret < 0)
  157. return ret;
  158. spl_image->size = fw_len;
  159. spl_image->entry_point = fw_data;
  160. spl_image->load_addr = fw_data;
  161. spl_image->os = IH_OS_U_BOOT;
  162. spl_image->name = "U-Boot";
  163. debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
  164. (int)sizeof(spl_image->name), spl_image->name,
  165. spl_image->load_addr, spl_image->size);
  166. #ifdef CONFIG_SPL_FIT_SIGNATURE
  167. images.verify = 1;
  168. #endif
  169. fit_image_load(&images, (ulong)header,
  170. &fit_uname_fdt, &fit_uname_config,
  171. IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
  172. FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
  173. conf_noffset = fit_conf_get_node((const void *)header,
  174. fit_uname_config);
  175. if (conf_noffset <= 0)
  176. return 0;
  177. for (idx = 0;
  178. uname = fdt_stringlist_get((const void *)header, conf_noffset,
  179. FIT_LOADABLE_PROP, idx,
  180. NULL), uname;
  181. idx++)
  182. {
  183. #ifdef CONFIG_SPL_FIT_SIGNATURE
  184. images.verify = 1;
  185. #endif
  186. ret = fit_image_load(&images, (ulong)header,
  187. &uname, &fit_uname_config,
  188. IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
  189. FIT_LOAD_OPTIONAL_NON_ZERO,
  190. &img_data, &img_len);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. #endif
  197. int spl_parse_image_header(struct spl_image_info *spl_image,
  198. const struct image_header *header)
  199. {
  200. #ifdef CONFIG_SPL_LOAD_FIT_FULL
  201. int ret = spl_load_fit_image(spl_image, header);
  202. if (!ret)
  203. return ret;
  204. #endif
  205. if (image_get_magic(header) == IH_MAGIC) {
  206. #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
  207. u32 header_size = sizeof(struct image_header);
  208. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  209. /*
  210. * On some system (e.g. powerpc), the load-address and
  211. * entry-point is located at address 0. We can't load
  212. * to 0-0x40. So skip header in this case.
  213. */
  214. spl_image->load_addr = image_get_load(header);
  215. spl_image->entry_point = image_get_ep(header);
  216. spl_image->size = image_get_data_size(header);
  217. } else {
  218. spl_image->entry_point = image_get_load(header);
  219. /* Load including the header */
  220. spl_image->load_addr = spl_image->entry_point -
  221. header_size;
  222. spl_image->size = image_get_data_size(header) +
  223. header_size;
  224. }
  225. spl_image->os = image_get_os(header);
  226. spl_image->name = image_get_name(header);
  227. debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
  228. IH_NMLEN, spl_image->name,
  229. spl_image->load_addr, spl_image->size);
  230. #else
  231. /* LEGACY image not supported */
  232. debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
  233. return -EINVAL;
  234. #endif
  235. } else {
  236. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  237. /*
  238. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  239. * code which loads images in SPL cannot guarantee that
  240. * absolutely all read errors will be reported.
  241. * An example is the LPC32XX MLC NAND driver, which
  242. * will consider that a completely unreadable NAND block
  243. * is bad, and thus should be skipped silently.
  244. */
  245. panic("** no mkimage signature but raw image not supported");
  246. #endif
  247. #ifdef CONFIG_SPL_OS_BOOT
  248. ulong start, end;
  249. if (!bootz_setup((ulong)header, &start, &end)) {
  250. spl_image->name = "Linux";
  251. spl_image->os = IH_OS_LINUX;
  252. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  253. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  254. spl_image->size = end - start;
  255. debug("spl: payload zImage, load addr: 0x%lx size: %d\n",
  256. spl_image->load_addr, spl_image->size);
  257. return 0;
  258. }
  259. #endif
  260. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  261. /* Signature not found - assume u-boot.bin */
  262. debug("mkimage signature not found - ih_magic = %x\n",
  263. header->ih_magic);
  264. spl_set_header_raw_uboot(spl_image);
  265. #else
  266. /* RAW image not supported, proceed to other boot methods. */
  267. debug("Raw boot image support not enabled, proceeding to other boot methods\n");
  268. return -EINVAL;
  269. #endif
  270. }
  271. return 0;
  272. }
  273. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  274. {
  275. typedef void __noreturn (*image_entry_noargs_t)(void);
  276. image_entry_noargs_t image_entry =
  277. (image_entry_noargs_t)spl_image->entry_point;
  278. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  279. image_entry();
  280. }
  281. static int spl_common_init(bool setup_malloc)
  282. {
  283. int ret;
  284. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  285. if (setup_malloc) {
  286. #ifdef CONFIG_MALLOC_F_ADDR
  287. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  288. #endif
  289. gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
  290. gd->malloc_ptr = 0;
  291. }
  292. #endif
  293. ret = bootstage_init(true);
  294. if (ret) {
  295. debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
  296. ret);
  297. return ret;
  298. }
  299. bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
  300. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  301. ret = fdtdec_setup();
  302. if (ret) {
  303. debug("fdtdec_setup() returned error %d\n", ret);
  304. return ret;
  305. }
  306. }
  307. if (CONFIG_IS_ENABLED(DM)) {
  308. bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
  309. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  310. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  311. bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
  312. if (ret) {
  313. debug("dm_init_and_scan() returned error %d\n", ret);
  314. return ret;
  315. }
  316. }
  317. return 0;
  318. }
  319. void spl_set_bd(void)
  320. {
  321. if (!gd->bd)
  322. gd->bd = &bdata;
  323. }
  324. int spl_early_init(void)
  325. {
  326. int ret;
  327. debug("%s\n", __func__);
  328. ret = spl_common_init(true);
  329. if (ret)
  330. return ret;
  331. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  332. return 0;
  333. }
  334. int spl_init(void)
  335. {
  336. int ret;
  337. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  338. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  339. debug("%s\n", __func__);
  340. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  341. ret = spl_common_init(setup_malloc);
  342. if (ret)
  343. return ret;
  344. }
  345. gd->flags |= GD_FLG_SPL_INIT;
  346. return 0;
  347. }
  348. #ifndef BOOT_DEVICE_NONE
  349. #define BOOT_DEVICE_NONE 0xdeadbeef
  350. #endif
  351. __weak void board_boot_order(u32 *spl_boot_list)
  352. {
  353. spl_boot_list[0] = spl_boot_device();
  354. }
  355. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  356. {
  357. struct spl_image_loader *drv =
  358. ll_entry_start(struct spl_image_loader, spl_image_loader);
  359. const int n_ents =
  360. ll_entry_count(struct spl_image_loader, spl_image_loader);
  361. struct spl_image_loader *entry;
  362. for (entry = drv; entry != drv + n_ents; entry++) {
  363. if (boot_device == entry->boot_device)
  364. return entry;
  365. }
  366. /* Not found */
  367. return NULL;
  368. }
  369. static int spl_load_image(struct spl_image_info *spl_image,
  370. struct spl_image_loader *loader)
  371. {
  372. struct spl_boot_device bootdev;
  373. bootdev.boot_device = loader->boot_device;
  374. bootdev.boot_device_name = NULL;
  375. return loader->load_image(spl_image, &bootdev);
  376. }
  377. /**
  378. * boot_from_devices() - Try loading an booting U-Boot from a list of devices
  379. *
  380. * @spl_image: Place to put the image details if successful
  381. * @spl_boot_list: List of boot devices to try
  382. * @count: Number of elements in spl_boot_list
  383. * @return 0 if OK, -ve on error
  384. */
  385. static int boot_from_devices(struct spl_image_info *spl_image,
  386. u32 spl_boot_list[], int count)
  387. {
  388. int i;
  389. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  390. struct spl_image_loader *loader;
  391. loader = spl_ll_find_loader(spl_boot_list[i]);
  392. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  393. if (loader)
  394. printf("Trying to boot from %s\n", loader->name);
  395. else
  396. puts("SPL: Unsupported Boot Device!\n");
  397. #endif
  398. if (loader && !spl_load_image(spl_image, loader)) {
  399. spl_image->boot_device = spl_boot_list[i];
  400. return 0;
  401. }
  402. }
  403. return -ENODEV;
  404. }
  405. void board_init_r(gd_t *dummy1, ulong dummy2)
  406. {
  407. u32 spl_boot_list[] = {
  408. BOOT_DEVICE_NONE,
  409. BOOT_DEVICE_NONE,
  410. BOOT_DEVICE_NONE,
  411. BOOT_DEVICE_NONE,
  412. BOOT_DEVICE_NONE,
  413. };
  414. struct spl_image_info spl_image;
  415. debug(">>spl:board_init_r()\n");
  416. spl_set_bd();
  417. #ifdef CONFIG_SPL_OS_BOOT
  418. dram_init_banksize();
  419. #endif
  420. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  421. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  422. CONFIG_SYS_SPL_MALLOC_SIZE);
  423. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  424. #endif
  425. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  426. if (spl_init())
  427. hang();
  428. }
  429. #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
  430. /*
  431. * timer_init() does not exist on PPC systems. The timer is initialized
  432. * and enabled (decrementer) in interrupt_init() here.
  433. */
  434. timer_init();
  435. #endif
  436. #if CONFIG_IS_ENABLED(BOARD_INIT)
  437. spl_board_init();
  438. #endif
  439. bootcount_inc();
  440. memset(&spl_image, '\0', sizeof(spl_image));
  441. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  442. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  443. #endif
  444. spl_image.boot_device = BOOT_DEVICE_NONE;
  445. board_boot_order(spl_boot_list);
  446. if (boot_from_devices(&spl_image, spl_boot_list,
  447. ARRAY_SIZE(spl_boot_list))) {
  448. puts("SPL: failed to boot from all boot devices\n");
  449. hang();
  450. }
  451. spl_perform_fixups(&spl_image);
  452. #ifdef CONFIG_CPU_V7M
  453. spl_image.entry_point |= 0x1;
  454. #endif
  455. switch (spl_image.os) {
  456. case IH_OS_U_BOOT:
  457. debug("Jumping to U-Boot\n");
  458. break;
  459. #if CONFIG_IS_ENABLED(ATF)
  460. case IH_OS_ARM_TRUSTED_FIRMWARE:
  461. debug("Jumping to U-Boot via ARM Trusted Firmware\n");
  462. spl_invoke_atf(&spl_image);
  463. break;
  464. #endif
  465. #if CONFIG_IS_ENABLED(OPTEE)
  466. case IH_OS_TEE:
  467. debug("Jumping to U-Boot via OP-TEE\n");
  468. spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
  469. (void *)spl_image.entry_point);
  470. break;
  471. #endif
  472. #ifdef CONFIG_SPL_OS_BOOT
  473. case IH_OS_LINUX:
  474. debug("Jumping to Linux\n");
  475. spl_fixup_fdt();
  476. spl_board_prepare_for_linux();
  477. jump_to_image_linux(&spl_image);
  478. #endif
  479. default:
  480. debug("Unsupported OS image.. Jumping nevertheless..\n");
  481. }
  482. #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  483. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  484. gd->malloc_ptr / 1024);
  485. #endif
  486. #ifdef CONFIG_BOOTSTAGE_STASH
  487. int ret;
  488. bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
  489. ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  490. CONFIG_BOOTSTAGE_STASH_SIZE);
  491. if (ret)
  492. debug("Failed to stash bootstage: err=%d\n", ret);
  493. #endif
  494. debug("loaded - jumping to U-Boot...\n");
  495. spl_board_prepare_for_boot();
  496. jump_to_image_no_args(&spl_image);
  497. }
  498. #ifdef CONFIG_SPL_SERIAL_SUPPORT
  499. /*
  500. * This requires UART clocks to be enabled. In order for this to work the
  501. * caller must ensure that the gd pointer is valid.
  502. */
  503. void preloader_console_init(void)
  504. {
  505. gd->baudrate = CONFIG_BAUDRATE;
  506. serial_init(); /* serial communications setup */
  507. gd->have_console = 1;
  508. #ifndef CONFIG_SPL_DISABLE_BANNER_PRINT
  509. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  510. U_BOOT_TIME " " U_BOOT_TZ ")\n");
  511. #endif
  512. #ifdef CONFIG_SPL_DISPLAY_PRINT
  513. spl_display_print();
  514. #endif
  515. }
  516. #endif
  517. /**
  518. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  519. *
  520. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  521. * for the main board_init_r() execution. This is typically because we need
  522. * more stack space for things like the MMC sub-system.
  523. *
  524. * This function calculates the stack position, copies the global_data into
  525. * place, sets the new gd (except for ARM, for which setting GD within a C
  526. * function may not always work) and returns the new stack position. The
  527. * caller is responsible for setting up the sp register and, in the case
  528. * of ARM, setting up gd.
  529. *
  530. * All of this is done using the same layout and alignments as done in
  531. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  532. *
  533. * @return new stack location, or 0 to use the same stack
  534. */
  535. ulong spl_relocate_stack_gd(void)
  536. {
  537. #ifdef CONFIG_SPL_STACK_R
  538. gd_t *new_gd;
  539. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  540. #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  541. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  542. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  543. gd->malloc_base = ptr;
  544. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  545. gd->malloc_ptr = 0;
  546. }
  547. #endif
  548. /* Get stack position: use 8-byte alignment for ABI compliance */
  549. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  550. new_gd = (gd_t *)ptr;
  551. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  552. #if CONFIG_IS_ENABLED(DM)
  553. dm_fixup_for_gd_move(new_gd);
  554. #endif
  555. #if !defined(CONFIG_ARM)
  556. gd = new_gd;
  557. #endif
  558. return ptr;
  559. #else
  560. return 0;
  561. #endif
  562. }