win-pvdrivers

changeset 424:37ed25854efa

Added 'copyconfig' utility to copy rtl8139 ip configuration to xen driver
author James Harper <james.harper@bendigoit.com.au>
date Thu Sep 04 22:26:18 2008 +1000 (2008-09-04)
parents 718d4567551d
children ea14db3ca6f2
files copyconfig/copyconfig.c copyconfig/makefile copyconfig/sources
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/copyconfig/copyconfig.c	Thu Sep 04 22:26:18 2008 +1000
     1.3 @@ -0,0 +1,230 @@
     1.4 +#include <basetyps.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <wtypes.h>
     1.7 +#include <initguid.h>
     1.8 +#include <stdio.h>
     1.9 +#include <string.h>
    1.10 +#include <winioctl.h>
    1.11 +#include <ntddndis.h>
    1.12 +
    1.13 +/*
    1.14 +1. Get an adapter id. You can get it from registry,
    1.15 +\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
    1.16 +- they are specified in this key.
    1.17 +
    1.18 +2. For each adapter id you have to open it using ZwCreateFile to obtain it's
    1.19 +handle to make ioctl's on,
    1.20 +
    1.21 +status = ZwOpenFile(&adapter, GENERIC_READ|GENERIC_WRITE, &oa, &ioStatus,
    1.22 +FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_NON_DIRECTORY_FILE);
    1.23 +
    1.24 +Don't forget to prepend \\??\\ at the begning of the adapter id string.
    1.25 +
    1.26 +3. Once you obtained valid apadter handle, you can query
    1.27 +OID_802_3_PERMANENT_ADDRESS:
    1.28 +
    1.29 +args = OID_802_3_PERMANENT_ADDRESS;
    1.30 +status = ZwDeviceIoControlFile(adapter, 0, 0, 0, &ioStatus,
    1.31 +IOCTL_NDIS_QUERY_GLOBAL_STATS, &args, sizeof(ULONG), controlbuff,
    1.32 +sizeofcontrolbuf);
    1.33 +
    1.34 +Make sure, that you can obtain adapter mac address while the device is up.
    1.35 +If it is down - imagine wi-fi apadter on notebook when it is switched off -
    1.36 +you're out of luck.
    1.37 +*/
    1.38 +
    1.39 +#define ADAPTER_TYPE_XEN
    1.40 +#define ADAPTER_TYPE_OTHER
    1.41 +
    1.42 +typedef struct adapter_details
    1.43 +{
    1.44 +  struct adapter_details *next;
    1.45 +  BYTE mac_address[6];
    1.46 +  CHAR xen_IpConfig_key_name[1024];
    1.47 +  CHAR other_IpConfig_key_name[1024];
    1.48 +} adapter_details_t;
    1.49 +
    1.50 +int __cdecl
    1.51 +main(
    1.52 +//    __in ULONG argc,
    1.53 +//    __in_ecount(argc) PCHAR argv[]
    1.54 +)
    1.55 +{
    1.56 +  HKEY key_handle;
    1.57 +  HKEY adapter_key_handle;
    1.58 +  LONG status;
    1.59 +  CHAR adapter_key_name[256];
    1.60 +  BYTE buf[1024];
    1.61 +  DWORD buf_len;
    1.62 +  CHAR *keyptr;
    1.63 +  HANDLE handle;
    1.64 +  CHAR filename[256];
    1.65 +  NDIS_STATISTICS_VALUE oid_req;
    1.66 +  int i;
    1.67 +  adapter_details_t *list_head = NULL, *prev;
    1.68 +  adapter_details_t *adapter;
    1.69 +  CHAR value_name[256];
    1.70 +  DWORD value_name_len;
    1.71 +  DWORD value_type;
    1.72 +  CHAR value_data[1024];
    1.73 +  DWORD value_data_len;
    1.74 +  HKEY src_key_handle;
    1.75 +  HKEY dst_key_handle;
    1.76 +  
    1.77 +  // Enumerate keys in \\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
    1.78 +  status = RegOpenKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters", &key_handle);
    1.79 +  if (status != ERROR_SUCCESS)
    1.80 +  {
    1.81 +    fprintf(stderr, "Cannot read registry - status = %08x\n", status);
    1.82 +    return 1;
    1.83 +  }
    1.84 +  printf("Enumerating adapters\n");
    1.85 +  buf_len = 39;
    1.86 +  for (i = 0; (status = RegEnumKeyEx(key_handle, i, adapter_key_name, &buf_len, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS; buf_len = 39, i++)
    1.87 +  {
    1.88 +    if (status == ERROR_MORE_DATA)
    1.89 +      continue; /* if the key is longer than a GUID then we aren't interested in it anyway */
    1.90 +    if (status != ERROR_SUCCESS)
    1.91 +      break;
    1.92 +    //printf("buf_len = %d, buf = %s\n", buf_len, buf);
    1.93 +    if (buf_len != 38)
    1.94 +      continue;
    1.95 +    /* check that the name looks like a guid */
    1.96 +    sprintf(filename, "\\\\.\\%s", adapter_key_name);
    1.97 +    handle = CreateFile(filename, FILE_GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    1.98 +    if (!handle)
    1.99 +      continue;
   1.100 +    oid_req.Oid = OID_802_3_PERMANENT_ADDRESS;
   1.101 +    oid_req.DataLength = 10;
   1.102 +    status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
   1.103 +    if (!status || buf_len != 6)
   1.104 +      continue;
   1.105 +    if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0 && buf[4] == 0 && buf[5] == 0)
   1.106 +      continue;
   1.107 +    printf("Found Adapter:\n MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
   1.108 +      buf[0], buf[1], buf[2],
   1.109 +      buf[3], buf[4], buf[5]);
   1.110 +    for (adapter = list_head, prev = NULL; adapter != NULL; prev = adapter, adapter = adapter->next)
   1.111 +    {
   1.112 +      if (memcmp(adapter->mac_address, buf, 6) == 0)
   1.113 +        break;
   1.114 +    }
   1.115 +    if (!adapter)
   1.116 +    {
   1.117 +      adapter = malloc(sizeof(adapter_details_t));
   1.118 +      if (prev == NULL)
   1.119 +        list_head = adapter;
   1.120 +      else
   1.121 +        prev->next = adapter;
   1.122 +      adapter->next = NULL;
   1.123 +      memcpy(adapter->mac_address, buf, 6);
   1.124 +      adapter->xen_IpConfig_key_name[0] = 0;
   1.125 +      adapter->other_IpConfig_key_name[0] = 0;
   1.126 +    }
   1.127 +
   1.128 +    oid_req.Oid = OID_GEN_VENDOR_DESCRIPTION;
   1.129 +    oid_req.DataLength = 256;
   1.130 +    status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
   1.131 +    if (!status)
   1.132 +    {
   1.133 +      printf(" Error opening. Ignoring\n");
   1.134 +      continue;
   1.135 +    }
   1.136 +    printf(" Description = %s\n", buf);
   1.137 +    if (strstr(buf, "Xen"))
   1.138 +    {
   1.139 +      printf(" Type = Xen\n");
   1.140 +      keyptr = adapter->xen_IpConfig_key_name;
   1.141 +    }
   1.142 +    else
   1.143 +    {
   1.144 +      printf(" Type = Other\n");
   1.145 +      keyptr = adapter->other_IpConfig_key_name;
   1.146 +    }
   1.147 +    if (keyptr[0])
   1.148 +    {
   1.149 +      printf(" Multiple Other or multiple Xen adapters exist with the same mac. Ignoring.\n");
   1.150 +      continue;
   1.151 +    }
   1.152 +    buf_len = 1024;
   1.153 +    
   1.154 +    status = RegOpenKey(key_handle, adapter_key_name, &adapter_key_handle);
   1.155 +    if (status != ERROR_SUCCESS)
   1.156 +    {
   1.157 +      printf(" Failed to read registry (%08x). Ignoring.\n", status);
   1.158 +      continue;
   1.159 +    }
   1.160 +    status = RegQueryValueEx(adapter_key_handle, "IpConfig", NULL, NULL, keyptr, &buf_len);
   1.161 +    if (status != ERROR_SUCCESS)
   1.162 +    {
   1.163 +      printf(" Failed to read registry (%08x). Ignoring.\n", status);
   1.164 +      continue;
   1.165 +    }
   1.166 +  }
   1.167 +
   1.168 +  printf("\nCloning IP Configurations\n");
   1.169 +  for (adapter = list_head; adapter != NULL; adapter = adapter->next)
   1.170 +  {
   1.171 +    printf("Considering MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
   1.172 +      adapter->mac_address[0], adapter->mac_address[1], adapter->mac_address[2],
   1.173 +      adapter->mac_address[3], adapter->mac_address[4], adapter->mac_address[5]);
   1.174 +    if (adapter->xen_IpConfig_key_name[0])
   1.175 +      printf(" Xen adapter present\n");
   1.176 +    else
   1.177 +      printf(" Xen adapter not present\n");
   1.178 +    if (adapter->other_IpConfig_key_name[0])
   1.179 +      printf(" Other adapter present\n");
   1.180 +    else
   1.181 +      printf(" Other adapter not present\n");
   1.182 +    
   1.183 +    if (adapter->xen_IpConfig_key_name[0] && adapter->other_IpConfig_key_name[0])
   1.184 +    {
   1.185 +      // open HKLM\SYSTEM\Services\%s
   1.186 +      sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->xen_IpConfig_key_name);
   1.187 +      status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &dst_key_handle);
   1.188 +      if (status != ERROR_SUCCESS)
   1.189 +      {
   1.190 +        printf(" Cannot open Xen adapter config key. Skipping.\n");
   1.191 +        continue;
   1.192 +      }
   1.193 +      sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->other_IpConfig_key_name);
   1.194 +      status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &src_key_handle);
   1.195 +      if (status != ERROR_SUCCESS)
   1.196 +      {
   1.197 +        printf(" Cannot open Other adapter config key. Skipping.\n");
   1.198 +        continue;
   1.199 +      }
   1.200 +      value_name_len = 256;
   1.201 +      value_data_len = 1024;
   1.202 +      while ((status = RegEnumValue(dst_key_handle, 0, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
   1.203 +      {
   1.204 +        RegDeleteValue(dst_key_handle, value_name);
   1.205 +        value_name_len = 256;
   1.206 +        value_data_len = 1024;
   1.207 +      }
   1.208 +      i = 0;
   1.209 +      value_name_len = 256;
   1.210 +      value_data_len = 1024;
   1.211 +      while ((status = RegEnumValue(src_key_handle, i, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
   1.212 +      {
   1.213 +        RegSetValueEx(dst_key_handle, value_name, 0, value_type, value_data, value_data_len);
   1.214 +        value_name_len = 256;
   1.215 +        value_data_len = 1024;
   1.216 +        i++;
   1.217 +      }
   1.218 +      printf(" Copied\n", buf);
   1.219 +    }
   1.220 +  }
   1.221 +  
   1.222 +  // loop through key names that look like GUIDs
   1.223 +  //   get OID_GEN_VENDOR_DESCRIPTION
   1.224 +  //   if description does not contain "RTL8139" then continue to next key name
   1.225 +  //   get OID_802_3_PERMANENT_ADDRESS
   1.226 +  //   get IpConfig path value registry
   1.227 +  //   again loop through key names that look like GUIDs
   1.228 +  //     if description does not contain "xen" then continue to next key name
   1.229 +  //     get IpConfig path value from registry
   1.230 +  //     delete contents of above path
   1.231 +  //     copy contents of RTL8139 IpConfig to Xen IpConfig (use RegCopyTree)
   1.232 +}
   1.233 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/copyconfig/makefile	Thu Sep 04 22:26:18 2008 +1000
     2.3 @@ -0,0 +1,9 @@
     2.4 +#
     2.5 +# DO NOT EDIT THIS FILE!!!  Edit .\sources. if you want to add a new source
     2.6 +# file to this component.  This file merely indirects to the real make file
     2.7 +# that is shared by all the driver components of the Windows NT DDK
     2.8 +#
     2.9 +
    2.10 +!INCLUDE $(NTMAKEENV)\makefile.def
    2.11 +
    2.12 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/copyconfig/sources	Thu Sep 04 22:26:18 2008 +1000
     3.3 @@ -0,0 +1,9 @@
     3.4 +TARGETNAME=copyconfig
     3.5 +TARGETTYPE=PROGRAM
     3.6 +TARGETPATH=..\Target
     3.7 +UMTYPE=console
     3.8 +UMENTRY=main
     3.9 +UMBASE=0x400000
    3.10 +_NT_TARGET_VERSION= $(_NT_TARGET_VERSION_WIN2K)
    3.11 +SOURCES=copyconfig.c
    3.12 +USE_MSVCRT=1