0%

bl efuse 操作 api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/****************************************************************************/ /**
* @brief Efuse lock reading for aes key
*
* @param index: index of key slot
* @param program: program to efuse entity or not
*
* @return None
*
*******************************************************************************/
void EF_Ctrl_Readlock_AES_Key(uint8_t index, uint8_t program)
{
uint32_t tmpVal;

if (index > 5) {
return;
}

/* Switch to AHB clock */
EF_Ctrl_Sw_AHB_Clk_0();

tmpVal = BL_RD_REG(EF_DATA_BASE, EF_DATA_0_LOCK);
tmpVal |= (1 << (index + 26));
BL_WR_REG(EF_DATA_BASE, EF_DATA_0_LOCK, tmpVal);

if (program) {
EF_Ctrl_Program_Efuse_0();
}
}

/****************************************************************************/ /**
* @brief Efuse write AES key
*
* @param index: index of key slot
* @param keyData: key data buffer
* @param len: key data length in words
* @param program: program to efuse entity or not
*
* @return None
*
*******************************************************************************/
void EF_Ctrl_Write_AES_Key(uint8_t index, uint32_t *keyData, uint32_t len, uint8_t program)
{
uint32_t *pAESKeyStart0 = (uint32_t *)(EF_DATA_BASE + 0x1C);

if (index > 5) {
return;
}

/* Switch to AHB clock */
EF_Ctrl_Sw_AHB_Clk_0();

/* Every key is 4 words len*/
BL702_MemCpy4(pAESKeyStart0 + index * 4, keyData, len);

if (program) {
EF_Ctrl_Program_Efuse_0();
}
}

代码示例:

假设key为

1
2
3
4
5
6
7
8
9
10
11
12
uint32_t *keyData = {
0x41cb3b9a,
0x7cf9f7cf,
0xdc0dd12f,
0x6c3ed53a,
0x00a0c824,
0xc737923e,
0xe2f055f1,
0x34afa0f7,
}
// 写入key_slot2 key_slot3
EF_Ctrl_Write_AES_Key(2, keyData, 8, 1);

先做实验, 验证key_slot2, key_slot3 是否正常写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/****************************************************************************/ /**
* @brief Efuse read AES key from specified region and index
*
* @param index: index of key slot
* @param keyData: key data buffer
* @param len: key data length in words
*
* @return None
*
*******************************************************************************/
void EF_Ctrl_Read_AES_Key(uint8_t index, uint32_t *keyData, uint32_t len)
{
uint32_t *pAESKeyStart0 = (uint32_t *)(EF_DATA_BASE + 0x1C);

if (index > 5) {
return;
}

/* Trigger read data from efuse*/
EF_CTRL_LOAD_BEFORE_READ_R0;

/* Every key is 4 words len*/
BL702_MemCpy4(keyData, pAESKeyStart0 + index * 4, len);
}

示例代码, 读取key_slot2, key_slot3的key数据

1
2
uint32_t keyRead[8] = {0};
EF_Ctrl_Read_AES_Key(2, keyRead, 8);

判断keyRead 与 keyData 是否一致

如果前面写入正常, 最后需要对key_slot2, key_slot3 做读保护

1
2
3
4
// 写read lock, 读保护key_slot2
EF_Ctrl_Readlock_AES_Key(2, 1);
// 读保护key_slot3
EF_Ctrl_Readlock_AES_Key(3, 1);