spl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 <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. DECLARE_GLOBAL_DATA_PTR;
  22. #ifndef CONFIG_SYS_UBOOT_START
  23. #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
  24. #endif
  25. #ifndef CONFIG_SYS_MONITOR_LEN
  26. /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
  27. #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
  28. #endif
  29. u32 *boot_params_ptr = NULL;
  30. /* Define board data structure */
  31. static bd_t bdata __attribute__ ((section(".data")));
  32. /*
  33. * Board-specific Platform code can reimplement show_boot_progress () if needed
  34. */
  35. __weak void show_boot_progress(int val) {}
  36. /*
  37. * Default function to determine if u-boot or the OS should
  38. * be started. This implementation always returns 1.
  39. *
  40. * Please implement your own board specific funcion to do this.
  41. *
  42. * RETURN
  43. * 0 to not start u-boot
  44. * positive if u-boot should start
  45. */
  46. #ifdef CONFIG_SPL_OS_BOOT
  47. __weak int spl_start_uboot(void)
  48. {
  49. puts("SPL: Please implement spl_start_uboot() for your board\n");
  50. puts("SPL: Direct Linux boot not active!\n");
  51. return 1;
  52. }
  53. /* weak default platform specific function to initialize
  54. * dram banks
  55. */
  56. __weak int dram_init_banksize(void)
  57. {
  58. return 0;
  59. }
  60. /*
  61. * Weak default function for arch specific zImage check. Return zero
  62. * and fill start and end address if image is recognized.
  63. */
  64. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  65. {
  66. return 1;
  67. }
  68. #endif
  69. void spl_fixup_fdt(void)
  70. {
  71. #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
  72. void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  73. int err;
  74. err = fdt_check_header(fdt_blob);
  75. if (err < 0) {
  76. printf("fdt_root: %s\n", fdt_strerror(err));
  77. return;
  78. }
  79. /* fixup the memory dt node */
  80. err = fdt_shrink_to_minimum(fdt_blob, 0);
  81. if (err == 0) {
  82. printf("spl: fdt_shrink_to_minimum err - %d\n", err);
  83. return;
  84. }
  85. err = arch_fixup_fdt(fdt_blob);
  86. if (err) {
  87. printf("spl: arch_fixup_fdt err - %d\n", err);
  88. return;
  89. }
  90. #endif
  91. }
  92. /*
  93. * Weak default function for board specific cleanup/preparation before
  94. * Linux boot. Some boards/platforms might not need it, so just provide
  95. * an empty stub here.
  96. */
  97. __weak void spl_board_prepare_for_linux(void)
  98. {
  99. /* Nothing to do! */
  100. }
  101. __weak void spl_board_prepare_for_boot(void)
  102. {
  103. /* Nothing to do! */
  104. }
  105. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  106. {
  107. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  108. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  109. #ifdef CONFIG_CPU_V7M
  110. spl_image->entry_point |= 0x1;
  111. #endif
  112. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  113. spl_image->os = IH_OS_U_BOOT;
  114. spl_image->name = "U-Boot";
  115. }
  116. int spl_parse_image_header(struct spl_image_info *spl_image,
  117. const struct image_header *header)
  118. {
  119. if (image_get_magic(header) == IH_MAGIC) {
  120. #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
  121. u32 header_size = sizeof(struct image_header);
  122. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  123. /*
  124. * On some system (e.g. powerpc), the load-address and
  125. * entry-point is located at address 0. We can't load
  126. * to 0-0x40. So skip header in this case.
  127. */
  128. spl_image->load_addr = image_get_load(header);
  129. spl_image->entry_point = image_get_ep(header);
  130. spl_image->size = image_get_data_size(header);
  131. } else {
  132. spl_image->entry_point = image_get_load(header);
  133. /* Load including the header */
  134. spl_image->load_addr = spl_image->entry_point -
  135. header_size;
  136. spl_image->size = image_get_data_size(header) +
  137. header_size;
  138. }
  139. spl_image->os = image_get_os(header);
  140. spl_image->name = image_get_name(header);
  141. debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
  142. (int)sizeof(spl_image->name), spl_image->name,
  143. spl_image->load_addr, spl_image->size);
  144. #else
  145. /* LEGACY image not supported */
  146. debug("Legacy boot image support not enabled, proceeding to other boot methods");
  147. return -EINVAL;
  148. #endif
  149. } else {
  150. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  151. /*
  152. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  153. * code which loads images in SPL cannot guarantee that
  154. * absolutely all read errors will be reported.
  155. * An example is the LPC32XX MLC NAND driver, which
  156. * will consider that a completely unreadable NAND block
  157. * is bad, and thus should be skipped silently.
  158. */
  159. panic("** no mkimage signature but raw image not supported");
  160. #endif
  161. #ifdef CONFIG_SPL_OS_BOOT
  162. ulong start, end;
  163. if (!bootz_setup((ulong)header, &start, &end)) {
  164. spl_image->name = "Linux";
  165. spl_image->os = IH_OS_LINUX;
  166. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  167. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  168. spl_image->size = end - start;
  169. debug("spl: payload zImage, load addr: 0x%lx size: %d\n",
  170. spl_image->load_addr, spl_image->size);
  171. return 0;
  172. }
  173. #endif
  174. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  175. /* Signature not found - assume u-boot.bin */
  176. debug("mkimage signature not found - ih_magic = %x\n",
  177. header->ih_magic);
  178. spl_set_header_raw_uboot(spl_image);
  179. #else
  180. /* RAW image not supported, proceed to other boot methods. */
  181. debug("Raw boot image support not enabled, proceeding to other boot methods");
  182. return -EINVAL;
  183. #endif
  184. }
  185. return 0;
  186. }
  187. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  188. {
  189. typedef void __noreturn (*image_entry_noargs_t)(void);
  190. image_entry_noargs_t image_entry =
  191. (image_entry_noargs_t)spl_image->entry_point;
  192. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  193. image_entry();
  194. }
  195. static int spl_common_init(bool setup_malloc)
  196. {
  197. int ret;
  198. debug("spl_early_init()\n");
  199. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  200. if (setup_malloc) {
  201. #ifdef CONFIG_MALLOC_F_ADDR
  202. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  203. #endif
  204. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  205. gd->malloc_ptr = 0;
  206. }
  207. #endif
  208. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  209. ret = fdtdec_setup();
  210. if (ret) {
  211. debug("fdtdec_setup() returned error %d\n", ret);
  212. return ret;
  213. }
  214. }
  215. if (IS_ENABLED(CONFIG_SPL_DM)) {
  216. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  217. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  218. if (ret) {
  219. debug("dm_init_and_scan() returned error %d\n", ret);
  220. return ret;
  221. }
  222. }
  223. return 0;
  224. }
  225. int spl_early_init(void)
  226. {
  227. int ret;
  228. ret = spl_common_init(true);
  229. if (ret)
  230. return ret;
  231. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  232. return 0;
  233. }
  234. int spl_init(void)
  235. {
  236. int ret;
  237. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  238. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  239. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  240. ret = spl_common_init(setup_malloc);
  241. if (ret)
  242. return ret;
  243. }
  244. gd->flags |= GD_FLG_SPL_INIT;
  245. return 0;
  246. }
  247. #ifndef BOOT_DEVICE_NONE
  248. #define BOOT_DEVICE_NONE 0xdeadbeef
  249. #endif
  250. __weak void board_boot_order(u32 *spl_boot_list)
  251. {
  252. spl_boot_list[0] = spl_boot_device();
  253. }
  254. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  255. {
  256. struct spl_image_loader *drv =
  257. ll_entry_start(struct spl_image_loader, spl_image_loader);
  258. const int n_ents =
  259. ll_entry_count(struct spl_image_loader, spl_image_loader);
  260. struct spl_image_loader *entry;
  261. for (entry = drv; entry != drv + n_ents; entry++) {
  262. if (boot_device == entry->boot_device)
  263. return entry;
  264. }
  265. /* Not found */
  266. return NULL;
  267. }
  268. static int spl_load_image(struct spl_image_info *spl_image,
  269. struct spl_image_loader *loader)
  270. {
  271. struct spl_boot_device bootdev;
  272. bootdev.boot_device = loader->boot_device;
  273. bootdev.boot_device_name = NULL;
  274. return loader->load_image(spl_image, &bootdev);
  275. }
  276. /**
  277. * boot_from_devices() - Try loading an booting U-Boot from a list of devices
  278. *
  279. * @spl_image: Place to put the image details if successful
  280. * @spl_boot_list: List of boot devices to try
  281. * @count: Number of elements in spl_boot_list
  282. * @return 0 if OK, -ve on error
  283. */
  284. static int boot_from_devices(struct spl_image_info *spl_image,
  285. u32 spl_boot_list[], int count)
  286. {
  287. int i;
  288. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  289. struct spl_image_loader *loader;
  290. loader = spl_ll_find_loader(spl_boot_list[i]);
  291. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  292. if (loader)
  293. printf("Trying to boot from %s\n", loader->name);
  294. else
  295. puts("SPL: Unsupported Boot Device!\n");
  296. #endif
  297. if (loader && !spl_load_image(spl_image, loader))
  298. return 0;
  299. }
  300. return -ENODEV;
  301. }
  302. void board_init_r(gd_t *dummy1, ulong dummy2)
  303. {
  304. u32 spl_boot_list[] = {
  305. BOOT_DEVICE_NONE,
  306. BOOT_DEVICE_NONE,
  307. BOOT_DEVICE_NONE,
  308. BOOT_DEVICE_NONE,
  309. BOOT_DEVICE_NONE,
  310. };
  311. struct spl_image_info spl_image;
  312. debug(">>spl:board_init_r()\n");
  313. gd->bd = &bdata;
  314. #ifdef CONFIG_SPL_OS_BOOT
  315. dram_init_banksize();
  316. #endif
  317. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  318. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  319. CONFIG_SYS_SPL_MALLOC_SIZE);
  320. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  321. #endif
  322. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  323. if (spl_init())
  324. hang();
  325. }
  326. #ifndef CONFIG_PPC
  327. /*
  328. * timer_init() does not exist on PPC systems. The timer is initialized
  329. * and enabled (decrementer) in interrupt_init() here.
  330. */
  331. timer_init();
  332. #endif
  333. #ifdef CONFIG_SPL_BOARD_INIT
  334. spl_board_init();
  335. #endif
  336. memset(&spl_image, '\0', sizeof(spl_image));
  337. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  338. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  339. #endif
  340. board_boot_order(spl_boot_list);
  341. if (boot_from_devices(&spl_image, spl_boot_list,
  342. ARRAY_SIZE(spl_boot_list))) {
  343. puts("SPL: failed to boot from all boot devices\n");
  344. hang();
  345. }
  346. switch (spl_image.os) {
  347. case IH_OS_U_BOOT:
  348. debug("Jumping to U-Boot\n");
  349. break;
  350. #ifdef CONFIG_SPL_OS_BOOT
  351. case IH_OS_LINUX:
  352. debug("Jumping to Linux\n");
  353. spl_fixup_fdt();
  354. spl_board_prepare_for_linux();
  355. jump_to_image_linux(&spl_image);
  356. #endif
  357. default:
  358. debug("Unsupported OS image.. Jumping nevertheless..\n");
  359. }
  360. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  361. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  362. gd->malloc_ptr / 1024);
  363. #endif
  364. if (IS_ENABLED(CONFIG_SPL_ATF_SUPPORT)) {
  365. debug("loaded - jumping to U-Boot via ATF BL31.\n");
  366. bl31_entry();
  367. }
  368. debug("loaded - jumping to U-Boot...\n");
  369. spl_board_prepare_for_boot();
  370. jump_to_image_no_args(&spl_image);
  371. }
  372. /*
  373. * This requires UART clocks to be enabled. In order for this to work the
  374. * caller must ensure that the gd pointer is valid.
  375. */
  376. void preloader_console_init(void)
  377. {
  378. gd->baudrate = CONFIG_BAUDRATE;
  379. serial_init(); /* serial communications setup */
  380. gd->have_console = 1;
  381. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  382. U_BOOT_TIME ")\n");
  383. #ifdef CONFIG_SPL_DISPLAY_PRINT
  384. spl_display_print();
  385. #endif
  386. }
  387. /**
  388. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  389. *
  390. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  391. * for the main board_init_r() execution. This is typically because we need
  392. * more stack space for things like the MMC sub-system.
  393. *
  394. * This function calculates the stack position, copies the global_data into
  395. * place, sets the new gd (except for ARM, for which setting GD within a C
  396. * function may not always work) and returns the new stack position. The
  397. * caller is responsible for setting up the sp register and, in the case
  398. * of ARM, setting up gd.
  399. *
  400. * All of this is done using the same layout and alignments as done in
  401. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  402. *
  403. * @return new stack location, or 0 to use the same stack
  404. */
  405. ulong spl_relocate_stack_gd(void)
  406. {
  407. #ifdef CONFIG_SPL_STACK_R
  408. gd_t *new_gd;
  409. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  410. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  411. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  412. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  413. gd->malloc_base = ptr;
  414. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  415. gd->malloc_ptr = 0;
  416. }
  417. #endif
  418. /* Get stack position: use 8-byte alignment for ABI compliance */
  419. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  420. new_gd = (gd_t *)ptr;
  421. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  422. #if CONFIG_IS_ENABLED(DM)
  423. dm_fixup_for_gd_move(new_gd);
  424. #endif
  425. #if !defined(CONFIG_ARM)
  426. gd = new_gd;
  427. #endif
  428. return ptr;
  429. #else
  430. return 0;
  431. #endif
  432. }