27服务实现上电安全延时分析

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服务防暴力破解。

### Linux 内核中的定时器设置与管理 在 Linux ernel 中,定时器是一种重要的时间管理机制。通过 `struct timer_list` 数据结构可以定义并初始化一个定时器对象[^1]。 以下是创建和添加定时器到内核时间管理机制的一个典型例子: #### 初始化定时器 首先需要声明一个 `struct timer_list` 类型的对象,并对其进行初始化。可以通过函数 `setup_timer()` 或者直接调用 `init_timer()` 来完成这一步操作。 ```c #include <linux/timer.h> static struct timer_list my_timer; // 使用 setup_timer 进行初始化 void init_my_timer(void) { setup_timer(&my_timer, my_timer_callback, (unsigned long)NULL); } // 替代方法:使用 init_timer 手动指定回调函数和其他参数 void manual_init_my_timer(void) { init_timer(&my_timer); // 初始化 timer_list 结构体 my_timer.function = my_timer_callback; // 设置回调函数 my_timer.data = (unsigned long)NULL; // 可选数据传递给回调函数 } ``` #### 设置超时时间和启动定时器 一旦定时器被成功初始化之后,就可以为其设定到期时间(expires),并通过 `add_timer()` 函数将其加入内核的定时器队列中。通常情况下,到期时间是以 jiffies 表示的绝对值或者相对偏移量计算得出。 ```c #define MY_TIMER_DELAY (HZ / 2) /* 半秒 */ void start_my_timer(void) { unsigned long delay_jiffies = msecs_to_jiffies(500); // 转换毫秒至jiffies my_timer.expires = jiffies + delay_jiffies; add_timer(&my_timer); // 将定时器添加到内核链表中 } ``` 当定时器到达其预定的时间点时,会触发之前绑定好的回调函数执行相应的逻辑处理过程。 #### 删除定时器 如果不再需要某个已经激活的计时器,则应该及时删除它以防止意外行为发生。可利用 del_timer() 实现这一目的;而为了确保线程安全性,在多处理器环境下推荐采用 del_timer_sync() 方法替代之。 ```c void stop_my_timer(void) { del_timer(&my_timer); // 如果可能的话,请改用del_timer_sync() } ``` 以上就是关于如何正确配置以及向Linux Kernel 的 Time Management Mechanism 添加 Timer 的基本流程说明^。 另外需要注意的是,在虚拟化环境中可能存在两种类型的实现方式——硬件支持下的 HV Timer 和软件模拟出来的 SW Timer(HRTIMER)[^2]^ 。对于具体的命令行工具而言 , crash 工具提供了查看当前系统中存在的所有 timers 列表的功能 ,即运行 'crash>help timer' 后加上适当的选项即可获得相关信息[^3]^ 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汽车电子攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值