sec-common.c 11 KB

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