xref: /rk3399_rockchip-uboot/arch/arm/mach-omap2/sec-common.c (revision 4f65ee38131a180cc2d8bc2cfe76cca31a9d55a4)
1983e3700STom Rini /*
2983e3700STom Rini  *
3983e3700STom Rini  * Common security related functions for OMAP devices
4983e3700STom Rini  *
5983e3700STom Rini  * (C) Copyright 2016
6983e3700STom Rini  * Texas Instruments, <www.ti.com>
7983e3700STom Rini  *
8983e3700STom Rini  * Daniel Allred <d-allred@ti.com>
9983e3700STom Rini  * Andreas Dannenberg <dannenberg@ti.com>
10983e3700STom Rini  *
11983e3700STom Rini  * SPDX-License-Identifier: GPL-2.0+
12983e3700STom Rini  */
13983e3700STom Rini 
14983e3700STom Rini #include <common.h>
15983e3700STom Rini #include <stdarg.h>
16983e3700STom Rini 
17983e3700STom Rini #include <asm/arch/sys_proto.h>
18983e3700STom Rini #include <asm/omap_common.h>
19983e3700STom Rini #include <asm/omap_sec_common.h>
20983e3700STom Rini #include <asm/spl.h>
21983e3700STom Rini #include <spl.h>
22983e3700STom Rini 
23983e3700STom Rini /* Index for signature verify ROM API */
244ac19baeSAndrew F. Davis #ifdef CONFIG_AM33XX
254ac19baeSAndrew F. Davis #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX	(0x0000000C)
264ac19baeSAndrew F. Davis #else
27983e3700STom Rini #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX	(0x0000000E)
284ac19baeSAndrew F. Davis #endif
29983e3700STom Rini 
30983e3700STom Rini static uint32_t secure_rom_call_args[5] __aligned(ARCH_DMA_MINALIGN);
31983e3700STom Rini 
32983e3700STom Rini u32 secure_rom_call(u32 service, u32 proc_id, u32 flag, ...)
33983e3700STom Rini {
34983e3700STom Rini 	int i;
35983e3700STom Rini 	u32 num_args;
36983e3700STom Rini 	va_list ap;
37983e3700STom Rini 
38983e3700STom Rini 	va_start(ap, flag);
39983e3700STom Rini 
40983e3700STom Rini 	num_args = va_arg(ap, u32);
41983e3700STom Rini 
42983e3700STom Rini 	if (num_args > 4)
43983e3700STom Rini 		return 1;
44983e3700STom Rini 
45983e3700STom Rini 	/* Copy args to aligned args structure */
46983e3700STom Rini 	for (i = 0; i < num_args; i++)
47983e3700STom Rini 		secure_rom_call_args[i + 1] = va_arg(ap, u32);
48983e3700STom Rini 
49983e3700STom Rini 	secure_rom_call_args[0] = num_args;
50983e3700STom Rini 
51983e3700STom Rini 	va_end(ap);
52983e3700STom Rini 
53983e3700STom Rini 	/* if data cache is enabled, flush the aligned args structure */
54983e3700STom Rini 	flush_dcache_range(
55983e3700STom Rini 		(unsigned int)&secure_rom_call_args[0],
56983e3700STom Rini 		(unsigned int)&secure_rom_call_args[0] +
57983e3700STom Rini 		roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN));
58983e3700STom Rini 
59983e3700STom Rini 	return omap_smc_sec(service, proc_id, flag, secure_rom_call_args);
60983e3700STom Rini }
61983e3700STom Rini 
62983e3700STom Rini static u32 find_sig_start(char *image, size_t size)
63983e3700STom Rini {
64983e3700STom Rini 	char *image_end = image + size;
65983e3700STom Rini 	char *sig_start_magic = "CERT_";
66983e3700STom Rini 	int magic_str_len = strlen(sig_start_magic);
67983e3700STom Rini 	char *ch;
68983e3700STom Rini 
69983e3700STom Rini 	while (--image_end > image) {
70983e3700STom Rini 		if (*image_end == '_') {
71983e3700STom Rini 			ch = image_end - magic_str_len + 1;
72983e3700STom Rini 			if (!strncmp(ch, sig_start_magic, magic_str_len))
73983e3700STom Rini 				return (u32)ch;
74983e3700STom Rini 		}
75983e3700STom Rini 	}
76983e3700STom Rini 	return 0;
77983e3700STom Rini }
78983e3700STom Rini 
79983e3700STom Rini int secure_boot_verify_image(void **image, size_t *size)
80983e3700STom Rini {
81983e3700STom Rini 	int result = 1;
82983e3700STom Rini 	u32 cert_addr, sig_addr;
83983e3700STom Rini 	size_t cert_size;
84983e3700STom Rini 
85983e3700STom Rini 	/* Perform cache writeback on input buffer */
86983e3700STom Rini 	flush_dcache_range(
87983e3700STom Rini 		(u32)*image,
88983e3700STom Rini 		(u32)*image + roundup(*size, ARCH_DMA_MINALIGN));
89983e3700STom Rini 
90983e3700STom Rini 	cert_addr = (uint32_t)*image;
91983e3700STom Rini 	sig_addr = find_sig_start((char *)*image, *size);
92983e3700STom Rini 
93983e3700STom Rini 	if (sig_addr == 0) {
94983e3700STom Rini 		printf("No signature found in image!\n");
95983e3700STom Rini 		result = 1;
96983e3700STom Rini 		goto auth_exit;
97983e3700STom Rini 	}
98983e3700STom Rini 
99983e3700STom Rini 	*size = sig_addr - cert_addr;	/* Subtract out the signature size */
100983e3700STom Rini 	cert_size = *size;
101983e3700STom Rini 
102983e3700STom Rini 	/* Check if image load address is 32-bit aligned */
103983e3700STom Rini 	if (!IS_ALIGNED(cert_addr, 4)) {
104983e3700STom Rini 		printf("Image is not 4-byte aligned!\n");
105983e3700STom Rini 		result = 1;
106983e3700STom Rini 		goto auth_exit;
107983e3700STom Rini 	}
108983e3700STom Rini 
109983e3700STom Rini 	/* Image size also should be multiple of 4 */
110983e3700STom Rini 	if (!IS_ALIGNED(cert_size, 4)) {
111983e3700STom Rini 		printf("Image size is not 4-byte aligned!\n");
112983e3700STom Rini 		result = 1;
113983e3700STom Rini 		goto auth_exit;
114983e3700STom Rini 	}
115983e3700STom Rini 
116983e3700STom Rini 	/* Call ROM HAL API to verify certificate signature */
117983e3700STom Rini 	debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
118983e3700STom Rini 	      cert_addr, cert_size, sig_addr);
119983e3700STom Rini 
120983e3700STom Rini 	result = secure_rom_call(
121983e3700STom Rini 		API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
122983e3700STom Rini 		4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
123*4f65ee38SAndrew F. Davis 
124*4f65ee38SAndrew F. Davis 	/* Perform cache writeback on output buffer */
125*4f65ee38SAndrew F. Davis 	flush_dcache_range(
126*4f65ee38SAndrew F. Davis 		(u32)*image,
127*4f65ee38SAndrew F. Davis 		(u32)*image + roundup(*size, ARCH_DMA_MINALIGN));
128*4f65ee38SAndrew F. Davis 
129983e3700STom Rini auth_exit:
130983e3700STom Rini 	if (result != 0) {
131983e3700STom Rini 		printf("Authentication failed!\n");
132983e3700STom Rini 		printf("Return Value = %08X\n", result);
133983e3700STom Rini 		hang();
134983e3700STom Rini 	}
135983e3700STom Rini 
136983e3700STom Rini 	/*
137983e3700STom Rini 	 * Output notification of successful authentication as well the name of
138983e3700STom Rini 	 * the signing certificate used to re-assure the user that the secure
139983e3700STom Rini 	 * code is being processed as expected. However suppress any such log
140983e3700STom Rini 	 * output in case of building for SPL and booting via YMODEM. This is
141983e3700STom Rini 	 * done to avoid disturbing the YMODEM serial protocol transactions.
142983e3700STom Rini 	 */
143983e3700STom Rini 	if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
144983e3700STom Rini 	      IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
145983e3700STom Rini 	      spl_boot_device() == BOOT_DEVICE_UART))
146983e3700STom Rini 		printf("Authentication passed: %s\n", (char *)sig_addr);
147983e3700STom Rini 
148983e3700STom Rini 	return result;
149983e3700STom Rini }
150