sec-fxns.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. *
  3. * Security related functions for OMAP5 class devices
  4. *
  5. * (C) Copyright 2016
  6. * Texas Instruments, <www.ti.com>
  7. *
  8. * Daniel Allred <d-allred@ti.com>
  9. * Harinarayan Bhatta <harinarayan@ti.com>
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <stdarg.h>
  15. #include <asm/arch/sys_proto.h>
  16. #include <asm/omap_common.h>
  17. #include <asm/omap_sec_common.h>
  18. #include <asm/spl.h>
  19. #include <spl.h>
  20. #include <asm/cache.h>
  21. #include <mapmem.h>
  22. #include <tee/optee.h>
  23. /* Index for signature PPA-based TI HAL APIs */
  24. #define PPA_HAL_SERVICES_START_INDEX (0x200)
  25. #define PPA_SERV_HAL_TEE_LOAD_MASTER (PPA_HAL_SERVICES_START_INDEX + 23)
  26. #define PPA_SERV_HAL_TEE_LOAD_SLAVE (PPA_HAL_SERVICES_START_INDEX + 24)
  27. #define PPA_SERV_HAL_SETUP_SEC_RESVD_REGION (PPA_HAL_SERVICES_START_INDEX + 25)
  28. #define PPA_SERV_HAL_SETUP_EMIF_FW_REGION (PPA_HAL_SERVICES_START_INDEX + 26)
  29. #define PPA_SERV_HAL_LOCK_EMIF_FW (PPA_HAL_SERVICES_START_INDEX + 27)
  30. int tee_loaded = 0;
  31. /* Argument for PPA_SERV_HAL_TEE_LOAD_MASTER */
  32. struct ppa_tee_load_info {
  33. u32 tee_sec_mem_start; /* Physical start address reserved for TEE */
  34. u32 tee_sec_mem_size; /* Size of the memory reserved for TEE */
  35. u32 tee_cert_start; /* Address where signed TEE binary is loaded */
  36. u32 tee_cert_size; /* Size of TEE certificate (signed binary) */
  37. u32 tee_jump_addr; /* Address to jump to start TEE execution */
  38. u32 tee_arg0; /* argument to TEE jump function, in r0 */
  39. };
  40. static u32 get_sec_mem_start(void)
  41. {
  42. u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
  43. u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
  44. /*
  45. * Total reserved region is all contiguous with protected
  46. * region coming first, followed by the non-secure region.
  47. * If 0x0 start address is given, we simply put the reserved
  48. * region at the end of the external DRAM.
  49. */
  50. if (sec_mem_start == 0)
  51. sec_mem_start =
  52. (CONFIG_SYS_SDRAM_BASE +
  53. (omap_sdram_size() - sec_mem_size));
  54. return sec_mem_start;
  55. }
  56. int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
  57. uint32_t size, uint32_t access_perm,
  58. uint32_t initiator_perm)
  59. {
  60. int result = 1;
  61. /*
  62. * Call PPA HAL API to do any other general firewall
  63. * configuration for regions 1-6 of the EMIF firewall.
  64. */
  65. debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
  66. region_num, start_addr, size);
  67. result = secure_rom_call(
  68. PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
  69. (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
  70. size, access_perm, initiator_perm);
  71. if (result != 0) {
  72. puts("Secure EMIF Firewall Setup failed!\n");
  73. debug("Return Value = %x\n", result);
  74. }
  75. return result;
  76. }
  77. #if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
  78. CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
  79. #error "TI Secure EMIF: Protected size cannot be larger than total size."
  80. #endif
  81. int secure_emif_reserve(void)
  82. {
  83. int result = 1;
  84. u32 sec_mem_start = get_sec_mem_start();
  85. u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  86. /* If there is no protected region, there is no reservation to make */
  87. if (sec_prot_size == 0)
  88. return 0;
  89. /*
  90. * Call PPA HAL API to reserve a chunk of EMIF SDRAM
  91. * for secure world use. This region should be carved out
  92. * from use by any public code. EMIF firewall region 7
  93. * will be used to protect this block of memory.
  94. */
  95. result = secure_rom_call(
  96. PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
  97. 0, 0, 2, sec_mem_start, sec_prot_size);
  98. if (result != 0) {
  99. puts("SDRAM Firewall: Secure memory reservation failed!\n");
  100. debug("Return Value = %x\n", result);
  101. }
  102. return result;
  103. }
  104. int secure_emif_firewall_lock(void)
  105. {
  106. int result = 1;
  107. /*
  108. * Call PPA HAL API to lock the EMIF firewall configurations.
  109. * After this API is called, none of the PPA HAL APIs for
  110. * configuring the EMIF firewalls will be usable again (that
  111. * is, calls to those APIs will return failure and have no
  112. * effect).
  113. */
  114. result = secure_rom_call(
  115. PPA_SERV_HAL_LOCK_EMIF_FW,
  116. 0, 0, 0);
  117. if (result != 0) {
  118. puts("Secure EMIF Firewall Lock failed!\n");
  119. debug("Return Value = %x\n", result);
  120. }
  121. return result;
  122. }
  123. static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
  124. int secure_tee_install(u32 addr)
  125. {
  126. struct optee_header *hdr;
  127. void *loadptr;
  128. u32 tee_file_size;
  129. u32 sec_mem_start = get_sec_mem_start();
  130. const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  131. u32 *smc_cpu1_params;
  132. u32 ret;
  133. /* If there is no protected region, there is no place to put the TEE */
  134. if (size == 0) {
  135. printf("Error loading TEE, no protected memory region available\n");
  136. return -ENOBUFS;
  137. }
  138. hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
  139. /* 280 bytes = size of signature */
  140. tee_file_size = hdr->init_size + hdr->paged_size +
  141. sizeof(struct optee_header) + 280;
  142. if ((hdr->magic != OPTEE_MAGIC) ||
  143. (hdr->version != OPTEE_VERSION) ||
  144. (hdr->init_load_addr_hi != 0) ||
  145. (hdr->init_load_addr_lo < (sec_mem_start + sizeof(struct optee_header))) ||
  146. (tee_file_size > size) ||
  147. ((hdr->init_load_addr_lo + tee_file_size - 1) >
  148. (sec_mem_start + size - 1))) {
  149. printf("Error in TEE header. Check load address and sizes\n");
  150. unmap_sysmem(hdr);
  151. return CMD_RET_FAILURE;
  152. }
  153. tee_info.tee_sec_mem_start = sec_mem_start;
  154. tee_info.tee_sec_mem_size = size;
  155. tee_info.tee_jump_addr = hdr->init_load_addr_lo;
  156. tee_info.tee_cert_start = addr;
  157. tee_info.tee_cert_size = tee_file_size;
  158. tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
  159. unmap_sysmem(hdr);
  160. loadptr = map_sysmem(addr, tee_file_size);
  161. debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
  162. debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
  163. debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
  164. debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
  165. debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
  166. debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
  167. debug("tee_file_size = %d\n", tee_file_size);
  168. #if !defined(CONFIG_SYS_DCACHE_OFF)
  169. flush_dcache_range(
  170. rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
  171. roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
  172. flush_dcache_range((u32)&tee_info, (u32)&tee_info +
  173. roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
  174. #endif
  175. unmap_sysmem(loadptr);
  176. ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
  177. if (ret) {
  178. printf("TEE_LOAD_MASTER Failed\n");
  179. return ret;
  180. }
  181. printf("TEE_LOAD_MASTER Done\n");
  182. if (!is_dra72x()) {
  183. /* Reuse the tee_info buffer for SMC params */
  184. smc_cpu1_params = (u32 *)&tee_info;
  185. smc_cpu1_params[0] = 0;
  186. #if !defined(CONFIG_SYS_DCACHE_OFF)
  187. flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
  188. roundup(sizeof(u32), ARCH_DMA_MINALIGN));
  189. #endif
  190. ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
  191. smc_cpu1_params);
  192. if (ret) {
  193. printf("TEE_LOAD_SLAVE Failed\n");
  194. return ret;
  195. }
  196. printf("TEE_LOAD_SLAVE Done\n");
  197. }
  198. tee_loaded = 1;
  199. return 0;
  200. }