RK3288Android5.1 通过cpu串号固定以太网MAC地址

固定mac地址流程:

    关闭原先的读取MAC地址文件->获取cpu串号->根据cpu串号计算一组能用的MAC地址->设置MAC地址。

此种固定方式即使重刷固件也不会改变MAC地址

 

生成MAC流程

1.全局变量system_serial_low为串号的低8位,system_serial_high为串号的高8位,先获取这两个值

2.判断这两个值是否小于0,如果小于0则取其绝对值

3.将这两个值转换为字符串,依次将字符串的两位转化为MAC地址的其中一个数据,

   例如:读到的system_serial_high = 123456789,system_serial_low=789abcdef

           则MAC = 12:34:56:78:ab:cd

 4.最终判断MAC地址第一位是否为偶数,如果不是偶数,就当前这位数减1,使其变成偶数。

5.详细过程查看对应补丁:fixedMac.patch:https://download.csdn.net/download/Mrdeath/12624890

diff --git a/frameworks/base/services/core/java/com/android/server/WatchdogManagerService.java b/frameworks/base/services/core/java/com/android/server/WatchdogManagerService.java
index 2c32ec3d15..57ab825dcd 100644
--- a/frameworks/base/services/core/java/com/android/server/WatchdogManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/WatchdogManagerService.java
@@ -155,8 +155,8 @@ public class WatchdogManagerService extends IWatchdogManager.Stub {
  
         mWatchdogReceiver = new WatchdogReceiver();
        getNetworkManagementService();
-       Message msg = mHandler.obtainMessage(MSG_SET_MAC);
-       mHandler.sendMessageAtTime(msg, SystemClock.uptimeMillis() + 10 * 1000);
+       //Message msg = mHandler.obtainMessage(MSG_SET_MAC);
+       //mHandler.sendMessageAtTime(msg, SystemClock.uptimeMillis() + 10 * 1000);
    }
  
    private boolean checkMacAddress(String macAddr) {
diff --git a/kernel/drivers/net/ethernet/rockchip/gmac/stmmac_main.c b/kernel/drivers/net/ethernet/rockchip/gmac/stmmac_main.c
index 5d2df5b4ff..6860e6e5fd 100755
--- a/kernel/drivers/net/ethernet/rockchip/gmac/stmmac_main.c
+++ b/kernel/drivers/net/ethernet/rockchip/gmac/stmmac_main.c
@@ -51,6 +51,9 @@
 #include "stmmac_ptp.h"
 #include "stmmac.h"
 #include "../eth_mac.h"
+#include <asm/system_info.h>
+#include <linux/ctype.h>
+
  
 #undef STMMAC_DEBUG
 /*#define STMMAC_DEBUG*/
@@ -1564,6 +1567,195 @@ static int stmmac_get_hw_features(struct stmmac_priv *priv)
    return hw_cap;
 }
  
+
+//整型输入x,输出对应的16进制字符串
+void int_to_hex_str(long int x,char *p)
+{
+   int t,i=0;
+   if(x<0)
+   {
+       x=-x;
+       *p='-';
+       p++;
+   }
+   if(x==0)
+       *p='0';
+   for(i=0;i<10;i++)
+   {
+       t=x%16;
+       x=x/16;
+       if(t<10)
+       *(p+9-i)=t+'0';
+       else
+       *(p+9-i)=t+'A'-10;
+       if(x==0)
+       {
+           t=9-i;
+           break;
+       }
+   }
+   for( i=0;i<10-t;i++)
+   {
+       *(p+i)=*(p+t+i);
+       *(p+t+i)='\0';
+   }
+}
+
+/*
+ * 将字符转换为数值
+ * */ 
+int c2i(char ch) 
+{ 
+        if(isdigit(ch)) 
+                return ch - 48; 
+ 
+        if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'z' ) 
+                return -1; 
+ 
+        if(isalpha(ch)) 
+                return isupper(ch) ? ch - 55 : ch - 87; 
+ 
+        return -1; 
+} 
+ 
+/*
+ * 功能:将十六进制字符串转换为整型(int)数值
+ * */ 
+int hex2dec(char *hex) 
+{ 
+        int len; 
+        int num = 0; 
+        int temp; 
+        int bits; 
+        int i; 
+         
+        len = strlen(hex); 
+ 
+        for (i=0, temp=0; i<len; i++, temp=0) 
+        { 
+                temp = c2i( *(hex + i) ); 
+                bits = (len - i - 1) * 4; 
+                temp = temp << bits; 
+ 
+                num = num | temp; 
+        } 
+ 
+        return num; 
+}
+
+
+int eth_mac_from_cpu_serial(char *addr)
+{
+   char serial_low[9] = {0};
+   char serial_high[9] = {0};
+        char addr1[3],addr2[3],addr3[3],addr4[3],addr5[3],addr6[3];
+   int final_addr1 = 0;
+   int i = 0;
+   long long system_serial_low_final = system_serial_low;
+   long long system_serial_high_final = system_serial_high;
+   if (system_serial_low_final == 0 && system_serial_high_final == 0)
+        {
+       printk("system_serial_low system_serial_high read err\n");
+       return -ENODEV;
+   }
+   if(system_serial_low_final < 0)
+   {
+       system_serial_low_final = -system_serial_low_final;
+   }
+   for(i=0;system_serial_low_final < 268435456;i++)
+   {
+      
+       system_serial_low_final = system_serial_low_final * 16 + i;
+       printk("for system_serial_low = %lld\n ",system_serial_low_final);
+   }
+   if(system_serial_high_final < 0)
+   {
+       system_serial_high_final = -system_serial_high_final;
+   }
+   for(i=0;system_serial_high_final < 268435456;i++)
+        {
+                system_serial_high_final = system_serial_high_final * 16 + i;
+       printk("for system_serial_high = %lld\n ",system_serial_high_final);
+       msleep(500);
+        }
+
+   printk("system_serial_low = %d,system_serial_high = %d\n",system_serial_low,system_serial_high);
+   printk("system_serial_low_final = %lld,system_serial_high_final = %lld\n",system_serial_low_final,system_serial_high_final);
+
+   int_to_hex_str(system_serial_low_final,serial_low);
+   int_to_hex_str(system_serial_high_final,serial_high);
+   printk("system_serial_low = %s,system_serial_high = %s\n",serial_low,serial_high);
+   if(serial_high[0] != '-')
+   {
+       addr1[0] = serial_high[0];
+       addr1[1] = serial_high[1];
+       addr1[2] = '\0';
+       addr2[0] = serial_high[2];
+       addr2[1] = serial_high[3];
+       addr2[2] = '\0';
+       addr3[0] = serial_high[4];
+       addr3[1] = serial_high[5];
+       addr3[2] = '\0';
+       addr4[0] = serial_high[6];
+       addr4[1] = serial_high[7];
+       addr4[2] = '\0';
+   }
+   else
+   {
+       addr1[0] = serial_high[1];
+       addr1[1] = serial_high[2];
+       addr1[2] = '\0';
+       addr2[0] = serial_high[3];
+       addr2[1] = serial_high[4];
+       addr2[2] = '\0';
+       addr3[0] = serial_high[5];
+       addr3[1] = serial_high[6];
+       addr3[2] = '\0';
+       addr4[0] = serial_high[7];
+       addr4[1] = serial_high[8];
+       addr4[2] = '\0';
+
+
+   }
+   if(serial_low[0] != '-')
+   {
+       addr5[0] = serial_low[4];
+       addr5[1] = serial_low[5];
+       addr5[2] = '\0';
+       addr6[0] = serial_low[6];
+       addr6[1] = serial_low[7];
+       addr6[2] = '\0';
+   }
+   else
+   {
+       addr5[0] = serial_low[5];
+       addr5[1] = serial_low[6];
+       addr5[2] = '\0';
+       addr6[0] = serial_low[7];
+       addr6[1] = serial_low[8];
+       addr6[2] = '\0';
+
+   }
+   final_addr1 = hex2dec(addr1);
+   if(final_addr1 % 2 != 0)
+   {
+       addr[0] = final_addr1 -1 ;
+   }
+   else
+   {
+       addr[0] = final_addr1;
+   }
+   addr[1] = hex2dec(addr2);
+   addr[2] = hex2dec(addr3);
+   addr[3] = hex2dec(addr4);
+   addr[4] = hex2dec(addr5);
+   addr[5] = hex2dec(addr6);  
+        return 0;
+
+
+}
+
+
 /**
  * stmmac_check_ether_addr: check if the MAC addr is valid
  * @priv: driver private structure
@@ -1577,8 +1769,15 @@ static void stmmac_check_ether_addr(struct stmmac_priv *priv)
        priv->hw->mac->get_umac_addr((void __iomem *)
                         priv->dev->base_addr,
                         priv->dev->dev_addr, 0);
-       if (!is_valid_ether_addr(priv->dev->dev_addr))
+       if (!is_valid_ether_addr(priv->dev->dev_addr)){
+           //如果idb里有写入MAC就从idb里获取
            eth_mac_idb(priv->dev->dev_addr);
+       }
+       if (!is_valid_ether_addr(priv->dev->dev_addr)){
+           //如果idb不能设置正确MAC,就自己读取cpu串号,生成MAC
+           eth_mac_from_cpu_serial(priv->dev->dev_addr);
+       }
+
        if (!is_valid_ether_addr(priv->dev->dev_addr))
            eth_hw_addr_random(priv->dev);
    }

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值