spl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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(struct spl_boot_device *bootdev)
  168. {
  169. struct image_header *header;
  170. header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
  171. #if defined(CONFIG_SPL_DFU_SUPPORT)
  172. if (bootdev->boot_device == BOOT_DEVICE_DFU)
  173. spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
  174. #endif
  175. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  176. image_get_magic(header) == FDT_MAGIC) {
  177. struct spl_load_info load;
  178. debug("Found FIT\n");
  179. load.bl_len = 1;
  180. load.read = spl_ram_load_read;
  181. spl_load_simple_fit(&load, 0, header);
  182. } else {
  183. debug("Legacy image\n");
  184. /*
  185. * Get the header. It will point to an address defined by
  186. * handoff which will tell where the image located inside
  187. * the flash. For now, it will temporary fixed to address
  188. * pointed by U-Boot.
  189. */
  190. header = (struct image_header *)
  191. (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
  192. spl_parse_image_header(&spl_image, header);
  193. }
  194. return 0;
  195. }
  196. SPL_LOAD_IMAGE_METHOD(0, BOOT_DEVICE_RAM, spl_ram_load_image);
  197. #if defined(CONFIG_SPL_DFU_SUPPORT)
  198. SPL_LOAD_IMAGE_METHOD(0, BOOT_DEVICE_DFU, spl_ram_load_image);
  199. #endif
  200. #endif
  201. int spl_init(void)
  202. {
  203. int ret;
  204. debug("spl_init()\n");
  205. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  206. #ifdef CONFIG_MALLOC_F_ADDR
  207. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  208. #endif
  209. gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
  210. gd->malloc_ptr = 0;
  211. #endif
  212. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  213. ret = fdtdec_setup();
  214. if (ret) {
  215. debug("fdtdec_setup() returned error %d\n", ret);
  216. return ret;
  217. }
  218. }
  219. if (IS_ENABLED(CONFIG_SPL_DM)) {
  220. /* With CONFIG_OF_PLATDATA, bring in all devices */
  221. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  222. if (ret) {
  223. debug("dm_init_and_scan() returned error %d\n", ret);
  224. return ret;
  225. }
  226. }
  227. gd->flags |= GD_FLG_SPL_INIT;
  228. return 0;
  229. }
  230. #ifndef BOOT_DEVICE_NONE
  231. #define BOOT_DEVICE_NONE 0xdeadbeef
  232. #endif
  233. static u32 spl_boot_list[] = {
  234. BOOT_DEVICE_NONE,
  235. BOOT_DEVICE_NONE,
  236. BOOT_DEVICE_NONE,
  237. BOOT_DEVICE_NONE,
  238. BOOT_DEVICE_NONE,
  239. };
  240. __weak void board_boot_order(u32 *spl_boot_list)
  241. {
  242. spl_boot_list[0] = spl_boot_device();
  243. }
  244. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  245. __weak void spl_board_announce_boot_device(void) { }
  246. #endif
  247. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  248. struct boot_device_name {
  249. u32 boot_dev;
  250. const char *name;
  251. };
  252. struct boot_device_name boot_name_table[] = {
  253. #ifdef CONFIG_SPL_RAM_DEVICE
  254. { BOOT_DEVICE_RAM, "RAM" },
  255. #endif
  256. #ifdef CONFIG_SPL_MMC_SUPPORT
  257. { BOOT_DEVICE_MMC1, "MMC1" },
  258. { BOOT_DEVICE_MMC2, "MMC2" },
  259. { BOOT_DEVICE_MMC2_2, "MMC2_2" },
  260. #endif
  261. #ifdef CONFIG_SPL_NAND_SUPPORT
  262. { BOOT_DEVICE_NAND, "NAND" },
  263. #endif
  264. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  265. { BOOT_DEVICE_ONENAND, "OneNAND" },
  266. #endif
  267. #ifdef CONFIG_SPL_NOR_SUPPORT
  268. { BOOT_DEVICE_NOR, "NOR" },
  269. #endif
  270. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  271. { BOOT_DEVICE_UART, "UART" },
  272. #endif
  273. #if defined(CONFIG_SPL_SPI_SUPPORT) || defined(CONFIG_SPL_SPI_FLASH_SUPPORT)
  274. { BOOT_DEVICE_SPI, "SPI" },
  275. #endif
  276. #ifdef CONFIG_SPL_ETH_SUPPORT
  277. #ifdef CONFIG_SPL_ETH_DEVICE
  278. { BOOT_DEVICE_CPGMAC, "eth device" },
  279. #else
  280. { BOOT_DEVICE_CPGMAC, "net" },
  281. #endif
  282. #endif
  283. #ifdef CONFIG_SPL_USBETH_SUPPORT
  284. { BOOT_DEVICE_USBETH, "USB eth" },
  285. #endif
  286. #ifdef CONFIG_SPL_USB_SUPPORT
  287. { BOOT_DEVICE_USB, "USB" },
  288. #endif
  289. #ifdef CONFIG_SPL_DFU_SUPPORT
  290. { BOOT_DEVICE_DFU, "USB DFU" },
  291. #endif
  292. #ifdef CONFIG_SPL_SATA_SUPPORT
  293. { BOOT_DEVICE_SATA, "SATA" },
  294. #endif
  295. /* Keep this entry last */
  296. { BOOT_DEVICE_NONE, "unknown boot device" },
  297. };
  298. static void announce_boot_device(u32 boot_device)
  299. {
  300. int i;
  301. puts("Trying to boot from ");
  302. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  303. if (boot_device == BOOT_DEVICE_BOARD) {
  304. spl_board_announce_boot_device();
  305. puts("\n");
  306. return;
  307. }
  308. #endif
  309. for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
  310. if (boot_name_table[i].boot_dev == boot_device)
  311. break;
  312. }
  313. printf("%s\n", boot_name_table[i].name);
  314. }
  315. #else
  316. static inline void announce_boot_device(u32 boot_device) { }
  317. #endif
  318. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  319. {
  320. struct spl_image_loader *drv =
  321. ll_entry_start(struct spl_image_loader, spl_image_loader);
  322. const int n_ents =
  323. ll_entry_count(struct spl_image_loader, spl_image_loader);
  324. struct spl_image_loader *entry;
  325. for (entry = drv; entry != drv + n_ents; entry++) {
  326. if (boot_device == entry->boot_device)
  327. return entry;
  328. }
  329. /* Not found */
  330. return NULL;
  331. }
  332. static int spl_load_image(u32 boot_device)
  333. {
  334. struct spl_boot_device bootdev;
  335. struct spl_image_loader *loader = spl_ll_find_loader(boot_device);
  336. bootdev.boot_device = boot_device;
  337. bootdev.boot_device_name = NULL;
  338. if (loader)
  339. return loader->load_image(&bootdev);
  340. switch (boot_device) {
  341. #ifdef CONFIG_SPL_MMC_SUPPORT
  342. case BOOT_DEVICE_MMC1:
  343. case BOOT_DEVICE_MMC2:
  344. case BOOT_DEVICE_MMC2_2:
  345. return spl_mmc_load_image(&bootdev);
  346. #endif
  347. #ifdef CONFIG_SPL_UBI
  348. case BOOT_DEVICE_NAND:
  349. case BOOT_DEVICE_ONENAND:
  350. return spl_ubi_load_image(&bootdev);
  351. #else
  352. #ifdef CONFIG_SPL_NAND_SUPPORT
  353. case BOOT_DEVICE_NAND:
  354. return spl_nand_load_image(&bootdev);
  355. #endif
  356. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  357. case BOOT_DEVICE_ONENAND:
  358. return spl_onenand_load_image(&bootdev);
  359. #endif
  360. #endif
  361. #ifdef CONFIG_SPL_NOR_SUPPORT
  362. case BOOT_DEVICE_NOR:
  363. return spl_nor_load_image(&bootdev);
  364. #endif
  365. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  366. case BOOT_DEVICE_UART:
  367. return spl_ymodem_load_image(&bootdev);
  368. #endif
  369. #if defined(CONFIG_SPL_SPI_SUPPORT) || defined(CONFIG_SPL_SPI_FLASH_SUPPORT)
  370. case BOOT_DEVICE_SPI:
  371. return spl_spi_load_image(&bootdev);
  372. #endif
  373. #ifdef CONFIG_SPL_ETH_SUPPORT
  374. case BOOT_DEVICE_CPGMAC:
  375. #ifdef CONFIG_SPL_ETH_DEVICE
  376. bootdev.boot_device_name = CONFIG_SPL_ETH_DEVICE;
  377. #endif
  378. return spl_net_load_image(&bootdev);
  379. #endif
  380. #ifdef CONFIG_SPL_USBETH_SUPPORT
  381. case BOOT_DEVICE_USBETH:
  382. bootdev.boot_device_name = "usb_ether";
  383. return spl_net_load_image(&bootdev);
  384. #endif
  385. #ifdef CONFIG_SPL_USB_SUPPORT
  386. case BOOT_DEVICE_USB:
  387. return spl_usb_load_image(&bootdev);
  388. #endif
  389. #ifdef CONFIG_SPL_SATA_SUPPORT
  390. case BOOT_DEVICE_SATA:
  391. return spl_sata_load_image(&bootdev);
  392. #endif
  393. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  394. case BOOT_DEVICE_BOARD:
  395. return spl_board_load_image(&bootdev);
  396. #endif
  397. default:
  398. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  399. puts("SPL: Unsupported Boot Device!\n");
  400. #endif
  401. return -ENODEV;
  402. }
  403. return -EINVAL;
  404. }
  405. void board_init_r(gd_t *dummy1, ulong dummy2)
  406. {
  407. int i;
  408. debug(">>spl:board_init_r()\n");
  409. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  410. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  411. CONFIG_SYS_SPL_MALLOC_SIZE);
  412. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  413. #endif
  414. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  415. if (spl_init())
  416. hang();
  417. }
  418. #ifndef CONFIG_PPC
  419. /*
  420. * timer_init() does not exist on PPC systems. The timer is initialized
  421. * and enabled (decrementer) in interrupt_init() here.
  422. */
  423. timer_init();
  424. #endif
  425. #ifdef CONFIG_SPL_BOARD_INIT
  426. spl_board_init();
  427. #endif
  428. board_boot_order(spl_boot_list);
  429. for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
  430. spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  431. announce_boot_device(spl_boot_list[i]);
  432. if (!spl_load_image(spl_boot_list[i]))
  433. break;
  434. }
  435. if (i == ARRAY_SIZE(spl_boot_list) ||
  436. spl_boot_list[i] == BOOT_DEVICE_NONE) {
  437. puts("SPL: failed to boot from all boot devices\n");
  438. hang();
  439. }
  440. switch (spl_image.os) {
  441. case IH_OS_U_BOOT:
  442. debug("Jumping to U-Boot\n");
  443. break;
  444. #ifdef CONFIG_SPL_OS_BOOT
  445. case IH_OS_LINUX:
  446. debug("Jumping to Linux\n");
  447. spl_board_prepare_for_linux();
  448. jump_to_image_linux(&spl_image,
  449. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  450. #endif
  451. default:
  452. debug("Unsupported OS image.. Jumping nevertheless..\n");
  453. }
  454. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  455. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  456. gd->malloc_ptr / 1024);
  457. #endif
  458. debug("loaded - jumping to U-Boot...");
  459. spl_board_prepare_for_boot();
  460. jump_to_image_no_args(&spl_image);
  461. }
  462. /*
  463. * This requires UART clocks to be enabled. In order for this to work the
  464. * caller must ensure that the gd pointer is valid.
  465. */
  466. void preloader_console_init(void)
  467. {
  468. gd->bd = &bdata;
  469. gd->baudrate = CONFIG_BAUDRATE;
  470. serial_init(); /* serial communications setup */
  471. gd->have_console = 1;
  472. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  473. U_BOOT_TIME ")\n");
  474. #ifdef CONFIG_SPL_DISPLAY_PRINT
  475. spl_display_print();
  476. #endif
  477. }
  478. /**
  479. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  480. *
  481. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  482. * for the main board_init_r() execution. This is typically because we need
  483. * more stack space for things like the MMC sub-system.
  484. *
  485. * This function calculates the stack position, copies the global_data into
  486. * place, sets the new gd (except for ARM, for which setting GD within a C
  487. * function may not always work) and returns the new stack position. The
  488. * caller is responsible for setting up the sp register and, in the case
  489. * of ARM, setting up gd.
  490. *
  491. * All of this is done using the same layout and alignments as done in
  492. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  493. *
  494. * @return new stack location, or 0 to use the same stack
  495. */
  496. ulong spl_relocate_stack_gd(void)
  497. {
  498. #ifdef CONFIG_SPL_STACK_R
  499. gd_t *new_gd;
  500. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  501. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  502. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  503. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  504. gd->malloc_base = ptr;
  505. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  506. gd->malloc_ptr = 0;
  507. }
  508. #endif
  509. /* Get stack position: use 8-byte alignment for ABI compliance */
  510. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  511. new_gd = (gd_t *)ptr;
  512. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  513. #if !defined(CONFIG_ARM)
  514. gd = new_gd;
  515. #endif
  516. return ptr;
  517. #else
  518. return 0;
  519. #endif
  520. }