blob: 4b69de89ebdc63a3d7c759c632f7e995706d1ff0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
gaurav ranac3a50422015-02-27 09:45:35 +05302/*
3 * Copyright 2015 Freescale Semiconductor, Inc.
gaurav ranac3a50422015-02-27 09:45:35 +05304 */
5
6#ifndef _FSL_VALIDATE_H_
7#define _FSL_VALIDATE_H_
8
9#include <fsl_sec.h>
10#include <fsl_sec_mon.h>
gaurav ranac3a50422015-02-27 09:45:35 +053011#include <linux/types.h>
12
Simon Glassed38aef2020-05-10 11:40:03 -060013struct cmd_tbl;
14
gaurav ranac3a50422015-02-27 09:45:35 +053015#define WORD_SIZE 4
16
17/* Minimum and maximum size of RSA signature length in bits */
18#define KEY_SIZE 4096
19#define KEY_SIZE_BYTES (KEY_SIZE/8)
20#define KEY_SIZE_WORDS (KEY_SIZE_BYTES/(WORD_SIZE))
21
22extern struct jobring jr;
23
gaurav ranac3a50422015-02-27 09:45:35 +053024/* Barker code size in bytes */
25#define ESBC_BARKER_LEN 4 /* barker code length in ESBC uboot client */
26 /* header */
27
28/* No-error return values */
29#define ESBC_VALID_HDR 0 /* header is valid */
30
31/* Maximum number of SG entries allowed */
32#define MAX_SG_ENTRIES 8
33
Saksham Jain6121f082016-03-23 16:24:34 +053034/* Different Header Struct for LS-CH3 */
35#ifdef CONFIG_ESBC_HDR_LS
36struct fsl_secboot_img_hdr {
37 u8 barker[ESBC_BARKER_LEN]; /* barker code */
38 u32 srk_tbl_off;
39 struct {
40 u8 num_srk;
41 u8 srk_sel;
42 u8 reserve;
Saksham Jain6121f082016-03-23 16:24:34 +053043 } len_kr;
Udit Agarwal990a9972017-02-09 21:36:11 +053044 u8 ie_flag;
Saksham Jain6121f082016-03-23 16:24:34 +053045
46 u32 uid_flag;
47
48 u32 psign; /* signature offset */
49 u32 sign_len; /* length of the signature in bytes */
50
51 u64 pimg64; /* 64 bit pointer to ESBC Image */
52 u32 img_size; /* ESBC client image size in bytes */
53 u32 ie_key_sel;
54
55 u32 fsl_uid_0;
56 u32 fsl_uid_1;
57 u32 oem_uid_0;
58 u32 oem_uid_1;
59 u32 oem_uid_2;
60 u32 oem_uid_3;
61 u32 oem_uid_4;
62 u32 reserved1[3];
63};
64
65#ifdef CONFIG_KEY_REVOCATION
66/* Srk table and key revocation check */
67#define UNREVOCABLE_KEY 8
68#define ALIGN_REVOC_KEY 7
69#define MAX_KEY_ENTRIES 8
70#endif
71
Udit Agarwal990a9972017-02-09 21:36:11 +053072#if defined(CONFIG_FSL_ISBC_KEY_EXT)
73#define IE_FLAG_MASK 0x1
74#define SCRATCH_IE_LOW_ADR 13
75#define SCRATCH_IE_HIGH_ADR 14
76#endif
Saksham Jain6121f082016-03-23 16:24:34 +053077
78#else /* CONFIG_ESBC_HDR_LS */
79
gaurav ranac3a50422015-02-27 09:45:35 +053080/*
81 * ESBC uboot client header structure.
82 * The struct contain the following fields
83 * barker code
84 * public key offset
85 * pub key length
86 * signature offset
87 * length of the signature
88 * ptr to SG table
89 * no of entries in SG table
90 * esbc ptr
91 * size of esbc
92 * esbc entry point
93 * Scatter gather flag
94 * UID flag
95 * FSL UID
96 * OEM UID
97 * Here, pub key is modulus concatenated with exponent
98 * of equal length
99 */
100struct fsl_secboot_img_hdr {
101 u8 barker[ESBC_BARKER_LEN]; /* barker code */
102 union {
103 u32 pkey; /* public key offset */
104#ifdef CONFIG_KEY_REVOCATION
105 u32 srk_tbl_off;
106#endif
107 };
108
109 union {
110 u32 key_len; /* pub key length in bytes */
111#ifdef CONFIG_KEY_REVOCATION
112 struct {
113 u32 srk_table_flag:8;
114 u32 srk_sel:8;
115 u32 num_srk:16;
116 } len_kr;
117#endif
118 };
119
120 u32 psign; /* signature offset */
121 u32 sign_len; /* length of the signature in bytes */
122 union {
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530123 u32 psgtable; /* ptr to SG table */
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530124#ifndef CONFIG_ESBC_ADDR_64BIT
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530125 u32 pimg; /* ptr to ESBC client image */
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530126#endif
gaurav ranac3a50422015-02-27 09:45:35 +0530127 };
128 union {
129 u32 sg_entries; /* no of entries in SG table */
130 u32 img_size; /* ESBC client image size in bytes */
131 };
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530132 u32 img_start; /* ESBC client entry point */
gaurav ranac3a50422015-02-27 09:45:35 +0530133 u32 sg_flag; /* Scatter gather flag */
134 u32 uid_flag;
135 u32 fsl_uid_0;
136 u32 oem_uid_0;
137 u32 reserved1[2];
138 u32 fsl_uid_1;
139 u32 oem_uid_1;
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530140 union {
141 u32 reserved2[2];
142#ifdef CONFIG_ESBC_ADDR_64BIT
143 u64 pimg64; /* 64 bit pointer to ESBC Image */
144#endif
145 };
gaurav ranac3a50422015-02-27 09:45:35 +0530146 u32 ie_flag;
147 u32 ie_key_sel;
148};
149
Saksham Jain6121f082016-03-23 16:24:34 +0530150#ifdef CONFIG_KEY_REVOCATION
151/* Srk table and key revocation check */
152#define SRK_FLAG 0x01
153#define UNREVOCABLE_KEY 4
154#define ALIGN_REVOC_KEY 3
155#define MAX_KEY_ENTRIES 4
156#endif
157
Udit Agarwal990a9972017-02-09 21:36:11 +0530158#if defined(CONFIG_FSL_ISBC_KEY_EXT)
159#define IE_FLAG_MASK 0xFFFFFFFF
160#endif
161
Saksham Jain6121f082016-03-23 16:24:34 +0530162#endif /* CONFIG_ESBC_HDR_LS */
163
gaurav ranac3a50422015-02-27 09:45:35 +0530164#if defined(CONFIG_FSL_ISBC_KEY_EXT)
165struct ie_key_table {
166 u32 key_len;
167 u8 pkey[2 * KEY_SIZE_BYTES];
168};
169
170struct ie_key_info {
171 uint32_t key_revok;
172 uint32_t num_keys;
173 struct ie_key_table ie_key_tbl[32];
174};
175#endif
176
177#ifdef CONFIG_KEY_REVOCATION
178struct srk_table {
179 u32 key_len;
180 u8 pkey[2 * KEY_SIZE_BYTES];
181};
182#endif
183
184/*
185 * SG table.
186 */
187#if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
188/*
189 * This struct contains the following fields
190 * length of the segment
191 * source address
192 */
193struct fsl_secboot_sg_table {
194 u32 len; /* length of the segment in bytes */
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530195 u32 src_addr; /* ptr to the data segment */
gaurav ranac3a50422015-02-27 09:45:35 +0530196};
197#else
198/*
199 * This struct contains the following fields
200 * length of the segment
201 * Destination Target ID
202 * source address
203 * destination address
204 */
205struct fsl_secboot_sg_table {
206 u32 len;
207 u32 trgt_id;
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530208 u32 src_addr;
209 u32 dst_addr;
gaurav ranac3a50422015-02-27 09:45:35 +0530210};
211#endif
212
Udit Agarwal990a9972017-02-09 21:36:11 +0530213/* ESBC global structure.
214 * Data to be used across verification of different images.
Flavio Suligoie2323aa2020-01-16 11:32:17 +0100215 * Stores following Data:
Udit Agarwal990a9972017-02-09 21:36:11 +0530216 * IE Table
217 */
218struct fsl_secboot_glb {
219#if defined(CONFIG_FSL_ISBC_KEY_EXT)
220 uintptr_t ie_addr;
221 struct ie_key_info ie_tbl;
222#endif
223};
gaurav ranac3a50422015-02-27 09:45:35 +0530224/*
225 * ESBC private structure.
226 * Private structure used by ESBC to store following fields
227 * ESBC client key
228 * ESBC client key hash
229 * ESBC client Signature
230 * Encoded hash recovered from signature
231 * Encoded hash of ESBC client header plus ESBC client image
232 */
233struct fsl_secboot_img_priv {
234 uint32_t hdr_location;
Udit Agarwal990a9972017-02-09 21:36:11 +0530235 uintptr_t ie_addr;
gaurav ranac3a50422015-02-27 09:45:35 +0530236 u32 key_len;
237 struct fsl_secboot_img_hdr hdr;
238
239 u8 img_key[2 * KEY_SIZE_BYTES]; /* ESBC client key */
240 u8 img_key_hash[32]; /* ESBC client key hash */
241
242#ifdef CONFIG_KEY_REVOCATION
243 struct srk_table srk_tbl[MAX_KEY_ENTRIES];
244#endif
245 u8 img_sign[KEY_SIZE_BYTES]; /* ESBC client signature */
246
247 u8 img_encoded_hash[KEY_SIZE_BYTES]; /* EM wrt RSA PKCSv1.5 */
248 /* Includes hash recovered after
249 * signature verification
250 */
251
252 u8 img_encoded_hash_second[KEY_SIZE_BYTES];/* EM' wrt RSA PKCSv1.5 */
253 /* Includes hash of
254 * ESBC client header plus
255 * ESBC client image
256 */
257
258 struct fsl_secboot_sg_table sgtbl[MAX_SG_ENTRIES]; /* SG table */
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530259 uintptr_t ehdrloc; /* ESBC Header location */
Saksham Jain04fcf522016-03-23 16:24:45 +0530260 uintptr_t *img_addr_ptr; /* ESBC Image Location */
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530261 uint32_t img_size; /* ESBC Image Size */
gaurav ranac3a50422015-02-27 09:45:35 +0530262};
263
Simon Glassed38aef2020-05-10 11:40:03 -0600264int do_esbc_halt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Saksham Jain7f048b32016-03-23 16:24:44 +0530265
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530266int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
Saksham Jain04fcf522016-03-23 16:24:45 +0530267 uintptr_t *img_addr_ptr);
Simon Glassed38aef2020-05-10 11:40:03 -0600268int fsl_secboot_blob_encap(struct cmd_tbl *cmdtp, int flag, int argc,
269 char *const argv[]);
270int fsl_secboot_blob_decap(struct cmd_tbl *cmdtp, int flag, int argc,
271 char *const argv[]);
gaurav ranac3a50422015-02-27 09:45:35 +0530272
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +0530273int fsl_check_boot_mode_secure(void);
274int fsl_setenv_chain_of_trust(void);
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400275
276/*
Michal Simek50fa1182023-05-17 09:17:16 +0200277 * This function is used to validate the main U-Boot binary from
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400278 * SPL just before passing control to it using QorIQ Trust
Michal Simek50fa1182023-05-17 09:17:16 +0200279 * Architecture header (appended to U-Boot image).
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400280 */
281void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr);
Tom Rinicb189262022-12-02 16:42:50 -0500282
283/*
284 * This header is appended at end of image and copied to DDR along
285 * with the U-Boot image and later used as part of the validation
286 * flow
287 */
288#define FSL_U_BOOT_HDR_SIZE (16 << 10)
gaurav ranac3a50422015-02-27 09:45:35 +0530289#endif