Openwrt永久修改MAC地址源代码

*
 * flash MAC  
 * Copyright (C) 2014 WRTnode machine team.
 * This program is free software; you can redistribute it and/or modify
 *
 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
 */

#include <stdio.h>             
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#define MEMERASE        _IOW('M', 2, struct erase_info_user)

struct erase_info_user {
        uint32_t start;
        uint32_t length;
};

int main(int argc,char *argv[])
{
    int fd,sz,i,offset=0x04;
    int mac[6];
    sz = 0x10000;
    unsigned char *buf;
    buf = (unsigned char *)malloc(sz);
    for(i=0;i<6;i++){
        mac[i] = strtol(argv[i+1], NULL, 16);
    }
    if(mac[0]%4 !=0)
    {
        printf("mac[0]%4==0,please input a effective MAC again\n");
        return -1;
    }
    fd = open("/dev/mtd2", O_RDWR | O_SYNC);
    if (fd < 0) {
        fprintf(stderr, "failed to open mtd2\n");
        free(buf);
        return -1;
    }
      //read-----------
      lseek(fd, 0, SEEK_SET);
    //backup
    if (read(fd, buf, sz) != sz) {
        fprintf(stderr, "failed to read %d bytes from mtd2\n",
                sz);
        free(buf);
        close(fd);
        return -1;
    }
    //erase
    struct erase_info_user ei;
    lseek(fd, 0, SEEK_SET);
    ei.start = 0;
    ei.length = sz;
    if (ioctl(fd, MEMERASE, &ei) < 0) {
        fprintf(stderr, "failed to erase mtd2\n");
        free(buf);
        close(fd);
        return -1;
    }
        for(i=0;i<6;i++){
            *(buf + (offset) + i) = mac[i];
        }
    //write
    lseek(fd, 0, SEEK_SET);
     if (write(fd, buf, sz) == -1) {
        fprintf(stderr, "failed to write mtd%d\n", i);
        free(buf);
        close(fd);
        return -1;
    }
    free(buf);
    close(fd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值