27服务有个特殊需求,上电或者复位前安全访问失败时,上电的10s内需要启用安全延时,回复0x37。
从代码层面来讲,执行顺序流分析如下:
关键点:一个宏 DCM_STATE_SEC_ATT_CNTR_EXT_STORAGE_ENABLED
对应两个callout 函数:
Dcm_Svc27ReadAttemptCounter
Dcm_Svc27WriteAttemptCounter
这两个函数都在Dcm_Svc27Task中被调用。
在Dcm_Svc27ReadAttemptCounter函数中,调用Dcm_Svc27UtiGetAttemptCntr函数,通过具体的回调函数,获取应用层往下传递的次数2.代码如下。
DCM_LOCAL_INLINE FUNC(Dcm_TskTaskEvOptType, DCM_CODE) Dcm_Svc27ReadAttemptCounter(Dcm_TskTaskEvOptType ev
,Dcm_TskTaskEvPtrType pPostEv)
{
Dcm_TskTaskEvOptType lResultEv = ev;
/* #10 Restore the attempt counter values from the application */
Dcm_Svc27UtiGetAttemptCntr(Dcm_SingletonContext.Diag.Services.Svc27.GetAttOpStatus
,&Dcm_SingletonContext.Diag.Services.Svc27.GetAttCntrEventMask);
Dcm_Svc27UtiGetAttemptCntr函数中,通过函数指针访问应用层callout函数,获取应用层设置的安全访问错误计数器,并启用延时机制。
/* #30 Try to read the attempt counter from the application */
lResult = pSecLevelInfo->GetAttemptCntrFunc(opStatus, &lAttemptCount); /* SBSW_DCM_CALL_FUNCPTR_SVC27SECLEVEL */
/* #40 If everything was ok: */
if(lResult == DCM_E_OK)
{
/*
* Note: Using critical sections is not necessary here because the features "power on delay" and
* "external attempt counter storage" are used mutually exclusive per security level.
*/
/* #50 Mark the security level to prevent that this API is called again */
Dcm_UtiBitOpClr(uint32, *levelMask, Dcm_UtiGetBitFromIndex(uint32, lSecLvlIter)); /* SBSW_DCM_PARAM_PTR_WRITE */
/* #60 If the already initialized attempt counter has to be updated: */
if(lAttemptCount != 0u)
{
/* #70 Store the attempt counter */
Dcm_Svc27CounterSet(lSecLvlIter, lAttemptCount);
/* #80 If the current attempt counter value exceeded the maximum number of allowed attempts: */
if(lAttemptCount >= Dcm_CfgStateSecurityInfo[lSecLvlIter].NumAttempts)
{
/* #90 Set the delay timer value. The timer will be started later with that value */
Dcm_Svc27TimerSet(lSecLvlIter, Dcm_CfgStateSecurityInfo[lSecLvlIter].DelayTimeInvKey);
}
此时完成了上电的安全延时启动。
Dcm_Svc27WriteAttemptCounter函数对应完成的是下电前的couter存储。
当应用层的key比较失败时,传到底层后,会调用上层的SetAttmptCounter函数,将当前的值告诉应用层,如果大于1,则存储EEPROM,供上电后读取。
借助以上方法,可以实现动态的27服务防暴力破解。