win-pvdrivers

annotate copyconfig/copyconfig.c @ 731:3b46c7474a82

Fix installer so that it definitely waits until all drivers are installed properly
author James Harper <james.harper@bendigoit.com.au>
date Sat Jan 02 10:22:04 2010 +1100 (2010-01-02)
parents 5bdb7251370c
children 996f1c7d2cb3
rev   line source
steve@460 1 #pragma warning(disable: 4201)
james@424 2 #include <basetyps.h>
james@424 3 #include <stdlib.h>
james@424 4 #include <wtypes.h>
james@424 5 #include <initguid.h>
james@424 6 #include <stdio.h>
james@424 7 #include <string.h>
james@424 8 #include <winioctl.h>
james@424 9 #include <ntddndis.h>
james@716 10 #include <strsafe.h>
james@424 11
james@424 12 #define ADAPTER_TYPE_XEN
james@424 13 #define ADAPTER_TYPE_OTHER
james@424 14
james@716 15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
james@716 16
james@424 17 typedef struct adapter_details
james@424 18 {
james@424 19 struct adapter_details *next;
james@424 20 BYTE mac_address[6];
james@424 21 CHAR xen_IpConfig_key_name[1024];
james@424 22 CHAR other_IpConfig_key_name[1024];
james@424 23 } adapter_details_t;
james@424 24
james@424 25 int __cdecl
james@424 26 main(
james@424 27 // __in ULONG argc,
james@424 28 // __in_ecount(argc) PCHAR argv[]
james@424 29 )
james@424 30 {
james@424 31 HKEY key_handle;
james@424 32 HKEY adapter_key_handle;
james@424 33 LONG status;
james@424 34 CHAR adapter_key_name[256];
james@424 35 BYTE buf[1024];
james@424 36 DWORD buf_len;
james@424 37 CHAR *keyptr;
james@424 38 HANDLE handle;
james@424 39 CHAR filename[256];
james@424 40 NDIS_STATISTICS_VALUE oid_req;
james@424 41 int i;
james@424 42 adapter_details_t *list_head = NULL, *prev;
james@424 43 adapter_details_t *adapter;
james@424 44 CHAR value_name[256];
james@424 45 DWORD value_name_len;
james@424 46 DWORD value_type;
james@424 47 CHAR value_data[1024];
james@424 48 DWORD value_data_len;
james@424 49 HKEY src_key_handle;
james@424 50 HKEY dst_key_handle;
james@424 51
james@424 52 // Enumerate keys in \\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
james@424 53 status = RegOpenKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters", &key_handle);
james@424 54 if (status != ERROR_SUCCESS)
james@424 55 {
james@424 56 fprintf(stderr, "Cannot read registry - status = %08x\n", status);
james@424 57 return 1;
james@424 58 }
james@424 59 printf("Enumerating adapters\n");
james@424 60 buf_len = 39;
james@424 61 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++)
james@424 62 {
james@424 63 if (status == ERROR_MORE_DATA)
james@424 64 continue; /* if the key is longer than a GUID then we aren't interested in it anyway */
james@424 65 if (status != ERROR_SUCCESS)
james@424 66 break;
james@424 67 //printf("buf_len = %d, buf = %s\n", buf_len, buf);
james@424 68 if (buf_len != 38)
james@424 69 continue;
james@424 70 /* check that the name looks like a guid */
james@716 71 StringCbPrintfA(filename, ARRAY_SIZE(filename), "\\\\.\\%s", adapter_key_name);
james@424 72 handle = CreateFile(filename, FILE_GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
james@424 73 if (!handle)
james@424 74 continue;
james@424 75 oid_req.Oid = OID_802_3_PERMANENT_ADDRESS;
james@424 76 oid_req.DataLength = 10;
james@424 77 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
james@424 78 if (!status || buf_len != 6)
james@424 79 continue;
james@424 80 if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0 && buf[4] == 0 && buf[5] == 0)
james@424 81 continue;
james@424 82 printf("Found Adapter:\n MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
james@424 83 buf[0], buf[1], buf[2],
james@424 84 buf[3], buf[4], buf[5]);
james@424 85 for (adapter = list_head, prev = NULL; adapter != NULL; prev = adapter, adapter = adapter->next)
james@424 86 {
james@424 87 if (memcmp(adapter->mac_address, buf, 6) == 0)
james@424 88 break;
james@424 89 }
james@424 90 if (!adapter)
james@424 91 {
james@424 92 adapter = malloc(sizeof(adapter_details_t));
james@424 93 if (prev == NULL)
james@424 94 list_head = adapter;
james@424 95 else
james@424 96 prev->next = adapter;
james@424 97 adapter->next = NULL;
james@424 98 memcpy(adapter->mac_address, buf, 6);
james@424 99 adapter->xen_IpConfig_key_name[0] = 0;
james@424 100 adapter->other_IpConfig_key_name[0] = 0;
james@424 101 }
james@424 102
james@424 103 oid_req.Oid = OID_GEN_VENDOR_DESCRIPTION;
james@424 104 oid_req.DataLength = 256;
james@424 105 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
james@424 106 if (!status)
james@424 107 {
james@424 108 printf(" Error opening. Ignoring\n");
james@424 109 continue;
james@424 110 }
james@424 111 printf(" Description = %s\n", buf);
steve@460 112 if (strstr((char *)buf, "Xen"))
james@424 113 {
james@424 114 printf(" Type = Xen\n");
james@424 115 keyptr = adapter->xen_IpConfig_key_name;
james@424 116 }
james@424 117 else
james@424 118 {
james@424 119 printf(" Type = Other\n");
james@424 120 keyptr = adapter->other_IpConfig_key_name;
james@424 121 }
james@424 122 if (keyptr[0])
james@424 123 {
james@424 124 printf(" Multiple Other or multiple Xen adapters exist with the same mac. Ignoring.\n");
james@424 125 continue;
james@424 126 }
james@424 127 buf_len = 1024;
james@424 128
james@424 129 status = RegOpenKey(key_handle, adapter_key_name, &adapter_key_handle);
james@424 130 if (status != ERROR_SUCCESS)
james@424 131 {
james@424 132 printf(" Failed to read registry (%08x). Ignoring.\n", status);
james@424 133 continue;
james@424 134 }
steve@460 135 status = RegQueryValueEx(adapter_key_handle, "IpConfig", NULL, NULL, (LPBYTE)keyptr, &buf_len);
james@424 136 if (status != ERROR_SUCCESS)
james@424 137 {
james@424 138 printf(" Failed to read registry (%08x). Ignoring.\n", status);
james@424 139 continue;
james@424 140 }
james@424 141 }
james@424 142
james@424 143 printf("\nCloning IP Configurations\n");
james@424 144 for (adapter = list_head; adapter != NULL; adapter = adapter->next)
james@424 145 {
james@424 146 printf("Considering MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
james@424 147 adapter->mac_address[0], adapter->mac_address[1], adapter->mac_address[2],
james@424 148 adapter->mac_address[3], adapter->mac_address[4], adapter->mac_address[5]);
james@424 149 if (adapter->xen_IpConfig_key_name[0])
james@424 150 printf(" Xen adapter present\n");
james@424 151 else
james@424 152 printf(" Xen adapter not present\n");
james@424 153 if (adapter->other_IpConfig_key_name[0])
james@424 154 printf(" Other adapter present\n");
james@424 155 else
james@424 156 printf(" Other adapter not present\n");
james@424 157
james@424 158 if (adapter->xen_IpConfig_key_name[0] && adapter->other_IpConfig_key_name[0])
james@424 159 {
james@424 160 // open HKLM\SYSTEM\Services\%s
james@716 161 StringCbPrintfA((char *)buf, ARRAY_SIZE(buf), "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->xen_IpConfig_key_name);
steve@460 162 status = RegOpenKey(HKEY_LOCAL_MACHINE, (LPCSTR)buf, &dst_key_handle);
james@424 163 if (status != ERROR_SUCCESS)
james@424 164 {
james@424 165 printf(" Cannot open Xen adapter config key. Skipping.\n");
james@424 166 continue;
james@424 167 }
james@716 168 StringCbPrintfA((char *)buf, ARRAY_SIZE(buf), "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->other_IpConfig_key_name);
steve@460 169 status = RegOpenKey(HKEY_LOCAL_MACHINE, (LPCSTR)buf, &src_key_handle);
james@424 170 if (status != ERROR_SUCCESS)
james@424 171 {
james@424 172 printf(" Cannot open Other adapter config key. Skipping.\n");
james@424 173 continue;
james@424 174 }
james@424 175 value_name_len = 256;
james@424 176 value_data_len = 1024;
steve@460 177 while ((status = RegEnumValue(dst_key_handle, 0, value_name, &value_name_len, NULL, &value_type, (LPBYTE)value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
james@424 178 {
james@424 179 RegDeleteValue(dst_key_handle, value_name);
james@424 180 value_name_len = 256;
james@424 181 value_data_len = 1024;
james@424 182 }
james@424 183 i = 0;
james@424 184 value_name_len = 256;
james@424 185 value_data_len = 1024;
steve@460 186 while ((status = RegEnumValue(src_key_handle, i, value_name, &value_name_len, NULL, &value_type, (LPBYTE)value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
james@424 187 {
steve@460 188 RegSetValueEx(dst_key_handle, value_name, 0, value_type, (BYTE *)value_data, value_data_len);
james@424 189 value_name_len = 256;
james@424 190 value_data_len = 1024;
james@424 191 i++;
james@424 192 }
james@424 193 printf(" Copied\n", buf);
james@424 194 }
james@424 195 }
james@731 196 return 0;
james@424 197
james@424 198 // loop through key names that look like GUIDs
james@424 199 // get OID_GEN_VENDOR_DESCRIPTION
james@424 200 // if description does not contain "RTL8139" then continue to next key name
james@424 201 // get OID_802_3_PERMANENT_ADDRESS
james@424 202 // get IpConfig path value registry
james@424 203 // again loop through key names that look like GUIDs
james@424 204 // if description does not contain "xen" then continue to next key name
james@424 205 // get IpConfig path value from registry
james@424 206 // delete contents of above path
james@424 207 // copy contents of RTL8139 IpConfig to Xen IpConfig (use RegCopyTree)
james@424 208 }
james@424 209