blob: f3df8affa3c2cd03156070735312958821590820 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -05002/*
3 *
4 * Common security related functions for OMAP devices
5 *
Andrew F. Davis79081522017-07-10 14:45:49 -05006 * (C) Copyright 2016-2017
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -05007 * Texas Instruments, <www.ti.com>
8 *
9 * Daniel Allred <d-allred@ti.com>
10 * Andreas Dannenberg <dannenberg@ti.com>
Andrew F. Davis79081522017-07-10 14:45:49 -050011 * Harinarayan Bhatta <harinarayan@ti.com>
12 * Andrew F. Davis <afd@ti.com>
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050013 */
14
15#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070017#include <init.h>
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050018#include <stdarg.h>
19
20#include <asm/arch/sys_proto.h>
Andrew F. Davis79081522017-07-10 14:45:49 -050021#include <asm/cache.h>
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050022#include <asm/omap_common.h>
23#include <asm/omap_sec_common.h>
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -050024#include <asm/spl.h>
Andrew F. Davis79081522017-07-10 14:45:49 -050025#include <asm/ti-common/sys_proto.h>
26#include <mapmem.h>
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -050027#include <spl.h>
Andrew F. Davis79081522017-07-10 14:45:49 -050028#include <tee/optee.h>
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -050029
30/* Index for signature verify ROM API */
Andrew F. Davis3d1e5992017-01-11 10:19:52 -060031#ifdef CONFIG_AM33XX
32#define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000C)
33#else
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -050034#define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000E)
Andrew F. Davis3d1e5992017-01-11 10:19:52 -060035#endif
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050036
Andrew F. Davis79081522017-07-10 14:45:49 -050037/* Index for signature PPA-based TI HAL APIs */
38#define PPA_HAL_SERVICES_START_INDEX (0x200)
39#define PPA_SERV_HAL_TEE_LOAD_MASTER (PPA_HAL_SERVICES_START_INDEX + 23)
40#define PPA_SERV_HAL_TEE_LOAD_SLAVE (PPA_HAL_SERVICES_START_INDEX + 24)
41#define PPA_SERV_HAL_SETUP_SEC_RESVD_REGION (PPA_HAL_SERVICES_START_INDEX + 25)
42#define PPA_SERV_HAL_SETUP_EMIF_FW_REGION (PPA_HAL_SERVICES_START_INDEX + 26)
43#define PPA_SERV_HAL_LOCK_EMIF_FW (PPA_HAL_SERVICES_START_INDEX + 27)
44
Madan Srinivas396a2f92017-09-20 14:37:36 -050045/* Offset of header size if image is signed as ISW */
46#define HEADER_SIZE_OFFSET (0x6D)
47
Andrew F. Davis79081522017-07-10 14:45:49 -050048int tee_loaded = 0;
49
50/* Argument for PPA_SERV_HAL_TEE_LOAD_MASTER */
51struct ppa_tee_load_info {
52 u32 tee_sec_mem_start; /* Physical start address reserved for TEE */
53 u32 tee_sec_mem_size; /* Size of the memory reserved for TEE */
54 u32 tee_cert_start; /* Address where signed TEE binary is loaded */
55 u32 tee_cert_size; /* Size of TEE certificate (signed binary) */
56 u32 tee_jump_addr; /* Address to jump to start TEE execution */
57 u32 tee_arg0; /* argument to TEE jump function, in r0 */
58};
59
Andrew F. Davisa46bb752018-01-16 14:25:48 -060060static uint32_t secure_rom_call_args[5] __aligned(ARCH_DMA_MINALIGN) __section(".data");
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050061
62u32 secure_rom_call(u32 service, u32 proc_id, u32 flag, ...)
63{
64 int i;
65 u32 num_args;
66 va_list ap;
67
68 va_start(ap, flag);
69
70 num_args = va_arg(ap, u32);
71
xypron.glpk@gmx.de3f1f8d52017-04-15 12:29:20 +020072 if (num_args > 4) {
73 va_end(ap);
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050074 return 1;
xypron.glpk@gmx.de3f1f8d52017-04-15 12:29:20 +020075 }
Andreas Dannenberg6469e1a2016-06-27 09:19:18 -050076
77 /* Copy args to aligned args structure */
78 for (i = 0; i < num_args; i++)
79 secure_rom_call_args[i + 1] = va_arg(ap, u32);
80
81 secure_rom_call_args[0] = num_args;
82
83 va_end(ap);
84
85 /* if data cache is enabled, flush the aligned args structure */
86 flush_dcache_range(
87 (unsigned int)&secure_rom_call_args[0],
88 (unsigned int)&secure_rom_call_args[0] +
89 roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN));
90
91 return omap_smc_sec(service, proc_id, flag, secure_rom_call_args);
92}
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -050093
94static u32 find_sig_start(char *image, size_t size)
95{
96 char *image_end = image + size;
97 char *sig_start_magic = "CERT_";
98 int magic_str_len = strlen(sig_start_magic);
99 char *ch;
100
101 while (--image_end > image) {
102 if (*image_end == '_') {
103 ch = image_end - magic_str_len + 1;
104 if (!strncmp(ch, sig_start_magic, magic_str_len))
105 return (u32)ch;
106 }
107 }
108 return 0;
109}
110
111int secure_boot_verify_image(void **image, size_t *size)
112{
113 int result = 1;
114 u32 cert_addr, sig_addr;
115 size_t cert_size;
116
117 /* Perform cache writeback on input buffer */
118 flush_dcache_range(
Andrew F. Davis6c11ebf2017-07-26 14:53:19 -0500119 rounddown((u32)*image, ARCH_DMA_MINALIGN),
120 roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -0500121
122 cert_addr = (uint32_t)*image;
123 sig_addr = find_sig_start((char *)*image, *size);
124
125 if (sig_addr == 0) {
126 printf("No signature found in image!\n");
127 result = 1;
128 goto auth_exit;
129 }
130
131 *size = sig_addr - cert_addr; /* Subtract out the signature size */
Madan Srinivas396a2f92017-09-20 14:37:36 -0500132 /* Subtract header if present */
133 if (strncmp((char *)sig_addr, "CERT_ISW_", 9) == 0)
Madan Srinivasda9e12b2018-01-09 14:32:41 -0600134 *size -= ((u32 *)*image)[HEADER_SIZE_OFFSET];
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -0500135 cert_size = *size;
136
137 /* Check if image load address is 32-bit aligned */
138 if (!IS_ALIGNED(cert_addr, 4)) {
139 printf("Image is not 4-byte aligned!\n");
140 result = 1;
141 goto auth_exit;
142 }
143
144 /* Image size also should be multiple of 4 */
145 if (!IS_ALIGNED(cert_size, 4)) {
146 printf("Image size is not 4-byte aligned!\n");
147 result = 1;
148 goto auth_exit;
149 }
150
151 /* Call ROM HAL API to verify certificate signature */
152 debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
153 cert_addr, cert_size, sig_addr);
154
155 result = secure_rom_call(
156 API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
157 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
Andrew F. Daviscdabe1d2017-02-22 17:46:39 -0600158
159 /* Perform cache writeback on output buffer */
160 flush_dcache_range(
Andrew F. Davis6c11ebf2017-07-26 14:53:19 -0500161 rounddown((u32)*image, ARCH_DMA_MINALIGN),
162 roundup((u32)*image + *size, ARCH_DMA_MINALIGN));
Andrew F. Daviscdabe1d2017-02-22 17:46:39 -0600163
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -0500164auth_exit:
165 if (result != 0) {
166 printf("Authentication failed!\n");
167 printf("Return Value = %08X\n", result);
168 hang();
169 }
170
171 /*
Andrew F. Davis1956f072018-01-09 14:33:54 -0600172 * Output notification of successful authentication to re-assure the
173 * user that the secure code is being processed as expected. However
174 * suppress any such log output in case of building for SPL and booting
175 * via YMODEM. This is done to avoid disturbing the YMODEM serial
176 * protocol transactions.
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -0500177 */
178 if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
179 IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
180 spl_boot_device() == BOOT_DEVICE_UART))
Andrew F. Davis1956f072018-01-09 14:33:54 -0600181 printf("Authentication passed\n");
Andreas Dannenberg1549e2f2016-06-27 09:19:19 -0500182
183 return result;
184}
Andrew F. Davis79081522017-07-10 14:45:49 -0500185
Andrew F. Davisbb80d4d2017-07-10 14:45:50 -0500186u32 get_sec_mem_start(void)
Andrew F. Davis79081522017-07-10 14:45:49 -0500187{
188 u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
189 u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
190 /*
191 * Total reserved region is all contiguous with protected
192 * region coming first, followed by the non-secure region.
193 * If 0x0 start address is given, we simply put the reserved
194 * region at the end of the external DRAM.
195 */
196 if (sec_mem_start == 0)
197 sec_mem_start =
198 (CONFIG_SYS_SDRAM_BASE + (
199#if defined(CONFIG_OMAP54XX)
200 omap_sdram_size()
201#else
202 get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
203 CONFIG_MAX_RAM_BANK_SIZE)
204#endif
205 - sec_mem_size));
206 return sec_mem_start;
207}
208
209int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
210 uint32_t size, uint32_t access_perm,
211 uint32_t initiator_perm)
212{
213 int result = 1;
214
215 /*
216 * Call PPA HAL API to do any other general firewall
217 * configuration for regions 1-6 of the EMIF firewall.
218 */
219 debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
220 region_num, start_addr, size);
221
222 result = secure_rom_call(
223 PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
224 (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
225 size, access_perm, initiator_perm);
226
227 if (result != 0) {
228 puts("Secure EMIF Firewall Setup failed!\n");
229 debug("Return Value = %x\n", result);
230 }
231
232 return result;
233}
234
235#if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
236 CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
237#error "TI Secure EMIF: Protected size cannot be larger than total size."
238#endif
239int secure_emif_reserve(void)
240{
241 int result = 1;
242 u32 sec_mem_start = get_sec_mem_start();
243 u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
244
245 /* If there is no protected region, there is no reservation to make */
246 if (sec_prot_size == 0)
247 return 0;
248
249 /*
250 * Call PPA HAL API to reserve a chunk of EMIF SDRAM
251 * for secure world use. This region should be carved out
252 * from use by any public code. EMIF firewall region 7
253 * will be used to protect this block of memory.
254 */
255 result = secure_rom_call(
256 PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
257 0, 0, 2, sec_mem_start, sec_prot_size);
258
259 if (result != 0) {
260 puts("SDRAM Firewall: Secure memory reservation failed!\n");
261 debug("Return Value = %x\n", result);
262 }
263
264 return result;
265}
266
267int secure_emif_firewall_lock(void)
268{
269 int result = 1;
270
271 /*
272 * Call PPA HAL API to lock the EMIF firewall configurations.
273 * After this API is called, none of the PPA HAL APIs for
274 * configuring the EMIF firewalls will be usable again (that
275 * is, calls to those APIs will return failure and have no
276 * effect).
277 */
278
279 result = secure_rom_call(
280 PPA_SERV_HAL_LOCK_EMIF_FW,
281 0, 0, 0);
282
283 if (result != 0) {
284 puts("Secure EMIF Firewall Lock failed!\n");
285 debug("Return Value = %x\n", result);
286 }
287
288 return result;
289}
290
291static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
292
293int secure_tee_install(u32 addr)
294{
295 struct optee_header *hdr;
296 void *loadptr;
297 u32 tee_file_size;
298 u32 sec_mem_start = get_sec_mem_start();
299 const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
300 u32 ret;
301
302 /* If there is no protected region, there is no place to put the TEE */
303 if (size == 0) {
304 printf("Error loading TEE, no protected memory region available\n");
305 return -ENOBUFS;
306 }
307
308 hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
309 /* 280 bytes = size of signature */
310 tee_file_size = hdr->init_size + hdr->paged_size +
311 sizeof(struct optee_header) + 280;
312
313 if ((hdr->magic != OPTEE_MAGIC) ||
314 (hdr->version != OPTEE_VERSION) ||
Harinarayan Bhattae66501a2017-09-13 13:27:44 -0500315 (tee_file_size > size)) {
316 printf("Error in TEE header. Check firewall and TEE sizes\n");
Andrew F. Davis79081522017-07-10 14:45:49 -0500317 unmap_sysmem(hdr);
318 return CMD_RET_FAILURE;
319 }
320
321 tee_info.tee_sec_mem_start = sec_mem_start;
322 tee_info.tee_sec_mem_size = size;
323 tee_info.tee_jump_addr = hdr->init_load_addr_lo;
324 tee_info.tee_cert_start = addr;
325 tee_info.tee_cert_size = tee_file_size;
326 tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
327 unmap_sysmem(hdr);
328 loadptr = map_sysmem(addr, tee_file_size);
329
330 debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
331 debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
332 debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
333 debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
334 debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
335 debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
336 debug("tee_file_size = %d\n", tee_file_size);
337
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400338#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Andrew F. Davis79081522017-07-10 14:45:49 -0500339 flush_dcache_range(
340 rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
341 roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
342
343 flush_dcache_range((u32)&tee_info, (u32)&tee_info +
344 roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
345#endif
346 unmap_sysmem(loadptr);
347
348 ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
349 if (ret) {
350 printf("TEE_LOAD_MASTER Failed\n");
351 return ret;
352 }
353 printf("TEE_LOAD_MASTER Done\n");
354
355#if defined(CONFIG_OMAP54XX)
356 if (!is_dra72x()) {
357 u32 *smc_cpu1_params;
358 /* Reuse the tee_info buffer for SMC params */
359 smc_cpu1_params = (u32 *)&tee_info;
360 smc_cpu1_params[0] = 0;
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400361#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Andrew F. Davis79081522017-07-10 14:45:49 -0500362 flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
363 roundup(sizeof(u32), ARCH_DMA_MINALIGN));
364#endif
365 ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
366 smc_cpu1_params);
367 if (ret) {
368 printf("TEE_LOAD_SLAVE Failed\n");
369 return ret;
370 }
371 printf("TEE_LOAD_SLAVE Done\n");
372 }
373#endif
374
375 tee_loaded = 1;
376
377 return 0;
378}