spl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. struct spl_image_info spl_image;
  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. /*
  54. * Weak default function for arch specific zImage check. Return zero
  55. * and fill start and end address if image is recognized.
  56. */
  57. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  58. {
  59. return 1;
  60. }
  61. #endif
  62. /*
  63. * Weak default function for board specific cleanup/preparation before
  64. * Linux boot. Some boards/platforms might not need it, so just provide
  65. * an empty stub here.
  66. */
  67. __weak void spl_board_prepare_for_linux(void)
  68. {
  69. /* Nothing to do! */
  70. }
  71. __weak void spl_board_prepare_for_boot(void)
  72. {
  73. /* Nothing to do! */
  74. }
  75. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  76. {
  77. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  78. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  79. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  80. spl_image->os = IH_OS_U_BOOT;
  81. spl_image->name = "U-Boot";
  82. }
  83. int spl_parse_image_header(struct spl_image_info *spl_image,
  84. const struct image_header *header)
  85. {
  86. u32 header_size = sizeof(struct image_header);
  87. if (image_get_magic(header) == IH_MAGIC) {
  88. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  89. /*
  90. * On some system (e.g. powerpc), the load-address and
  91. * entry-point is located at address 0. We can't load
  92. * to 0-0x40. So skip header in this case.
  93. */
  94. spl_image->load_addr = image_get_load(header);
  95. spl_image->entry_point = image_get_ep(header);
  96. spl_image->size = image_get_data_size(header);
  97. } else {
  98. spl_image->entry_point = image_get_load(header);
  99. /* Load including the header */
  100. spl_image->load_addr = spl_image->entry_point -
  101. header_size;
  102. spl_image->size = image_get_data_size(header) +
  103. header_size;
  104. }
  105. spl_image->os = image_get_os(header);
  106. spl_image->name = image_get_name(header);
  107. debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
  108. (int)sizeof(spl_image->name), spl_image->name,
  109. spl_image->load_addr, spl_image->size);
  110. } else {
  111. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  112. /*
  113. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  114. * code which loads images in SPL cannot guarantee that
  115. * absolutely all read errors will be reported.
  116. * An example is the LPC32XX MLC NAND driver, which
  117. * will consider that a completely unreadable NAND block
  118. * is bad, and thus should be skipped silently.
  119. */
  120. panic("** no mkimage signature but raw image not supported");
  121. #endif
  122. #ifdef CONFIG_SPL_OS_BOOT
  123. ulong start, end;
  124. if (!bootz_setup((ulong)header, &start, &end)) {
  125. spl_image->name = "Linux";
  126. spl_image->os = IH_OS_LINUX;
  127. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  128. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  129. spl_image->size = end - start;
  130. debug("spl: payload zImage, load addr: 0x%x size: %d\n",
  131. spl_image->load_addr, spl_image->size);
  132. return 0;
  133. }
  134. #endif
  135. #ifdef CONFIG_SPL_ABORT_ON_RAW_IMAGE
  136. /* Signature not found, proceed to other boot methods. */
  137. return -EINVAL;
  138. #else
  139. /* Signature not found - assume u-boot.bin */
  140. debug("mkimage signature not found - ih_magic = %x\n",
  141. header->ih_magic);
  142. spl_set_header_raw_uboot(spl_image);
  143. #endif
  144. }
  145. return 0;
  146. }
  147. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  148. {
  149. typedef void __noreturn (*image_entry_noargs_t)(void);
  150. image_entry_noargs_t image_entry =
  151. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  152. debug("image entry point: 0x%X\n", spl_image->entry_point);
  153. image_entry();
  154. }
  155. #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
  156. # define CONFIG_SPL_LOAD_FIT_ADDRESS 0
  157. #endif
  158. #if defined(CONFIG_SPL_RAM_DEVICE) || defined(CONFIG_SPL_DFU_SUPPORT)
  159. static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
  160. ulong count, void *buf)
  161. {
  162. debug("%s: sector %lx, count %lx, buf %lx\n",
  163. __func__, sector, count, (ulong)buf);
  164. memcpy(buf, (void *)(CONFIG_SPL_LOAD_FIT_ADDRESS + sector), count);
  165. return count;
  166. }
  167. static int spl_ram_load_image(void)
  168. {
  169. struct image_header *header;
  170. header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
  171. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  172. image_get_magic(header) == FDT_MAGIC) {
  173. struct spl_load_info load;
  174. debug("Found FIT\n");
  175. load.bl_len = 1;
  176. load.read = spl_ram_load_read;
  177. spl_load_simple_fit(&load, 0, header);
  178. } else {
  179. debug("Legacy image\n");
  180. /*
  181. * Get the header. It will point to an address defined by
  182. * handoff which will tell where the image located inside
  183. * the flash. For now, it will temporary fixed to address
  184. * pointed by U-Boot.
  185. */
  186. header = (struct image_header *)
  187. (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
  188. spl_parse_image_header(&spl_image, header);
  189. }
  190. return 0;
  191. }
  192. #endif
  193. int spl_init(void)
  194. {
  195. int ret;
  196. debug("spl_init()\n");
  197. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  198. #ifdef CONFIG_MALLOC_F_ADDR
  199. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  200. #endif
  201. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  202. gd->malloc_ptr = 0;
  203. #endif
  204. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  205. ret = fdtdec_setup();
  206. if (ret) {
  207. debug("fdtdec_setup() returned error %d\n", ret);
  208. return ret;
  209. }
  210. }
  211. if (IS_ENABLED(CONFIG_SPL_DM)) {
  212. /* With CONFIG_OF_PLATDATA, bring in all devices */
  213. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  214. if (ret) {
  215. debug("dm_init_and_scan() returned error %d\n", ret);
  216. return ret;
  217. }
  218. }
  219. gd->flags |= GD_FLG_SPL_INIT;
  220. return 0;
  221. }
  222. #ifndef BOOT_DEVICE_NONE
  223. #define BOOT_DEVICE_NONE 0xdeadbeef
  224. #endif
  225. static u32 spl_boot_list[] = {
  226. BOOT_DEVICE_NONE,
  227. BOOT_DEVICE_NONE,
  228. BOOT_DEVICE_NONE,
  229. BOOT_DEVICE_NONE,
  230. BOOT_DEVICE_NONE,
  231. };
  232. __weak void board_boot_order(u32 *spl_boot_list)
  233. {
  234. spl_boot_list[0] = spl_boot_device();
  235. }
  236. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  237. __weak void spl_board_announce_boot_device(void) { }
  238. #endif
  239. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  240. struct boot_device_name {
  241. u32 boot_dev;
  242. const char *name;
  243. };
  244. struct boot_device_name boot_name_table[] = {
  245. #ifdef CONFIG_SPL_RAM_DEVICE
  246. { BOOT_DEVICE_RAM, "RAM" },
  247. #endif
  248. #ifdef CONFIG_SPL_MMC_SUPPORT
  249. { BOOT_DEVICE_MMC1, "MMC1" },
  250. { BOOT_DEVICE_MMC2, "MMC2" },
  251. { BOOT_DEVICE_MMC2_2, "MMC2_2" },
  252. #endif
  253. #ifdef CONFIG_SPL_NAND_SUPPORT
  254. { BOOT_DEVICE_NAND, "NAND" },
  255. #endif
  256. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  257. { BOOT_DEVICE_ONENAND, "OneNAND" },
  258. #endif
  259. #ifdef CONFIG_SPL_NOR_SUPPORT
  260. { BOOT_DEVICE_NOR, "NOR" },
  261. #endif
  262. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  263. { BOOT_DEVICE_UART, "UART" },
  264. #endif
  265. #if defined(CONFIG_SPL_SPI_SUPPORT) || defined(CONFIG_SPL_SPI_FLASH_SUPPORT)
  266. { BOOT_DEVICE_SPI, "SPI" },
  267. #endif
  268. #ifdef CONFIG_SPL_ETH_SUPPORT
  269. #ifdef CONFIG_SPL_ETH_DEVICE
  270. { BOOT_DEVICE_CPGMAC, "eth device" },
  271. #else
  272. { BOOT_DEVICE_CPGMAC, "net" },
  273. #endif
  274. #endif
  275. #ifdef CONFIG_SPL_USBETH_SUPPORT
  276. { BOOT_DEVICE_USBETH, "USB eth" },
  277. #endif
  278. #ifdef CONFIG_SPL_USB_SUPPORT
  279. { BOOT_DEVICE_USB, "USB" },
  280. #endif
  281. #ifdef CONFIG_SPL_DFU_SUPPORT
  282. { BOOT_DEVICE_DFU, "USB DFU" },
  283. #endif
  284. #ifdef CONFIG_SPL_SATA_SUPPORT
  285. { BOOT_DEVICE_SATA, "SATA" },
  286. #endif
  287. /* Keep this entry last */
  288. { BOOT_DEVICE_NONE, "unknown boot device" },
  289. };
  290. static void announce_boot_device(u32 boot_device)
  291. {
  292. int i;
  293. puts("Trying to boot from ");
  294. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  295. if (boot_device == BOOT_DEVICE_BOARD) {
  296. spl_board_announce_boot_device();
  297. puts("\n");
  298. return;
  299. }
  300. #endif
  301. for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
  302. if (boot_name_table[i].boot_dev == boot_device)
  303. break;
  304. }
  305. printf("%s\n", boot_name_table[i].name);
  306. }
  307. #else
  308. static inline void announce_boot_device(u32 boot_device) { }
  309. #endif
  310. static int spl_load_image(u32 boot_device)
  311. {
  312. switch (boot_device) {
  313. #ifdef CONFIG_SPL_RAM_DEVICE
  314. case BOOT_DEVICE_RAM:
  315. return spl_ram_load_image();
  316. #endif
  317. #ifdef CONFIG_SPL_MMC_SUPPORT
  318. case BOOT_DEVICE_MMC1:
  319. case BOOT_DEVICE_MMC2:
  320. case BOOT_DEVICE_MMC2_2:
  321. return spl_mmc_load_image(boot_device);
  322. #endif
  323. #ifdef CONFIG_SPL_UBI
  324. case BOOT_DEVICE_NAND:
  325. case BOOT_DEVICE_ONENAND:
  326. return spl_ubi_load_image(boot_device);
  327. #else
  328. #ifdef CONFIG_SPL_NAND_SUPPORT
  329. case BOOT_DEVICE_NAND:
  330. return spl_nand_load_image();
  331. #endif
  332. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  333. case BOOT_DEVICE_ONENAND:
  334. return spl_onenand_load_image();
  335. #endif
  336. #endif
  337. #ifdef CONFIG_SPL_NOR_SUPPORT
  338. case BOOT_DEVICE_NOR:
  339. return spl_nor_load_image();
  340. #endif
  341. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  342. case BOOT_DEVICE_UART:
  343. return spl_ymodem_load_image();
  344. #endif
  345. #if defined(CONFIG_SPL_SPI_SUPPORT) || defined(CONFIG_SPL_SPI_FLASH_SUPPORT)
  346. case BOOT_DEVICE_SPI:
  347. return spl_spi_load_image();
  348. #endif
  349. #ifdef CONFIG_SPL_ETH_SUPPORT
  350. case BOOT_DEVICE_CPGMAC:
  351. #ifdef CONFIG_SPL_ETH_DEVICE
  352. return spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
  353. #else
  354. return spl_net_load_image(NULL);
  355. #endif
  356. #endif
  357. #ifdef CONFIG_SPL_USBETH_SUPPORT
  358. case BOOT_DEVICE_USBETH:
  359. return spl_net_load_image("usb_ether");
  360. #endif
  361. #ifdef CONFIG_SPL_USB_SUPPORT
  362. case BOOT_DEVICE_USB:
  363. return spl_usb_load_image();
  364. #endif
  365. #ifdef CONFIG_SPL_DFU_SUPPORT
  366. case BOOT_DEVICE_DFU:
  367. spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
  368. return spl_ram_load_image();
  369. #endif
  370. #ifdef CONFIG_SPL_SATA_SUPPORT
  371. case BOOT_DEVICE_SATA:
  372. return spl_sata_load_image();
  373. #endif
  374. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  375. case BOOT_DEVICE_BOARD:
  376. return spl_board_load_image();
  377. #endif
  378. default:
  379. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  380. puts("SPL: Unsupported Boot Device!\n");
  381. #endif
  382. return -ENODEV;
  383. }
  384. return -EINVAL;
  385. }
  386. void board_init_r(gd_t *dummy1, ulong dummy2)
  387. {
  388. int i;
  389. debug(">>spl:board_init_r()\n");
  390. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  391. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  392. CONFIG_SYS_SPL_MALLOC_SIZE);
  393. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  394. #endif
  395. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  396. if (spl_init())
  397. hang();
  398. }
  399. #ifndef CONFIG_PPC
  400. /*
  401. * timer_init() does not exist on PPC systems. The timer is initialized
  402. * and enabled (decrementer) in interrupt_init() here.
  403. */
  404. timer_init();
  405. #endif
  406. #ifdef CONFIG_SPL_BOARD_INIT
  407. spl_board_init();
  408. #endif
  409. board_boot_order(spl_boot_list);
  410. for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
  411. spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  412. announce_boot_device(spl_boot_list[i]);
  413. if (!spl_load_image(spl_boot_list[i]))
  414. break;
  415. }
  416. if (i == ARRAY_SIZE(spl_boot_list) ||
  417. spl_boot_list[i] == BOOT_DEVICE_NONE) {
  418. puts("SPL: failed to boot from all boot devices\n");
  419. hang();
  420. }
  421. switch (spl_image.os) {
  422. case IH_OS_U_BOOT:
  423. debug("Jumping to U-Boot\n");
  424. break;
  425. #ifdef CONFIG_SPL_OS_BOOT
  426. case IH_OS_LINUX:
  427. debug("Jumping to Linux\n");
  428. spl_board_prepare_for_linux();
  429. jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  430. #endif
  431. default:
  432. debug("Unsupported OS image.. Jumping nevertheless..\n");
  433. }
  434. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  435. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  436. gd->malloc_ptr / 1024);
  437. #endif
  438. debug("loaded - jumping to U-Boot...");
  439. spl_board_prepare_for_boot();
  440. jump_to_image_no_args(&spl_image);
  441. }
  442. /*
  443. * This requires UART clocks to be enabled. In order for this to work the
  444. * caller must ensure that the gd pointer is valid.
  445. */
  446. void preloader_console_init(void)
  447. {
  448. gd->bd = &bdata;
  449. gd->baudrate = CONFIG_BAUDRATE;
  450. serial_init(); /* serial communications setup */
  451. gd->have_console = 1;
  452. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  453. U_BOOT_TIME ")\n");
  454. #ifdef CONFIG_SPL_DISPLAY_PRINT
  455. spl_display_print();
  456. #endif
  457. }
  458. /**
  459. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  460. *
  461. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  462. * for the main board_init_r() execution. This is typically because we need
  463. * more stack space for things like the MMC sub-system.
  464. *
  465. * This function calculates the stack position, copies the global_data into
  466. * place, sets the new gd (except for ARM, for which setting GD within a C
  467. * function may not always work) and returns the new stack position. The
  468. * caller is responsible for setting up the sp register and, in the case
  469. * of ARM, setting up gd.
  470. *
  471. * All of this is done using the same layout and alignments as done in
  472. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  473. *
  474. * @return new stack location, or 0 to use the same stack
  475. */
  476. ulong spl_relocate_stack_gd(void)
  477. {
  478. #ifdef CONFIG_SPL_STACK_R
  479. gd_t *new_gd;
  480. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  481. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  482. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  483. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  484. gd->malloc_base = ptr;
  485. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  486. gd->malloc_ptr = 0;
  487. }
  488. #endif
  489. /* Get stack position: use 8-byte alignment for ABI compliance */
  490. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  491. new_gd = (gd_t *)ptr;
  492. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  493. #if !defined(CONFIG_ARM)
  494. gd = new_gd;
  495. #endif
  496. return ptr;
  497. #else
  498. return 0;
  499. #endif
  500. }