sec-common.c 11 KB

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