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. /* 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);
  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. return 1;
  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. rounddown((u32)*image, ARCH_DMA_MINALIGN),
  96. roundup((u32)*image + *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. /* Subtract header if present */
  106. if (strncmp((char *)sig_addr, "CERT_ISW_", 9) == 0)
  107. *size -= ((u32 *)*image)[HEADER_SIZE_OFFSET];
  108. cert_size = *size;
  109. /* Check if image load address is 32-bit aligned */
  110. if (!IS_ALIGNED(cert_addr, 4)) {
  111. printf("Image is not 4-byte aligned!\n");
  112. result = 1;
  113. goto auth_exit;
  114. }
  115. /* Image size also should be multiple of 4 */
  116. if (!IS_ALIGNED(cert_size, 4)) {
  117. printf("Image size is not 4-byte aligned!\n");
  118. result = 1;
  119. goto auth_exit;
  120. }
  121. /* Call ROM HAL API to verify certificate signature */
  122. debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
  123. cert_addr, cert_size, sig_addr);
  124. result = secure_rom_call(
  125. API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
  126. 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
  127. /* Perform cache writeback on output buffer */
  128. flush_dcache_range(
  129. rounddown((u32)*image, ARCH_DMA_MINALIGN),
  130. roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
  131. auth_exit:
  132. if (result != 0) {
  133. printf("Authentication failed!\n");
  134. printf("Return Value = %08X\n", result);
  135. hang();
  136. }
  137. /*
  138. * Output notification of successful authentication as well the name of
  139. * the signing certificate used to re-assure the user that the secure
  140. * code is being processed as expected. However suppress any such log
  141. * output in case of building for SPL and booting via YMODEM. This is
  142. * done to avoid disturbing the YMODEM serial protocol transactions.
  143. */
  144. if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
  145. IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
  146. spl_boot_device() == BOOT_DEVICE_UART))
  147. printf("Authentication passed: %s\n", (char *)sig_addr);
  148. return result;
  149. }
  150. u32 get_sec_mem_start(void)
  151. {
  152. u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
  153. u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
  154. /*
  155. * Total reserved region is all contiguous with protected
  156. * region coming first, followed by the non-secure region.
  157. * If 0x0 start address is given, we simply put the reserved
  158. * region at the end of the external DRAM.
  159. */
  160. if (sec_mem_start == 0)
  161. sec_mem_start =
  162. (CONFIG_SYS_SDRAM_BASE + (
  163. #if defined(CONFIG_OMAP54XX)
  164. omap_sdram_size()
  165. #else
  166. get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  167. CONFIG_MAX_RAM_BANK_SIZE)
  168. #endif
  169. - sec_mem_size));
  170. return sec_mem_start;
  171. }
  172. int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
  173. uint32_t size, uint32_t access_perm,
  174. uint32_t initiator_perm)
  175. {
  176. int result = 1;
  177. /*
  178. * Call PPA HAL API to do any other general firewall
  179. * configuration for regions 1-6 of the EMIF firewall.
  180. */
  181. debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
  182. region_num, start_addr, size);
  183. result = secure_rom_call(
  184. PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
  185. (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
  186. size, access_perm, initiator_perm);
  187. if (result != 0) {
  188. puts("Secure EMIF Firewall Setup failed!\n");
  189. debug("Return Value = %x\n", result);
  190. }
  191. return result;
  192. }
  193. #if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
  194. CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
  195. #error "TI Secure EMIF: Protected size cannot be larger than total size."
  196. #endif
  197. int secure_emif_reserve(void)
  198. {
  199. int result = 1;
  200. u32 sec_mem_start = get_sec_mem_start();
  201. u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  202. /* If there is no protected region, there is no reservation to make */
  203. if (sec_prot_size == 0)
  204. return 0;
  205. /*
  206. * Call PPA HAL API to reserve a chunk of EMIF SDRAM
  207. * for secure world use. This region should be carved out
  208. * from use by any public code. EMIF firewall region 7
  209. * will be used to protect this block of memory.
  210. */
  211. result = secure_rom_call(
  212. PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
  213. 0, 0, 2, sec_mem_start, sec_prot_size);
  214. if (result != 0) {
  215. puts("SDRAM Firewall: Secure memory reservation failed!\n");
  216. debug("Return Value = %x\n", result);
  217. }
  218. return result;
  219. }
  220. int secure_emif_firewall_lock(void)
  221. {
  222. int result = 1;
  223. /*
  224. * Call PPA HAL API to lock the EMIF firewall configurations.
  225. * After this API is called, none of the PPA HAL APIs for
  226. * configuring the EMIF firewalls will be usable again (that
  227. * is, calls to those APIs will return failure and have no
  228. * effect).
  229. */
  230. result = secure_rom_call(
  231. PPA_SERV_HAL_LOCK_EMIF_FW,
  232. 0, 0, 0);
  233. if (result != 0) {
  234. puts("Secure EMIF Firewall Lock failed!\n");
  235. debug("Return Value = %x\n", result);
  236. }
  237. return result;
  238. }
  239. static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
  240. int secure_tee_install(u32 addr)
  241. {
  242. struct optee_header *hdr;
  243. void *loadptr;
  244. u32 tee_file_size;
  245. u32 sec_mem_start = get_sec_mem_start();
  246. const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
  247. u32 ret;
  248. /* If there is no protected region, there is no place to put the TEE */
  249. if (size == 0) {
  250. printf("Error loading TEE, no protected memory region available\n");
  251. return -ENOBUFS;
  252. }
  253. hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
  254. /* 280 bytes = size of signature */
  255. tee_file_size = hdr->init_size + hdr->paged_size +
  256. sizeof(struct optee_header) + 280;
  257. if ((hdr->magic != OPTEE_MAGIC) ||
  258. (hdr->version != OPTEE_VERSION) ||
  259. (tee_file_size > size)) {
  260. printf("Error in TEE header. Check firewall and TEE sizes\n");
  261. unmap_sysmem(hdr);
  262. return CMD_RET_FAILURE;
  263. }
  264. tee_info.tee_sec_mem_start = sec_mem_start;
  265. tee_info.tee_sec_mem_size = size;
  266. tee_info.tee_jump_addr = hdr->init_load_addr_lo;
  267. tee_info.tee_cert_start = addr;
  268. tee_info.tee_cert_size = tee_file_size;
  269. tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
  270. unmap_sysmem(hdr);
  271. loadptr = map_sysmem(addr, tee_file_size);
  272. debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
  273. debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
  274. debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
  275. debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
  276. debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
  277. debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
  278. debug("tee_file_size = %d\n", tee_file_size);
  279. #if !defined(CONFIG_SYS_DCACHE_OFF)
  280. flush_dcache_range(
  281. rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
  282. roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
  283. flush_dcache_range((u32)&tee_info, (u32)&tee_info +
  284. roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
  285. #endif
  286. unmap_sysmem(loadptr);
  287. ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
  288. if (ret) {
  289. printf("TEE_LOAD_MASTER Failed\n");
  290. return ret;
  291. }
  292. printf("TEE_LOAD_MASTER Done\n");
  293. #if defined(CONFIG_OMAP54XX)
  294. if (!is_dra72x()) {
  295. u32 *smc_cpu1_params;
  296. /* Reuse the tee_info buffer for SMC params */
  297. smc_cpu1_params = (u32 *)&tee_info;
  298. smc_cpu1_params[0] = 0;
  299. #if !defined(CONFIG_SYS_DCACHE_OFF)
  300. flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
  301. roundup(sizeof(u32), ARCH_DMA_MINALIGN));
  302. #endif
  303. ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
  304. smc_cpu1_params);
  305. if (ret) {
  306. printf("TEE_LOAD_SLAVE Failed\n");
  307. return ret;
  308. }
  309. printf("TEE_LOAD_SLAVE Done\n");
  310. }
  311. #endif
  312. tee_loaded = 1;
  313. return 0;
  314. }