sec-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * Common security related functions for OMAP devices
  5. *
  6. * (C) Copyright 2016-2017
  7. * Texas Instruments, <www.ti.com>
  8. *
  9. * Daniel Allred <d-allred@ti.com>
  10. * Andreas Dannenberg <dannenberg@ti.com>
  11. * Harinarayan Bhatta <harinarayan@ti.com>
  12. * Andrew F. Davis <afd@ti.com>
  13. */
  14. #include <common.h>
  15. #include <cpu_func.h>
  16. #include <stdarg.h>
  17. #include <asm/arch/sys_proto.h>
  18. #include <asm/cache.h>
  19. #include <asm/omap_common.h>
  20. #include <asm/omap_sec_common.h>
  21. #include <asm/spl.h>
  22. #include <asm/ti-common/sys_proto.h>
  23. #include <mapmem.h>
  24. #include <spl.h>
  25. #include <tee/optee.h>
  26. /* Index for signature verify ROM API */
  27. #ifdef CONFIG_AM33XX
  28. #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000C)
  29. #else
  30. #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000E)
  31. #endif
  32. /* Index for signature PPA-based TI HAL APIs */
  33. #define PPA_HAL_SERVICES_START_INDEX (0x200)
  34. #define PPA_SERV_HAL_TEE_LOAD_MASTER (PPA_HAL_SERVICES_START_INDEX + 23)
  35. #define PPA_SERV_HAL_TEE_LOAD_SLAVE (PPA_HAL_SERVICES_START_INDEX + 24)
  36. #define PPA_SERV_HAL_SETUP_SEC_RESVD_REGION (PPA_HAL_SERVICES_START_INDEX + 25)
  37. #define PPA_SERV_HAL_SETUP_EMIF_FW_REGION (PPA_HAL_SERVICES_START_INDEX + 26)
  38. #define PPA_SERV_HAL_LOCK_EMIF_FW (PPA_HAL_SERVICES_START_INDEX + 27)
  39. /* Offset of header size if image is signed as ISW */
  40. #define HEADER_SIZE_OFFSET (0x6D)
  41. int tee_loaded = 0;
  42. /* Argument for PPA_SERV_HAL_TEE_LOAD_MASTER */
  43. struct ppa_tee_load_info {
  44. u32 tee_sec_mem_start; /* Physical start address reserved for TEE */
  45. u32 tee_sec_mem_size; /* Size of the memory reserved for TEE */
  46. u32 tee_cert_start; /* Address where signed TEE binary is loaded */
  47. u32 tee_cert_size; /* Size of TEE certificate (signed binary) */
  48. u32 tee_jump_addr; /* Address to jump to start TEE execution */
  49. u32 tee_arg0; /* argument to TEE jump function, in r0 */
  50. };
  51. static uint32_t secure_rom_call_args[5] __aligned(ARCH_DMA_MINALIGN) __section(".data");
  52. u32 secure_rom_call(u32 service, u32 proc_id, u32 flag, ...)
  53. {
  54. int i;
  55. u32 num_args;
  56. va_list ap;
  57. va_start(ap, flag);
  58. num_args = va_arg(ap, u32);
  59. if (num_args > 4) {
  60. va_end(ap);
  61. return 1;
  62. }
  63. /* Copy args to aligned args structure */
  64. for (i = 0; i < num_args; i++)
  65. secure_rom_call_args[i + 1] = va_arg(ap, u32);
  66. secure_rom_call_args[0] = num_args;
  67. va_end(ap);
  68. /* if data cache is enabled, flush the aligned args structure */
  69. flush_dcache_range(
  70. (unsigned int)&secure_rom_call_args[0],
  71. (unsigned int)&secure_rom_call_args[0] +
  72. roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN));
  73. return omap_smc_sec(service, proc_id, flag, secure_rom_call_args);
  74. }
  75. static u32 find_sig_start(char *image, size_t size)
  76. {
  77. char *image_end = image + size;
  78. char *sig_start_magic = "CERT_";
  79. int magic_str_len = strlen(sig_start_magic);
  80. char *ch;
  81. while (--image_end > image) {
  82. if (*image_end == '_') {
  83. ch = image_end - magic_str_len + 1;
  84. if (!strncmp(ch, sig_start_magic, magic_str_len))
  85. return (u32)ch;
  86. }
  87. }
  88. return 0;
  89. }
  90. int secure_boot_verify_image(void **image, size_t *size)
  91. {
  92. int result = 1;
  93. u32 cert_addr, sig_addr;
  94. size_t cert_size;
  95. /* Perform cache writeback on input buffer */
  96. flush_dcache_range(
  97. rounddown((u32)*image, ARCH_DMA_MINALIGN),
  98. roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
  99. cert_addr = (uint32_t)*image;
  100. sig_addr = find_sig_start((char *)*image, *size);
  101. if (sig_addr == 0) {
  102. printf("No signature found in image!\n");
  103. result = 1;
  104. goto auth_exit;
  105. }
  106. *size = sig_addr - cert_addr; /* Subtract out the signature size */
  107. /* Subtract header if present */
  108. if (strncmp((char *)sig_addr, "CERT_ISW_", 9) == 0)
  109. *size -= ((u32 *)*image)[HEADER_SIZE_OFFSET];
  110. cert_size = *size;
  111. /* Check if image load address is 32-bit aligned */
  112. if (!IS_ALIGNED(cert_addr, 4)) {
  113. printf("Image is not 4-byte aligned!\n");
  114. result = 1;
  115. goto auth_exit;
  116. }
  117. /* Image size also should be multiple of 4 */
  118. if (!IS_ALIGNED(cert_size, 4)) {
  119. printf("Image size is not 4-byte aligned!\n");
  120. result = 1;
  121. goto auth_exit;
  122. }
  123. /* Call ROM HAL API to verify certificate signature */
  124. debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
  125. cert_addr, cert_size, sig_addr);
  126. result = secure_rom_call(
  127. API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
  128. 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
  129. /* Perform cache writeback on output buffer */
  130. flush_dcache_range(
  131. rounddown((u32)*image, ARCH_DMA_MINALIGN),
  132. roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
  133. auth_exit:
  134. if (result != 0) {
  135. printf("Authentication failed!\n");
  136. printf("Return Value = %08X\n", result);
  137. hang();
  138. }
  139. /*
  140. * Output notification of successful authentication to re-assure the
  141. * user that the secure code is being processed as expected. However
  142. * suppress any such log output in case of building for SPL and booting
  143. * via YMODEM. This is done to avoid disturbing the YMODEM serial
  144. * protocol transactions.
  145. */
  146. if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
  147. IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
  148. spl_boot_device() == BOOT_DEVICE_UART))
  149. printf("Authentication passed\n");
  150. return result;
  151. }
  152. u32 get_sec_mem_start(void)
  153. {
  154. u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
  155. u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
  156. /*
  157. * Total reserved region is all contiguous with protected
  158. * region coming first, followed by the non-secure region.
  159. * If 0x0 start address is given, we simply put the reserved
  160. * region at the end of the external DRAM.
  161. */
  162. if (sec_mem_start == 0)
  163. sec_mem_start =
  164. (CONFIG_SYS_SDRAM_BASE + (
  165. #if defined(CONFIG_OMAP54XX)
  166. omap_sdram_size()
  167. #else
  168. get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  169. CONFIG_MAX_RAM_BANK_SIZE)
  170. #endif
  171. - sec_mem_size));
  172. return sec_mem_start;
  173. }
  174. int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
  175. uint32_t size, uint32_t access_perm,
  176. uint32_t initiator_perm)
  177. {
  178. int result = 1;
  179. /*
  180. * Call PPA HAL API to do any other general firewall
  181. * configuration for regions 1-6 of the EMIF firewall.
  182. */
  183. debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
  184. region_num, start_addr, size);
  185. result = secure_rom_call(
  186. PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
  187. (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
  188. size, access_perm, initiator_perm);
  189. if (result != 0) {
  190. puts("Secure EMIF Firewall Setup failed!\n");
  191. debug("Return Value = %x\n", result);
  192. }
  193. return result;
  194. }
  195. #if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
  196. CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
  197. #error "TI Secure EMIF: Protected size cannot be larger than total size."
  198. #endif
  199. int secure_emif_reserve(void)
  200. {
  201. int result = 1;
  202. u32 sec_mem_start = get_sec_mem_start();
  203. u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  204. /* If there is no protected region, there is no reservation to make */
  205. if (sec_prot_size == 0)
  206. return 0;
  207. /*
  208. * Call PPA HAL API to reserve a chunk of EMIF SDRAM
  209. * for secure world use. This region should be carved out
  210. * from use by any public code. EMIF firewall region 7
  211. * will be used to protect this block of memory.
  212. */
  213. result = secure_rom_call(
  214. PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
  215. 0, 0, 2, sec_mem_start, sec_prot_size);
  216. if (result != 0) {
  217. puts("SDRAM Firewall: Secure memory reservation failed!\n");
  218. debug("Return Value = %x\n", result);
  219. }
  220. return result;
  221. }
  222. int secure_emif_firewall_lock(void)
  223. {
  224. int result = 1;
  225. /*
  226. * Call PPA HAL API to lock the EMIF firewall configurations.
  227. * After this API is called, none of the PPA HAL APIs for
  228. * configuring the EMIF firewalls will be usable again (that
  229. * is, calls to those APIs will return failure and have no
  230. * effect).
  231. */
  232. result = secure_rom_call(
  233. PPA_SERV_HAL_LOCK_EMIF_FW,
  234. 0, 0, 0);
  235. if (result != 0) {
  236. puts("Secure EMIF Firewall Lock failed!\n");
  237. debug("Return Value = %x\n", result);
  238. }
  239. return result;
  240. }
  241. static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
  242. int secure_tee_install(u32 addr)
  243. {
  244. struct optee_header *hdr;
  245. void *loadptr;
  246. u32 tee_file_size;
  247. u32 sec_mem_start = get_sec_mem_start();
  248. const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  249. u32 ret;
  250. /* If there is no protected region, there is no place to put the TEE */
  251. if (size == 0) {
  252. printf("Error loading TEE, no protected memory region available\n");
  253. return -ENOBUFS;
  254. }
  255. hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
  256. /* 280 bytes = size of signature */
  257. tee_file_size = hdr->init_size + hdr->paged_size +
  258. sizeof(struct optee_header) + 280;
  259. if ((hdr->magic != OPTEE_MAGIC) ||
  260. (hdr->version != OPTEE_VERSION) ||
  261. (tee_file_size > size)) {
  262. printf("Error in TEE header. Check firewall and TEE sizes\n");
  263. unmap_sysmem(hdr);
  264. return CMD_RET_FAILURE;
  265. }
  266. tee_info.tee_sec_mem_start = sec_mem_start;
  267. tee_info.tee_sec_mem_size = size;
  268. tee_info.tee_jump_addr = hdr->init_load_addr_lo;
  269. tee_info.tee_cert_start = addr;
  270. tee_info.tee_cert_size = tee_file_size;
  271. tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
  272. unmap_sysmem(hdr);
  273. loadptr = map_sysmem(addr, tee_file_size);
  274. debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
  275. debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
  276. debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
  277. debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
  278. debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
  279. debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
  280. debug("tee_file_size = %d\n", tee_file_size);
  281. #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
  282. flush_dcache_range(
  283. rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
  284. roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
  285. flush_dcache_range((u32)&tee_info, (u32)&tee_info +
  286. roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
  287. #endif
  288. unmap_sysmem(loadptr);
  289. ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
  290. if (ret) {
  291. printf("TEE_LOAD_MASTER Failed\n");
  292. return ret;
  293. }
  294. printf("TEE_LOAD_MASTER Done\n");
  295. #if defined(CONFIG_OMAP54XX)
  296. if (!is_dra72x()) {
  297. u32 *smc_cpu1_params;
  298. /* Reuse the tee_info buffer for SMC params */
  299. smc_cpu1_params = (u32 *)&tee_info;
  300. smc_cpu1_params[0] = 0;
  301. #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
  302. flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
  303. roundup(sizeof(u32), ARCH_DMA_MINALIGN));
  304. #endif
  305. ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
  306. smc_cpu1_params);
  307. if (ret) {
  308. printf("TEE_LOAD_SLAVE Failed\n");
  309. return ret;
  310. }
  311. printf("TEE_LOAD_SLAVE Done\n");
  312. }
  313. #endif
  314. tee_loaded = 1;
  315. return 0;
  316. }