blob: 22697a263a85b7a53638c1e8b1cd8a5015d58f28 [file] [log] [blame]
Andrew F. Davis2ed41072019-04-12 12:54:45 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * K3: Security functions
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2018-2022 Texas Instruments Incorporated - https://www.ti.com/
Andrew F. Davis2ed41072019-04-12 12:54:45 -04006 * Andrew F. Davis <afd@ti.com>
7 */
8
Andrew Davise74e3bb2022-07-15 11:34:33 -05009#include <asm/io.h>
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050010#include <cpu_func.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040011#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060013#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040016#include <linux/soc/ti/ti_sci_protocol.h>
17#include <mach/spl.h>
18#include <spl.h>
Andrew Davisd95fcd82022-10-07 12:12:29 -050019#include <linux/dma-mapping.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040020
Andrew Davise74e3bb2022-07-15 11:34:33 -050021#include "common.h"
22
23static bool ti_secure_cert_detected(void *p_image)
24{
25 /* Primitive certificate detection, check for DER starting with
26 * two 4-Octet SEQUENCE tags
27 */
28 return (((u8 *)p_image)[0] == 0x30 && ((u8 *)p_image)[1] == 0x82 &&
29 ((u8 *)p_image)[4] == 0x30 && ((u8 *)p_image)[5] == 0x82);
30}
31
Andrew Davis0ce85432022-07-15 11:34:35 -050032/* Primitive certificate length, assumes one 2-Octet sized SEQUENCE */
33static size_t ti_secure_cert_length(void *p_image)
34{
35 size_t seq_length = be16_to_cpu(readw_relaxed(p_image + 2));
36 /* Add 4 for the SEQUENCE tag length */
37 return seq_length + 4;
38}
39
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053040void ti_secure_image_check_binary(void **p_image, size_t *p_size)
Andrew F. Davis2ed41072019-04-12 12:54:45 -040041{
Andrew F. Davis2ed41072019-04-12 12:54:45 -040042 u32 image_size;
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053043 size_t cert_length;
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050044 image_size = *p_size;
Andrew F. Davis2ed41072019-04-12 12:54:45 -040045
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053046 if (!image_size) {
47 debug("%s: Image size is %d\n", __func__, image_size);
Andrew Davis0ce85432022-07-15 11:34:35 -050048 return;
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053049 }
Andrew Davis0ce85432022-07-15 11:34:35 -050050
51 if (get_device_type() == K3_DEVICE_TYPE_GP) {
52 if (ti_secure_cert_detected(*p_image)) {
53 printf("Warning: Detected image signing certificate on GP device. "
54 "Skipping certificate to prevent boot failure. "
55 "This will fail if the image was also encrypted\n");
56
57 cert_length = ti_secure_cert_length(*p_image);
58 if (cert_length > *p_size) {
59 printf("Invalid signing certificate size\n");
60 return;
61 }
62
63 *p_image += cert_length;
64 *p_size -= cert_length;
65 }
66
Andrew Davis2045cf12022-07-15 11:34:34 -050067 return;
Andrew Davis0ce85432022-07-15 11:34:35 -050068 }
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053069}
70
71void ti_secure_image_post_process(void **p_image, size_t *p_size)
72{
73 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
74 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
75 u64 image_addr;
76 u32 image_size;
77 int ret;
78
79 image_size = *p_size;
80 if (!image_size) {
81 debug("%s: Image size is %d\n", __func__, image_size);
82 return;
83 }
Andrew Davise74e3bb2022-07-15 11:34:33 -050084
Manorit Chawdhry3615e742023-07-25 13:09:22 +053085 if (get_device_type() == K3_DEVICE_TYPE_GP)
Manorit Chawdhrydb01bcc2023-05-18 12:44:17 +053086 return;
87
Manorit Chawdhry3615e742023-07-25 13:09:22 +053088 if (get_device_type() != K3_DEVICE_TYPE_HS_SE &&
89 !ti_secure_cert_detected(*p_image)) {
90 printf("Warning: Did not detect image signing certificate. "
91 "Skipping authentication to prevent boot failure. "
92 "This will fail on Security Enforcing(HS-SE) devices\n");
93 return;
94 }
95
Andrew Davisd95fcd82022-10-07 12:12:29 -050096 /* Clean out image so it can be seen by system firmware */
97 image_addr = dma_map_single(*p_image, *p_size, DMA_BIDIRECTIONAL);
98
Andrew F. Davis2ed41072019-04-12 12:54:45 -040099 debug("Authenticating image at address 0x%016llx\n", image_addr);
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -0500100 debug("Authenticating image of size %d bytes\n", image_size);
101
Andrew F. Davis2ed41072019-04-12 12:54:45 -0400102 /* Authenticate image */
103 ret = proc_ops->proc_auth_boot_image(ti_sci, &image_addr, &image_size);
104 if (ret) {
105 printf("Authentication failed!\n");
106 hang();
107 }
108
Andrew Davisd95fcd82022-10-07 12:12:29 -0500109 /* Invalidate any stale lines over data written by system firmware */
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -0500110 if (image_size)
Andrew Davisd95fcd82022-10-07 12:12:29 -0500111 dma_unmap_single(image_addr, image_size, DMA_BIDIRECTIONAL);
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -0500112
Andrew F. Davis2ed41072019-04-12 12:54:45 -0400113 /*
114 * The image_size returned may be 0 when the authentication process has
115 * moved the image. When this happens no further processing on the
116 * image is needed or often even possible as it may have also been
117 * placed behind a firewall when moved.
118 */
119 *p_size = image_size;
120
121 /*
122 * Output notification of successful authentication to re-assure the
123 * user that the secure code is being processed as expected. However
124 * suppress any such log output in case of building for SPL and booting
125 * via YMODEM. This is done to avoid disturbing the YMODEM serial
126 * protocol transactions.
127 */
128 if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
129 IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
130 spl_boot_device() == BOOT_DEVICE_UART))
131 printf("Authentication passed\n");
132}