全志平台动态mac地址解决办法记录

 
  

代码路径:lichee\linux-3.4\drivers\net\ethernet\sunxi\emac\sunxi_emac.c   在此网口驱动中找到mac获取函数:wemac_get_macaddr()

更改后的代码为:

static void wemac_get_macaddr(wemac_board_info_t *db)
{
struct net_device *ndev = db->ndev;
int i,flag=1,result=0;
char *p = mac_str;
struct file *filp=NULL;
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);

    set_fs(get_ds());
filp = filp_open("/data/existMacAddr",O_CREAT|O_RDWR,0666);//由于内核中此驱动挂起较早,多数文件系统还没挂载,因此如果将模块打到内核中,是实现不了目的的,我暂时选择将模块开机挂载,不打入内核,望大牛看到此文,能给出更好的文件路径方案,最好是能打入内核。
if(IS_ERR(filp)){
  printk("open /data/existMacAddr err,maybe it's exist!!"); 
goto err1;
}

for(i=0;i<6;i++,p++)
ndev->dev_addr[i] = simple_strtoul(p, &p, 16);


#ifdef DYNAMIC_MAC_SYSCONFIG
    
    script_item_u emac_mac;
    if(SCIRPT_ITEM_VALUE_TYPE_STR != script_get_item("dynameic", "MAC", &emac_mac)){
        printk(KERN_WARNING "In sysconfig.fex emac mac address is not valid!\n");
    } else if(!is_valid_ether_addr(ndev->dev_addr)){
        emac_mac.str[12] = '\0';
        for (i=0; i < 6; i++){ 
char emac_tmp[3]=":::";
memcpy(emac_tmp, (char *)(emac_mac.str+i*2), 2);
emac_tmp[2]=':';
ndev->dev_addr[i] = simple_strtoul(emac_tmp, NULL, 16);
        }
    }


#endif
if(!(filp->f_op)|| !(filp->f_op->read) || !(filp->f_op->write)){
printk("file /data/existMacAddr cann't readable or writable?\n");
goto err1;
}

char cmp_buf[6] = {0};
filp->f_op->llseek(filp, 0, 0);
int ret = filp->f_op->read(filp,ndev->dev_addr,6,&filp->f_pos);
if(ret<0) {
printk("read file error?\n");
goto err1;
}
printk("result of file [/data/existMacAddr] read=%d\n",ret);
if(0 != memcmp(ndev->dev_addr,cmp_buf,6))
{
flag = 0;
printk(" /data/existMacAddr is not empty!\n");
}
if (!is_valid_ether_addr(ndev->dev_addr) && flag){
eth_hw_addr_random(ndev);
if(filp->f_op->write(filp,ndev->dev_addr,strlen(ndev->dev_addr),&filp->f_pos)<0){
printk("write file error?\n");
goto err1;
}

}
if (!is_valid_ether_addr(ndev->dev_addr))
printk(KERN_ERR "Invalid MAC address. Please set it using ifconfig\n");
err1:
set_fs(old_fs);
if(filp_close(filp,NULL))
       printk("Config_FileOperation:close file fail\n");
};

你可能感兴趣的:(android)