这个是让r8152 走nvram读取eth mac地址,这部分代码可以参考用于kernel读取nvram 数据。
updateMac这个不用关心,是客制化的东西。
这个 修改主要在 Android 系统中修改了网络驱动和 MAC 地址更新的相关代码,使得系统在启动时从 NVRAM 中加载 MAC 地址,并禁用了 MAC 地址的更新。
修改的文件:
kernel-3.10/drivers/net/usb/r8152.c
vendor/mediatek/proprietary/external/updateMac/updateMac.cpp
文件修改说明:
r8152.c
: 在r8152
驱动中,添加了从 NVRAM 中读取 MAC 地址的代码。当系统启动时,驱动会首先尝试从 NVRAM 中读取 MAC 地址,如果读取失败或者读取到的 MAC 地址无效,那么系统会生成一个随机的 MAC 地址。updateMac.cpp
: 在updateMac
程序中,注释掉了更新 MAC 地址的代码,使得系统不再更新 MAC 地址。
--- kernel-3.10/drivers/net/usb/r8152.c | 88 +++++++++++++++++++++- .../proprietary/external/updateMac/updateMac.cpp | 4 +- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/kernel-3.10/drivers/net/usb/r8152.c b/kernel-3.10/drivers/net/usb/r8152.c index 61d3cd6..36b1c30 100755 --- a/kernel-3.10/drivers/net/usb/r8152.c +++ b/kernel-3.10/drivers/net/usb/r8152.c @@ -1191,12 +1191,74 @@ out1: return ret; } +#define IS_SUPPORT_NVRAM 1 +#if IS_SUPPORT_NVRAM +#define ETHERNET_NVRAM_CUSTOM_NAME "/data/nvram/APCFG/APRDEB/WIFI" +unsigned char g_nvram_macaddr[6]; + +static int nvram_read(char *filename, char *buf, ssize_t len, int offset) +{ + struct file *fd; + //ssize_t ret; + int retLen = -1; + + mm_segment_t old_fs = get_fs(); + set_fs(KERNEL_DS); + + fd = filp_open(filename, O_WRONLY|O_CREAT, 0644); + + if(IS_ERR(fd)) { + printk("failed to open!!\n"); + return -1; + } + do{ + if ((fd->f_op == NULL) || (fd->f_op->read == NULL)) + { + printk("file can not be read!!\n"); + break; + } + + if (fd->f_pos != offset) { + if (fd->f_op->llseek) { + if(fd->f_op->llseek(fd, offset, 0) != offset) { + printk("[nvram_read] : failed to seek!!\n"); + break; + } + } else { + fd->f_pos = offset; + } + } + + retLen = fd->f_op->read(fd, + buf, + len, + &fd->f_pos); + + }while(false); + + filp_close(fd, NULL); + + set_fs(old_fs); + + return retLen; +} + +int load_nvram_macaddr( char * filename, char * buf, int len) +{ + return nvram_read( filename, buf, len, sizeof(u16) + sizeof(u16)); +} + +#endif + static int set_ethernet_addr(struct r8152 *tp) { struct net_device *dev = tp->netdev; struct sockaddr sa; int ret; - +#if IS_SUPPORT_NVRAM + int i = 0; + printk("nvram rtl8152---set_ethernet_addr---\n"); +#endif if (tp->version == RTL_VER_01) ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data); else @@ -1207,8 +1269,32 @@ static int set_ethernet_addr(struct r8152 *tp) } else if (!is_valid_ether_addr(sa.sa_data)) { netif_err(tp, probe, dev, "Invalid ether addr %pM\n", sa.sa_data); +#if IS_SUPPORT_NVRAM + g_nvram_macaddr[0] = 0; + g_nvram_macaddr[1] = 0; + g_nvram_macaddr[2] = 0; + g_nvram_macaddr[3] = 0; + g_nvram_macaddr[4] = 0; + g_nvram_macaddr[5] = 0; + load_nvram_macaddr(ETHERNET_NVRAM_CUSTOM_NAME, + (char *)&g_nvram_macaddr, sizeof(g_nvram_macaddr)); + printk("Read nvram : g_nvram_macaddr %02x%02x%02x%02x%02x%02x\n", + g_nvram_macaddr[0], g_nvram_macaddr[1], g_nvram_macaddr[2], + g_nvram_macaddr[3], g_nvram_macaddr[4], g_nvram_macaddr[5]); + for(i = 0; i < 6; i++){ + if(i == 1){ + sa.sa_data[i] = g_nvram_macaddr[i] + 2; + }else{ + sa.sa_data[i] = g_nvram_macaddr[i]; + } + } + printk("Read nvram : sa.sa_data %02x%02x%02x%02x%02x%02x\n", + g_nvram_macaddr[0], g_nvram_macaddr[1], g_nvram_macaddr[2], + g_nvram_macaddr[3], g_nvram_macaddr[4], g_nvram_macaddr[5]); +#else eth_hw_addr_random(dev); ether_addr_copy(sa.sa_data, dev->dev_addr); +#endif ret = rtl8152_set_mac_address(dev, &sa); netif_info(tp, probe, dev, "Random ether addr %pM\n", sa.sa_data); diff --git a/vendor/mediatek/proprietary/external/updateMac/updateMac.cpp b/vendor/mediatek/proprietary/external/updateMac/updateMac.cpp index f13d55f..163dfda 100755 --- a/vendor/mediatek/proprietary/external/updateMac/updateMac.cpp +++ b/vendor/mediatek/proprietary/external/updateMac/updateMac.cpp @@ -76,7 +76,7 @@ int GetMacParamFromNVRam(WIFI_CFG_PARAM_STRUCT *pmacParamNB) } int main(){ - int i =0; + /*int i =0; struct ifreq ifreq; int socket_id; int ret; @@ -127,7 +127,7 @@ int main(){ strncpy(ifreq.ifr_name, ifname, IFNAMSIZ); ifreq.ifr_flags |= IFF_UP; // ifconfig up ret = ioctl(socket_id, SIOCSIFFLAGS, &ifreq); - close(socket_id); + close(socket_id);*/ return 0; }