1 /*
2  * Copyright 2001 Andreas Mohr
3  * Copyright 2005-2006 Herv� Poussineau
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 #ifndef __SETUPAPI_PRIVATE_H
21 #define __SETUPAPI_PRIVATE_H
22 
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <share.h>
28 #include <wchar.h>
29 
30 #define WIN32_NO_STATUS
31 #define COBJMACROS
32 #include <windows.h>
33 #include <aclapi.h>
34 #include <cfgmgr32.h>
35 #include <fdi.h>
36 #include <reason.h>
37 #include <regstr.h>
38 #include <sddl.h>
39 #include <setupapi.h>
40 #include <shlobj.h>
41 #include <wine/debug.h>
42 #include <wine/unicode.h>
43 #define NTOS_MODE_USER
44 #include <ndk/ntndk.h>
45 
46 #include <pseh/pseh2.h>
47 
48 /* This hack definition is necessary as long as setupapi
49    depends on Wine "compatibility" headers */
50 typedef ULONG RESOURCEID;
51 typedef RESOURCEID *PRESOURCEID;
52 
53 #include <pnp_c.h>
54 #include "rpc_private.h"
55 #include "resource.h"
56 
57 #ifdef __REACTOS__
58 #undef __WINESRC__
59 #endif
60 
61 #define SETUP_DEVICE_INFO_SET_MAGIC 0xd00ff057
62 #define SETUP_CLASS_IMAGE_LIST_MAGIC 0xd00ff058
63 
64 struct DeviceInterface /* Element of DeviceInfo.InterfaceListHead */
65 {
66     LIST_ENTRY ListEntry;
67 
68     /* Link to is parent device */
69     struct DeviceInfo *DeviceInfo;
70     GUID InterfaceClassGuid;
71 
72 
73     /* SPINT_ACTIVE : the interface is active/enabled
74      * SPINT_DEFAULT: the interface is the default interface for the device class
75      * SPINT_REMOVED: the interface is removed
76      */
77     DWORD Flags;
78 
79     /* Contains the symbolic link of this interface, for example
80      * \\?\ACPI#PNP0501#4&2658d0a0&0#{GUID} */
81     WCHAR SymbolicLink[ANYSIZE_ARRAY];
82 };
83 
84 /* We don't want to open the .inf file to read only one information in it, so keep a handle to it once it
85  * has been already loaded once. Keep also a reference counter */
86 struct InfFileDetails
87 {
88     /* Handle to the .inf file */
89     HINF hInf;
90     /* Reference count to this object. Once it raises 0, the .inf file is
91      * automatically closed and this memory structure is deleted */
92     LONG References;
93 
94     /* Contains the directory name of the .inf file.
95      * Points into szData at then end of the structure */
96     PCWSTR DirectoryName;
97     /* Contains the .inf file name (without directory name).
98      * Points into szData at then end of the structure */
99     PCWSTR FileName;
100 
101     /* Variable size array (contains data for DirectoryName and FileName) */
102     WCHAR szData[ANYSIZE_ARRAY];
103 };
104 
105 struct DriverInfoElement /* Element of DeviceInfoSet.DriverListHead and DeviceInfo.DriverListHead */
106 {
107     LIST_ENTRY ListEntry;
108 
109     SP_DRVINSTALL_PARAMS Params;
110     ULARGE_INTEGER DriverDate;
111     SP_DRVINFO_DATA_V2_W Info;
112     SP_DRVINFO_DETAIL_DATA_W Details;
113     GUID ClassGuid;
114     LPWSTR MatchingId;
115     struct InfFileDetails *InfFileDetails;
116 };
117 
118 struct ClassInstallParams
119 {
120     PSP_PROPCHANGE_PARAMS PropChangeParams;
121     PSP_ADDPROPERTYPAGE_DATA AddPropertyPageData;
122 };
123 
124 struct DeviceInfo /* Element of DeviceInfoSet.ListHead */
125 {
126     LIST_ENTRY ListEntry;
127     /* Used when dealing with CM_* functions */
128     DEVINST dnDevInst;
129 
130     /* Link to parent DeviceInfoSet */
131     struct DeviceInfoSet *set;
132 
133     /* Reserved Field of SP_DEVINSTALL_PARAMS_W structure
134      * points to a struct DriverInfoElement */
135     SP_DEVINSTALL_PARAMS_W InstallParams;
136 
137     /* Information about devnode:
138      * - instanceId:
139      *       "Root\*PNP0501" for example.
140      *       It doesn't contain the unique ID for the device
141      *       (points into the Data field at the end of the structure)
142      *       WARNING: no NULL char exist between instanceId and UniqueId
143      *       in Data field!
144      * - UniqueId
145      *       "5&1be2108e&0" or "0000"
146      *       If DICD_GENERATE_ID is specified in creation flags,
147      *       this unique ID is autogenerated using 4 digits, base 10
148      *       (points into the Data field at the end of the structure)
149      * - DeviceDescription
150      *       String which identifies the device. Can be NULL. If not NULL,
151      *       points into the Data field at the end of the structure
152      * - ClassGuid
153      *       Identifies the class of this device. It is GUID_NULL if the
154      *       device has not been installed
155      * - CreationFlags
156      *       Is a combination of:
157      *       - DICD_GENERATE_ID
158      *              the unique ID needs to be generated
159      *       - DICD_INHERIT_CLASSDRVS
160      *              inherit driver of the device info set (== same pointer)
161      */
162     PCWSTR instanceId;
163     PCWSTR UniqueId;
164     PCWSTR DeviceDescription;
165     GUID ClassGuid;
166     DWORD CreationFlags;
167 
168     /* If CreationFlags contains DICD_INHERIT_CLASSDRVS, this list is invalid */
169     /* If the driver is not searched/detected, this list is empty */
170     LIST_ENTRY DriverListHead; /* List of struct DriverInfoElement */
171 
172     /* List of interfaces implemented by this device */
173     LIST_ENTRY InterfaceListHead; /* List of struct DeviceInterface */
174 
175     /* Used by SetupDiGetClassInstallParamsW/SetupDiSetClassInstallParamsW */
176     struct ClassInstallParams ClassInstallParams;
177 
178     /* Variable size array (contains data for instanceId, UniqueId, DeviceDescription) */
179     WCHAR Data[ANYSIZE_ARRAY];
180 };
181 
182 struct DeviceInfoSet /* HDEVINFO */
183 {
184     DWORD magic; /* SETUP_DEVICE_INFO_SET_MAGIC */
185     /* If != GUID_NULL, only devices of this class can be in the device info set */
186     GUID ClassGuid;
187     /* Local or distant HKEY_LOCAL_MACHINE registry key */
188     HKEY HKLM;
189     /* Used when dealing with CM_* functions */
190     HMACHINE hMachine;
191 
192     /* Reserved Field points to a struct DriverInfoElement */
193     SP_DEVINSTALL_PARAMS_W InstallParams;
194 
195     /* List of struct DriverInfoElement (if no driver has been
196      * searched/detected, this list is empty) */
197     LIST_ENTRY DriverListHead;
198 
199     /* List of struct DeviceInfo */
200     LIST_ENTRY ListHead;
201     struct DeviceInfo *SelectedDevice;
202 
203     /* Used by SetupDiGetClassInstallParamsW/SetupDiSetClassInstallParamsW */
204     struct ClassInstallParams ClassInstallParams;
205 
206     /* Contains the name of the remote computer ('\\COMPUTERNAME' for example),
207      * or NULL if related to local machine. Points into szData field at the
208      * end of the structure */
209     PCWSTR MachineName;
210 
211     /* Variable size array (contains data for MachineName) */
212     WCHAR szData[ANYSIZE_ARRAY];
213 };
214 
215 struct ClassImageList
216 {
217     DWORD magic; /* SETUP_CLASS_IMAGE_LIST_MAGIC */
218 
219     /* Number of GUIDs contained in Guids and IconIndexes arrays */
220     DWORD NumberOfGuids;
221     /* Array of GUIDs associated to icons of the image list. Its size
222      * is NumberOfGuids and is pointing after the end this structure */
223     GUID* Guids;
224     /* Array of corresponding icons index in the image list. Its size
225      * is NumberOfGuids and is pointing after the end this structure */
226     INT* IconIndexes;
227 };
228 
229 struct FileLog /* HSPFILELOG */
230 {
231     DWORD ReadOnly;
232     DWORD SystemLog;
233     LPWSTR LogName;
234 };
235 
236 extern HINSTANCE hInstance;
237 #define RC_STRING_MAX_SIZE 256
238 
239 #define REG_INSTALLEDFILES "System\\CurrentControlSet\\Control\\InstalledFiles"
240 #define REGPART_RENAME "\\Rename"
241 #define REG_VERSIONCONFLICT "Software\\Microsoft\\VersionConflictManager"
242 
243 inline static WCHAR *strdupAtoW( const char *str )
244 {
245     WCHAR *ret = NULL;
246     if (str)
247     {
248         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
249         if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
250             MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
251     }
252     return ret;
253 }
254 
255 /* string substitutions */
256 
257 struct inf_file;
258 extern const WCHAR *DIRID_get_string( int dirid );
259 extern unsigned int PARSER_string_substA( const struct inf_file *file, const WCHAR *text,
260                                           char *buffer, unsigned int size );
261 extern unsigned int PARSER_string_substW( const struct inf_file *file, const WCHAR *text,
262                                           WCHAR *buffer, unsigned int size );
263 extern const WCHAR *PARSER_get_inf_filename( HINF hinf );
264 extern WCHAR *PARSER_get_src_root( HINF hinf );
265 extern WCHAR *PARSER_get_dest_dir( INFCONTEXT *context );
266 
267 /* support for Ascii queue callback functions */
268 
269 struct callback_WtoA_context
270 {
271     void               *orig_context;
272     PSP_FILE_CALLBACK_A orig_handler;
273 };
274 
275 UINT CALLBACK QUEUE_callback_WtoA( void *context, UINT notification, UINT_PTR, UINT_PTR );
276 
277 /* from msvcrt/sys/stat.h */
278 #define _S_IWRITE 0x0080
279 #define _S_IREAD  0x0100
280 
281 extern HINSTANCE hInstance;
282 extern OSVERSIONINFOW OsVersionInfo;
283 
284 /* devinst.c */
285 
286 BOOL
287 CreateDeviceInfo(
288     IN struct DeviceInfoSet *list,
289     IN LPCWSTR InstancePath,
290     IN LPCGUID pClassGuid,
291     OUT struct DeviceInfo **pDeviceInfo);
292 
293 LONG
294 SETUP_CreateDevicesList(
295     IN OUT struct DeviceInfoSet *list,
296     IN PCWSTR MachineName OPTIONAL,
297     IN CONST GUID *Class OPTIONAL,
298     IN PCWSTR Enumerator OPTIONAL);
299 
300 /* driver.c */
301 
302 struct InfFileDetails *
303 CreateInfFileDetails(
304     IN LPCWSTR FullInfFileName);
305 
306 VOID
307 DereferenceInfFile(struct InfFileDetails* infFile);
308 
309 BOOL
310 DestroyDriverInfoElement(struct DriverInfoElement* driverInfo);
311 
312 /* install.c */
313 
314 BOOL
315 GetStringField( PINFCONTEXT context, DWORD index, PWSTR *value);
316 
317 /* interface.c */
318 
319 BOOL
320 DestroyDeviceInterface(
321     struct DeviceInterface* deviceInterface);
322 
323 LONG
324 SETUP_CreateInterfaceList(
325     struct DeviceInfoSet *list,
326     PCWSTR MachineName,
327     CONST GUID *InterfaceGuid,
328     PCWSTR DeviceInstanceW /* OPTIONAL */,
329     BOOL OnlyPresentInterfaces);
330 
331 /* misc.c */
332 
333 DWORD
334 GetFunctionPointer(
335     IN PWSTR InstallerName,
336     OUT HMODULE* ModulePointer,
337     OUT PVOID* FunctionPointer);
338 
339 DWORD
340 FreeFunctionPointer(
341     IN HMODULE ModulePointer,
342     IN PVOID FunctionPointer);
343 
344 DWORD
345 WINAPI
346 pSetupStringFromGuid(LPGUID lpGUID, PWSTR pString, DWORD dwStringLen);
347 
348 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst);
349 
350 VOID WINAPI MyFree(LPVOID lpMem);
351 LPVOID WINAPI MyMalloc(DWORD dwSize);
352 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize);
353 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc);
354 BOOL WINAPI IsUserAdmin(VOID);
355 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage);
356 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage);
357 
358 /* parser.c */
359 
360 typedef BOOL (*FIND_CALLBACK)(LPCWSTR SectionName, PVOID Context);
361 BOOL EnumerateSectionsStartingWith(HINF hInf, LPCWSTR pStr, FIND_CALLBACK Callback, PVOID Context);
362 
363 #endif /* __SETUPAPI_PRIVATE_H */
364