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

来源:互联网 发布:浏览器广告拦截软件 编辑:程序博客网 时间:2024/06/10 08:42

http://blog.csdn.net/gaoling179/article/details/5982511

 

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

 

下面是我解决的方法:

 

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

 

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


WORD iMAC[3]={0x3322,0x5544,0x0F66};  //添加MAC地址的原始数据 
//添加函数,读取注册表中的MAC地址的值 
void ReadRegsister() 
{ 
    HKEY hkMAC = NULL; 
    DWORD MAC; 
    DWORD dwStatus, dwType, dwSize; 
        TCHAR gszBaseInstance[256] = _T("
//Comm//CS8900//Parms"); 
    // open the registry key and read our configuration 
    dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC); 
    dwType = REG_DWORD;

    if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD)  
        { 
        dwSize = sizeof(DWORD); 
        //下面读取失败的话那么就用初始值作为MAC地址 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC12"), NULL, &dwType, (LPBYTE) &MAC, &dwSize); 
        if (dwStatus == ERROR_SUCCESS) 
        { 
            iMAC[0]=MAC; 
        } 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC34"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);   
        if (dwStatus == ERROR_SUCCESS) 
        { 
            iMAC[1]=MAC; 
        } 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC56"), NULL, &dwType, (LPBYTE) &MAC, &dwSize);   
        if (dwStatus == ERROR_SUCCESS) 
        { 
            iMAC[2]=MAC; 
        } 
    }

}

int initCS() 
{    
    CS8900WriteRegister(PKTPG_LINE_CTL, LINE_CTL_10_BASE_T); 
    CS8900WriteRegister(PKTPG_RX_CFG, RX_CFG_RX_OK_I_E); 
    CS8900WriteRegister(PKTPG_RX_CTL,RX_CTL_RX_OK_A | RX_CTL_IND_ADDR_A |RX_CTL_BROADCAST_A); 
    CS8900WriteRegister(PKTPG_TX_CFG, 0);  
    CS8900WriteRegister(PKTPG_BUF_CFG, 0);  
     
    //modify by constantine  
    ReadRegsister(); 
    CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 0, iMAC[0]); 
    CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 2, iMAC[1]); 
    CS8900WriteRegister(PKTPG_INDIVISUAL_ADDR + 4, iMAC[2]);

    initIrq(); 
    return TRUE; 
}

BOOLEAN CS8900ReadEthernetAddress( 
    IN PCS8900_ADAPTER Adapter 
) 
{ 
//modify by constantine 
    //Adapter->PermanentAddress[0] = 0x22; 
    //Adapter->PermanentAddress[1] = 0x33; 
    //Adapter->PermanentAddress[2] = 0x44; 
    //Adapter->PermanentAddress[3] = 0x55; 
    //Adapter->PermanentAddress[4] = 0x66; 
    //Adapter->PermanentAddress[5] = 0x0F; 
     
    Adapter->PermanentAddress[0] = iMAC[0]& 0x00FF; 
    Adapter->PermanentAddress[1] = iMAC[0]>>8; 
    Adapter->PermanentAddress[2] = iMAC[1]& 0x00FF; 
    Adapter->PermanentAddress[3] = iMAC[1]>>8; 
    Adapter->PermanentAddress[4] = iMAC[2]& 0x00FF; 
    Adapter->PermanentAddress[5] = iMAC[2]>>8;  
//end     
    RETAILMSG(1, 
        (TEXT("CS8900: PermanentAddress [ %02x-%02x-%02x-%02x-%02x-%02x ]/r/n"), 
            Adapter->PermanentAddress[0], 
            Adapter->PermanentAddress[1], 
            Adapter->PermanentAddress[2], 
            Adapter->PermanentAddress[3], 
            Adapter->PermanentAddress[4], 
            Adapter->PermanentAddress[5]));

    // 
    // Use the burned in address as the station address, unless the 
    // registry specified an override value. 
    // 
    if ((Adapter->StationAddress[0] == 0x00) & 
        (Adapter->StationAddress[1] == 0x00) & 
        (Adapter->StationAddress[2] == 0x00) & 
        (Adapter->StationAddress[3] == 0x00) & 
        (Adapter->StationAddress[4] == 0x00) & 
        (Adapter->StationAddress[5] == 0x00) 
        ) 
    { 
        RETAILMSG(1, (TEXT("CS8900: StationAddress Modified!.../r/n"))); 
        Adapter->StationAddress[0] = Adapter->PermanentAddress[0]; 
        Adapter->StationAddress[1] = Adapter->PermanentAddress[1]; 
        Adapter->StationAddress[2] = Adapter->PermanentAddress[2]; 
        Adapter->StationAddress[3] = Adapter->PermanentAddress[3]; 
        Adapter->StationAddress[4] = Adapter->PermanentAddress[4]; 
        Adapter->StationAddress[5] = Adapter->PermanentAddress[5]; 
    }

    return(TRUE); 
}
 

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

 

3.写代码修改注册表

下面是关键代码:

 

void CWINCEMACDlg::ReadRegsister() 
{ 
    HKEY hkMAC = NULL; 
    DWORD MAC=0; 
    DWORD dwStatus, dwType, dwSize; 
    TCHAR gszBaseInstance[256] = _T("//Comm//CS8900//Parms"); 
    // open the registry key and read our configuration 
    dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC); 
    dwType = REG_DWORD;

    if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD)  
    { 
        dwSize = sizeof(DWORD); 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC12"), NULL, &dwType, (LPBYTE) &MAC, &dwSize); 
        if (dwStatus == ERROR_SUCCESS) 
        { 
            m_EDT0.Format(_T("%02x"),(MAC & 0x00ff)); 
            m_EDT1.Format(_T("%02x"),(MAC >> 8)); 
        } 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC34"), NULL, &dwType, (LPBYTE) &MAC, &dwSize); 
        if (dwStatus == ERROR_SUCCESS) 
        { 
            m_EDT2.Format(_T("%02x"),(MAC & 0x00ff)); 
            m_EDT3.Format(_T("%02x"),(MAC >> 8)); 
        } 
        dwStatus = RegQueryValueEx(hkMAC, _T("MAC56"), NULL, &dwType, (LPBYTE) &MAC, &dwSize); 
        if (dwStatus == ERROR_SUCCESS) 
        { 
            m_EDT4.Format(_T("%02x"),(MAC & 0x00ff)); 
            m_EDT5.Format(_T("%02x"),(MAC >> 8)); 
        } 
    } 
    RegCloseKey( hkMAC ); 
    UpdateData(FALSE); 
}


void CWINCEMACDlg::WriteRegsister() 
{ 
    HKEY hkMAC = NULL; 
    DWORD dwStatus, dwSize; 
    TCHAR gszBaseInstance[256] = _T("//Comm//CS8900//Parms"); 
    dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gszBaseInstance, 0, KEY_ALL_ACCESS, &hkMAC);

    if(dwStatus == ERROR_SUCCESS )  
    { 
        dwSize = sizeof(DWORD); 
        int value; 
        TCHAR num[4]={0}; 
        BYTE * lpBuffer; 
        UpdateData(TRUE); 
                value=0; 
        wsprintf(num,L"0x%s", m_EDT1.GetBuffer(0)); 
        value += _ttoi(num)<<8; 
        wsprintf(num,L"0x%s", m_EDT0.GetBuffer(0)); 
        value += _ttoi(num); 
        lpBuffer = (BYTE *)&value; 
        RegSetValueEx(hkMAC, _T("MAC12"), 0, REG_DWORD, lpBuffer,dwSize);

        value=0; 
        wsprintf(num,L"0x%s", m_EDT3.GetBuffer(0)); 
        value += _ttoi(num)<<8; 
        wsprintf(num,L"0x%s", m_EDT2.GetBuffer(0)); 
        value += _ttoi(num); 
        lpBuffer = (BYTE *)&value; 
        RegSetValueEx(hkMAC, _T("MAC34"), 0, REG_DWORD, lpBuffer,dwSize);

        value=0; 
        wsprintf(num,L"0x%s", m_EDT5.GetBuffer(0)); 
        value += _ttoi(num)<<8; 
        wsprintf(num,L"0x%s", m_EDT4.GetBuffer(0)); 
        value += _ttoi(num); 
        lpBuffer = (BYTE *)&value; 
        RegSetValueEx(hkMAC, _T("MAC56"), 0, REG_DWORD, lpBuffer,dwSize); 
    } 
    RegCloseKey( hkMAC ); 
}
 

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。当然还有别的。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/constantine/archive/2008/08/28/2842286.aspx

 

原创粉丝点击