sec-common.c 10 KB

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