spl.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010
  4. * Texas Instruments, <www.ti.com>
  5. *
  6. * Aneesh V <aneesh@ti.com>
  7. */
  8. #include <common.h>
  9. #include <bloblist.h>
  10. #include <binman_sym.h>
  11. #include <bootstage.h>
  12. #include <dm.h>
  13. #include <handoff.h>
  14. #include <hang.h>
  15. #include <init.h>
  16. #include <irq_func.h>
  17. #include <log.h>
  18. #include <mapmem.h>
  19. #include <serial.h>
  20. #include <spl.h>
  21. #include <system-constants.h>
  22. #include <asm/global_data.h>
  23. #include <asm-generic/gpio.h>
  24. #include <asm/u-boot.h>
  25. #include <nand.h>
  26. #include <fat.h>
  27. #include <u-boot/crc.h>
  28. #if CONFIG_IS_ENABLED(BANNER_PRINT)
  29. #include <timestamp.h>
  30. #endif
  31. #include <version.h>
  32. #include <image.h>
  33. #include <malloc.h>
  34. #include <mapmem.h>
  35. #include <dm/root.h>
  36. #include <dm/util.h>
  37. #include <dm/device-internal.h>
  38. #include <dm/uclass-internal.h>
  39. #include <linux/compiler.h>
  40. #include <fdt_support.h>
  41. #include <bootcount.h>
  42. #include <wdt.h>
  43. DECLARE_GLOBAL_DATA_PTR;
  44. DECLARE_BINMAN_MAGIC_SYM;
  45. #ifndef CFG_SYS_UBOOT_START
  46. #define CFG_SYS_UBOOT_START CONFIG_TEXT_BASE
  47. #endif
  48. u32 *boot_params_ptr = NULL;
  49. #if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
  50. /* See spl.h for information about this */
  51. binman_sym_declare(ulong, u_boot_any, image_pos);
  52. binman_sym_declare(ulong, u_boot_any, size);
  53. #ifdef CONFIG_TPL
  54. binman_sym_declare(ulong, u_boot_spl_any, image_pos);
  55. binman_sym_declare(ulong, u_boot_spl_any, size);
  56. #endif
  57. #ifdef CONFIG_VPL
  58. binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
  59. binman_sym_declare(ulong, u_boot_vpl_any, size);
  60. #endif
  61. #endif /* BINMAN_UBOOT_SYMBOLS */
  62. /* Define board data structure */
  63. static struct bd_info bdata __attribute__ ((section(".data")));
  64. #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
  65. /*
  66. * Board-specific Platform code can reimplement show_boot_progress () if needed
  67. */
  68. __weak void show_boot_progress(int val) {}
  69. #endif
  70. #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
  71. defined(CONFIG_SPL_ATF)
  72. /* weak, default platform-specific function to initialize dram banks */
  73. __weak int dram_init_banksize(void)
  74. {
  75. return 0;
  76. }
  77. #endif
  78. /*
  79. * Default function to determine if u-boot or the OS should
  80. * be started. This implementation always returns 1.
  81. *
  82. * Please implement your own board specific funcion to do this.
  83. *
  84. * RETURN
  85. * 0 to not start u-boot
  86. * positive if u-boot should start
  87. */
  88. #if CONFIG_IS_ENABLED(OS_BOOT)
  89. __weak int spl_start_uboot(void)
  90. {
  91. puts(SPL_TPL_PROMPT
  92. "Please implement spl_start_uboot() for your board\n");
  93. puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
  94. return 1;
  95. }
  96. /*
  97. * Weak default function for arch specific zImage check. Return zero
  98. * and fill start and end address if image is recognized.
  99. */
  100. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  101. {
  102. return 1;
  103. }
  104. int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
  105. {
  106. return 1;
  107. }
  108. #endif
  109. /* Weak default function for arch/board-specific fixups to the spl_image_info */
  110. void __weak spl_perform_fixups(struct spl_image_info *spl_image)
  111. {
  112. }
  113. void spl_fixup_fdt(void *fdt_blob)
  114. {
  115. #if defined(CONFIG_SPL_OF_LIBFDT)
  116. int err;
  117. if (!fdt_blob)
  118. return;
  119. err = fdt_check_header(fdt_blob);
  120. if (err < 0) {
  121. printf("fdt_root: %s\n", fdt_strerror(err));
  122. return;
  123. }
  124. /* fixup the memory dt node */
  125. err = fdt_shrink_to_minimum(fdt_blob, 0);
  126. if (err == 0) {
  127. printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
  128. return;
  129. }
  130. err = arch_fixup_fdt(fdt_blob);
  131. if (err) {
  132. printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
  133. return;
  134. }
  135. #endif
  136. }
  137. ulong spl_get_image_pos(void)
  138. {
  139. if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
  140. return BINMAN_SYM_MISSING;
  141. #ifdef CONFIG_VPL
  142. if (spl_next_phase() == PHASE_VPL)
  143. return binman_sym(ulong, u_boot_vpl_any, image_pos);
  144. #endif
  145. return spl_next_phase() == PHASE_SPL ?
  146. binman_sym(ulong, u_boot_spl_any, image_pos) :
  147. binman_sym(ulong, u_boot_any, image_pos);
  148. }
  149. ulong spl_get_image_size(void)
  150. {
  151. if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
  152. return BINMAN_SYM_MISSING;
  153. #ifdef CONFIG_VPL
  154. if (spl_next_phase() == PHASE_VPL)
  155. return binman_sym(ulong, u_boot_vpl_any, size);
  156. #endif
  157. return spl_next_phase() == PHASE_SPL ?
  158. binman_sym(ulong, u_boot_spl_any, size) :
  159. binman_sym(ulong, u_boot_any, size);
  160. }
  161. ulong spl_get_image_text_base(void)
  162. {
  163. #ifdef CONFIG_VPL
  164. if (spl_next_phase() == PHASE_VPL)
  165. return CONFIG_VPL_TEXT_BASE;
  166. #endif
  167. return spl_next_phase() == PHASE_SPL ? CONFIG_SPL_TEXT_BASE :
  168. CONFIG_TEXT_BASE;
  169. }
  170. /*
  171. * Weak default function for board specific cleanup/preparation before
  172. * Linux boot. Some boards/platforms might not need it, so just provide
  173. * an empty stub here.
  174. */
  175. __weak void spl_board_prepare_for_linux(void)
  176. {
  177. /* Nothing to do! */
  178. }
  179. __weak void spl_board_prepare_for_optee(void *fdt)
  180. {
  181. }
  182. __weak const char *spl_board_loader_name(u32 boot_device)
  183. {
  184. return NULL;
  185. }
  186. #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
  187. __weak void __noreturn jump_to_image_optee(struct spl_image_info *spl_image)
  188. {
  189. spl_optee_entry(NULL, NULL, spl_image->fdt_addr,
  190. (void *)spl_image->entry_point);
  191. }
  192. #endif
  193. __weak void spl_board_prepare_for_boot(void)
  194. {
  195. /* Nothing to do! */
  196. }
  197. __weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
  198. {
  199. return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
  200. }
  201. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  202. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  203. {
  204. ulong u_boot_pos = spl_get_image_pos();
  205. #if CONFIG_SYS_MONITOR_LEN != 0
  206. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  207. #else
  208. /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
  209. spl_image->size = 200 * 1024;
  210. #endif
  211. /*
  212. * Binman error cases: address of the end of the previous region or the
  213. * start of the image's entry area (usually 0) if there is no previous
  214. * region.
  215. */
  216. if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
  217. /* Binman does not support separated entry addresses */
  218. spl_image->entry_point = u_boot_pos;
  219. spl_image->load_addr = u_boot_pos;
  220. } else {
  221. spl_image->entry_point = CFG_SYS_UBOOT_START;
  222. spl_image->load_addr = CONFIG_TEXT_BASE;
  223. }
  224. spl_image->os = IH_OS_U_BOOT;
  225. spl_image->name = "U-Boot";
  226. }
  227. #endif
  228. #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
  229. /* Parse and load full fitImage in SPL */
  230. static int spl_load_fit_image(struct spl_image_info *spl_image,
  231. const struct legacy_img_hdr *header)
  232. {
  233. struct bootm_headers images;
  234. const char *fit_uname_config = NULL;
  235. uintptr_t fdt_hack;
  236. const char *uname;
  237. ulong fw_data = 0, dt_data = 0, img_data = 0;
  238. ulong fw_len = 0, dt_len = 0, img_len = 0;
  239. int idx, conf_noffset;
  240. int ret;
  241. #ifdef CONFIG_SPL_FIT_SIGNATURE
  242. images.verify = 1;
  243. #endif
  244. ret = fit_image_load(&images, (ulong)header,
  245. NULL, &fit_uname_config,
  246. IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
  247. FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
  248. if (ret >= 0) {
  249. printf("DEPRECATED: 'standalone = ' property.");
  250. printf("Please use either 'firmware =' or 'kernel ='\n");
  251. } else {
  252. ret = fit_image_load(&images, (ulong)header, NULL,
  253. &fit_uname_config, IH_ARCH_DEFAULT,
  254. IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
  255. &fw_data, &fw_len);
  256. }
  257. if (ret < 0) {
  258. ret = fit_image_load(&images, (ulong)header, NULL,
  259. &fit_uname_config, IH_ARCH_DEFAULT,
  260. IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
  261. &fw_data, &fw_len);
  262. }
  263. if (ret < 0)
  264. return ret;
  265. spl_image->size = fw_len;
  266. spl_image->entry_point = fw_data;
  267. spl_image->load_addr = fw_data;
  268. if (fit_image_get_os(header, ret, &spl_image->os))
  269. spl_image->os = IH_OS_INVALID;
  270. spl_image->name = genimg_get_os_name(spl_image->os);
  271. debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
  272. spl_image->name, spl_image->load_addr, spl_image->size);
  273. #ifdef CONFIG_SPL_FIT_SIGNATURE
  274. images.verify = 1;
  275. #endif
  276. ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
  277. IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
  278. FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
  279. if (ret >= 0) {
  280. spl_image->fdt_addr = (void *)dt_data;
  281. if (spl_image->os == IH_OS_U_BOOT) {
  282. /* HACK: U-Boot expects FDT at a specific address */
  283. fdt_hack = spl_image->load_addr + spl_image->size;
  284. fdt_hack = (fdt_hack + 3) & ~3;
  285. debug("Relocating FDT to %p\n", spl_image->fdt_addr);
  286. memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
  287. }
  288. }
  289. conf_noffset = fit_conf_get_node((const void *)header,
  290. fit_uname_config);
  291. if (conf_noffset < 0)
  292. return 0;
  293. for (idx = 0;
  294. uname = fdt_stringlist_get((const void *)header, conf_noffset,
  295. FIT_LOADABLE_PROP, idx,
  296. NULL), uname;
  297. idx++)
  298. {
  299. #ifdef CONFIG_SPL_FIT_SIGNATURE
  300. images.verify = 1;
  301. #endif
  302. ret = fit_image_load(&images, (ulong)header,
  303. &uname, &fit_uname_config,
  304. IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
  305. FIT_LOAD_OPTIONAL_NON_ZERO,
  306. &img_data, &img_len);
  307. if (ret < 0)
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. #endif
  313. __weak int spl_parse_board_header(struct spl_image_info *spl_image,
  314. const struct spl_boot_device *bootdev,
  315. const void *image_header, size_t size)
  316. {
  317. return -EINVAL;
  318. }
  319. __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
  320. const struct legacy_img_hdr *header)
  321. {
  322. /* LEGACY image not supported */
  323. debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
  324. return -EINVAL;
  325. }
  326. int spl_parse_image_header(struct spl_image_info *spl_image,
  327. const struct spl_boot_device *bootdev,
  328. const struct legacy_img_hdr *header)
  329. {
  330. #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
  331. int ret = spl_load_fit_image(spl_image, header);
  332. if (!ret)
  333. return ret;
  334. #endif
  335. if (image_get_magic(header) == IH_MAGIC) {
  336. int ret;
  337. ret = spl_parse_legacy_header(spl_image, header);
  338. if (ret)
  339. return ret;
  340. } else {
  341. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  342. /*
  343. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  344. * code which loads images in SPL cannot guarantee that
  345. * absolutely all read errors will be reported.
  346. * An example is the LPC32XX MLC NAND driver, which
  347. * will consider that a completely unreadable NAND block
  348. * is bad, and thus should be skipped silently.
  349. */
  350. panic("** no mkimage signature but raw image not supported");
  351. #endif
  352. #if CONFIG_IS_ENABLED(OS_BOOT)
  353. #if defined(CMD_BOOTI)
  354. ulong start, size;
  355. if (!booti_setup((ulong)header, &start, &size, 0)) {
  356. spl_image->name = "Linux";
  357. spl_image->os = IH_OS_LINUX;
  358. spl_image->load_addr = start;
  359. spl_image->entry_point = start;
  360. spl_image->size = size;
  361. debug(SPL_TPL_PROMPT
  362. "payload Image, load addr: 0x%lx size: %d\n",
  363. spl_image->load_addr, spl_image->size);
  364. return 0;
  365. }
  366. #elif defined(CMD_BOOTZ)
  367. ulong start, end;
  368. if (!bootz_setup((ulong)header, &start, &end)) {
  369. spl_image->name = "Linux";
  370. spl_image->os = IH_OS_LINUX;
  371. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  372. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  373. spl_image->size = end - start;
  374. debug(SPL_TPL_PROMPT
  375. "payload zImage, load addr: 0x%lx size: %d\n",
  376. spl_image->load_addr, spl_image->size);
  377. return 0;
  378. }
  379. #endif
  380. #endif
  381. if (!spl_parse_board_header(spl_image, bootdev, (const void *)header, sizeof(*header)))
  382. return 0;
  383. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  384. /* Signature not found - assume u-boot.bin */
  385. debug("mkimage signature not found - ih_magic = %x\n",
  386. header->ih_magic);
  387. spl_set_header_raw_uboot(spl_image);
  388. #else
  389. /* RAW image not supported, proceed to other boot methods. */
  390. debug("Raw boot image support not enabled, proceeding to other boot methods\n");
  391. return -EINVAL;
  392. #endif
  393. }
  394. return 0;
  395. }
  396. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  397. {
  398. typedef void __noreturn (*image_entry_noargs_t)(void);
  399. image_entry_noargs_t image_entry =
  400. (image_entry_noargs_t)spl_image->entry_point;
  401. debug("image entry point: 0x%lx\n", spl_image->entry_point);
  402. image_entry();
  403. }
  404. #if CONFIG_IS_ENABLED(HANDOFF)
  405. /**
  406. * Set up the SPL hand-off information
  407. *
  408. * This is initially empty (zero) but can be written by
  409. */
  410. static int setup_spl_handoff(void)
  411. {
  412. struct spl_handoff *ho;
  413. ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
  414. if (!ho)
  415. return -ENOENT;
  416. return 0;
  417. }
  418. __weak int handoff_arch_save(struct spl_handoff *ho)
  419. {
  420. return 0;
  421. }
  422. static int write_spl_handoff(void)
  423. {
  424. struct spl_handoff *ho;
  425. int ret;
  426. ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
  427. if (!ho)
  428. return -ENOENT;
  429. handoff_save_dram(ho);
  430. ret = handoff_arch_save(ho);
  431. if (ret)
  432. return ret;
  433. debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
  434. return 0;
  435. }
  436. #else
  437. static inline int setup_spl_handoff(void) { return 0; }
  438. static inline int write_spl_handoff(void) { return 0; }
  439. #endif /* HANDOFF */
  440. /**
  441. * get_bootstage_id() - Get the bootstage ID to emit
  442. *
  443. * @start: true if this is for starting SPL, false for ending it
  444. * Return: bootstage ID to use
  445. */
  446. static enum bootstage_id get_bootstage_id(bool start)
  447. {
  448. enum u_boot_phase phase = spl_phase();
  449. if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
  450. return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
  451. else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
  452. return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
  453. else
  454. return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
  455. }
  456. static int spl_common_init(bool setup_malloc)
  457. {
  458. int ret;
  459. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  460. if (setup_malloc) {
  461. #ifdef CFG_MALLOC_F_ADDR
  462. gd->malloc_base = CFG_MALLOC_F_ADDR;
  463. #endif
  464. gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
  465. gd->malloc_ptr = 0;
  466. }
  467. #endif
  468. ret = bootstage_init(u_boot_first_phase());
  469. if (ret) {
  470. debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
  471. ret);
  472. return ret;
  473. }
  474. #ifdef CONFIG_BOOTSTAGE_STASH
  475. if (!u_boot_first_phase()) {
  476. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  477. CONFIG_BOOTSTAGE_STASH_SIZE);
  478. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  479. if (ret)
  480. debug("%s: Failed to unstash bootstage: ret=%d\n",
  481. __func__, ret);
  482. }
  483. #endif /* CONFIG_BOOTSTAGE_STASH */
  484. bootstage_mark_name(get_bootstage_id(true),
  485. spl_phase_name(spl_phase()));
  486. #if CONFIG_IS_ENABLED(LOG)
  487. ret = log_init();
  488. if (ret) {
  489. debug("%s: Failed to set up logging\n", __func__);
  490. return ret;
  491. }
  492. #endif
  493. if (CONFIG_IS_ENABLED(OF_REAL)) {
  494. ret = fdtdec_setup();
  495. if (ret) {
  496. debug("fdtdec_setup() returned error %d\n", ret);
  497. return ret;
  498. }
  499. }
  500. if (CONFIG_IS_ENABLED(DM)) {
  501. bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
  502. spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
  503. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  504. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  505. bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
  506. if (ret) {
  507. debug("dm_init_and_scan() returned error %d\n", ret);
  508. return ret;
  509. }
  510. }
  511. return 0;
  512. }
  513. void spl_set_bd(void)
  514. {
  515. /*
  516. * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
  517. * writeable.
  518. */
  519. if (!gd->bd)
  520. gd->bd = &bdata;
  521. }
  522. int spl_early_init(void)
  523. {
  524. int ret;
  525. debug("%s\n", __func__);
  526. ret = spl_common_init(true);
  527. if (ret)
  528. return ret;
  529. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  530. return 0;
  531. }
  532. int spl_init(void)
  533. {
  534. int ret;
  535. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  536. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  537. debug("%s\n", __func__);
  538. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  539. ret = spl_common_init(setup_malloc);
  540. if (ret)
  541. return ret;
  542. }
  543. gd->flags |= GD_FLG_SPL_INIT;
  544. return 0;
  545. }
  546. #ifndef BOOT_DEVICE_NONE
  547. #define BOOT_DEVICE_NONE 0xdeadbeef
  548. #endif
  549. __weak void board_boot_order(u32 *spl_boot_list)
  550. {
  551. spl_boot_list[0] = spl_boot_device();
  552. }
  553. __weak int spl_check_board_image(struct spl_image_info *spl_image,
  554. const struct spl_boot_device *bootdev)
  555. {
  556. return 0;
  557. }
  558. static int spl_load_image(struct spl_image_info *spl_image,
  559. struct spl_image_loader *loader)
  560. {
  561. int ret;
  562. struct spl_boot_device bootdev;
  563. bootdev.boot_device = loader->boot_device;
  564. bootdev.boot_device_name = NULL;
  565. ret = loader->load_image(spl_image, &bootdev);
  566. #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
  567. if (!ret && spl_image->dcrc_length) {
  568. /* check data crc */
  569. ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
  570. spl_image->dcrc_length, CHUNKSZ_CRC32);
  571. if (dcrc != spl_image->dcrc) {
  572. puts("SPL: Image data CRC check failed!\n");
  573. ret = -EINVAL;
  574. }
  575. }
  576. #endif
  577. if (!ret)
  578. ret = spl_check_board_image(spl_image, &bootdev);
  579. return ret;
  580. }
  581. /**
  582. * boot_from_devices() - Try loading a booting U-Boot from a list of devices
  583. *
  584. * @spl_image: Place to put the image details if successful
  585. * @spl_boot_list: List of boot devices to try
  586. * @count: Number of elements in spl_boot_list
  587. * Return: 0 if OK, -ENODEV if there were no boot devices
  588. * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
  589. * devices but none worked
  590. */
  591. static int boot_from_devices(struct spl_image_info *spl_image,
  592. u32 spl_boot_list[], int count)
  593. {
  594. struct spl_image_loader *drv =
  595. ll_entry_start(struct spl_image_loader, spl_image_loader);
  596. const int n_ents =
  597. ll_entry_count(struct spl_image_loader, spl_image_loader);
  598. int ret = -ENODEV;
  599. int i;
  600. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  601. struct spl_image_loader *loader;
  602. int bootdev = spl_boot_list[i];
  603. if (CONFIG_IS_ENABLED(SHOW_ERRORS))
  604. ret = -ENXIO;
  605. for (loader = drv; loader != drv + n_ents; loader++) {
  606. if (bootdev != loader->boot_device)
  607. continue;
  608. if (!CONFIG_IS_ENABLED(SILENT_CONSOLE)) {
  609. if (loader)
  610. printf("Trying to boot from %s\n",
  611. spl_loader_name(loader));
  612. else if (CONFIG_IS_ENABLED(SHOW_ERRORS)) {
  613. printf(SPL_TPL_PROMPT
  614. "Unsupported Boot Device %d\n",
  615. bootdev);
  616. } else {
  617. puts(SPL_TPL_PROMPT
  618. "Unsupported Boot Device!\n");
  619. }
  620. }
  621. if (loader &&
  622. !spl_load_image(spl_image, loader)) {
  623. spl_image->boot_device = bootdev;
  624. return 0;
  625. }
  626. }
  627. }
  628. return ret;
  629. }
  630. #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
  631. void board_init_f(ulong dummy)
  632. {
  633. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  634. int ret;
  635. ret = spl_early_init();
  636. if (ret) {
  637. debug("spl_early_init() failed: %d\n", ret);
  638. hang();
  639. }
  640. }
  641. preloader_console_init();
  642. }
  643. #endif
  644. void board_init_r(gd_t *dummy1, ulong dummy2)
  645. {
  646. u32 spl_boot_list[] = {
  647. BOOT_DEVICE_NONE,
  648. BOOT_DEVICE_NONE,
  649. BOOT_DEVICE_NONE,
  650. BOOT_DEVICE_NONE,
  651. BOOT_DEVICE_NONE,
  652. };
  653. struct spl_image_info spl_image;
  654. int ret;
  655. debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
  656. spl_set_bd();
  657. #if defined(CONFIG_SYS_SPL_MALLOC)
  658. mem_malloc_init(SYS_SPL_MALLOC_START, CONFIG_SYS_SPL_MALLOC_SIZE);
  659. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  660. #endif
  661. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  662. if (spl_init())
  663. hang();
  664. }
  665. #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
  666. /*
  667. * timer_init() does not exist on PPC systems. The timer is initialized
  668. * and enabled (decrementer) in interrupt_init() here.
  669. */
  670. timer_init();
  671. #endif
  672. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  673. ret = bloblist_init();
  674. if (ret) {
  675. debug("%s: Failed to set up bloblist: ret=%d\n",
  676. __func__, ret);
  677. puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
  678. hang();
  679. }
  680. }
  681. if (CONFIG_IS_ENABLED(HANDOFF)) {
  682. int ret;
  683. ret = setup_spl_handoff();
  684. if (ret) {
  685. puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
  686. hang();
  687. }
  688. }
  689. #if CONFIG_IS_ENABLED(BOARD_INIT)
  690. spl_board_init();
  691. #endif
  692. #if defined(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT)
  693. initr_watchdog();
  694. #endif
  695. if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
  696. IS_ENABLED(CONFIG_SPL_ATF))
  697. dram_init_banksize();
  698. if (CONFIG_IS_ENABLED(PCI)) {
  699. ret = pci_init();
  700. if (ret)
  701. puts(SPL_TPL_PROMPT "Cannot initialize PCI\n");
  702. /* Don't fail. We still can try other boot methods. */
  703. }
  704. bootcount_inc();
  705. /* Dump driver model states to aid analysis */
  706. if (CONFIG_IS_ENABLED(DM_STATS)) {
  707. struct dm_stats mem;
  708. dm_get_mem(&mem);
  709. dm_dump_mem(&mem);
  710. }
  711. memset(&spl_image, '\0', sizeof(spl_image));
  712. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  713. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  714. #endif
  715. spl_image.boot_device = BOOT_DEVICE_NONE;
  716. board_boot_order(spl_boot_list);
  717. ret = boot_from_devices(&spl_image, spl_boot_list,
  718. ARRAY_SIZE(spl_boot_list));
  719. if (ret) {
  720. if (CONFIG_IS_ENABLED(SHOW_ERRORS) &&
  721. CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT))
  722. printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
  723. ret);
  724. else
  725. puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
  726. hang();
  727. }
  728. spl_perform_fixups(&spl_image);
  729. if (CONFIG_IS_ENABLED(HANDOFF)) {
  730. ret = write_spl_handoff();
  731. if (ret)
  732. printf(SPL_TPL_PROMPT
  733. "SPL hand-off write failed (err=%d)\n", ret);
  734. }
  735. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  736. ret = bloblist_finish();
  737. if (ret)
  738. printf("Warning: Failed to finish bloblist (ret=%d)\n",
  739. ret);
  740. }
  741. switch (spl_image.os) {
  742. case IH_OS_U_BOOT:
  743. debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
  744. break;
  745. #if CONFIG_IS_ENABLED(ATF)
  746. case IH_OS_ARM_TRUSTED_FIRMWARE:
  747. debug("Jumping to U-Boot via ARM Trusted Firmware\n");
  748. spl_fixup_fdt(spl_image.fdt_addr);
  749. spl_invoke_atf(&spl_image);
  750. break;
  751. #endif
  752. #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
  753. case IH_OS_TEE:
  754. debug("Jumping to U-Boot via OP-TEE\n");
  755. spl_board_prepare_for_optee(spl_image.fdt_addr);
  756. jump_to_image_optee(&spl_image);
  757. break;
  758. #endif
  759. #if CONFIG_IS_ENABLED(OPENSBI)
  760. case IH_OS_OPENSBI:
  761. debug("Jumping to U-Boot via RISC-V OpenSBI\n");
  762. spl_invoke_opensbi(&spl_image);
  763. break;
  764. #endif
  765. #if CONFIG_IS_ENABLED(OS_BOOT)
  766. case IH_OS_LINUX:
  767. debug("Jumping to Linux\n");
  768. #if defined(CONFIG_SYS_SPL_ARGS_ADDR)
  769. spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  770. #endif
  771. spl_board_prepare_for_linux();
  772. jump_to_image_linux(&spl_image);
  773. #endif
  774. default:
  775. debug("Unsupported OS image.. Jumping nevertheless..\n");
  776. }
  777. #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  778. debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
  779. gd->malloc_ptr / 1024);
  780. #endif
  781. bootstage_mark_name(get_bootstage_id(false), "end phase");
  782. #ifdef CONFIG_BOOTSTAGE_STASH
  783. ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  784. CONFIG_BOOTSTAGE_STASH_SIZE);
  785. if (ret)
  786. debug("Failed to stash bootstage: err=%d\n", ret);
  787. #endif
  788. if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
  789. struct udevice *dev;
  790. int rc;
  791. rc = uclass_find_device(UCLASS_VIDEO, 0, &dev);
  792. if (!rc && dev) {
  793. rc = device_remove(dev, DM_REMOVE_NORMAL);
  794. if (rc)
  795. printf("Cannot remove video device '%s' (err=%d)\n",
  796. dev->name, rc);
  797. }
  798. }
  799. spl_board_prepare_for_boot();
  800. jump_to_image_no_args(&spl_image);
  801. }
  802. /*
  803. * This requires UART clocks to be enabled. In order for this to work the
  804. * caller must ensure that the gd pointer is valid.
  805. */
  806. void preloader_console_init(void)
  807. {
  808. #ifdef CONFIG_SPL_SERIAL
  809. gd->baudrate = CONFIG_BAUDRATE;
  810. serial_init(); /* serial communications setup */
  811. gd->have_console = 1;
  812. #if CONFIG_IS_ENABLED(BANNER_PRINT)
  813. puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
  814. U_BOOT_TIME " " U_BOOT_TZ ")\n");
  815. #endif
  816. #ifdef CONFIG_SPL_DISPLAY_PRINT
  817. spl_display_print();
  818. #endif
  819. #endif
  820. }
  821. /**
  822. * This function is called before the stack is changed from initial stack to
  823. * relocated stack. It tries to dump the stack size used
  824. */
  825. __weak void spl_relocate_stack_check(void)
  826. {
  827. #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
  828. ulong init_sp = gd->start_addr_sp;
  829. ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
  830. u8 *ptr = (u8 *)stack_bottom;
  831. ulong i;
  832. for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
  833. if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
  834. break;
  835. ptr++;
  836. }
  837. printf("SPL initial stack usage: %lu bytes\n",
  838. CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
  839. #endif
  840. }
  841. /**
  842. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  843. *
  844. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  845. * for the main board_init_r() execution. This is typically because we need
  846. * more stack space for things like the MMC sub-system.
  847. *
  848. * This function calculates the stack position, copies the global_data into
  849. * place, sets the new gd (except for ARM, for which setting GD within a C
  850. * function may not always work) and returns the new stack position. The
  851. * caller is responsible for setting up the sp register and, in the case
  852. * of ARM, setting up gd.
  853. *
  854. * All of this is done using the same layout and alignments as done in
  855. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  856. *
  857. * Return: new stack location, or 0 to use the same stack
  858. */
  859. ulong spl_relocate_stack_gd(void)
  860. {
  861. #ifdef CONFIG_SPL_STACK_R
  862. gd_t *new_gd;
  863. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  864. if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
  865. spl_relocate_stack_check();
  866. #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  867. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  868. debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
  869. gd->malloc_ptr, gd->malloc_ptr / 1024);
  870. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  871. gd->malloc_base = ptr;
  872. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  873. gd->malloc_ptr = 0;
  874. }
  875. #endif
  876. /* Get stack position: use 8-byte alignment for ABI compliance */
  877. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  878. gd->start_addr_sp = ptr;
  879. new_gd = (gd_t *)ptr;
  880. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  881. #if CONFIG_IS_ENABLED(DM)
  882. dm_fixup_for_gd_move(new_gd);
  883. #endif
  884. #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
  885. gd = new_gd;
  886. #endif
  887. return ptr;
  888. #else
  889. return 0;
  890. #endif
  891. }
  892. #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
  893. ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
  894. (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
  895. void bootcount_store(ulong a)
  896. {
  897. }
  898. ulong bootcount_load(void)
  899. {
  900. return 0;
  901. }
  902. #endif