am335x 一个按键实现重置 ip 和 root passwd

* 其实做法很简单,我连按键驱动都没有去写,读取 gpio 的值然后 拷贝了一份 /etc/network/interfaces_bak 为 interfaces ,用脚本重新设置了一次root 密码。

* 代码如下:

    # 在开机的脚本把需要的 GPIO节点打开
    # 位置为 /etc/init.d/S90aplex
    echo 29  > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio29/direction
    echo 44  > /sys/class/gpio/export
    // 将这份代码编译出来然后加入开机脚本后台运行
    #include <unistd.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>

    #define VAL_FILE  "/sys/class/gpio/gpio44/value"

    int main(void)
    {
        int val_fd, ret_val;
        char val;
    
        val_fd = open(VAL_FILE, O_RDONLY);
        if (val_fd < 0)
        {   
            perror("open device");
            return -1; 
        }   
        while(1)
        {   
            lseek(val_fd, SEEK_SET, 0); 
            ret_val = read(val_fd, &val, 1); 
            if (val == '0')
            {   
                system("/etc/init.d/reset_ip.sh");
                sleep(1);
            }
            usleep(500000);
        }

        close(val_fd);
        return 0;
    }
    # reset_ip.sh
    #!/bin/sh

    passwd root <<EOF
    root
    root
    EOF
    cp /etc/network/interfaces_bak /etc/network/interfaces -rf 
    /etc/init.d/S40network restart
    rm /etc/ssh/ssh_host*
    sync
原文地址:https://www.cnblogs.com/chenfulin5/p/6912827.html