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 42*1ecd2a2fSxypron.glpk@gmx.de if (num_args > 4) { 43*1ecd2a2fSxypron.glpk@gmx.de va_end(ap); 44983e3700STom Rini return 1; 45*1ecd2a2fSxypron.glpk@gmx.de } 46983e3700STom Rini 47983e3700STom Rini /* Copy args to aligned args structure */ 48983e3700STom Rini for (i = 0; i < num_args; i++) 49983e3700STom Rini secure_rom_call_args[i + 1] = va_arg(ap, u32); 50983e3700STom Rini 51983e3700STom Rini secure_rom_call_args[0] = num_args; 52983e3700STom Rini 53983e3700STom Rini va_end(ap); 54983e3700STom Rini 55983e3700STom Rini /* if data cache is enabled, flush the aligned args structure */ 56983e3700STom Rini flush_dcache_range( 57983e3700STom Rini (unsigned int)&secure_rom_call_args[0], 58983e3700STom Rini (unsigned int)&secure_rom_call_args[0] + 59983e3700STom Rini roundup(sizeof(secure_rom_call_args), ARCH_DMA_MINALIGN)); 60983e3700STom Rini 61983e3700STom Rini return omap_smc_sec(service, proc_id, flag, secure_rom_call_args); 62983e3700STom Rini } 63983e3700STom Rini 64983e3700STom Rini static u32 find_sig_start(char *image, size_t size) 65983e3700STom Rini { 66983e3700STom Rini char *image_end = image + size; 67983e3700STom Rini char *sig_start_magic = "CERT_"; 68983e3700STom Rini int magic_str_len = strlen(sig_start_magic); 69983e3700STom Rini char *ch; 70983e3700STom Rini 71983e3700STom Rini while (--image_end > image) { 72983e3700STom Rini if (*image_end == '_') { 73983e3700STom Rini ch = image_end - magic_str_len + 1; 74983e3700STom Rini if (!strncmp(ch, sig_start_magic, magic_str_len)) 75983e3700STom Rini return (u32)ch; 76983e3700STom Rini } 77983e3700STom Rini } 78983e3700STom Rini return 0; 79983e3700STom Rini } 80983e3700STom Rini 81983e3700STom Rini int secure_boot_verify_image(void **image, size_t *size) 82983e3700STom Rini { 83983e3700STom Rini int result = 1; 84983e3700STom Rini u32 cert_addr, sig_addr; 85983e3700STom Rini size_t cert_size; 86983e3700STom Rini 87983e3700STom Rini /* Perform cache writeback on input buffer */ 88983e3700STom Rini flush_dcache_range( 89983e3700STom Rini (u32)*image, 90983e3700STom Rini (u32)*image + roundup(*size, ARCH_DMA_MINALIGN)); 91983e3700STom Rini 92983e3700STom Rini cert_addr = (uint32_t)*image; 93983e3700STom Rini sig_addr = find_sig_start((char *)*image, *size); 94983e3700STom Rini 95983e3700STom Rini if (sig_addr == 0) { 96983e3700STom Rini printf("No signature found in image!\n"); 97983e3700STom Rini result = 1; 98983e3700STom Rini goto auth_exit; 99983e3700STom Rini } 100983e3700STom Rini 101983e3700STom Rini *size = sig_addr - cert_addr; /* Subtract out the signature size */ 102983e3700STom Rini cert_size = *size; 103983e3700STom Rini 104983e3700STom Rini /* Check if image load address is 32-bit aligned */ 105983e3700STom Rini if (!IS_ALIGNED(cert_addr, 4)) { 106983e3700STom Rini printf("Image is not 4-byte aligned!\n"); 107983e3700STom Rini result = 1; 108983e3700STom Rini goto auth_exit; 109983e3700STom Rini } 110983e3700STom Rini 111983e3700STom Rini /* Image size also should be multiple of 4 */ 112983e3700STom Rini if (!IS_ALIGNED(cert_size, 4)) { 113983e3700STom Rini printf("Image size is not 4-byte aligned!\n"); 114983e3700STom Rini result = 1; 115983e3700STom Rini goto auth_exit; 116983e3700STom Rini } 117983e3700STom Rini 118983e3700STom Rini /* Call ROM HAL API to verify certificate signature */ 119983e3700STom Rini debug("%s: load_addr = %x, size = %x, sig_addr = %x\n", __func__, 120983e3700STom Rini cert_addr, cert_size, sig_addr); 121983e3700STom Rini 122983e3700STom Rini result = secure_rom_call( 123983e3700STom Rini API_HAL_KM_VERIFYCERTIFICATESIGNATURE_INDEX, 0, 0, 124983e3700STom Rini 4, cert_addr, cert_size, sig_addr, 0xFFFFFFFF); 1254f65ee38SAndrew F. Davis 1264f65ee38SAndrew F. Davis /* Perform cache writeback on output buffer */ 1274f65ee38SAndrew F. Davis flush_dcache_range( 1284f65ee38SAndrew F. Davis (u32)*image, 1294f65ee38SAndrew F. Davis (u32)*image + roundup(*size, ARCH_DMA_MINALIGN)); 1304f65ee38SAndrew F. Davis 131983e3700STom Rini auth_exit: 132983e3700STom Rini if (result != 0) { 133983e3700STom Rini printf("Authentication failed!\n"); 134983e3700STom Rini printf("Return Value = %08X\n", result); 135983e3700STom Rini hang(); 136983e3700STom Rini } 137983e3700STom Rini 138983e3700STom Rini /* 139983e3700STom Rini * Output notification of successful authentication as well the name of 140983e3700STom Rini * the signing certificate used to re-assure the user that the secure 141983e3700STom Rini * code is being processed as expected. However suppress any such log 142983e3700STom Rini * output in case of building for SPL and booting via YMODEM. This is 143983e3700STom Rini * done to avoid disturbing the YMODEM serial protocol transactions. 144983e3700STom Rini */ 145983e3700STom Rini if (!(IS_ENABLED(CONFIG_SPL_BUILD) && 146983e3700STom Rini IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) && 147983e3700STom Rini spl_boot_device() == BOOT_DEVICE_UART)) 148983e3700STom Rini printf("Authentication passed: %s\n", (char *)sig_addr); 149983e3700STom Rini 150983e3700STom Rini return result; 151983e3700STom Rini } 152