1 /*
2  * Copyright 2017-2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef CSF_HDR_H
9 #define CSF_HDR_H
10 
11 #include "caam.h"
12 #include "hash.h"
13 #include "rsa.h"
14 
15 /* Barker code size in bytes */
16 #define CSF_BARKER_LEN	4	/* barker code length in ESBC uboot client */
17 				/* header */
18 
19 #ifdef CSF_HDR_CH3
20 struct csf_hdr {
21 	uint8_t barker[CSF_BARKER_LEN];	/* 0x00 Barker code */
22 	uint32_t srk_tbl_off;		/* 0x04 SRK Table Offset */
23 
24 	struct {
25 		uint8_t num_srk;	/* 0x08 No. of keys */
26 		uint8_t srk_sel;	/*  Key no. to be used */
27 		uint8_t reserve;	/* 0x0a rseerved */
28 	} len_kr;
29 	uint8_t ie_flag;
30 
31 	uint32_t uid_flag;
32 
33 	uint32_t psign;			/* 0x10 signature offset */
34 	uint32_t sign_len;			/* 0x14 length of signature */
35 
36 	union {
37 		struct {
38 			uint32_t sg_table_offset; /* 0x18 SG Table Offset */
39 			uint32_t sg_entries;	  /* 0x1c no of entries in SG */
40 		} sg_isbc;
41 		uint64_t img_addr;	/* 64 bit pointer to ESBC Image */
42 	};
43 
44 	union {
45 		struct {
46 			uint32_t img_size;   /* ESBC client img size in bytes */
47 			uint32_t ie_key_sel;
48 		} img;
49 		uint64_t entry_point;	  /* 0x20-0x24 ESBC entry point */
50 	};
51 
52 	uint32_t fsl_uid_0;			/* 0x28 Freescale unique id 0 */
53 	uint32_t fsl_uid_1;			/* 0x2c Freescale unique id 1 */
54 	uint32_t oem_uid_0;			/* 0x30 OEM unique id 0 */
55 	uint32_t oem_uid_1;			/* 0x34 OEM unique id 1 */
56 	uint32_t oem_uid_2;			/* 0x38 OEM unique id 2 */
57 	uint32_t oem_uid_3;			/* 0x3c OEM unique id 3 */
58 	uint32_t oem_uid_4;			/* 0x40 OEM unique id 4 */
59 
60 	uint32_t reserved[3];		/* 0x44 - 0x4f */
61 };
62 
63 /* Srk table and key revocation check */
64 #define UNREVOCABLE_KEY	8
65 #define REVOC_KEY_ALIGN 7
66 #define MAX_KEY_ENTRIES 8
67 
68 #else
69 
70 /* CSF header for Chassis 2 */
71 struct csf_hdr {
72 	uint8_t barker[CSF_BARKER_LEN];	/* barker code */
73 	union {
74 		uint32_t pkey;		/* public key offset */
75 		uint32_t srk_tbl_off;
76 	};
77 
78 	union {
79 		uint32_t key_len;		/* pub key length in bytes */
80 		struct {
81 			uint32_t srk_table_flag:8;
82 			uint32_t srk_sel:8;
83 			uint32_t num_srk:16;
84 		} len_kr;
85 	};
86 
87 	uint32_t psign;		/* signature offset */
88 	uint32_t sign_len;		/* length of the signature in bytes */
89 
90 	/* SG Table used by ISBC header */
91 	union {
92 		struct {
93 			uint32_t sg_table_offset; /* 0x14 SG Table Offset */
94 			uint32_t sg_entries;	/* no of entries in SG table */
95 		} sg_isbc;
96 		struct {
97 			uint32_t reserved1;	/* Reserved field */
98 			uint32_t img_size;	/* ESBC img size in bytes */
99 		} img;
100 	};
101 
102 	uint32_t entry_point;		/* ESBC client entry point */
103 	uint32_t reserved2;		/* Scatter gather flag */
104 	uint32_t uid_flag;
105 	uint32_t fsl_uid_0;
106 	uint32_t oem_uid_0;
107 	uint32_t reserved3[2];
108 	uint32_t fsl_uid_1;
109 	uint32_t oem_uid_1;
110 
111 	/* The entries below aren't present in ISBC header */
112 	uint64_t img_addr;	/* 64 bit pointer to ESBC Image */
113 	uint32_t ie_flag;
114 	uint32_t ie_key_sel;
115 };
116 
117 /* Srk table and key revocation check */
118 #define UNREVOCABLE_KEY	4
119 #define REVOC_KEY_ALIGN 3
120 #define MAX_KEY_ENTRIES 4
121 
122 #endif
123 
124 struct srk_table {
125 	uint32_t key_len;
126 	uint8_t pkey[2 * RSA_4K_KEY_SZ_BYTES];
127 };
128 
129 /*
130  * This struct contains the following fields
131  * length of the segment
132  * Destination Target ID
133  * source address
134  * destination address
135  */
136 struct sg_table {
137 	uint32_t len;			/* Length of Image */
138 	uint32_t res1;
139 	union {
140 		uint64_t src_addr;	/* SRC Address of Image */
141 		struct {
142 			uint32_t src_addr;
143 			uint32_t dst_addr;
144 		} img;
145 	};
146 };
147 
148 int validate_esbc_header(void *img_hdr, void **img_key, uint32_t *key_len,
149 			 void **img_sign, uint32_t *sign_len,
150 			 enum sig_alg *algo);
151 
152 int calc_img_hash(struct csf_hdr *hdr, void *img_addr, uint32_t img_size,
153 		  uint8_t *img_hash, uint32_t *hash_len);
154 
155 #endif
156