sec-common.c 11 KB

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