blob: 6179f7373aa7991759058a6b5bad3290cc4123f9 [file] [log] [blame]
Andrew F. Davis2ed41072019-04-12 12:54:45 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * K3: Security functions
4 *
Andrew Davise74e3bb2022-07-15 11:34:33 -05005 * Copyright (C) 2018-2022 Texas Instruments Incorporated - http://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. Davis2ed41072019-04-12 12:54:45 -040010#include <common.h>
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050011#include <cpu_func.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040012#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070013#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060014#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040017#include <linux/soc/ti/ti_sci_protocol.h>
18#include <mach/spl.h>
19#include <spl.h>
Andrew Davisd95fcd82022-10-07 12:12:29 -050020#include <linux/dma-mapping.h>
Andrew F. Davis2ed41072019-04-12 12:54:45 -040021
Andrew Davise74e3bb2022-07-15 11:34:33 -050022#include "common.h"
23
24static bool ti_secure_cert_detected(void *p_image)
25{
26 /* Primitive certificate detection, check for DER starting with
27 * two 4-Octet SEQUENCE tags
28 */
29 return (((u8 *)p_image)[0] == 0x30 && ((u8 *)p_image)[1] == 0x82 &&
30 ((u8 *)p_image)[4] == 0x30 && ((u8 *)p_image)[5] == 0x82);
31}
32
Andrew Davis0ce85432022-07-15 11:34:35 -050033/* Primitive certificate length, assumes one 2-Octet sized SEQUENCE */
34static size_t ti_secure_cert_length(void *p_image)
35{
36 size_t seq_length = be16_to_cpu(readw_relaxed(p_image + 2));
37 /* Add 4 for the SEQUENCE tag length */
38 return seq_length + 4;
39}
40
Tero Kristo738c5902021-06-11 11:45:19 +030041void ti_secure_image_post_process(void **p_image, size_t *p_size)
Andrew F. Davis2ed41072019-04-12 12:54:45 -040042{
Lokesh Vutlaac9ca952019-09-09 12:47:38 +053043 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
44 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
Andrew Davis0ce85432022-07-15 11:34:35 -050045 size_t cert_length;
Andrew F. Davis2ed41072019-04-12 12:54:45 -040046 u64 image_addr;
47 u32 image_size;
48 int ret;
49
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050050 image_size = *p_size;
Andrew F. Davis2ed41072019-04-12 12:54:45 -040051
Andrew Davis0ce85432022-07-15 11:34:35 -050052 if (!image_size)
53 return;
54
55 if (get_device_type() == K3_DEVICE_TYPE_GP) {
56 if (ti_secure_cert_detected(*p_image)) {
57 printf("Warning: Detected image signing certificate on GP device. "
58 "Skipping certificate to prevent boot failure. "
59 "This will fail if the image was also encrypted\n");
60
61 cert_length = ti_secure_cert_length(*p_image);
62 if (cert_length > *p_size) {
63 printf("Invalid signing certificate size\n");
64 return;
65 }
66
67 *p_image += cert_length;
68 *p_size -= cert_length;
69 }
70
Andrew Davis2045cf12022-07-15 11:34:34 -050071 return;
Andrew Davis0ce85432022-07-15 11:34:35 -050072 }
Andrew Davis2045cf12022-07-15 11:34:34 -050073
Andrew Davise74e3bb2022-07-15 11:34:33 -050074 if (get_device_type() != K3_DEVICE_TYPE_HS_SE &&
75 !ti_secure_cert_detected(*p_image)) {
76 printf("Warning: Did not detect image signing certificate. "
77 "Skipping authentication to prevent boot failure. "
78 "This will fail on Security Enforcing(HS-SE) devices\n");
79 return;
80 }
81
Andrew Davisd95fcd82022-10-07 12:12:29 -050082 /* Clean out image so it can be seen by system firmware */
83 image_addr = dma_map_single(*p_image, *p_size, DMA_BIDIRECTIONAL);
84
Andrew F. Davis2ed41072019-04-12 12:54:45 -040085 debug("Authenticating image at address 0x%016llx\n", image_addr);
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050086 debug("Authenticating image of size %d bytes\n", image_size);
87
Andrew F. Davis2ed41072019-04-12 12:54:45 -040088 /* Authenticate image */
89 ret = proc_ops->proc_auth_boot_image(ti_sci, &image_addr, &image_size);
90 if (ret) {
91 printf("Authentication failed!\n");
92 hang();
93 }
94
Andrew Davisd95fcd82022-10-07 12:12:29 -050095 /* Invalidate any stale lines over data written by system firmware */
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050096 if (image_size)
Andrew Davisd95fcd82022-10-07 12:12:29 -050097 dma_unmap_single(image_addr, image_size, DMA_BIDIRECTIONAL);
Andrew F. Davis2dc1e6e2020-01-07 18:22:29 -050098
Andrew F. Davis2ed41072019-04-12 12:54:45 -040099 /*
100 * The image_size returned may be 0 when the authentication process has
101 * moved the image. When this happens no further processing on the
102 * image is needed or often even possible as it may have also been
103 * placed behind a firewall when moved.
104 */
105 *p_size = image_size;
106
107 /*
108 * Output notification of successful authentication to re-assure the
109 * user that the secure code is being processed as expected. However
110 * suppress any such log output in case of building for SPL and booting
111 * via YMODEM. This is done to avoid disturbing the YMODEM serial
112 * protocol transactions.
113 */
114 if (!(IS_ENABLED(CONFIG_SPL_BUILD) &&
115 IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) &&
116 spl_boot_device() == BOOT_DEVICE_UART))
117 printf("Authentication passed\n");
118}