fsl_chain_of_trust.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2015 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <env.h>
  8. #include <fsl_validate.h>
  9. #include <fsl_secboot_err.h>
  10. #include <fsl_sfp.h>
  11. #include <dm/root.h>
  12. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FRAMEWORK)
  13. #include <spl.h>
  14. #endif
  15. #ifdef CONFIG_ADDR_MAP
  16. #include <asm/mmu.h>
  17. #endif
  18. #ifdef CONFIG_FSL_CORENET
  19. #include <asm/fsl_pamu.h>
  20. #endif
  21. #ifdef CONFIG_ARCH_LS1021A
  22. #include <asm/arch/immap_ls102xa.h>
  23. #endif
  24. #if defined(CONFIG_MPC85xx)
  25. #define CONFIG_DCFG_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
  26. #else
  27. #define CONFIG_DCFG_ADDR CONFIG_SYS_FSL_GUTS_ADDR
  28. #endif
  29. #ifdef CONFIG_SYS_FSL_CCSR_GUR_LE
  30. #define gur_in32(a) in_le32(a)
  31. #else
  32. #define gur_in32(a) in_be32(a)
  33. #endif
  34. /* Check the Boot Mode. If Secure, return 1 else return 0 */
  35. int fsl_check_boot_mode_secure(void)
  36. {
  37. uint32_t val;
  38. struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
  39. struct ccsr_gur __iomem *gur = (void *)(CONFIG_DCFG_ADDR);
  40. val = sfp_in32(&sfp_regs->ospr) & ITS_MASK;
  41. if (val == ITS_MASK)
  42. return 1;
  43. #if defined(CONFIG_FSL_CORENET) || !defined(CONFIG_MPC85xx)
  44. /* For PBL based platforms check the SB_EN bit in RCWSR */
  45. val = gur_in32(&gur->rcwsr[RCW_SB_EN_REG_INDEX - 1]) & RCW_SB_EN_MASK;
  46. if (val == RCW_SB_EN_MASK)
  47. return 1;
  48. #endif
  49. #if defined(CONFIG_MPC85xx) && !defined(CONFIG_FSL_CORENET)
  50. /* For Non-PBL Platforms, check the Device Status register 2*/
  51. val = gur_in32(&gur->pordevsr2) & MPC85xx_PORDEVSR2_SBC_MASK;
  52. if (val != MPC85xx_PORDEVSR2_SBC_MASK)
  53. return 1;
  54. #endif
  55. return 0;
  56. }
  57. #ifndef CONFIG_SPL_BUILD
  58. int fsl_setenv_chain_of_trust(void)
  59. {
  60. /* Check Boot Mode
  61. * If Boot Mode is Non-Secure, no changes are required
  62. */
  63. if (fsl_check_boot_mode_secure() == 0)
  64. return 0;
  65. /* If Boot mode is Secure, set the environment variables
  66. * bootdelay = 0 (To disable Boot Prompt)
  67. * bootcmd = CONFIG_CHAIN_BOOT_CMD (Validate and execute Boot script)
  68. */
  69. env_set("bootdelay", "-2");
  70. #ifdef CONFIG_ARM
  71. env_set("secureboot", "y");
  72. #else
  73. env_set("bootcmd", CONFIG_CHAIN_BOOT_CMD);
  74. #endif
  75. return 0;
  76. }
  77. #endif
  78. #ifdef CONFIG_SPL_BUILD
  79. void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr)
  80. {
  81. int res;
  82. /*
  83. * Check Boot Mode
  84. * If Boot Mode is Non-Secure, skip validation
  85. */
  86. if (fsl_check_boot_mode_secure() == 0)
  87. return;
  88. printf("SPL: Validating U-Boot image\n");
  89. #ifdef CONFIG_ADDR_MAP
  90. init_addr_map();
  91. #endif
  92. #ifdef CONFIG_FSL_CORENET
  93. if (pamu_init() < 0)
  94. fsl_secboot_handle_error(ERROR_ESBC_PAMU_INIT);
  95. #endif
  96. #ifdef CONFIG_FSL_CAAM
  97. if (sec_init() < 0)
  98. fsl_secboot_handle_error(ERROR_ESBC_SEC_INIT);
  99. #endif
  100. /*
  101. * dm_init_and_scan() is called as part of common SPL framework, so no
  102. * need to call it again but in case of powerpc platforms which currently
  103. * do not use common SPL framework, so need to call this function here.
  104. */
  105. #if defined(CONFIG_SPL_DM) && (!defined(CONFIG_SPL_FRAMEWORK))
  106. dm_init_and_scan(true);
  107. #endif
  108. res = fsl_secboot_validate(hdr_addr, CONFIG_SPL_UBOOT_KEY_HASH,
  109. &img_addr);
  110. if (res == 0)
  111. printf("SPL: Validation of U-boot successful\n");
  112. }
  113. #ifdef CONFIG_SPL_FRAMEWORK
  114. /* Override weak funtion defined in SPL framework to enable validation
  115. * of main u-boot image before jumping to u-boot image.
  116. */
  117. void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  118. {
  119. typedef void __noreturn (*image_entry_noargs_t)(void);
  120. uint32_t hdr_addr;
  121. image_entry_noargs_t image_entry =
  122. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  123. hdr_addr = (spl_image->entry_point + spl_image->size -
  124. CONFIG_U_BOOT_HDR_SIZE);
  125. spl_validate_uboot(hdr_addr, (uintptr_t)spl_image->entry_point);
  126. /*
  127. * In case of failure in validation, spl_validate_uboot would
  128. * not return back in case of Production environment with ITS=1.
  129. * Thus U-Boot will not start.
  130. * In Development environment (ITS=0 and SB_EN=1), the function
  131. * may return back in case of non-fatal failures.
  132. */
  133. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  134. image_entry();
  135. }
  136. #endif /* ifdef CONFIG_SPL_FRAMEWORK */
  137. #endif /* ifdef CONFIG_SPL_BUILD */