win-pvdrivers

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