spl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Aneesh V <aneesh@ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <binman_sym.h>
  11. #include <dm.h>
  12. #include <spl.h>
  13. #include <asm/u-boot.h>
  14. #include <nand.h>
  15. #include <fat.h>
  16. #include <version.h>
  17. #include <image.h>
  18. #include <malloc.h>
  19. #include <dm/root.h>
  20. #include <linux/compiler.h>
  21. #include <fdt_support.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, 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. void spl_fixup_fdt(void)
  73. {
  74. #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
  75. void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  76. int err;
  77. err = fdt_check_header(fdt_blob);
  78. if (err < 0) {
  79. printf("fdt_root: %s\n", fdt_strerror(err));
  80. return;
  81. }
  82. /* fixup the memory dt node */
  83. err = fdt_shrink_to_minimum(fdt_blob, 0);
  84. if (err == 0) {
  85. printf("spl: fdt_shrink_to_minimum err - %d\n", err);
  86. return;
  87. }
  88. err = arch_fixup_fdt(fdt_blob);
  89. if (err) {
  90. printf("spl: arch_fixup_fdt err - %d\n", err);
  91. return;
  92. }
  93. #endif
  94. }
  95. /*
  96. * Weak default function for board specific cleanup/preparation before
  97. * Linux boot. Some boards/platforms might not need it, so just provide
  98. * an empty stub here.
  99. */
  100. __weak void spl_board_prepare_for_linux(void)
  101. {
  102. /* Nothing to do! */
  103. }
  104. __weak void spl_board_prepare_for_boot(void)
  105. {
  106. /* Nothing to do! */
  107. }
  108. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  109. {
  110. ulong u_boot_pos = binman_sym(ulong, u_boot_any, pos);
  111. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  112. /*
  113. * Binman error cases: address of the end of the previous region or the
  114. * start of the image's entry area (usually 0) if there is no previous
  115. * region.
  116. */
  117. if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
  118. /* Binman does not support separated entry addresses */
  119. spl_image->entry_point = u_boot_pos;
  120. spl_image->load_addr = u_boot_pos;
  121. } else {
  122. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  123. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  124. }
  125. spl_image->os = IH_OS_U_BOOT;
  126. spl_image->name = "U-Boot";
  127. }
  128. int spl_parse_image_header(struct spl_image_info *spl_image,
  129. const struct image_header *header)
  130. {
  131. if (image_get_magic(header) == IH_MAGIC) {
  132. #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
  133. u32 header_size = sizeof(struct image_header);
  134. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  135. /*
  136. * On some system (e.g. powerpc), the load-address and
  137. * entry-point is located at address 0. We can't load
  138. * to 0-0x40. So skip header in this case.
  139. */
  140. spl_image->load_addr = image_get_load(header);
  141. spl_image->entry_point = image_get_ep(header);
  142. spl_image->size = image_get_data_size(header);
  143. } else {
  144. spl_image->entry_point = image_get_load(header);
  145. /* Load including the header */
  146. spl_image->load_addr = spl_image->entry_point -
  147. header_size;
  148. spl_image->size = image_get_data_size(header) +
  149. header_size;
  150. }
  151. spl_image->os = image_get_os(header);
  152. spl_image->name = image_get_name(header);
  153. debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
  154. IH_NMLEN, spl_image->name,
  155. spl_image->load_addr, spl_image->size);
  156. #else
  157. /* LEGACY image not supported */
  158. debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
  159. return -EINVAL;
  160. #endif
  161. } else {
  162. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  163. /*
  164. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  165. * code which loads images in SPL cannot guarantee that
  166. * absolutely all read errors will be reported.
  167. * An example is the LPC32XX MLC NAND driver, which
  168. * will consider that a completely unreadable NAND block
  169. * is bad, and thus should be skipped silently.
  170. */
  171. panic("** no mkimage signature but raw image not supported");
  172. #endif
  173. #ifdef CONFIG_SPL_OS_BOOT
  174. ulong start, end;
  175. if (!bootz_setup((ulong)header, &start, &end)) {
  176. spl_image->name = "Linux";
  177. spl_image->os = IH_OS_LINUX;
  178. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  179. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  180. spl_image->size = end - start;
  181. debug("spl: payload zImage, load addr: 0x%lx size: %d\n",
  182. spl_image->load_addr, spl_image->size);
  183. return 0;
  184. }
  185. #endif
  186. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  187. /* Signature not found - assume u-boot.bin */
  188. debug("mkimage signature not found - ih_magic = %x\n",
  189. header->ih_magic);
  190. spl_set_header_raw_uboot(spl_image);
  191. #else
  192. /* RAW image not supported, proceed to other boot methods. */
  193. debug("Raw boot image support not enabled, proceeding to other boot methods\n");
  194. return -EINVAL;
  195. #endif
  196. }
  197. return 0;
  198. }
  199. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  200. {
  201. typedef void __noreturn (*image_entry_noargs_t)(void);
  202. image_entry_noargs_t image_entry =
  203. (image_entry_noargs_t)spl_image->entry_point;
  204. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  205. image_entry();
  206. }
  207. static int spl_common_init(bool setup_malloc)
  208. {
  209. int ret;
  210. debug("spl_early_init()\n");
  211. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  212. if (setup_malloc) {
  213. #ifdef CONFIG_MALLOC_F_ADDR
  214. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  215. #endif
  216. gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
  217. gd->malloc_ptr = 0;
  218. }
  219. #endif
  220. ret = bootstage_init(true);
  221. if (ret) {
  222. debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
  223. ret);
  224. return ret;
  225. }
  226. bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
  227. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  228. ret = fdtdec_setup();
  229. if (ret) {
  230. debug("fdtdec_setup() returned error %d\n", ret);
  231. return ret;
  232. }
  233. }
  234. if (CONFIG_IS_ENABLED(DM)) {
  235. bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
  236. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  237. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  238. bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
  239. if (ret) {
  240. debug("dm_init_and_scan() returned error %d\n", ret);
  241. return ret;
  242. }
  243. }
  244. return 0;
  245. }
  246. void spl_set_bd(void)
  247. {
  248. if (!gd->bd)
  249. gd->bd = &bdata;
  250. }
  251. int spl_early_init(void)
  252. {
  253. int ret;
  254. ret = spl_common_init(true);
  255. if (ret)
  256. return ret;
  257. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  258. return 0;
  259. }
  260. int spl_init(void)
  261. {
  262. int ret;
  263. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  264. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  265. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  266. ret = spl_common_init(setup_malloc);
  267. if (ret)
  268. return ret;
  269. }
  270. gd->flags |= GD_FLG_SPL_INIT;
  271. return 0;
  272. }
  273. #ifndef BOOT_DEVICE_NONE
  274. #define BOOT_DEVICE_NONE 0xdeadbeef
  275. #endif
  276. __weak void board_boot_order(u32 *spl_boot_list)
  277. {
  278. spl_boot_list[0] = spl_boot_device();
  279. }
  280. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  281. {
  282. struct spl_image_loader *drv =
  283. ll_entry_start(struct spl_image_loader, spl_image_loader);
  284. const int n_ents =
  285. ll_entry_count(struct spl_image_loader, spl_image_loader);
  286. struct spl_image_loader *entry;
  287. for (entry = drv; entry != drv + n_ents; entry++) {
  288. if (boot_device == entry->boot_device)
  289. return entry;
  290. }
  291. /* Not found */
  292. return NULL;
  293. }
  294. static int spl_load_image(struct spl_image_info *spl_image,
  295. struct spl_image_loader *loader)
  296. {
  297. struct spl_boot_device bootdev;
  298. bootdev.boot_device = loader->boot_device;
  299. bootdev.boot_device_name = NULL;
  300. return loader->load_image(spl_image, &bootdev);
  301. }
  302. /**
  303. * boot_from_devices() - Try loading an booting U-Boot from a list of devices
  304. *
  305. * @spl_image: Place to put the image details if successful
  306. * @spl_boot_list: List of boot devices to try
  307. * @count: Number of elements in spl_boot_list
  308. * @return 0 if OK, -ve on error
  309. */
  310. static int boot_from_devices(struct spl_image_info *spl_image,
  311. u32 spl_boot_list[], int count)
  312. {
  313. int i;
  314. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  315. struct spl_image_loader *loader;
  316. loader = spl_ll_find_loader(spl_boot_list[i]);
  317. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  318. if (loader)
  319. printf("Trying to boot from %s\n", loader->name);
  320. else
  321. puts("SPL: Unsupported Boot Device!\n");
  322. #endif
  323. if (loader && !spl_load_image(spl_image, loader))
  324. return 0;
  325. }
  326. return -ENODEV;
  327. }
  328. void board_init_r(gd_t *dummy1, ulong dummy2)
  329. {
  330. u32 spl_boot_list[] = {
  331. BOOT_DEVICE_NONE,
  332. BOOT_DEVICE_NONE,
  333. BOOT_DEVICE_NONE,
  334. BOOT_DEVICE_NONE,
  335. BOOT_DEVICE_NONE,
  336. };
  337. struct spl_image_info spl_image;
  338. debug(">>spl:board_init_r()\n");
  339. spl_set_bd();
  340. #ifdef CONFIG_SPL_OS_BOOT
  341. dram_init_banksize();
  342. #endif
  343. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  344. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  345. CONFIG_SYS_SPL_MALLOC_SIZE);
  346. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  347. #endif
  348. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  349. if (spl_init())
  350. hang();
  351. }
  352. #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
  353. /*
  354. * timer_init() does not exist on PPC systems. The timer is initialized
  355. * and enabled (decrementer) in interrupt_init() here.
  356. */
  357. timer_init();
  358. #endif
  359. #if CONFIG_IS_ENABLED(BOARD_INIT)
  360. spl_board_init();
  361. #endif
  362. memset(&spl_image, '\0', sizeof(spl_image));
  363. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  364. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  365. #endif
  366. board_boot_order(spl_boot_list);
  367. if (boot_from_devices(&spl_image, spl_boot_list,
  368. ARRAY_SIZE(spl_boot_list))) {
  369. puts("SPL: failed to boot from all boot devices\n");
  370. hang();
  371. }
  372. #ifdef CONFIG_CPU_V7M
  373. spl_image.entry_point |= 0x1;
  374. #endif
  375. switch (spl_image.os) {
  376. case IH_OS_U_BOOT:
  377. debug("Jumping to U-Boot\n");
  378. break;
  379. #if CONFIG_IS_ENABLED(ATF)
  380. case IH_OS_ARM_TRUSTED_FIRMWARE:
  381. debug("Jumping to U-Boot via ARM Trusted Firmware\n");
  382. spl_invoke_atf(&spl_image);
  383. break;
  384. #endif
  385. #ifdef CONFIG_SPL_OS_BOOT
  386. case IH_OS_LINUX:
  387. debug("Jumping to Linux\n");
  388. spl_fixup_fdt();
  389. spl_board_prepare_for_linux();
  390. jump_to_image_linux(&spl_image);
  391. #endif
  392. default:
  393. debug("Unsupported OS image.. Jumping nevertheless..\n");
  394. }
  395. #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  396. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  397. gd->malloc_ptr / 1024);
  398. #endif
  399. #ifdef CONFIG_BOOTSTAGE_STASH
  400. int ret;
  401. bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
  402. ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  403. CONFIG_BOOTSTAGE_STASH_SIZE);
  404. if (ret)
  405. debug("Failed to stash bootstage: err=%d\n", ret);
  406. #endif
  407. debug("loaded - jumping to U-Boot...\n");
  408. spl_board_prepare_for_boot();
  409. jump_to_image_no_args(&spl_image);
  410. }
  411. #ifdef CONFIG_SPL_SERIAL_SUPPORT
  412. /*
  413. * This requires UART clocks to be enabled. In order for this to work the
  414. * caller must ensure that the gd pointer is valid.
  415. */
  416. void preloader_console_init(void)
  417. {
  418. gd->baudrate = CONFIG_BAUDRATE;
  419. serial_init(); /* serial communications setup */
  420. gd->have_console = 1;
  421. #ifndef CONFIG_SPL_DISABLE_BANNER_PRINT
  422. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  423. U_BOOT_TIME " " U_BOOT_TZ ")\n");
  424. #endif
  425. #ifdef CONFIG_SPL_DISPLAY_PRINT
  426. spl_display_print();
  427. #endif
  428. }
  429. #endif
  430. /**
  431. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  432. *
  433. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  434. * for the main board_init_r() execution. This is typically because we need
  435. * more stack space for things like the MMC sub-system.
  436. *
  437. * This function calculates the stack position, copies the global_data into
  438. * place, sets the new gd (except for ARM, for which setting GD within a C
  439. * function may not always work) and returns the new stack position. The
  440. * caller is responsible for setting up the sp register and, in the case
  441. * of ARM, setting up gd.
  442. *
  443. * All of this is done using the same layout and alignments as done in
  444. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  445. *
  446. * @return new stack location, or 0 to use the same stack
  447. */
  448. ulong spl_relocate_stack_gd(void)
  449. {
  450. #ifdef CONFIG_SPL_STACK_R
  451. gd_t *new_gd;
  452. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  453. #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  454. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  455. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  456. gd->malloc_base = ptr;
  457. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  458. gd->malloc_ptr = 0;
  459. }
  460. #endif
  461. /* Get stack position: use 8-byte alignment for ABI compliance */
  462. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  463. new_gd = (gd_t *)ptr;
  464. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  465. #if CONFIG_IS_ENABLED(DM)
  466. dm_fixup_for_gd_move(new_gd);
  467. #endif
  468. #if !defined(CONFIG_ARM)
  469. gd = new_gd;
  470. #endif
  471. return ptr;
  472. #else
  473. return 0;
  474. #endif
  475. }