2019-04-12 16:54:45 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* K3: Security functions
|
|
|
|
*
|
2022-07-15 16:34:33 +00:00
|
|
|
* Copyright (C) 2018-2022 Texas Instruments Incorporated - http://www.ti.com/
|
2019-04-12 16:54:45 +00:00
|
|
|
* Andrew F. Davis <afd@ti.com>
|
|
|
|
*/
|
|
|
|
|
2022-07-15 16:34:33 +00:00
|
|
|
#include <asm/io.h>
|
2019-04-12 16:54:45 +00:00
|
|
|
#include <common.h>
|
2020-01-07 23:22:29 +00:00
|
|
|
#include <cpu_func.h>
|
2019-04-12 16:54:45 +00:00
|
|
|
#include <dm.h>
|
2019-12-28 17:45:07 +00:00
|
|
|
#include <hang.h>
|
2020-05-10 17:40:01 +00:00
|
|
|
#include <image.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2019-04-12 16:54:45 +00:00
|
|
|
#include <linux/soc/ti/ti_sci_protocol.h>
|
|
|
|
#include <mach/spl.h>
|
|
|
|
#include <spl.h>
|
2022-10-07 17:12:29 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
2019-04-12 16:54:45 +00:00
|
|
|
|
2022-07-15 16:34:33 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
static bool ti_secure_cert_detected(void *p_image)
|
|
|
|
{
|
|
|
|
/* Primitive certificate detection, check for DER starting with
|
|
|
|
* two 4-Octet SEQUENCE tags
|
|
|
|
*/
|
|
|
|
return (((u8 *)p_image)[0] == 0x30 && ((u8 *)p_image)[1] == 0x82 &&
|
|
|
|
((u8 *)p_image)[4] == 0x30 && ((u8 *)p_image)[5] == 0x82);
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:34:35 +00:00
|
|
|
/* Primitive certificate length, assumes one 2-Octet sized SEQUENCE */
|
|
|
|
static size_t ti_secure_cert_length(void *p_image)
|
|
|
|
{
|
|
|
|
size_t seq_length = be16_to_cpu(readw_relaxed(p_image + 2));
|
|
|
|
/* Add 4 for the SEQUENCE tag length */
|
|
|
|
return seq_length + 4;
|
|
|
|
}
|
|
|
|
|
2021-06-11 08:45:19 +00:00
|
|
|
void ti_secure_image_post_process(void **p_image, size_t *p_size)
|
2019-04-12 16:54:45 +00:00
|
|
|
{
|
2019-09-09 07:17:38 +00:00
|
|
|
struct ti_sci_handle *ti_sci = get_ti_sci_handle();
|
|
|
|
struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
|
2022-07-15 16:34:35 +00:00
|
|
|
size_t cert_length;
|
2019-04-12 16:54:45 +00:00
|
|
|
u64 image_addr;
|
|
|
|
u32 image_size;
|
|
|
|
int ret;
|
|
|
|
|
2020-01-07 23:22:29 +00:00
|
|
|
image_size = *p_size;
|
2019-04-12 16:54:45 +00:00
|
|
|
|
2022-07-15 16:34:35 +00:00
|
|
|
if (!image_size)
|
2022-07-15 16:34:34 +00:00
|
|
|
return;
|
|
|
|
|
2022-07-15 16:34:35 +00:00
|
|
|
if (get_device_type() == K3_DEVICE_TYPE_GP) {
|
|
|
|
if (ti_secure_cert_detected(*p_image)) {
|
|
|
|
printf("Warning: Detected image signing certificate on GP device. "
|
|
|
|
"Skipping certificate to prevent boot failure. "
|
|
|
|
"This will fail if the image was also encrypted\n");
|
|
|
|
|
|
|
|
cert_length = ti_secure_cert_length(*p_image);
|
|
|
|
if (cert_length > *p_size) {
|
|
|
|
printf("Invalid signing certificate size\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p_image += cert_length;
|
|
|
|
*p_size -= cert_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:34:33 +00:00
|
|
|
if (get_device_type() != K3_DEVICE_TYPE_HS_SE &&
|
|
|
|
!ti_secure_cert_detected(*p_image)) {
|
|
|
|
printf("Warning: Did not detect image signing certificate. "
|
|
|
|
"Skipping authentication to prevent boot failure. "
|
|
|
|
"This will fail on Security Enforcing(HS-SE) devices\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-07 17:12:29 +00:00
|
|
|
/* Clean out image so it can be seen by system firmware */
|
|
|
|
image_addr = dma_map_single(*p_image, *p_size, DMA_BIDIRECTIONAL);
|
|
|
|
|
2019-04-12 16:54:45 +00:00
|
|
|
debug("Authenticating image at address 0x%016llx\n", image_addr);
|
2020-01-07 23:22:29 +00:00
|
|
|
debug("Authenticating image of size %d bytes\n", image_size);
|
|
|
|
|
2019-04-12 16:54:45 +00:00
|
|
|
/* Authenticate image */
|
|
|
|
ret = proc_ops->proc_auth_boot_image(ti_sci, &image_addr, &image_size);
|
|
|
|
if (ret) {
|
|
|
|
printf("Authentication failed!\n");
|
|
|
|
hang();
|
|
|
|
}
|
|
|
|
|
2022-10-07 17:12:29 +00:00
|
|
|
/* Invalidate any stale lines over data written by system firmware */
|
2020-01-07 23:22:29 +00:00
|
|
|
if (image_size)
|
2022-10-07 17:12:29 +00:00
|
|
|
dma_unmap_single(image_addr, image_size, DMA_BIDIRECTIONAL);
|
2020-01-07 23:22:29 +00:00
|
|
|
|
2019-04-12 16:54:45 +00:00
|
|
|
/*
|
|
|
|
* The image_size returned may be 0 when the authentication process has
|
|
|
|
* moved the image. When this happens no further processing on the
|
|
|
|
* image is needed or often even possible as it may have also been
|
|
|
|
* placed behind a firewall when moved.
|
|
|
|
*/
|
|
|
|
*p_size = image_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output notification of successful authentication to re-assure the
|
|
|
|
* user that the secure code is being processed as expected. However
|
|
|
|
* suppress any such log output in case of building for SPL and booting
|
|
|
|
* via YMODEM. This is done to avoid disturbing the YMODEM serial
|
|
|
|
* protocol transactions.
|
|
|
|
*/
|
|
|
|
if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
|
|
|
|
IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
|
|
|
|
spl_boot_device() == BOOT_DEVICE_UART))
|
|
|
|
printf("Authentication passed\n");
|
|
|
|
}
|