1 // SPDX-License-Identifier: GPL-2.0+
4 * Common security related functions for OMAP devices
6 * (C) Copyright 2016-2017
7 * Texas Instruments, <www.ti.com>
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>
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>
23 #include <asm/ti-common/sys_proto.h>
26 #include <tee/optee.h>
28 /* Index for signature verify ROM API */
30 #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000C)
32 #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000E)
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)
43 /* Offset of header size if image is signed as ISW */
44 #define HEADER_SIZE_OFFSET (0x6D)
48 /* Argument for PPA_SERV_HAL_TEE_LOAD_MASTER */
49 struct ppa_tee_load_info {
50 u32 tee_sec_mem_start; /* Physical start address reserved for TEE */
51 u32 tee_sec_mem_size; /* Size of the memory reserved for TEE */
52 u32 tee_cert_start; /* Address where signed TEE binary is loaded */
53 u32 tee_cert_size; /* Size of TEE certificate (signed binary) */
54 u32 tee_jump_addr; /* Address to jump to start TEE execution */
55 u32 tee_arg0; /* argument to TEE jump function, in r0 */
58 static uint32_t secure_rom_call_args[5] __aligned(ARCH_DMA_MINALIGN) __section(".data");
60 u32 secure_rom_call(u32 service, u32 proc_id, u32 flag, ...)
68 num_args = va_arg(ap, u32);
75 /* Copy args to aligned args structure */
76 for (i = 0; i < num_args; i++)
77 secure_rom_call_args[i + 1] = va_arg(ap, u32);
79 secure_rom_call_args[0] = num_args;
83 /* if data cache is enabled, flush the aligned args structure */
85 (unsigned int)&secure_rom_call_args[0],
86 (unsigned int)&secure_rom_call_args[0] +
87 roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN));
89 return omap_smc_sec(service, proc_id, flag, secure_rom_call_args);
92 static u32 find_sig_start(char *image, size_t size)
94 char *image_end = image + size;
95 char *sig_start_magic = "CERT_";
96 int magic_str_len = strlen(sig_start_magic);
99 while (--image_end > image) {
100 if (*image_end == '_') {
101 ch = image_end - magic_str_len + 1;
102 if (!strncmp(ch, sig_start_magic, magic_str_len))
109 int secure_boot_verify_image(void **image, size_t *size)
112 u32 cert_addr, sig_addr;
115 /* Perform cache writeback on input buffer */
117 rounddown((u32)*image, ARCH_DMA_MINALIGN),
118 roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
120 cert_addr = (uint32_t)*image;
121 sig_addr = find_sig_start((char *)*image, *size);
124 printf("No signature found in image!\n");
129 *size = sig_addr - cert_addr; /* Subtract out the signature size */
130 /* Subtract header if present */
131 if (strncmp((char *)sig_addr, "CERT_ISW_", 9) == 0)
132 *size -= ((u32 *)*image)[HEADER_SIZE_OFFSET];
135 /* Check if image load address is 32-bit aligned */
136 if (!IS_ALIGNED(cert_addr, 4)) {
137 printf("Image is not 4-byte aligned!\n");
142 /* Image size also should be multiple of 4 */
143 if (!IS_ALIGNED(cert_size, 4)) {
144 printf("Image size is not 4-byte aligned!\n");
149 /* Call ROM HAL API to verify certificate signature */
150 debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
151 cert_addr, cert_size, sig_addr);
153 result = secure_rom_call(
154 API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
155 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
157 /* Perform cache writeback on output buffer */
159 rounddown((u32)*image, ARCH_DMA_MINALIGN),
160 roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
164 printf("Authentication failed!\n");
165 printf("Return Value = %08X\n", result);
170 * Output notification of successful authentication to re-assure the
171 * user that the secure code is being processed as expected. However
172 * suppress any such log output in case of building for SPL and booting
173 * via YMODEM. This is done to avoid disturbing the YMODEM serial
174 * protocol transactions.
176 if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
177 IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
178 spl_boot_device() == BOOT_DEVICE_UART))
179 printf("Authentication passed\n");
184 u32 get_sec_mem_start(void)
186 u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
187 u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
189 * Total reserved region is all contiguous with protected
190 * region coming first, followed by the non-secure region.
191 * If 0x0 start address is given, we simply put the reserved
192 * region at the end of the external DRAM.
194 if (sec_mem_start == 0)
196 (CONFIG_SYS_SDRAM_BASE + (
197 #if defined(CONFIG_OMAP54XX)
200 get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
201 CONFIG_MAX_RAM_BANK_SIZE)
204 return sec_mem_start;
207 int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
208 uint32_t size, uint32_t access_perm,
209 uint32_t initiator_perm)
214 * Call PPA HAL API to do any other general firewall
215 * configuration for regions 1-6 of the EMIF firewall.
217 debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
218 region_num, start_addr, size);
220 result = secure_rom_call(
221 PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
222 (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
223 size, access_perm, initiator_perm);
226 puts("Secure EMIF Firewall Setup failed!\n");
227 debug("Return Value = %x\n", result);
233 #if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
234 CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
235 #error "TI Secure EMIF: Protected size cannot be larger than total size."
237 int secure_emif_reserve(void)
240 u32 sec_mem_start = get_sec_mem_start();
241 u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
243 /* If there is no protected region, there is no reservation to make */
244 if (sec_prot_size == 0)
248 * Call PPA HAL API to reserve a chunk of EMIF SDRAM
249 * for secure world use. This region should be carved out
250 * from use by any public code. EMIF firewall region 7
251 * will be used to protect this block of memory.
253 result = secure_rom_call(
254 PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
255 0, 0, 2, sec_mem_start, sec_prot_size);
258 puts("SDRAM Firewall: Secure memory reservation failed!\n");
259 debug("Return Value = %x\n", result);
265 int secure_emif_firewall_lock(void)
270 * Call PPA HAL API to lock the EMIF firewall configurations.
271 * After this API is called, none of the PPA HAL APIs for
272 * configuring the EMIF firewalls will be usable again (that
273 * is, calls to those APIs will return failure and have no
277 result = secure_rom_call(
278 PPA_SERV_HAL_LOCK_EMIF_FW,
282 puts("Secure EMIF Firewall Lock failed!\n");
283 debug("Return Value = %x\n", result);
289 static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
291 int secure_tee_install(u32 addr)
293 struct optee_header *hdr;
296 u32 sec_mem_start = get_sec_mem_start();
297 const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
300 /* If there is no protected region, there is no place to put the TEE */
302 printf("Error loading TEE, no protected memory region available\n");
306 hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
307 /* 280 bytes = size of signature */
308 tee_file_size = hdr->init_size + hdr->paged_size +
309 sizeof(struct optee_header) + 280;
311 if ((hdr->magic != OPTEE_MAGIC) ||
312 (hdr->version != OPTEE_VERSION) ||
313 (tee_file_size > size)) {
314 printf("Error in TEE header. Check firewall and TEE sizes\n");
316 return CMD_RET_FAILURE;
319 tee_info.tee_sec_mem_start = sec_mem_start;
320 tee_info.tee_sec_mem_size = size;
321 tee_info.tee_jump_addr = hdr->init_load_addr_lo;
322 tee_info.tee_cert_start = addr;
323 tee_info.tee_cert_size = tee_file_size;
324 tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
326 loadptr = map_sysmem(addr, tee_file_size);
328 debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
329 debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
330 debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
331 debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
332 debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
333 debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
334 debug("tee_file_size = %d\n", tee_file_size);
336 #if !defined(CONFIG_SYS_DCACHE_OFF)
338 rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
339 roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
341 flush_dcache_range((u32)&tee_info, (u32)&tee_info +
342 roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
344 unmap_sysmem(loadptr);
346 ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
348 printf("TEE_LOAD_MASTER Failed\n");
351 printf("TEE_LOAD_MASTER Done\n");
353 #if defined(CONFIG_OMAP54XX)
355 u32 *smc_cpu1_params;
356 /* Reuse the tee_info buffer for SMC params */
357 smc_cpu1_params = (u32 *)&tee_info;
358 smc_cpu1_params[0] = 0;
359 #if !defined(CONFIG_SYS_DCACHE_OFF)
360 flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
361 roundup(sizeof(u32), ARCH_DMA_MINALIGN));
363 ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
366 printf("TEE_LOAD_SLAVE Failed\n");
369 printf("TEE_LOAD_SLAVE Done\n");