blob: 657f4533bcd3d589ae507010a499e76a672a7047 [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 Jain119c7002022-06-23 16:31:35 +05304 * Copyright 2021-2022 NXP
gaurav ranac3a50422015-02-27 09:45:35 +05305 */
6
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.h>
Simon Glass11c89f32017-05-17 17:18:03 -06008#include <dm.h>
gaurav ranac3a50422015-02-27 09:45:35 +05309#include <fsl_validate.h>
10#include <fsl_secboot_err.h>
11#include <fsl_sfp.h>
12#include <fsl_sec.h>
13#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
gaurav ranac3a50422015-02-27 09:45:35 +053015#include <malloc.h>
gaurav ranac3a50422015-02-27 09:45:35 +053016#include <u-boot/rsa-mod-exp.h>
17#include <hash.h>
18#include <fsl_secboot_err.h>
York Sunc4f047c2017-03-27 11:41:03 -070019#ifdef CONFIG_ARCH_LS1021A
gaurav ranac3a50422015-02-27 09:45:35 +053020#include <asm/arch/immap_ls102xa.h>
21#endif
Gaurav Jain119c7002022-06-23 16:31:35 +053022#include <dm/lists.h>
gaurav ranac3a50422015-02-27 09:45:35 +053023
24#define SHA256_BITS 256
25#define SHA256_BYTES (256/8)
26#define SHA256_NIBBLES (256/4)
27#define NUM_HEX_CHARS (sizeof(ulong) * 2)
28
Aneesh Bansal24b8fae2015-12-08 14:14:13 +053029#define CHECK_KEY_LEN(key_len) (((key_len) == 2 * KEY_SIZE_BYTES / 4) || \
30 ((key_len) == 2 * KEY_SIZE_BYTES / 2) || \
31 ((key_len) == 2 * KEY_SIZE_BYTES))
Tom Rini6fb86c12022-12-02 16:42:21 -050032#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
Udit Agarwal990a9972017-02-09 21:36:11 +053033/* Global data structure */
34static struct fsl_secboot_glb glb;
35#endif
Aneesh Bansal24b8fae2015-12-08 14:14:13 +053036
gaurav ranac3a50422015-02-27 09:45:35 +053037/* This array contains DER value for SHA-256 */
38static const u8 hash_identifier[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
39 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
40 0x04, 0x20
41 };
42
43static u8 hash_val[SHA256_BYTES];
Saksham Jain6121f082016-03-23 16:24:34 +053044
45#ifdef CONFIG_ESBC_HDR_LS
46/* New Barker Code for LS ESBC Header */
47static const u8 barker_code[ESBC_BARKER_LEN] = { 0x12, 0x19, 0x20, 0x01 };
48#else
gaurav ranac3a50422015-02-27 09:45:35 +053049static const u8 barker_code[ESBC_BARKER_LEN] = { 0x68, 0x39, 0x27, 0x81 };
Saksham Jain6121f082016-03-23 16:24:34 +053050#endif
gaurav ranac3a50422015-02-27 09:45:35 +053051
52void branch_to_self(void) __attribute__ ((noreturn));
53
54/*
55 * This function will put core in infinite loop.
56 * This will be called when the ESBC can not proceed further due
57 * to some unknown errors.
58 */
59void branch_to_self(void)
60{
61 printf("Core is in infinite loop due to errors.\n");
62self:
63 goto self;
64}
65
Tom Rini6fb86c12022-12-02 16:42:21 -050066#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
gaurav ranac3a50422015-02-27 09:45:35 +053067static u32 check_ie(struct fsl_secboot_img_priv *img)
68{
Udit Agarwal990a9972017-02-09 21:36:11 +053069 if (img->hdr.ie_flag & IE_FLAG_MASK)
gaurav ranac3a50422015-02-27 09:45:35 +053070 return 1;
71
72 return 0;
73}
74
75/* This function returns the CSF Header Address of uboot
76 * For MPC85xx based platforms, the LAW mapping for NOR
77 * flash changes in uboot code. Hence the offset needs
78 * to be calculated and added to the new NOR flash base
79 * address
80 */
81#if defined(CONFIG_MPC85xx)
Tom Rini063c9382022-07-23 13:05:03 -040082#include <flash.h>
83
Aneesh Bansal9c028fa2015-09-17 16:16:34 +053084int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
gaurav ranac3a50422015-02-27 09:45:35 +053085{
Tom Rinid5c3bf22022-10-28 20:27:12 -040086 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_MPC85xx_GUTS_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +053087 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
Tom Rini6a5dccc2022-11-16 13:10:41 -050088 u32 csf_flash_offset = csf_hdr_addr & ~(CFG_SYS_PBI_FLASH_BASE);
Aneesh Bansal9c028fa2015-09-17 16:16:34 +053089 u32 flash_addr, addr;
gaurav ranac3a50422015-02-27 09:45:35 +053090 int found = 0;
91 int i = 0;
92
93 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
94 flash_addr = flash_info[i].start[0];
95 addr = flash_info[i].start[0] + csf_flash_offset;
96 if (memcmp((u8 *)addr, barker_code, ESBC_BARKER_LEN) == 0) {
Aneesh Bansal9c028fa2015-09-17 16:16:34 +053097 debug("Barker found on addr %x\n", addr);
gaurav ranac3a50422015-02-27 09:45:35 +053098 found = 1;
99 break;
100 }
101 }
102
103 if (!found)
104 return -1;
105
106 *csf_addr = addr;
107 *flash_base_addr = flash_addr;
108
109 return 0;
110}
111#else
112/* For platforms like LS1020, correct flash address is present in
113 * the header. So the function reqturns flash base address as 0
114 */
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530115int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
gaurav ranac3a50422015-02-27 09:45:35 +0530116{
Tom Rini376b88a2022-10-28 20:27:13 -0400117 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +0530118 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
119
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530120 if (memcmp((u8 *)(uintptr_t)csf_hdr_addr,
121 barker_code, ESBC_BARKER_LEN))
gaurav ranac3a50422015-02-27 09:45:35 +0530122 return -1;
123
124 *csf_addr = csf_hdr_addr;
125 *flash_base_addr = 0;
126 return 0;
127}
128#endif
129
Udit Agarwal990a9972017-02-09 21:36:11 +0530130#if defined(CONFIG_ESBC_HDR_LS)
131static int get_ie_info_addr(uintptr_t *ie_addr)
132{
Tom Rini376b88a2022-10-28 20:27:13 -0400133 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Udit Agarwal990a9972017-02-09 21:36:11 +0530134 /* For LS-CH3, the address of IE Table is
135 * stated in Scratch13 and scratch14 of DCFG.
136 * Bootrom validates this table while validating uboot.
137 * DCFG is LE*/
138 *ie_addr = in_le32(&gur->scratchrw[SCRATCH_IE_HIGH_ADR - 1]);
139 *ie_addr = *ie_addr << 32;
140 *ie_addr |= in_le32(&gur->scratchrw[SCRATCH_IE_LOW_ADR - 1]);
141 return 0;
142}
143#else /* CONFIG_ESBC_HDR_LS */
144static int get_ie_info_addr(uintptr_t *ie_addr)
gaurav ranac3a50422015-02-27 09:45:35 +0530145{
146 struct fsl_secboot_img_hdr *hdr;
147 struct fsl_secboot_sg_table *sg_tbl;
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530148 u32 flash_base_addr, csf_addr;
gaurav ranac3a50422015-02-27 09:45:35 +0530149
150 if (get_csf_base_addr(&csf_addr, &flash_base_addr))
151 return -1;
152
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530153 hdr = (struct fsl_secboot_img_hdr *)(uintptr_t)csf_addr;
gaurav ranac3a50422015-02-27 09:45:35 +0530154
155 /* For SoC's with Trust Architecture v1 with corenet bus
156 * the sg table field in CSF header has absolute address
157 * for sg table in memory. In other Trust Architecture,
158 * this field specifies the offset of sg table from the
159 * base address of CSF Header
160 */
161#if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
162 sg_tbl = (struct fsl_secboot_sg_table *)
Tom Rini6a5dccc2022-11-16 13:10:41 -0500163 (((u32)hdr->psgtable & ~(CFG_SYS_PBI_FLASH_BASE)) +
gaurav ranac3a50422015-02-27 09:45:35 +0530164 flash_base_addr);
165#else
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530166 sg_tbl = (struct fsl_secboot_sg_table *)(uintptr_t)(csf_addr +
Aneesh Bansal9c028fa2015-09-17 16:16:34 +0530167 (u32)hdr->psgtable);
gaurav ranac3a50422015-02-27 09:45:35 +0530168#endif
169
170 /* IE Key Table is the first entry in the SG Table */
171#if defined(CONFIG_MPC85xx)
Udit Agarwal990a9972017-02-09 21:36:11 +0530172 *ie_addr = (uintptr_t)((sg_tbl->src_addr &
Tom Rini6a5dccc2022-11-16 13:10:41 -0500173 ~(CFG_SYS_PBI_FLASH_BASE)) +
Udit Agarwal990a9972017-02-09 21:36:11 +0530174 flash_base_addr);
gaurav ranac3a50422015-02-27 09:45:35 +0530175#else
Udit Agarwal990a9972017-02-09 21:36:11 +0530176 *ie_addr = (uintptr_t)sg_tbl->src_addr;
gaurav ranac3a50422015-02-27 09:45:35 +0530177#endif
178
Udit Agarwal990a9972017-02-09 21:36:11 +0530179 debug("IE Table address is %lx\n", *ie_addr);
gaurav ranac3a50422015-02-27 09:45:35 +0530180 return 0;
181}
Udit Agarwal990a9972017-02-09 21:36:11 +0530182#endif /* CONFIG_ESBC_HDR_LS */
gaurav ranac3a50422015-02-27 09:45:35 +0530183#endif
184
185#ifdef CONFIG_KEY_REVOCATION
186/* This function checks srk_table_flag in header and set/reset srk_flag.*/
187static u32 check_srk(struct fsl_secboot_img_priv *img)
188{
Saksham Jain6121f082016-03-23 16:24:34 +0530189#ifdef CONFIG_ESBC_HDR_LS
Udit Agarwal990a9972017-02-09 21:36:11 +0530190 /* In LS, No SRK Flag as SRK is always present if IE not present*/
Tom Rini6fb86c12022-12-02 16:42:21 -0500191#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
Udit Agarwal990a9972017-02-09 21:36:11 +0530192 return !check_ie(img);
193#endif
Saksham Jain6121f082016-03-23 16:24:34 +0530194 return 1;
195#else
gaurav ranac3a50422015-02-27 09:45:35 +0530196 if (img->hdr.len_kr.srk_table_flag & SRK_FLAG)
197 return 1;
198
199 return 0;
Saksham Jain6121f082016-03-23 16:24:34 +0530200#endif
gaurav ranac3a50422015-02-27 09:45:35 +0530201}
202
203/* This function returns ospr's key_revoc values.*/
204static u32 get_key_revoc(void)
205{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500206 struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +0530207 return (sfp_in32(&sfp_regs->ospr) & OSPR_KEY_REVOC_MASK) >>
208 OSPR_KEY_REVOC_SHIFT;
209}
210
211/* This function checks if selected key is revoked or not.*/
212static u32 is_key_revoked(u32 keynum, u32 rev_flag)
213{
214 if (keynum == UNREVOCABLE_KEY)
215 return 0;
216
217 if ((u32)(1 << (ALIGN_REVOC_KEY - keynum)) & rev_flag)
218 return 1;
219
220 return 0;
221}
222
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530223/* It read validates srk_table key lengths.*/
224static u32 read_validate_srk_tbl(struct fsl_secboot_img_priv *img)
gaurav ranac3a50422015-02-27 09:45:35 +0530225{
226 int i = 0;
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530227 u32 ret, key_num, key_revoc_flag, size;
228 struct fsl_secboot_img_hdr *hdr = &img->hdr;
229 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
230
231 if ((hdr->len_kr.num_srk == 0) ||
232 (hdr->len_kr.num_srk > MAX_KEY_ENTRIES))
233 return ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY;
234
235 key_num = hdr->len_kr.srk_sel;
236 if (key_num == 0 || key_num > hdr->len_kr.num_srk)
237 return ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM;
238
239 /* Get revoc key from sfp */
240 key_revoc_flag = get_key_revoc();
241 ret = is_key_revoked(key_num, key_revoc_flag);
242 if (ret)
243 return ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED;
244
245 size = hdr->len_kr.num_srk * sizeof(struct srk_table);
246
247 memcpy(&img->srk_tbl, esbc + hdr->srk_tbl_off, size);
248
249 for (i = 0; i < hdr->len_kr.num_srk; i++) {
250 if (!CHECK_KEY_LEN(img->srk_tbl[i].key_len))
gaurav ranac3a50422015-02-27 09:45:35 +0530251 return ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN;
252 }
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530253
254 img->key_len = img->srk_tbl[key_num - 1].key_len;
255
256 memcpy(&img->img_key, &(img->srk_tbl[key_num - 1].pkey),
257 img->key_len);
258
gaurav ranac3a50422015-02-27 09:45:35 +0530259 return 0;
260}
261#endif
262
Saksham Jain6121f082016-03-23 16:24:34 +0530263#ifndef CONFIG_ESBC_HDR_LS
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530264static u32 read_validate_single_key(struct fsl_secboot_img_priv *img)
265{
266 struct fsl_secboot_img_hdr *hdr = &img->hdr;
267 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
268
269 /* check key length */
270 if (!CHECK_KEY_LEN(hdr->key_len))
271 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN;
272
273 memcpy(&img->img_key, esbc + hdr->pkey, hdr->key_len);
274
275 img->key_len = hdr->key_len;
276
277 return 0;
278}
Saksham Jain6121f082016-03-23 16:24:34 +0530279#endif /* CONFIG_ESBC_HDR_LS */
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530280
Tom Rini6fb86c12022-12-02 16:42:21 -0500281#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
Udit Agarwal990a9972017-02-09 21:36:11 +0530282
283static void install_ie_tbl(uintptr_t ie_tbl_addr,
284 struct fsl_secboot_img_priv *img)
285{
286 /* Copy IE tbl to Global Data */
287 memcpy(&glb.ie_tbl, (u8 *)ie_tbl_addr, sizeof(struct ie_key_info));
288 img->ie_addr = (uintptr_t)&glb.ie_tbl;
289 glb.ie_addr = img->ie_addr;
290}
291
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530292static u32 read_validate_ie_tbl(struct fsl_secboot_img_priv *img)
293{
294 struct fsl_secboot_img_hdr *hdr = &img->hdr;
295 u32 ie_key_len, ie_revoc_flag, ie_num;
296 struct ie_key_info *ie_info;
297
Udit Agarwal990a9972017-02-09 21:36:11 +0530298 if (!img->ie_addr) {
299 if (get_ie_info_addr(&img->ie_addr))
300 return ERROR_IE_TABLE_NOT_FOUND;
301 else
302 install_ie_tbl(img->ie_addr, img);
303 }
304
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530305 ie_info = (struct ie_key_info *)(uintptr_t)img->ie_addr;
306 if (ie_info->num_keys == 0 || ie_info->num_keys > 32)
307 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY;
308
309 ie_num = hdr->ie_key_sel;
310 if (ie_num == 0 || ie_num > ie_info->num_keys)
311 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM;
312
313 ie_revoc_flag = ie_info->key_revok;
314 if ((u32)(1 << (ie_num - 1)) & ie_revoc_flag)
315 return ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED;
316
317 ie_key_len = ie_info->ie_key_tbl[ie_num - 1].key_len;
318
319 if (!CHECK_KEY_LEN(ie_key_len))
320 return ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN;
321
322 memcpy(&img->img_key, &(ie_info->ie_key_tbl[ie_num - 1].pkey),
323 ie_key_len);
324
325 img->key_len = ie_key_len;
326 return 0;
327}
328#endif
329
gaurav ranac3a50422015-02-27 09:45:35 +0530330/* This function return length of public key.*/
331static inline u32 get_key_len(struct fsl_secboot_img_priv *img)
332{
333 return img->key_len;
334}
335
336/*
337 * Handles the ESBC uboot client header verification failure.
338 * This function handles all the errors which might occur in the
339 * parsing and checking of ESBC uboot client header. It will also
340 * set the error bits in the SEC_MON.
341 */
342static void fsl_secboot_header_verification_failure(void)
343{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500344 struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +0530345
346 /* 29th bit of OSPR is ITS */
347 u32 its = sfp_in32(&sfp_regs->ospr) >> 2;
348
Sumit Gargbc17f982016-08-31 08:54:15 -0400349 if (its == 1)
350 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
351 else
352 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
gaurav ranac3a50422015-02-27 09:45:35 +0530353
354 printf("Generating reset request\n");
355 do_reset(NULL, 0, 0, NULL);
Saksham Jain7f048b32016-03-23 16:24:44 +0530356 /* If reset doesn't coocur, halt execution */
357 do_esbc_halt(NULL, 0, 0, NULL);
gaurav ranac3a50422015-02-27 09:45:35 +0530358}
359
360/*
361 * Handles the ESBC uboot client image verification failure.
362 * This function handles all the errors which might occur in the
363 * public key hash comparison and signature verification of
364 * ESBC uboot client image. It will also
365 * set the error bits in the SEC_MON.
366 */
367static void fsl_secboot_image_verification_failure(void)
368{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500369 struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +0530370
Aneesh Bansal52b4b2d2015-10-12 22:05:50 +0530371 u32 its = (sfp_in32(&sfp_regs->ospr) & ITS_MASK) >> ITS_BIT;
gaurav ranac3a50422015-02-27 09:45:35 +0530372
Sumit Gargbc17f982016-08-31 08:54:15 -0400373 if (its == 1) {
374 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
gaurav ranac3a50422015-02-27 09:45:35 +0530375
Sumit Gargbc17f982016-08-31 08:54:15 -0400376 printf("Generating reset request\n");
377 do_reset(NULL, 0, 0, NULL);
378 /* If reset doesn't coocur, halt execution */
379 do_esbc_halt(NULL, 0, 0, NULL);
Saksham Jain7f048b32016-03-23 16:24:44 +0530380
Sumit Gargbc17f982016-08-31 08:54:15 -0400381 } else {
382 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
gaurav ranac3a50422015-02-27 09:45:35 +0530383 }
384}
385
386static void fsl_secboot_bootscript_parse_failure(void)
387{
388 fsl_secboot_header_verification_failure();
389}
390
391/*
392 * Handles the errors in esbc boot.
393 * This function handles all the errors which might occur in the
394 * esbc boot phase. It will call the appropriate api to log the
395 * errors and set the error bits in the SEC_MON.
396 */
397void fsl_secboot_handle_error(int error)
398{
Ruchika Guptad6b89202017-04-17 18:07:17 +0530399#ifndef CONFIG_SPL_BUILD
gaurav ranac3a50422015-02-27 09:45:35 +0530400 const struct fsl_secboot_errcode *e;
401
402 for (e = fsl_secboot_errcodes; e->errcode != ERROR_ESBC_CLIENT_MAX;
403 e++) {
404 if (e->errcode == error)
405 printf("ERROR :: %x :: %s\n", error, e->name);
406 }
Ruchika Guptad6b89202017-04-17 18:07:17 +0530407#else
408 printf("ERROR :: %x\n", error);
409#endif
gaurav ranac3a50422015-02-27 09:45:35 +0530410
Aneesh Bansal8a47d5c2016-01-22 16:37:28 +0530411 /* If Boot Mode is secure, transition the SNVS state and issue
412 * reset based on type of failure and ITS setting.
413 * If Boot mode is non-secure, return from this function.
414 */
415 if (fsl_check_boot_mode_secure() == 0)
416 return;
417
gaurav ranac3a50422015-02-27 09:45:35 +0530418 switch (error) {
419 case ERROR_ESBC_CLIENT_HEADER_BARKER:
420 case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
421 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
422 case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
423 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
424 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
425 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
426 case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
427 case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
428 case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
Saksham Jain6121f082016-03-23 16:24:34 +0530429 case ERROR_KEY_TABLE_NOT_FOUND:
gaurav ranac3a50422015-02-27 09:45:35 +0530430#ifdef CONFIG_KEY_REVOCATION
431 case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
432 case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
433 case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
434 case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
435#endif
Tom Rini6fb86c12022-12-02 16:42:21 -0500436#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
gaurav ranac3a50422015-02-27 09:45:35 +0530437 /*@fallthrough@*/
438 case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
439 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
440 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
441 case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
442 case ERROR_IE_TABLE_NOT_FOUND:
443#endif
444 fsl_secboot_header_verification_failure();
445 break;
446 case ERROR_ESBC_SEC_RESET:
447 case ERROR_ESBC_SEC_DEQ:
448 case ERROR_ESBC_SEC_ENQ:
449 case ERROR_ESBC_SEC_DEQ_TO:
450 case ERROR_ESBC_SEC_JOBQ_STATUS:
451 case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
452 case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
453 fsl_secboot_image_verification_failure();
454 break;
455 case ERROR_ESBC_MISSING_BOOTM:
456 fsl_secboot_bootscript_parse_failure();
457 break;
458 case ERROR_ESBC_WRONG_CMD:
459 default:
460 branch_to_self();
461 break;
462 }
463}
464
465static void fsl_secblk_handle_error(int error)
466{
467 switch (error) {
468 case ERROR_ESBC_SEC_ENQ:
469 fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
470 break;
471 case ERROR_ESBC_SEC_DEQ:
472 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
473 break;
474 case ERROR_ESBC_SEC_DEQ_TO:
475 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
476 break;
477 default:
478 printf("Job Queue Output status %x\n", error);
479 fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
480 break;
481 }
482}
483
484/*
485 * Calculate hash of key obtained via offset present in ESBC uboot
486 * client hdr. This function calculates the hash of key which is obtained
487 * through offset present in ESBC uboot client header.
488 */
489static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
490{
491 struct hash_algo *algo;
492 void *ctx;
493 int i, srk = 0;
494 int ret = 0;
495 const char *algo_name = "sha256";
496
497 /* Calculate hash of the esbc key */
498 ret = hash_progressive_lookup_algo(algo_name, &algo);
499 if (ret)
500 return ret;
501
502 ret = algo->hash_init(algo, &ctx);
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200503 if (ret)
gaurav ranac3a50422015-02-27 09:45:35 +0530504 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530505 /* Update hash for ESBC key */
506#ifdef CONFIG_KEY_REVOCATION
507 if (check_srk(img)) {
508 ret = algo->hash_update(algo, ctx,
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530509 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
510 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
gaurav ranac3a50422015-02-27 09:45:35 +0530511 srk = 1;
512 }
513#endif
514 if (!srk)
515 ret = algo->hash_update(algo, ctx,
516 img->img_key, img->key_len, 1);
517 if (ret)
518 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530519 /* Copy hash at destination buffer */
520 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200521 if (ret) {
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200522 free(ctx);
gaurav ranac3a50422015-02-27 09:45:35 +0530523 return ret;
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200524 }
gaurav ranac3a50422015-02-27 09:45:35 +0530525 for (i = 0; i < SHA256_BYTES; i++)
526 img->img_key_hash[i] = hash_val[i];
527
528 return 0;
529}
530
531/*
532 * Calculate hash of ESBC hdr and ESBC. This function calculates the
533 * single hash of ESBC header and ESBC image. If SG flag is on, all
534 * SG entries are also hashed alongwith the complete SG table.
535 */
536static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
537{
538 struct hash_algo *algo;
539 void *ctx;
540 int ret = 0;
541 int key_hash = 0;
542 const char *algo_name = "sha256";
543
544 /* Calculate the hash of the ESBC */
545 ret = hash_progressive_lookup_algo(algo_name, &algo);
546 if (ret)
547 return ret;
548
549 ret = algo->hash_init(algo, &ctx);
550 /* Copy hash at destination buffer */
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200551 if (ret)
gaurav ranac3a50422015-02-27 09:45:35 +0530552 return ret;
553
554 /* Update hash for CSF Header */
555 ret = algo->hash_update(algo, ctx,
556 (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200557 if (ret)
gaurav ranac3a50422015-02-27 09:45:35 +0530558 return ret;
559
560 /* Update the hash with that of srk table if srk flag is 1
561 * If IE Table is selected, key is not added in the hash
562 * If neither srk table nor IE key table available, add key
563 * from header in the hash calculation
564 */
565#ifdef CONFIG_KEY_REVOCATION
566 if (check_srk(img)) {
567 ret = algo->hash_update(algo, ctx,
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530568 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
569 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
gaurav ranac3a50422015-02-27 09:45:35 +0530570 key_hash = 1;
571 }
572#endif
Tom Rini6fb86c12022-12-02 16:42:21 -0500573#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
gaurav ranac3a50422015-02-27 09:45:35 +0530574 if (!key_hash && check_ie(img))
575 key_hash = 1;
576#endif
Saksham Jain6121f082016-03-23 16:24:34 +0530577#ifndef CONFIG_ESBC_HDR_LS
578/* No single key support in LS ESBC header */
579 if (!key_hash) {
gaurav ranac3a50422015-02-27 09:45:35 +0530580 ret = algo->hash_update(algo, ctx,
581 img->img_key, img->hdr.key_len, 0);
Saksham Jain6121f082016-03-23 16:24:34 +0530582 key_hash = 1;
583 }
584#endif
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200585 if (ret)
gaurav ranac3a50422015-02-27 09:45:35 +0530586 return ret;
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200587 if (!key_hash) {
588 free(ctx);
Saksham Jain6121f082016-03-23 16:24:34 +0530589 return ERROR_KEY_TABLE_NOT_FOUND;
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200590 }
gaurav ranac3a50422015-02-27 09:45:35 +0530591 /* Update hash for actual Image */
592 ret = algo->hash_update(algo, ctx,
Saksham Jain04fcf522016-03-23 16:24:45 +0530593 (u8 *)(*(img->img_addr_ptr)), img->img_size, 1);
Kshitiz Varshneyc4686322021-09-19 17:09:53 +0200594 if (ret)
gaurav ranac3a50422015-02-27 09:45:35 +0530595 return ret;
596
597 /* Copy hash at destination buffer */
598 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200599 if (ret) {
600 free(ctx);
gaurav ranac3a50422015-02-27 09:45:35 +0530601 return ret;
Kshitiz Varshney295e07d2021-08-01 14:31:45 +0200602 }
gaurav ranac3a50422015-02-27 09:45:35 +0530603 return 0;
604}
605
606/*
607 * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
608 * pointers for padding, DER value and hash. And finally, constructs EM'
609 * which includes hash of complete CSF header and ESBC image. If SG flag
610 * is on, hash of SG table and entries is also included.
611 */
612static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
613{
614 /*
615 * RSA PKCSv1.5 encoding format for encoded message is below
616 * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
617 * PS is Padding String
618 * DER is DER value for SHA-256
619 * Hash is SHA-256 hash
620 * *********************************************************
621 * representative points to first byte of EM initially and is
622 * filled with 0x0
623 * representative is incremented by 1 and second byte is filled
624 * with 0x1
625 * padding points to third byte of EM
626 * digest points to full length of EM - 32 bytes
627 * hash_id (DER value) points to 19 bytes before pDigest
628 * separator is one byte which separates padding and DER
629 */
630
631 size_t len;
632 u8 *representative;
633 u8 *padding, *digest;
634 u8 *hash_id, *separator;
635 int i;
636
637 len = (get_key_len(img) / 2) - 1;
638 representative = img->img_encoded_hash_second;
639 representative[0] = 0;
640 representative[1] = 1; /* block type 1 */
641
642 padding = &representative[2];
643 digest = &representative[1] + len - 32;
644 hash_id = digest - sizeof(hash_identifier);
645 separator = hash_id - 1;
646
647 /* fill padding area pointed by padding with 0xff */
648 memset(padding, 0xff, separator - padding);
649
650 /* fill byte pointed by separator */
651 *separator = 0;
652
653 /* fill SHA-256 DER value pointed by HashId */
654 memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
655
656 /* fill hash pointed by Digest */
657 for (i = 0; i < SHA256_BYTES; i++)
658 digest[i] = hash_val[i];
659}
660
661/*
662 * Reads and validates the ESBC client header.
663 * This function reads key and signature from the ESBC client header.
664 * If Scatter/Gather flag is on, lengths and offsets of images
665 * present as SG entries are also read. This function also checks
666 * whether the header is valid or not.
667 */
668static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
669{
gaurav ranac3a50422015-02-27 09:45:35 +0530670 struct fsl_secboot_img_hdr *hdr = &img->hdr;
Aneesh Bansalb3e98202015-12-08 13:54:29 +0530671 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
gaurav ranac3a50422015-02-27 09:45:35 +0530672 u8 *k, *s;
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530673 u32 ret = 0;
674
gaurav ranac3a50422015-02-27 09:45:35 +0530675 int key_found = 0;
676
677 /* check barker code */
678 if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
679 return ERROR_ESBC_CLIENT_HEADER_BARKER;
680
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530681 /* If Image Address is not passed as argument to function,
682 * then Address and Size must be read from the Header.
683 */
Saksham Jain04fcf522016-03-23 16:24:45 +0530684 if (*(img->img_addr_ptr) == 0) {
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530685 #ifdef CONFIG_ESBC_ADDR_64BIT
Saksham Jain04fcf522016-03-23 16:24:45 +0530686 *(img->img_addr_ptr) = hdr->pimg64;
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530687 #else
Saksham Jain04fcf522016-03-23 16:24:45 +0530688 *(img->img_addr_ptr) = hdr->pimg;
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530689 #endif
690 }
691
gaurav ranac3a50422015-02-27 09:45:35 +0530692 if (!hdr->img_size)
693 return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
694
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530695 img->img_size = hdr->img_size;
696
gaurav ranac3a50422015-02-27 09:45:35 +0530697 /* Key checking*/
698#ifdef CONFIG_KEY_REVOCATION
699 if (check_srk(img)) {
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530700 ret = read_validate_srk_tbl(img);
gaurav ranac3a50422015-02-27 09:45:35 +0530701 if (ret != 0)
702 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530703 key_found = 1;
704 }
705#endif
706
Tom Rini6fb86c12022-12-02 16:42:21 -0500707#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
gaurav ranac3a50422015-02-27 09:45:35 +0530708 if (!key_found && check_ie(img)) {
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530709 ret = read_validate_ie_tbl(img);
710 if (ret != 0)
711 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530712 key_found = 1;
713 }
714#endif
Saksham Jain6121f082016-03-23 16:24:34 +0530715#ifndef CONFIG_ESBC_HDR_LS
716/* Single Key Feature not available in LS ESBC Header */
gaurav ranac3a50422015-02-27 09:45:35 +0530717 if (key_found == 0) {
Aneesh Bansal24b8fae2015-12-08 14:14:13 +0530718 ret = read_validate_single_key(img);
719 if (ret != 0)
720 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530721 key_found = 1;
722 }
Saksham Jain6121f082016-03-23 16:24:34 +0530723#endif
724 if (!key_found)
725 return ERROR_KEY_TABLE_NOT_FOUND;
gaurav ranac3a50422015-02-27 09:45:35 +0530726
727 /* check signaure */
728 if (get_key_len(img) == 2 * hdr->sign_len) {
729 /* check signature length */
730 if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
731 (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
732 (hdr->sign_len == KEY_SIZE_BYTES)))
733 return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
734 } else {
735 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
736 }
737
738 memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
Saksham Jain6121f082016-03-23 16:24:34 +0530739/* No SG support in LS-CH3 */
740#ifndef CONFIG_ESBC_HDR_LS
gaurav ranac3a50422015-02-27 09:45:35 +0530741 /* No SG support */
742 if (hdr->sg_flag)
743 return ERROR_ESBC_CLIENT_HEADER_SG;
Saksham Jain6121f082016-03-23 16:24:34 +0530744#endif
gaurav ranac3a50422015-02-27 09:45:35 +0530745
746 /* modulus most significant bit should be set */
747 k = (u8 *)&img->img_key;
748
749 if ((k[0] & 0x80) == 0)
750 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
751
752 /* modulus value should be odd */
753 if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
754 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
755
756 /* Check signature value < modulus value */
757 s = (u8 *)&img->img_sign;
758
759 if (!(memcmp(s, k, hdr->sign_len) < 0))
760 return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
761
762 return ESBC_VALID_HDR;
763}
764
765static inline int str2longbe(const char *p, ulong *num)
766{
767 char *endptr;
768 ulong tmp;
769
770 if (!p) {
771 return 0;
772 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600773 tmp = hextoul(p, &endptr);
gaurav ranac3a50422015-02-27 09:45:35 +0530774 if (sizeof(ulong) == 4)
775 *num = cpu_to_be32(tmp);
776 else
777 *num = cpu_to_be64(tmp);
778 }
779
780 return *p != '\0' && *endptr == '\0';
781}
Aneesh Bansal9cd689c2015-12-08 14:14:14 +0530782/* Function to calculate the ESBC Image Hash
783 * and hash from Digital signature.
784 * The Two hash's are compared to yield the
785 * result of signature validation.
786 */
787static int calculate_cmp_img_sig(struct fsl_secboot_img_priv *img)
788{
789 int ret;
790 uint32_t key_len;
791 struct key_prop prop;
792#if !defined(USE_HOSTCC)
793 struct udevice *mod_exp_dev;
794#endif
795 ret = calc_esbchdr_esbc_hash(img);
796 if (ret)
797 return ret;
798
799 /* Construct encoded hash EM' wrt PKCSv1.5 */
800 construct_img_encoded_hash_second(img);
801
802 /* Fill prop structure for public key */
803 memset(&prop, 0, sizeof(struct key_prop));
804 key_len = get_key_len(img) / 2;
805 prop.modulus = img->img_key;
806 prop.public_exponent = img->img_key + key_len;
807 prop.num_bits = key_len * 8;
808 prop.exp_len = key_len;
809
Gaurav Jain119c7002022-06-23 16:31:35 +0530810#if defined(CONFIG_SPL_BUILD)
811 ret = device_bind_driver(NULL, "fsl_rsa_mod_exp", "fsl_rsa_mod_exp", NULL);
812 if (ret) {
813 printf("Couldn't bind fsl_rsa_mod_exp driver (%d)\n", ret);
814 return -EINVAL;
815 }
816#endif
Aneesh Bansal9cd689c2015-12-08 14:14:14 +0530817 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
818 if (ret) {
819 printf("RSA: Can't find Modular Exp implementation\n");
820 return -EINVAL;
821 }
822
823 ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
824 &prop, img->img_encoded_hash);
825 if (ret)
826 return ret;
827
828 /*
829 * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
830 * memcmp returns zero on success
831 * memcmp returns non-zero on failure
832 */
833 ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
834 img->hdr.sign_len);
835
836 if (ret)
837 return ERROR_ESBC_CLIENT_HASH_COMPARE_EM;
838
839 return 0;
840}
Udit Agarwal990a9972017-02-09 21:36:11 +0530841/* Function to initialize img priv and global data structure
842 */
843static int secboot_init(struct fsl_secboot_img_priv **img_ptr)
844{
845 *img_ptr = malloc(sizeof(struct fsl_secboot_img_priv));
846
847 struct fsl_secboot_img_priv *img = *img_ptr;
848
849 if (!img)
850 return -ENOMEM;
851 memset(img, 0, sizeof(struct fsl_secboot_img_priv));
852
Tom Rini6fb86c12022-12-02 16:42:21 -0500853#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
Udit Agarwal990a9972017-02-09 21:36:11 +0530854 if (glb.ie_addr)
855 img->ie_addr = glb.ie_addr;
856#endif
857 return 0;
858}
859
Saksham Jain04fcf522016-03-23 16:24:45 +0530860/* haddr - Address of the header of image to be validated.
861 * arg_hash_str - Option hash string. If provided, this
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400862 * overrides the key hash in the SFP fuses.
Saksham Jain04fcf522016-03-23 16:24:45 +0530863 * img_addr_ptr - Optional pointer to address of image to be validated.
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400864 * If non zero addr, this overrides the addr of image in header,
Saksham Jain04fcf522016-03-23 16:24:45 +0530865 * otherwise updated to image addr in header.
866 * Acts as both input and output of function.
867 * This pointer shouldn't be NULL.
868 */
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530869int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
Saksham Jain04fcf522016-03-23 16:24:45 +0530870 uintptr_t *img_addr_ptr)
gaurav ranac3a50422015-02-27 09:45:35 +0530871{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500872 struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
gaurav ranac3a50422015-02-27 09:45:35 +0530873 ulong hash[SHA256_BYTES/sizeof(ulong)];
874 char hash_str[NUM_HEX_CHARS + 1];
gaurav ranac3a50422015-02-27 09:45:35 +0530875 struct fsl_secboot_img_priv *img;
876 struct fsl_secboot_img_hdr *hdr;
877 void *esbc;
878 int ret, i, hash_cmd = 0;
879 u32 srk_hash[8];
gaurav ranac3a50422015-02-27 09:45:35 +0530880
Tom Rini27fcd312022-06-17 16:24:32 -0400881 if (strlen(arg_hash_str) != 0) {
Aneesh Bansal2b379bf2015-12-08 14:14:12 +0530882 const char *cp = arg_hash_str;
gaurav ranac3a50422015-02-27 09:45:35 +0530883 int i = 0;
884
885 if (*cp == '0' && *(cp + 1) == 'x')
886 cp += 2;
887
888 /* The input string expected is in hex, where
889 * each 4 bits would be represented by a hex
890 * sha256 hash is 256 bits long, which would mean
891 * num of characters = 256 / 4
892 */
893 if (strlen(cp) != SHA256_NIBBLES) {
894 printf("%s is not a 256 bits hex string as expected\n",
Aneesh Bansal2b379bf2015-12-08 14:14:12 +0530895 arg_hash_str);
gaurav ranac3a50422015-02-27 09:45:35 +0530896 return -1;
897 }
898
899 for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
900 strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
901 NUM_HEX_CHARS);
902 hash_str[NUM_HEX_CHARS] = '\0';
903 if (!str2longbe(hash_str, &hash[i])) {
904 printf("%s is not a 256 bits hex string ",
Aneesh Bansal2b379bf2015-12-08 14:14:12 +0530905 arg_hash_str);
gaurav ranac3a50422015-02-27 09:45:35 +0530906 return -1;
907 }
908 }
909
910 hash_cmd = 1;
911 }
912
Udit Agarwal990a9972017-02-09 21:36:11 +0530913 ret = secboot_init(&img);
914 if (ret)
915 goto exit;
gaurav ranac3a50422015-02-27 09:45:35 +0530916
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530917 /* Update the information in Private Struct */
gaurav ranac3a50422015-02-27 09:45:35 +0530918 hdr = &img->hdr;
Aneesh Bansal2b379bf2015-12-08 14:14:12 +0530919 img->ehdrloc = haddr;
Saksham Jain04fcf522016-03-23 16:24:45 +0530920 img->img_addr_ptr = img_addr_ptr;
Aneesh Bansal85921ba2015-12-08 14:14:15 +0530921 esbc = (u8 *)img->ehdrloc;
gaurav ranac3a50422015-02-27 09:45:35 +0530922
923 memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
924
925 /* read and validate esbc header */
926 ret = read_validate_esbc_client_header(img);
927
928 if (ret != ESBC_VALID_HDR) {
929 fsl_secboot_handle_error(ret);
930 goto exit;
931 }
932
933 /* SRKH present in SFP */
934 for (i = 0; i < NUM_SRKH_REGS; i++)
935 srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
936
937 /*
938 * Calculate hash of key obtained via offset present in
939 * ESBC uboot client hdr
940 */
941 ret = calc_img_key_hash(img);
942 if (ret) {
943 fsl_secblk_handle_error(ret);
944 goto exit;
945 }
946
947 /* Compare hash obtained above with SRK hash present in SFP */
948 if (hash_cmd)
949 ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
950 else
951 ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
952
Tom Rini6fb86c12022-12-02 16:42:21 -0500953#if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
gaurav ranac3a50422015-02-27 09:45:35 +0530954 if (!hash_cmd && check_ie(img))
955 ret = 0;
956#endif
957
958 if (ret != 0) {
959 fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
960 goto exit;
961 }
962
Aneesh Bansal9cd689c2015-12-08 14:14:14 +0530963 ret = calculate_cmp_img_sig(img);
gaurav ranac3a50422015-02-27 09:45:35 +0530964 if (ret) {
Aneesh Bansal9cd689c2015-12-08 14:14:14 +0530965 fsl_secboot_handle_error(ret);
gaurav ranac3a50422015-02-27 09:45:35 +0530966 goto exit;
967 }
968
gaurav ranac3a50422015-02-27 09:45:35 +0530969exit:
Udit Agarwal990a9972017-02-09 21:36:11 +0530970 /* Free Img as it was malloc'ed*/
971 free(img);
Aneesh Bansal2b379bf2015-12-08 14:14:12 +0530972 return ret;
gaurav ranac3a50422015-02-27 09:45:35 +0530973}