如何修改wince(s3c2410)的MAC地址?

来源:互联网 发布:用户画像数据分析建模 编辑:程序博客网 时间:2024/04/29 15:15

wince中的MAC地址是在驱动中写死的。如果不修改会造成一些网络方面的功能故障。比如不能互ping。

 

下面是我解决的方法:

 

1.添加如下注册表
[HKEY_LOCAL_MACHINE/Comm/CS8900/Parms]
"MAC12"=dword:3322
"MAC34"=dword:5544
"MAC56"=dword:0F66

 

2.修改CS8900驱动
在CS8900.c文件中,作如下修改:

  1. WORD iMAC3={0x3322,0x5544,0x0F66};  //添加MAC地址的原始数据
  2. //添加函数,读取注册表中的MAC地址的值
  3. void ReadRegsister()
  4. {
  5.     HKEY hkMAC = NULL;
  6.     DWORD MAC;
  7.     DWORD dwStatus, dwType, dwSize;
  8.         TCHAR gszBaseInstance[256] = _T("//Comm//CS8900//Parms");
  9.     // open the registry key and read our configuration
  10.     dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC);
  11.     dwType = REG_DWORD;
  12.     if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) 
  13.         {
  14.         dwSize = sizeof(DWORD);
  15.         //下面读取失败的话那么就用初始值作为MAC地址
  16.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC12"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);
  17.         if (dwStatus == ERROR_SUCCESS)
  18.         {
  19.             iMAC[0]=MAC;
  20.         }
  21.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC34"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);  
  22.         if (dwStatus == ERROR_SUCCESS)
  23.         {
  24.             iMAC[1]=MAC;
  25.         }
  26.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC56"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);  
  27.         if (dwStatus == ERROR_SUCCESS)
  28.         {
  29.             iMAC[2]=MAC;
  30.         }
  31.     }
  32. }
  33. int initCS()
  34. {   
  35.     CS8900WriteRegister(PKTPG_LINE_CTL, LINE_CTL_10_BASE_T);
  36.     CS8900WriteRegister(PKTPG_RX_CFG, RX_CFG_RX_OK_I_E);
  37.     CS8900WriteRegister(PKTPG_RX_CTL,RX_CTL_RX_OK_A | RX_CTL_IND_ADDR_A |RX_CTL_BROADCAST_A);
  38.     CS8900WriteRegister(PKTPG_TX_CFG, 0); 
  39.     CS8900WriteRegister(PKTPG_BUF_CFG, 0); 
  40.     
  41.     //modify by constantine 
  42.     ReadRegsister();
  43.     CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 0, iMAC[0]);
  44.     CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 2, iMAC[1]);
  45.     CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 4, iMAC[2]);
  46.     initIrq();
  47.     return TRUE;
  48. }
  49. BOOLEAN CS8900ReadEthernetAddress(
  50.     IN PCS8900_ADAPTER Adapter
  51. )
  52. {
  53. //modify by constantine
  54.     //Adapter->PermanentAddress[0] = 0x22;
  55.     //Adapter->PermanentAddress[1] = 0x33;
  56.     //Adapter->PermanentAddress[2] = 0x44;
  57.     //Adapter->PermanentAddress[3] = 0x55;
  58.     //Adapter->PermanentAddress[4] = 0x66;
  59.     //Adapter->PermanentAddress[5] = 0x0F;
  60.     
  61.     Adapter->PermanentAddress[0] = iMAC[0]& 0x00FF;
  62.     Adapter->PermanentAddress[1] = iMAC[0]>>8;
  63.     Adapter->PermanentAddress[2] = iMAC[1]& 0x00FF;
  64.     Adapter->PermanentAddress[3] = iMAC[1]>>8;
  65.     Adapter->PermanentAddress[4] = iMAC[2]& 0x00FF;
  66.     Adapter->PermanentAddress[5] = iMAC[2]>>8; 
  67. //end    
  68.     RETAILMSG(1,
  69.         (TEXT("CS8900: PermanentAddress [ %02x-%02x-%02x-%02x-%02x-%02x ]/r/n"),
  70.             Adapter->PermanentAddress[0],
  71.             Adapter->PermanentAddress[1],
  72.             Adapter->PermanentAddress[2],
  73.             Adapter->PermanentAddress[3],
  74.             Adapter->PermanentAddress[4],
  75.             Adapter->PermanentAddress[5]));
  76.     //
  77.     // Use the burned in address as the station address, unless the
  78.     // registry specified an override value.
  79.     //
  80.     if ((Adapter->StationAddress[0] == 0x00) &
  81.         (Adapter->StationAddress[1] == 0x00) &
  82.         (Adapter->StationAddress[2] == 0x00) &
  83.         (Adapter->StationAddress[3] == 0x00) &
  84.         (Adapter->StationAddress[4] == 0x00) &
  85.         (Adapter->StationAddress[5] == 0x00)
  86.         )
  87.     {
  88.         RETAILMSG(1, (TEXT("CS8900: StationAddress Modified!.../r/n")));
  89.         Adapter->StationAddress[0] = Adapter->PermanentAddress[0];
  90.         Adapter->StationAddress[1] = Adapter->PermanentAddress[1];
  91.         Adapter->StationAddress[2] = Adapter->PermanentAddress[2];
  92.         Adapter->StationAddress[3] = Adapter->PermanentAddress[3];
  93.         Adapter->StationAddress[4] = Adapter->PermanentAddress[4];
  94.         Adapter->StationAddress[5] = Adapter->PermanentAddress[5];
  95.     }
  96.     return(TRUE);
  97. }

 

编译后就可以了,这样就让驱动是从注册表中读取MAC地址了。那么接下来我们就有办法修改这个MAC地址了。

 

3.写代码修改注册表

下面是关键代码:

 

  1. void CWINCEMACDlg::ReadRegsister()
  2. {
  3.     HKEY hkMAC = NULL;
  4.     DWORD MAC=0;
  5.     DWORD dwStatus, dwType, dwSize;
  6.     TCHAR gszBaseInstance[256] = _T("//Comm//CS8900//Parms");
  7.     // open the registry key and read our configuration
  8.     dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC);
  9.     dwType = REG_DWORD;
  10.     if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) 
  11.     {
  12.         dwSize = sizeof(DWORD);
  13.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC12"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);
  14.         if (dwStatus == ERROR_SUCCESS)
  15.         {
  16.             m_EDT0.Format(_T("%02x"),(MAC & 0x00ff));
  17.             m_EDT1.Format(_T("%02x"),(MAC >> 8));
  18.         }
  19.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC34"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);
  20.         if (dwStatus == ERROR_SUCCESS)
  21.         {
  22.             m_EDT2.Format(_T("%02x"),(MAC & 0x00ff));
  23.             m_EDT3.Format(_T("%02x"),(MAC >> 8));
  24.         }
  25.         dwStatus = RegQueryValueEx(hkMAC, _T("MAC56"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);
  26.         if (dwStatus == ERROR_SUCCESS)
  27.         {
  28.             m_EDT4.Format(_T("%02x"),(MAC & 0x00ff));
  29.             m_EDT5.Format(_T("%02x"),(MAC >> 8));
  30.         }
  31.     }
  32.     RegCloseKey( hkMAC );
  33.     UpdateData(FALSE);
  34. }
  35. void CWINCEMACDlg::WriteRegsister()
  36. {
  37.     HKEY hkMAC = NULL;
  38.     DWORD dwStatus, dwSize;
  39.     TCHAR gszBaseInstance[256] = _T("//Comm//CS8900//Parms");
  40.     dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC);
  41.     if(dwStatus == ERROR_SUCCESS ) 
  42.     {
  43.         dwSize = sizeof(DWORD);
  44.         int value;
  45.         TCHAR num[4]={0};
  46.         BYTE * lpBuffer;
  47.         UpdateData(TRUE);
  48.                 value=0;
  49.         wsprintf(num,L"0x%s", m_EDT1.GetBuffer(0));
  50.         value += _ttoi(num)<<8;
  51.         wsprintf(num,L"0x%s", m_EDT0.GetBuffer(0));
  52.         value += _ttoi(num);
  53.         lpBuffer = (BYTE *)&value;
  54.         RegSetValueEx(hkMAC, _T("MAC12"), 0, REG_DWORD, lpBuffer,dwSize);
  55.         value=0;
  56.         wsprintf(num,L"0x%s", m_EDT3.GetBuffer(0));
  57.         value += _ttoi(num)<<8;
  58.         wsprintf(num,L"0x%s", m_EDT2.GetBuffer(0));
  59.         value += _ttoi(num);
  60.         lpBuffer = (BYTE *)&value;
  61.         RegSetValueEx(hkMAC, _T("MAC34"), 0, REG_DWORD, lpBuffer,dwSize);
  62.         value=0;
  63.         wsprintf(num,L"0x%s", m_EDT5.GetBuffer(0));
  64.         value += _ttoi(num)<<8;
  65.         wsprintf(num,L"0x%s", m_EDT4.GetBuffer(0));
  66.         value += _ttoi(num);
  67.         lpBuffer = (BYTE *)&value;
  68.         RegSetValueEx(hkMAC, _T("MAC56"), 0, REG_DWORD, lpBuffer,dwSize);
  69.     }
  70.     RegCloseKey( hkMAC );
  71. }

 

4.如果有外接EEPROM之类的话,那么一般是写在EEPROM里面的,而不是写注册表。如果这种实现方式必须采用Hive方式的注册表。不然也是保存不了的。

5.特别说明
MAC地址一般由12个16进制数字组成,每2个为一组,前面3组代表网络硬件制造商的编号,这个一般是由IEEE来分配。
后面3组代表该制造商所制造的某个网络产品的序列号。这样也就为什么我们说MAC地址是唯一的了(当然前提是你没有自己修改过)。
wince驱动由于比较特殊,6组值要写入3个IO里面一个IO只能写入16个字节,为了方便我就不按照正常定义为两个部分,
而是简单定义成3个部分,这样容易写代码。
必须说明的是MAC地址前3组值不是能随便更改的,我们一般就改动后3组就行了。非要改就要找一个制造商的编号才行。
另外如果不修改MAC地址不组网是没有问题的,但是如果组网就会有一些小问题,比如ping,wince之间无法相互ping。当然还有别的。