3 * Common security related functions for OMAP devices
6 * Texas Instruments, <www.ti.com>
8 * Daniel Allred <d-allred@ti.com>
9 * Andreas Dannenberg <dannenberg@ti.com>
11 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/arch/sys_proto.h>
18 #include <asm/omap_common.h>
19 #include <asm/omap_sec_common.h>
23 /* Index for signature verify ROM API */
25 #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000C)
27 #define API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX (0x0000000E)
30 static uint32_t secure_rom_call_args[5] __aligned(ARCH_DMA_MINALIGN);
32 u32 secure_rom_call(u32 service, u32 proc_id, u32 flag, ...)
40 num_args = va_arg(ap, u32);
45 /* Copy args to aligned args structure */
46 for (i = 0; i < num_args; i++)
47 secure_rom_call_args[i + 1] = va_arg(ap, u32);
49 secure_rom_call_args[0] = num_args;
53 /* if data cache is enabled, flush the aligned args structure */
55 (unsigned int)&secure_rom_call_args[0],
56 (unsigned int)&secure_rom_call_args[0] +
57 roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN));
59 return omap_smc_sec(service, proc_id, flag, secure_rom_call_args);
62 static u32 find_sig_start(char *image, size_t size)
64 char *image_end = image + size;
65 char *sig_start_magic = "CERT_";
66 int magic_str_len = strlen(sig_start_magic);
69 while (--image_end > image) {
70 if (*image_end == '_') {
71 ch = image_end - magic_str_len + 1;
72 if (!strncmp(ch, sig_start_magic, magic_str_len))
79 int secure_boot_verify_image(void **image, size_t *size)
82 u32 cert_addr, sig_addr;
85 /* Perform cache writeback on input buffer */
88 (u32)*image + roundup(*size, ARCH_DMA_MINALIGN));
90 cert_addr = (uint32_t)*image;
91 sig_addr = find_sig_start((char *)*image, *size);
94 printf("No signature found in image!\n");
99 *size = sig_addr - cert_addr; /* Subtract out the signature size */
102 /* Check if image load address is 32-bit aligned */
103 if (!IS_ALIGNED(cert_addr, 4)) {
104 printf("Image is not 4-byte aligned!\n");
109 /* Image size also should be multiple of 4 */
110 if (!IS_ALIGNED(cert_size, 4)) {
111 printf("Image size is not 4-byte aligned!\n");
116 /* Call ROM HAL API to verify certificate signature */
117 debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__,
118 cert_addr, cert_size, sig_addr);
120 result = secure_rom_call(
121 API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0,
122 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF);
124 /* Perform cache writeback on output buffer */
127 (u32)*image + roundup(*size, ARCH_DMA_MINALIGN));
131 printf("Authentication failed!\n");
132 printf("Return Value = %08X\n", result);
137 * Output notification of successful authentication as well the name of
138 * the signing certificate used to re-assure the user that the secure
139 * code is being processed as expected. However suppress any such log
140 * output in case of building for SPL and booting via YMODEM. This is
141 * done to avoid disturbing the YMODEM serial protocol transactions.
143 if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
144 IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
145 spl_boot_device() == BOOT_DEVICE_UART))
146 printf("Authentication passed: %s\n", (char *)sig_addr);