blob: 30bf2b3c1537d2bb43de67fa4713ffac4b9b5786 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*******************************************************************************
2 * Copyright (C) 2016, ZXIC Corporation.
3 *
4 * File Name:
5 * File Mark:
6 * Description:
7 * Others:
8 * Version: 1.0
9 * Author: zxic
10 * Date: 2016.12.15
11 * modify
12 ********************************************************************************/
13
14/****************************************************************************
15* Include files
16****************************************************************************/
17#include <malloc.h>
18#include <errno.h>
19#include <asm/io.h>
20#include <linux/mtd/mtd.h>
21#include <asm-generic/ioctl.h>
22#include <config.h>
23#include <common.h>
24#include <command.h>
25
26#include <../drivers/hash/drv_hash.h>
27#include <../drivers/rsa/drv_rsa.h>
xf.liaa4d92f2023-09-13 00:18:58 -070028#include <../drivers/hash/sha256.h>
lh9ed821d2023-04-07 01:36:19 -070029#include <secure_verify.h>
30#include <asm/arch/efuse.h>
xf.liaa4d92f2023-09-13 00:18:58 -070031#include <boot_mode.h>
lh9ed821d2023-04-07 01:36:19 -070032
33/****************************************************************************
34* Local Macros
35****************************************************************************/
36#define E_N_LEN 256
37#define HASH_LEN 128
38#define reg32(addr) (*(volatile unsigned long *)(addr))
39
40/****************************************************************************
41* Local Types
42****************************************************************************/
43
44
45/****************************************************************************
46* Global Value
47****************************************************************************/
48/* Disable Secure Verify as Default. */
49unsigned int guiEfuseStatus = 1;
50unsigned char g_ddr_size_flag = 0;
51
52static unsigned char gbEfuseReadFlag = 0;
53
xf.liaa4d92f2023-09-13 00:18:58 -070054otp_struct otpInfo;
55unsigned int guiOtpStatus = 1;
56extern int rootfs_flag;
57extern int m0_flag;
58extern int zsp_flag;
59extern int g_iftype;
60extern unsigned char g_nor_flag;
61
lh9ed821d2023-04-07 01:36:19 -070062/****************************************************************************
63* Local Funcs
64****************************************************************************/
65
66static void efuse_read_prepare(void)
67{
68 //start read efuse all 256bit
69 while((reg32(SYS_EFUSE_BASE + 0x4) & 1) == 1);// bit0=1 ctl is busy
70 reg32(SYS_EFUSE_BASE + 0x4) = 1;
71 while((reg32(SYS_EFUSE_BASE + 0x14) & 2) == 0);//bit1=0 read not over
72}
73
74static void efuse_program_32bit(u32 bit_offset, u32 data)
75{
76 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
77 reg32(SYS_EFUSE_BASE+0x8) = bit_offset;
78 reg32(SYS_EFUSE_BASE+0xc) = data;
79 reg32(SYS_EFUSE_BASE+0x4) = (0<<4)|(2<<1)|1;//cs0 program 32bit
80 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
81 reg32(SYS_EFUSE_BASE+0x4) = (1<<4)|(2<<1)|1;//cs1 program 32bit
82}
83
84void efuse_program_secure_en(u32 en)
85{
86 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
87 reg32(SYS_EFUSE_BASE+0x8) = 0; //bit[7:0] secure_boot_en
88 if(en)
89 reg32(SYS_EFUSE_BASE+0xc) = 0xFF;
90 reg32(SYS_EFUSE_BASE+0x4) = (0<<4)|(2<<1)|1;//cs0 program 32bit
91 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
92 reg32(SYS_EFUSE_BASE+0x4) = (1<<4)|(2<<1)|1;//cs1 program 32bit
93}
94
95void efuse_program_chip_flag(u32 flag)
96{
97 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
98 reg32(SYS_EFUSE_BASE+0x8) = 0; //bit[31:8] secure_chip_flag
99
100 if(flag&0xFF != 0)
101 reg32(SYS_EFUSE_BASE+0xc) = 0;
102 else
103 reg32(SYS_EFUSE_BASE+0xc) = flag;
104
105 reg32(SYS_EFUSE_BASE+0x4) = (0<<4)|(2<<1)|1;//cs0 program 32bit
106 while((reg32(SYS_EFUSE_BASE+0x4)&1)==1);// bit0=1 ctl is busy
107 reg32(SYS_EFUSE_BASE+0x4) = (1<<4)|(2<<1)|1;//cs1 program 32bit
108}
109
110void efuse_program_puk_hash(u32 *key)
111{
112 efuse_program_32bit(32, key[0]);
113 efuse_program_32bit(64, key[1]);
114 efuse_program_32bit(96, key[2]);
115 efuse_program_32bit(128, key[3]);
116}
117#if 0
118/**-------------------------------------------------------------------------------------------------------------------@n
119 * º¯ÊýÃû³Æ: chip_is_spe
120 * ¹¦ÄÜÃèÊö: ÓÃÓÚÅжÏоƬÊÇ·ñÎª×¨ÍøÐ¾Æ¬
121 * ·µ »Ø Öµ: 0,¹«ÍøÐ¾Æ¬ 1,×¨ÍøÐ¾Æ¬
122 *--------------------------------------------------------------------------------------------------------------------*/
123u32 chip_is_spe(void)
124{
125 u8 tmp_chip_flag[3] = {0x53, 0x50, 0x45}; /*SPE*/
126 u32 efuse_tmp_buf[5] = {0};
127 efuse_struct *psEfuseInfo = NULL;
128
129 psEfuseInfo = (efuse_struct *)efuse_tmp_buf;
130 memcpy(psEfuseInfo, EFUSE_RAM_BASE, sizeof(efuse_struct));
131
132 if(memcmp(psEfuseInfo->chip_flag,tmp_chip_flag,3))
133 /*chip com*/
134 return 0;
135
136 /*chip spe*/
137 return 1;
138}
139#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700140
141/*
142* Convert BIGENDIAN To SMALLENDIAN.
143*/
144void BIG2SMALL(u32 *pBuffer, u32 uiLen)
145{
146 u32 uiLoop;
147 u32 uiTmp = 0;
148
149 for (uiLoop = 0; uiLoop < uiLen/sizeof(u32); uiLoop++)
150 {
151 uiTmp = pBuffer[uiLoop];
152 pBuffer[uiLoop] = 0;
153 pBuffer[uiLoop] += (uiTmp & 0x000000FF) << 24;
154 pBuffer[uiLoop] += (uiTmp & 0x0000FF00) << 8;
155 pBuffer[uiLoop] += (uiTmp & 0x00FF0000) >> 8;
156 pBuffer[uiLoop] += (uiTmp & 0xFF000000) >> 24;
157 }
158}
159
160void get_otp_secure_verify_status(void)
161{
162 u32 uiLen;
163 otp_struct *psEfuseInfo = NULL;
164
165 /*
166 * 0. Èç¹ûsecure flag²»µÈÓÚ0£¬Í˳ö°²È«boot¡£
167 */
168 psEfuseInfo = (otp_struct*)&otpInfo;
169 if(psEfuseInfo->secure_flag != 0)
170 {
171 guiOtpStatus = 1; //Disable.
172 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag!=0xFF, SecureVerify->Disable.\n");
173 return ;
174 }
175
176 /*
177 * 1.´ÓefuseÖжÁ³öpuk_hash[255:0], ÅжÏÈç¹ûÈ«²¿Îª0Í˳ö°²È«boot¡£
178 */
179 for(uiLen = 0; uiLen < 8; uiLen++)
180 {
181 if(psEfuseInfo->puk_hash[uiLen] != 0)
182 {
183 break;
184 }
185 if(uiLen == 7)
186 {
187 guiOtpStatus = 1; //Disable.
188 BOOT_PRINTF(UBOOT_NOTICE, "PubKey_HASH=Invalid, SecureVerify->Disable.\n");
189 return ;
190 }
191 }
192 BOOT_PRINTF(UBOOT_NOTICE, "All Flag & Param is Valid, SecureVerify->ENABLE.\n");
193 guiOtpStatus = 0; //Enable.
194}
195
xf.li9d1a0e12023-09-20 01:43:20 -0700196void efuse_get_data(void)
lh9ed821d2023-04-07 01:36:19 -0700197{
198 efuse_struct *psEfuseInfo = NULL;
199 u32 uiLen = 0;
200
201 /* Secure Verify. 1->Disable, 0->Enable. */
202 if((reg32(EFUSE_BYPASS) & 1) == 1)
203 {
204 guiEfuseStatus = 1; //Disable.
205 BOOT_PRINTF(UBOOT_NOTICE, "EFUSE_BYPASS=1, SecureVerify->Disable.\n");
206 return ;
207 }
208
209 /* step 0*/
210 psEfuseInfo = (efuse_struct*)EFUSE_RAM_BASE;
211
212 /* get chip flag */
213 if(((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_WINBD_256M_DDR)
214 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_WINBD_256M_DDR)
215 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_UNILC_256M_DDR)
216 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_UNILC_256M_DDR)
217 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_APM_256M_DDR)
218 || ((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_APM_256M_DDR))
219 {
220 g_ddr_size_flag = CHIP_DDR_IS_32M;
221 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
222 }
223 else if(((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_UNILC_512M_DDR)
224 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_APM_512M_DDR)
225 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_GW_ESMT_512M_DDR)
226 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_UNILC_512M_DDR)
227 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_AZW_UNILC_512M_DDR)
228 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_APM_512M_DDR)
229 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECO_ZW_ESMT_512M_DDR))
230 {
231 g_ddr_size_flag = CHIP_DDR_IS_64M;
232 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
233 }
lh758261d2023-07-13 05:52:04 -0700234 else if(((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOSC_GW_NYC_2G_DDR)
xf.liaa4d92f2023-09-13 00:18:58 -0700235 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOGG_GW_NYC_2G_DDR)
236 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOGG_GW_NYC_NOR_2G_DDR)
237 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOSC_GW_NYC_NOR_2G_DDR))
lh9ed821d2023-04-07 01:36:19 -0700238 {
239 g_ddr_size_flag = CHIP_DDR_IS_256M;
240 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
xf.lica7c3fc2024-02-21 22:59:57 -0800241 }
242 else if(((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOSCC_GW_NYB_4G_DDR)
243 ||((psEfuseInfo->secure_flag >> 8) == ZX297520V3ECOGG_GW_NYB_4G_DDR))
244 {
245 g_ddr_size_flag = CHIP_DDR_IS_512M;
246 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
lh9ed821d2023-04-07 01:36:19 -0700247 }
248 else
249 {
250 g_ddr_size_flag = CHIP_DDR_IS_128M;
251 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
252 }
253
254 if((psEfuseInfo->secure_flag & 0xFF) != 0xFF)
255 {
256 guiEfuseStatus = 1; //Disable.
257 BOOT_PRINTF(UBOOT_NOTICE, "secure_flag!=0xFF, SecureVerify->Disable.\n");
258 return ;
259 }
260
261 /* step 1*/
262 for(uiLen = 0; uiLen < 4; uiLen++)
263 {
264 if(psEfuseInfo->puk_hash[uiLen] != 0)
265 {
266 break;
267 }
268 if(uiLen == 3)
269 {
270 guiEfuseStatus = 1; //Disable.
271 BOOT_PRINTF(UBOOT_NOTICE, "PubKey_HASH=Invalid, SecureVerify->Disable.\n");
272 return ;
273 }
274 }
275 BOOT_PRINTF(UBOOT_NOTICE, "All Flag & Param is Valid, SecureVerify->ENABLE.\n");
276 guiEfuseStatus = 0; //Enable.
277}
278
279static u8 efuse_cmp_word(u32* src, u32* dst, u32 cnt)
280{
281 u32 i;
282 for(i = 0; i < cnt; i++)
283 {
284 if(src[i] != dst[i])
285 {
286 return 1;
287 }
288 }
289 return 0;
290}
291
xf.liaa4d92f2023-09-13 00:18:58 -0700292u8 rootfs_secure_verify(u32 addr)
293{
294 if(g_nor_flag == 1)
295 {
296
297 u32 uiLen = 0;
298 u32 uiRet = -1;
299 u32 uiLoop = 0;
300 image_header_t *puiLegacyRootfsAddr = NULL;
301 sImageNewHeader *psRootfsHeader = NULL;
302 u32 *puiDataLoadAddr = NULL;
303 u32 *puiArrPubKeyEN = NULL;
304 u32 *puiArrPubKey = NULL;
305 u32 *puiArrHASH = NULL;
306 otp_struct *psEfuseInfo = NULL;
307 u32 uiHashResArr[8] = {0};
308 u32 uiHashResLen = 0;
309 u32 uiHashVerifySize = 0;
310 u32 uiRsaResArr[64] = {0};
311 u32 puiArrPubKeyE[64] = {0};
312 T_Rsa_Paramter sRSAInput;
313 u32 *puiRsaResAddr = NULL;
314 u32 sRamKey[9] = {0x0,0x33fa3e31,0x90d8d15a,0x073cee04,0x82ac24aa,0x2262748e,0x1a3663c7,0x4b603f6f,0xd098e0d8};
315 if(NULL == addr)
316 {
317 BOOT_PRINTF(UBOOT_ERR, "Bad Parameter(Empty Pointer)!\n");
318 return -1;
319 }
320
321 psRootfsHeader = (sImageNewHeader *)addr;
322 puiLegacyRootfsAddr = (image_header_t *)(addr + sizeof(sImageNewHeader));
323 uiHashVerifySize = ___htonl(puiLegacyRootfsAddr->ih_size) + sizeof(image_header_t);
324
325 //psEfuseInfo = (otp_struct*)sRamKey;
326 psEfuseInfo = (otp_struct*)&otpInfo;
327
328 /*
329 * step 0
330 */
331 puiArrPubKeyEN = psRootfsHeader->uiPubKeyRsaE;
332 uiLen = 380;
333 puiArrPubKey = &(psRootfsHeader->uiPubKeyRsaELen);
334 sha256_csum_wd((const unsigned char *)puiArrPubKey, uiLen, (unsigned char *)uiHashResArr);
335 uiHashResLen = 8;
336 /*´óС¶Ëת»»*/
337 BIG2SMALL(uiHashResArr, 32);
338
339 BOOT_PRINTF(UBOOT_DBG, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
340
341 if(efuse_cmp_word(psEfuseInfo->puk_hash, uiHashResArr, uiHashResLen))
342 {
343 BOOT_PRINTF(UBOOT_ERR, "PubKey Hash Verify -> Failed !\n");
344 return 1;
345 }
346 puiArrHASH = psRootfsHeader->uiHashY;
347
348 /*
349 * step 1
350 */
351 memcpy(puiArrPubKeyE+63, puiArrPubKeyEN, 4);
352 sRSAInput.udCalMode = RSA_MOD_EXPO_WITH_INIT;
353 sRSAInput.udNbitLen = 2048;
354 sRSAInput.udEbitLen = 2048;
355 sRSAInput.pudInputM = puiArrHASH;
356 sRSAInput.pudInputE = puiArrPubKeyE;
357 sRSAInput.pudInputN = psRootfsHeader->uiPubKeyRsaN;
358 sRSAInput.pudOutputP = uiRsaResArr;
359
360 uiRet = Rsa_Calculate(sRSAInput);
361 if(uiRet != 0)
362 {
363 BOOT_PRINTF(UBOOT_ERR, "Rsa_Calculate ERROR(0x%x)!\n", uiRet);
364 return 2;
365 }
366 puiRsaResAddr = sRSAInput.pudOutputP + (64 - uiHashResLen);
367
368 /*
369 * step 2
370 */
371 uiLen = uiHashVerifySize;
372 puiDataLoadAddr = CONFIG_SYS_SDRAM_ROOTFS_BASE - sizeof(image_header_t);
373
374 /* Cleanup Output Buffer. */
375 uiHashResLen = 0;
376 memset(uiHashResArr, 0, 8*sizeof(uiHashResArr[0]));
377
378 sha256_csum_wd((const unsigned char *)puiDataLoadAddr, uiLen, (unsigned char *)uiHashResArr);
379 uiHashResLen = 8;
380 /*´óС¶Ëת»»*/
381 BIG2SMALL(uiHashResArr, 32);
382
383 if(efuse_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen))
384 {
385 BOOT_PRINTF(UBOOT_ERR, "LegacyRootfs Verify -> Failed !\n");
386 return 3;
387 }
388
389 }
390 else
391 {
392
393 u32 uiLen = 0;
394 u32 uiRet = -1;
395 u32 uiLoop = 0;
396 image_header_t *puiLegacyRootfsAddr = NULL;
397 sImageNewHeader *psRootfsHeader = NULL;
398 u32 *puiDataLoadAddr = NULL;
399 u32 *puiArrPubKeyEN = NULL;
400 u32 *puiArrHASH = NULL;
401 efuse_struct *psEfuseInfo = NULL;
402 u32 uiHashResArr[4] = {0};
403 u32 uiHashResLen = 0;
404 u32 uiHashVerifySize = 0;
405 u32 uiRsaResArr[32] = {0};
406 u32 puiArrPubKeyE[32] = {0};
407 u32 puiArrPubKey[64] = {0};
408 T_Rsa_Paramter sRSAInput;
409 u32 *puiRsaResAddr = NULL;
xf.li9d1a0e12023-09-20 01:43:20 -0700410
411 if(NULL == addr)
xf.liaa4d92f2023-09-13 00:18:58 -0700412 {
413 BOOT_PRINTF(UBOOT_ERR, "Bad Parameter(Empty Pointer)!\n");
414 return -1;
415 }
416
417 psRootfsHeader = (sImageNewHeader *)addr;
418 puiLegacyRootfsAddr = (image_header_t *)(addr + sizeof(sImageNewHeader));
419 uiHashVerifySize = ___htonl(puiLegacyRootfsAddr->ih_size) + sizeof(image_header_t);
420
421 psEfuseInfo = (efuse_struct*)EFUSE_RAM_BASE;
422
423 /*
424 * step 0
425 */
426
427 uiLen = E_N_LEN;
428 puiArrPubKeyEN = psRootfsHeader->uiPubKeyRsaE;
429 memcpy(puiArrPubKeyE+31, puiArrPubKeyEN, 4);
430 memcpy(puiArrPubKey+31, puiArrPubKeyEN, 4);
431 memcpy(puiArrPubKey+32, psRootfsHeader->uiPubKeyRsaN, 128);
432
433 Hash_Calculate(HASH_MODE_MD5, HASH_SMALL_ENDIAN, puiArrPubKey, uiLen, NULL, 0, uiHashResArr, &uiHashResLen);
434
435 BOOT_PRINTF(UBOOT_DBG, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
436
437 if(efuse_cmp_word(psEfuseInfo->puk_hash, uiHashResArr, uiHashResLen))
438 {
439 BOOT_PRINTF(UBOOT_ERR, "PubKey Hash Verify -> Failed !\n");
440 return 1;
441 }
442 puiArrHASH = psRootfsHeader->uiHashY;
443
444 /*
445 * step 1
446 */
447 sRSAInput.udCalMode = RSA_MOD_EXPO_WITH_INIT;
448 sRSAInput.udNbitLen = 1024;
449 sRSAInput.udEbitLen = 1024;
450 sRSAInput.pudInputM = puiArrHASH;
451 sRSAInput.pudInputE = puiArrPubKeyE;
452 sRSAInput.pudInputN = psRootfsHeader->uiPubKeyRsaN;
453 sRSAInput.pudOutputP = uiRsaResArr;
454
455 uiRet = Rsa_Calculate(sRSAInput);
456 if(uiRet != 0)
457 {
458 BOOT_PRINTF(UBOOT_ERR, "Rsa_Calculate ERROR(0x%x)!\n", uiRet);
459 return 2;
460 }
461 puiRsaResAddr = sRSAInput.pudOutputP + (32 - uiHashResLen);
462
463 /*
464 * step 2
465 */
466 uiLen = uiHashVerifySize;
467 puiDataLoadAddr = CONFIG_SYS_SDRAM_ROOTFS_BASE - sizeof(image_header_t);
468
469 /* Cleanup Output Buffer. */
470 uiHashResLen = 0;
471 memset(uiHashResArr, 0, 4*sizeof(uiHashResArr[0]));
472
473 uiRet = Hash_Calculate(HASH_MODE_MD5, HASH_SMALL_ENDIAN, puiDataLoadAddr, uiLen, NULL, 0, uiHashResArr, &uiHashResLen);
474 if(uiRet != 0)
475 {
476 BOOT_PRINTF(UBOOT_ERR, "Hash_Calculate ERROR(0x%x)!\n", uiRet);
477 return -1;
478 }
479
480 if(efuse_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen))
481 {
482 BOOT_PRINTF(UBOOT_ERR, "LegacyRootfs Verify -> Failed !\n");
483 return 3;
484 }
485 }
486
487 return 0;
488}
489
lh9ed821d2023-04-07 01:36:19 -0700490u8 secure_verify(u32 addr)
491{
xf.liaa4d92f2023-09-13 00:18:58 -0700492 if(g_nor_flag != 1){
493 u32 uiLen = 0;
494 u32 uiRet = -1;
495 u32 uiLoop = 0;
496 image_header_t *puiLegacyImgAddr = NULL;
497 sImageNewHeader *psImageHeader = NULL;
498 u32 *puiDataLoadAddr = NULL;
499 u32 *puiArrPubKeyEN = NULL;
500 u32 *puiArrHASH = NULL;
lh9ed821d2023-04-07 01:36:19 -0700501
xf.liaa4d92f2023-09-13 00:18:58 -0700502 efuse_struct *psEfuseInfo = NULL;
lh9ed821d2023-04-07 01:36:19 -0700503
xf.liaa4d92f2023-09-13 00:18:58 -0700504 u32 uiHashResArr[4] = {0};
505 u32 uiHashResLen = 0;
506 u32 uiHashVerifySize = 0;
507 u32 uiRsaResArr[32] = {0};
508 u32 puiArrPubKeyE[32] = {0};
509 u32 puiArrPubKey[64] = {0};
lh9ed821d2023-04-07 01:36:19 -0700510
xf.liaa4d92f2023-09-13 00:18:58 -0700511 T_Rsa_Paramter sRSAInput;
512 u32 *puiRsaResAddr = NULL;
lh9ed821d2023-04-07 01:36:19 -0700513
xf.liaa4d92f2023-09-13 00:18:58 -0700514 if(NULL == addr)
515 {
516 BOOT_PRINTF(UBOOT_ERR, "Bad Parameter(Empty Pointer)!\n");
517 return -1;
518 }
519
520 psImageHeader = (sImageNewHeader *)addr;
521
522 if(rootfs_flag == 1)
523 {
524 puiLegacyImgAddr = (image_header_t *)(addr + sizeof(sImageNewHeader) + sizeof(sImageNewHeader) + sizeof(image_header_t));
525 uiHashVerifySize = ___htonl(puiLegacyImgAddr->ih_size) + sizeof(image_header_t) + sizeof(image_header_t) + sizeof(sImageNewHeader);
526 }
527 else
528 {
529 puiLegacyImgAddr = (image_header_t *)(addr + sizeof(sImageNewHeader));
530 uiHashVerifySize = ___htonl(puiLegacyImgAddr->ih_size) + sizeof(image_header_t);
531 }
532
533 psEfuseInfo = (efuse_struct*)EFUSE_RAM_BASE;
534
535 BOOT_PRINTF(UBOOT_NOTICE, "uiHashVerifySize=0x%x, EFUSE_RAM_BASE=0x%x.\n", uiHashVerifySize, EFUSE_RAM_BASE);
536
537 /*
538 * step 0
539 */
540 uiLen = E_N_LEN;
541 puiArrPubKeyEN = psImageHeader->uiPubKeyRsaE;
542 memcpy(puiArrPubKeyE+31, puiArrPubKeyEN, 4);
543 memcpy(puiArrPubKey+31, puiArrPubKeyEN, 4);
544 memcpy(puiArrPubKey+32, psImageHeader->uiPubKeyRsaN, 128);
545
546 Hash_Calculate(HASH_MODE_MD5, HASH_SMALL_ENDIAN, puiArrPubKey, uiLen, NULL, 0, uiHashResArr, &uiHashResLen);
547
548 BOOT_PRINTF(UBOOT_DBG, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
549
550 if(efuse_cmp_word(psEfuseInfo->puk_hash, uiHashResArr, uiHashResLen))
551 {
552 BOOT_PRINTF(UBOOT_ERR, "PubKey Hash Verify -> Failed !\n");
553 return 1;
554 }
555 puiArrHASH = psImageHeader->uiHashY;
556
557 /*
558 * step 1
559 */
560 sRSAInput.udCalMode = RSA_MOD_EXPO_WITH_INIT;
561 sRSAInput.udNbitLen = 1024;
562 sRSAInput.udEbitLen = 1024;
563 sRSAInput.pudInputM = puiArrHASH;
564 sRSAInput.pudInputE = puiArrPubKeyE;
565 sRSAInput.pudInputN = psImageHeader->uiPubKeyRsaN;
566 sRSAInput.pudOutputP = uiRsaResArr;
567
568 uiRet = Rsa_Calculate(sRSAInput);
569 if(uiRet != 0)
570 {
571 BOOT_PRINTF(UBOOT_ERR, "Rsa_Calculate ERROR(0x%x)!\n", uiRet);
572 return 2;
573 }
574 puiRsaResAddr = sRSAInput.pudOutputP + (32 - uiHashResLen);
575
576 /*
577 * step 2
578 */
579 uiLen = uiHashVerifySize;
580 if(rootfs_flag == 1)
581 {
582 puiDataLoadAddr = ___htonl(puiLegacyImgAddr->ih_load) - sizeof(image_header_t) - sizeof(image_header_t) - sizeof(sImageNewHeader);
583 }
584 else
585 {
586 if(1 == m0_flag || 1 == zsp_flag){
587 puiDataLoadAddr = (uchar *)addr + sizeof(sImageNewHeader);
588
589 }else{
590 puiDataLoadAddr = ___htonl(puiLegacyImgAddr->ih_load) - sizeof(image_header_t);
591 }
592
593 }
594
595 /* Cleanup Output Buffer. */
596 uiHashResLen = 0;
597 memset(uiHashResArr, 0, 4*sizeof(uiHashResArr[0]));
598
599 uiRet = Hash_Calculate(HASH_MODE_MD5, HASH_SMALL_ENDIAN, puiDataLoadAddr, uiLen, NULL, 0, uiHashResArr, &uiHashResLen);
600 if(uiRet != 0)
601 {
602 BOOT_PRINTF(UBOOT_ERR, "Hash_Calculate ERROR(0x%x)!\n", uiRet);
603 return -1;
604 }
605
606 if(efuse_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen))
607 {
608 BOOT_PRINTF(UBOOT_ERR, "LegacyImg Verify -> Failed !\n");
609 return 3;
610 }
611
lh9ed821d2023-04-07 01:36:19 -0700612 }
xf.liaa4d92f2023-09-13 00:18:58 -0700613 else
614 {
615 u32 uiLen = 0;
616 u32 uiRet = -1;
617 u32 uiLoop = 0;
618 image_header_t *puiLegacyImgAddr = NULL;
619 sImageNewHeader *psImageHeader = NULL;
620 u32 *puiDataLoadAddr = NULL;
621 u32 *puiArrPubKeyEN = NULL;
622 u32 *puiArrPubKey = NULL;
623 u32 *puiArrHASH = NULL;
624 otp_struct *psEfuseInfo = NULL;
625 u32 uiHashResArr[8] = {0};
626 u32 uiHashResLen = 0;
627 u32 uiHashVerifySize = 0;
628 u32 uiRsaResArr[64] = {0};
629 u32 puiArrPubKeyE[64] = {0};
630 T_Rsa_Paramter sRSAInput;
631 u32 *puiRsaResAddr = NULL;
632 u32 sRamKey[9] = {0x0,0x33fa3e31,0x90d8d15a,0x073cee04,0x82ac24aa,0x2262748e,0x1a3663c7,0x4b603f6f,0xd098e0d8};
633 if(NULL == addr)
634 {
635 BOOT_PRINTF(UBOOT_ERR, "Bad Parameter(Empty Pointer)!\n");
636 return -1;
637 }
lh9ed821d2023-04-07 01:36:19 -0700638
xf.liaa4d92f2023-09-13 00:18:58 -0700639 psImageHeader = (sImageNewHeader *)addr;
lh9ed821d2023-04-07 01:36:19 -0700640
xf.liaa4d92f2023-09-13 00:18:58 -0700641 if(rootfs_flag == 1)
642 {
643 puiLegacyImgAddr = (image_header_t *)(addr + sizeof(sImageNewHeader) + sizeof(sImageNewHeader) + sizeof(image_header_t));
644 uiHashVerifySize = ___htonl(puiLegacyImgAddr->ih_size) + sizeof(image_header_t) + sizeof(image_header_t) + sizeof(sImageNewHeader);
645 }
646 else
647 {
648 puiLegacyImgAddr = (image_header_t *)(addr + sizeof(sImageNewHeader));
649 uiHashVerifySize = ___htonl(puiLegacyImgAddr->ih_size) + sizeof(image_header_t);
650 }
lh9ed821d2023-04-07 01:36:19 -0700651
xf.liaa4d92f2023-09-13 00:18:58 -0700652 psEfuseInfo = (otp_struct*)&otpInfo;
653 //psEfuseInfo = (otp_struct*)sRamKey;
654
655 /*
656 * step 0
657 */
658 uiLen = 380;
659 puiArrPubKey = &(psImageHeader->uiPubKeyRsaELen);
660 puiArrPubKeyEN = psImageHeader->uiPubKeyRsaE;
661 sha256_csum_wd((const unsigned char *)puiArrPubKey, uiLen, (unsigned char *)uiHashResArr);
662 uiHashResLen = 8;
663 /*´óС¶Ëת»»*/
664 BIG2SMALL(uiHashResArr, 32);
lh9ed821d2023-04-07 01:36:19 -0700665
xf.liaa4d92f2023-09-13 00:18:58 -0700666 BOOT_PRINTF(UBOOT_DBG, "secure_flag=0x%x.\n", psEfuseInfo->secure_flag);
lh9ed821d2023-04-07 01:36:19 -0700667
xf.liaa4d92f2023-09-13 00:18:58 -0700668 if(efuse_cmp_word(psEfuseInfo->puk_hash, uiHashResArr, uiHashResLen))
669 {
670 BOOT_PRINTF(UBOOT_ERR, "PubKey Hash Verify -> Failed !\n");
671 return 1;
672 }
673 puiArrHASH = psImageHeader->uiHashY;
lh9ed821d2023-04-07 01:36:19 -0700674
xf.liaa4d92f2023-09-13 00:18:58 -0700675 /*
676 * step 1
677 */
678 memcpy(puiArrPubKeyE+63, puiArrPubKeyEN, 4);
679 sRSAInput.udCalMode = RSA_MOD_EXPO_WITH_INIT;
680 sRSAInput.udNbitLen = 2048;
681 sRSAInput.udEbitLen = 2048;
682 sRSAInput.pudInputM = puiArrHASH;
683 sRSAInput.pudInputE = puiArrPubKeyE;
684 sRSAInput.pudInputN = psImageHeader->uiPubKeyRsaN;
685 sRSAInput.pudOutputP = uiRsaResArr;
lh9ed821d2023-04-07 01:36:19 -0700686
xf.liaa4d92f2023-09-13 00:18:58 -0700687 uiRet = Rsa_Calculate(sRSAInput);
688 if(uiRet != 0)
689 {
690 BOOT_PRINTF(UBOOT_ERR, "Rsa_Calculate ERROR(0x%x)!\n", uiRet);
691 return 2;
692 }
693 puiRsaResAddr = sRSAInput.pudOutputP + (64 - uiHashResLen);
lh9ed821d2023-04-07 01:36:19 -0700694
xf.liaa4d92f2023-09-13 00:18:58 -0700695 /*
696 * step 2
697 */
698 uiLen = uiHashVerifySize;
699 if(rootfs_flag == 1)
700 {
701 puiDataLoadAddr = ___htonl(puiLegacyImgAddr->ih_load) - sizeof(image_header_t) - sizeof(image_header_t) - sizeof(sImageNewHeader);
702 }
703 else
704 {
705 if(1 == m0_flag || 1 == zsp_flag){
706 puiDataLoadAddr = (uchar *)addr + sizeof(sImageNewHeader);
707
708 }else{
709 puiDataLoadAddr = ___htonl(puiLegacyImgAddr->ih_load) - sizeof(image_header_t);
lh9ed821d2023-04-07 01:36:19 -0700710
xf.liaa4d92f2023-09-13 00:18:58 -0700711 }
712 }
713
714 uiHashResLen = 0;
715 memset(uiHashResArr, 0, 8*sizeof(uiHashResArr[0]));
lh9ed821d2023-04-07 01:36:19 -0700716
xf.liaa4d92f2023-09-13 00:18:58 -0700717 sha256_csum_wd((const unsigned char *)puiDataLoadAddr, uiLen, (unsigned char *)uiHashResArr);
718 uiHashResLen = 8;
719 /*´óС¶Ëת»»*/
720 BIG2SMALL(uiHashResArr, 32);
lh9ed821d2023-04-07 01:36:19 -0700721
xf.liaa4d92f2023-09-13 00:18:58 -0700722 if(efuse_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen))
723 {
724 BOOT_PRINTF(UBOOT_ERR, "LegacyImg Verify -> Failed !\n");
725 return 3;
726 }
lh9ed821d2023-04-07 01:36:19 -0700727
lh9ed821d2023-04-07 01:36:19 -0700728 }
729
730 return 0;
731}
732
733void efuse_get_devinfo(efuse_struct *efuse_info)
734{
735 u32 i;
736 efuse_struct *s_efuse = (efuse_struct*)EFUSE_RAM_BASE;
737
738 efuse_read_prepare();
739
740 efuse_info->secure_flag = s_efuse->secure_flag;
741
742 for(i = 0; i < 4; i ++)
743 {
744 efuse_info->puk_hash[i] = s_efuse->puk_hash[i];
745 }
746
747 for(i = 0; i < 3; i ++)
748 {
749 efuse_info->dev_id[i] = s_efuse->dev_id[i];
750
751 }
752
753}
754/*******************************************************************************
755* Function: efuse_init
756* Description:
757* Parameters:
758* Input:
759*
760* Output:
761*
762* Returns:
763*
764* Others:
765********************************************************************************/
766int efuse_init(void)
767{
768 /* Init Once is Enough. */
769 if(gbEfuseReadFlag == 0)
770 {
771 efuse_read_prepare();
772 gbEfuseReadFlag = 1;
773 BOOT_PRINTF(UBOOT_DBG, "gbEfuseReadFlag=1.\n");
774 }
775
776 /* Efuse Status decide ENABLE/DISABLE SecureVerify. */
777 efuse_get_data();
778
xf.liaa4d92f2023-09-13 00:18:58 -0700779 /*OTP status*/
780
781 if(g_nor_flag == 1)
782 {
783 g_iftype = IF_TYPE_NOR;
784 nand_init();
785 /*Çл»³õʼ»¯spi_nand*/
786 g_iftype = IF_TYPE_SPI_NAND;
787 nand_init();
788
789 get_otp_secure_verify_status();
790 }
791
lh9ed821d2023-04-07 01:36:19 -0700792 return 0;
793}
794
795