sec-common.c 10 KB

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