spl.c 10 KB

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