spl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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_UBI
  342. case BOOT_DEVICE_NAND:
  343. case BOOT_DEVICE_ONENAND:
  344. return spl_ubi_load_image(&bootdev);
  345. #else
  346. #ifdef CONFIG_SPL_NAND_SUPPORT
  347. case BOOT_DEVICE_NAND:
  348. return spl_nand_load_image(&bootdev);
  349. #endif
  350. #ifdef CONFIG_SPL_ONENAND_SUPPORT
  351. case BOOT_DEVICE_ONENAND:
  352. return spl_onenand_load_image(&bootdev);
  353. #endif
  354. #endif
  355. #ifdef CONFIG_SPL_NOR_SUPPORT
  356. case BOOT_DEVICE_NOR:
  357. return spl_nor_load_image(&bootdev);
  358. #endif
  359. #ifdef CONFIG_SPL_YMODEM_SUPPORT
  360. case BOOT_DEVICE_UART:
  361. return spl_ymodem_load_image(&bootdev);
  362. #endif
  363. #if defined(CONFIG_SPL_SPI_SUPPORT) || defined(CONFIG_SPL_SPI_FLASH_SUPPORT)
  364. case BOOT_DEVICE_SPI:
  365. return spl_spi_load_image(&bootdev);
  366. #endif
  367. #ifdef CONFIG_SPL_ETH_SUPPORT
  368. case BOOT_DEVICE_CPGMAC:
  369. #ifdef CONFIG_SPL_ETH_DEVICE
  370. bootdev.boot_device_name = CONFIG_SPL_ETH_DEVICE;
  371. #endif
  372. return spl_net_load_image(&bootdev);
  373. #endif
  374. #ifdef CONFIG_SPL_USBETH_SUPPORT
  375. case BOOT_DEVICE_USBETH:
  376. bootdev.boot_device_name = "usb_ether";
  377. return spl_net_load_image(&bootdev);
  378. #endif
  379. #ifdef CONFIG_SPL_USB_SUPPORT
  380. case BOOT_DEVICE_USB:
  381. return spl_usb_load_image(&bootdev);
  382. #endif
  383. #ifdef CONFIG_SPL_SATA_SUPPORT
  384. case BOOT_DEVICE_SATA:
  385. return spl_sata_load_image(&bootdev);
  386. #endif
  387. #ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
  388. case BOOT_DEVICE_BOARD:
  389. return spl_board_load_image(&bootdev);
  390. #endif
  391. default:
  392. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  393. puts("SPL: Unsupported Boot Device!\n");
  394. #endif
  395. return -ENODEV;
  396. }
  397. return -EINVAL;
  398. }
  399. void board_init_r(gd_t *dummy1, ulong dummy2)
  400. {
  401. int i;
  402. debug(">>spl:board_init_r()\n");
  403. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  404. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  405. CONFIG_SYS_SPL_MALLOC_SIZE);
  406. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  407. #endif
  408. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  409. if (spl_init())
  410. hang();
  411. }
  412. #ifndef CONFIG_PPC
  413. /*
  414. * timer_init() does not exist on PPC systems. The timer is initialized
  415. * and enabled (decrementer) in interrupt_init() here.
  416. */
  417. timer_init();
  418. #endif
  419. #ifdef CONFIG_SPL_BOARD_INIT
  420. spl_board_init();
  421. #endif
  422. board_boot_order(spl_boot_list);
  423. for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
  424. spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  425. announce_boot_device(spl_boot_list[i]);
  426. if (!spl_load_image(spl_boot_list[i]))
  427. break;
  428. }
  429. if (i == ARRAY_SIZE(spl_boot_list) ||
  430. spl_boot_list[i] == BOOT_DEVICE_NONE) {
  431. puts("SPL: failed to boot from all boot devices\n");
  432. hang();
  433. }
  434. switch (spl_image.os) {
  435. case IH_OS_U_BOOT:
  436. debug("Jumping to U-Boot\n");
  437. break;
  438. #ifdef CONFIG_SPL_OS_BOOT
  439. case IH_OS_LINUX:
  440. debug("Jumping to Linux\n");
  441. spl_board_prepare_for_linux();
  442. jump_to_image_linux(&spl_image,
  443. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  444. #endif
  445. default:
  446. debug("Unsupported OS image.. Jumping nevertheless..\n");
  447. }
  448. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  449. debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
  450. gd->malloc_ptr / 1024);
  451. #endif
  452. debug("loaded - jumping to U-Boot...");
  453. spl_board_prepare_for_boot();
  454. jump_to_image_no_args(&spl_image);
  455. }
  456. /*
  457. * This requires UART clocks to be enabled. In order for this to work the
  458. * caller must ensure that the gd pointer is valid.
  459. */
  460. void preloader_console_init(void)
  461. {
  462. gd->bd = &bdata;
  463. gd->baudrate = CONFIG_BAUDRATE;
  464. serial_init(); /* serial communications setup */
  465. gd->have_console = 1;
  466. puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
  467. U_BOOT_TIME ")\n");
  468. #ifdef CONFIG_SPL_DISPLAY_PRINT
  469. spl_display_print();
  470. #endif
  471. }
  472. /**
  473. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  474. *
  475. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  476. * for the main board_init_r() execution. This is typically because we need
  477. * more stack space for things like the MMC sub-system.
  478. *
  479. * This function calculates the stack position, copies the global_data into
  480. * place, sets the new gd (except for ARM, for which setting GD within a C
  481. * function may not always work) and returns the new stack position. The
  482. * caller is responsible for setting up the sp register and, in the case
  483. * of ARM, setting up gd.
  484. *
  485. * All of this is done using the same layout and alignments as done in
  486. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  487. *
  488. * @return new stack location, or 0 to use the same stack
  489. */
  490. ulong spl_relocate_stack_gd(void)
  491. {
  492. #ifdef CONFIG_SPL_STACK_R
  493. gd_t *new_gd;
  494. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  495. #ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
  496. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  497. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  498. gd->malloc_base = ptr;
  499. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  500. gd->malloc_ptr = 0;
  501. }
  502. #endif
  503. /* Get stack position: use 8-byte alignment for ABI compliance */
  504. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  505. new_gd = (gd_t *)ptr;
  506. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  507. #if !defined(CONFIG_ARM)
  508. gd = new_gd;
  509. #endif
  510. return ptr;
  511. #else
  512. return 0;
  513. #endif
  514. }