当前位置: 首页 > 工具软件 > failsafe > 使用案例 >

Openwrt 安全模式failsafe分析

东方淇
2023-12-01

安全模式作用:

只加载基本的文件系统,可以修改密码,网络配置等,可以进行升级系统,Uboot等操作;


依赖hotplug机制;

procd中进行preinit初始化;同时注册hotplug 事件;

文件/etc/hotplug-preinit.json

[
        [ "case", "ACTION", {
                "add": [
                        [ "if",
                                [ "has", "FIRMWARE" ],
                                [
                                        [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ],
                                        [ "load-firmware", "/lib/firmware" ],
                                        [ "return" ]
                                ]
                        ],
                ],
        }, ],
        [ "if",
                [ "and",
                        [ "eq", "SUBSYSTEM", "button" ],
                ],
                [ "exec", "/etc/rc.button/failsafe" ]
        ],
]
[ "exec", "/etc/rc.button/failsafe" ] 检测到按键运行此脚本

#!/bin/sh

[ "${TYPE}" = "switch" ] || echo ${BUTTON} > /tmp/failsafe_button
return 0

只要按键引脚有状态变化,即创建/tmp/failsafe_button 文件,将被脚本/lib/preinit/30_failsafe_wait用到

failsafe_wait() {
        FAILSAFE=
        grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
        if [ "$FAILSAFE" != "true" ]; then
                pi_failsafe_net_message=true
                preinit_net_echo "Please press button now to enter failsafe"
                pi_failsafe_net_message=false
                fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true
                [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -"
                [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe
        fi
}

boot_hook_add preinit_main failsafe_wait

fs_wait_for_key 函数处理按键 ' f ' 消息以及设定调试信息输出等级,此方式是通过系统调试串口进行;

[ -f "/tmp/failsafe_button" ] && FAILSAFE=true &&echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -"
此处 判断failsafe_button文件存在,即进入安全模式;


需要注意的是:procd 捕获按键信息,然后调用hotplug脚本,是在preinit阶段,此时overlay文件系统还未加载,使用的romfs,因此如果想禁用安全模式,不能在系统启动完后修改/etc/rc.button/failsafe脚本。因此preinit阶段,使用的romfs,用户修改的文件在overlayfs中,还未被加载;

So,禁用按键进入安全模式的方法是:在sysupgrade.bin固件编译阶段,修改/etc/rc.button/failsafe脚本,不创建/tmp/failsafe_button 文件即可;



 类似资料: