reshuffling of dlls
[reactos.git] / reactos / dll / win32 / setupapi / setupx_main.c
1 /*
2 * SETUPX library
3 *
4 * Copyright 1998,2000 Andreas Mohr
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * FIXME: Rather non-functional functions for now.
21 *
22 * See:
23 * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
24 * http://willemer.de/informatik/windows/inf_info.htm (German)
25 * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
26 * DDK: setupx.h
27 * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
28 * http://www.rdrop.com/~cary/html/inf_faq.html
29 * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
30 *
31 * Stuff tested with:
32 * - rs405deu.exe (German Acroread 4.05 setup)
33 * - ie5setup.exe
34 * - Netmeeting
35 *
36 * FIXME:
37 * - string handling is... weird ;) (buflen etc.)
38 * - memory leaks ?
39 * - separate that mess (but probably only when it's done completely)
40 *
41 * SETUPX consists of several parts with the following acronyms/prefixes:
42 * Di device installer (devinst.c ?)
43 * Gen generic installer (geninst.c ?)
44 * Ip .INF parsing (infparse.c)
45 * LDD logical device descriptor (ldd.c ?)
46 * LDID logical device ID
47 * SU setup (setup.c ?)
48 * Tp text processing (textproc.c ?)
49 * Vcp virtual copy module (vcp.c ?)
50 * ...
51 *
52 * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
53 * "close all open applications".
54 * All in all the design of it seems to be a bit weak.
55 * Not sure whether my implementation of it is better, though ;-)
56 */
57
58 #include <stdlib.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winreg.h"
65 #include "winerror.h"
66 #include "wine/winuser16.h"
67 #include "wownt32.h"
68 #include "wingdi.h"
69 #include "winuser.h"
70 #include "winnls.h"
71 #include "setupapi.h"
72 #include "setupx16.h"
73 #include "setupapi_private.h"
74 #include "winerror.h"
75 #include "wine/debug.h"
76
77 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
78
79 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
80
81 /***********************************************************************
82 * SURegOpenKey (SETUPX.47)
83 */
84 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, PHKEY retkey )
85 {
86 FIXME("(%p,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
87 return RegOpenKeyA( hkey, lpszSubKey, retkey );
88 }
89
90 /***********************************************************************
91 * SURegQueryValueEx (SETUPX.50)
92 */
93 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
94 LPDWORD lpdwReserved, LPDWORD lpdwType,
95 LPBYTE lpbData, LPDWORD lpcbData )
96 {
97 FIXME("(%p,%s,%p,%p,%p,%ld), semi-stub.\n",hkey,debugstr_a(lpszValueName),
98 lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
99 return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
100 lpbData, lpcbData );
101 }
102
103
104 /***********************************************************************
105 * InstallHinfSection (SETUPX.527)
106 *
107 * hwnd = parent window
108 * hinst = instance of SETUPX.DLL
109 * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
110 * Here "DefaultInstall" is the .inf file section to be installed (optional).
111 * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
112 *
113 * nCmdShow = nCmdShow of CreateProcess
114 */
115 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
116 {
117 InstallHinfSectionA( HWND_32(hwnd), HINSTANCE_32(hinst), lpszCmdLine, nCmdShow );
118 return OK;
119 }
120
121 typedef struct
122 {
123 LPCSTR RegValName;
124 LPCSTR StdString; /* fallback string; sub dir of windows directory */
125 } LDID_DATA;
126
127 static const LDID_DATA LDID_Data[34] =
128 {
129 { /* 0 (LDID_NULL) -- not defined */
130 NULL,
131 NULL
132 },
133 { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
134 "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
135 NULL
136 },
137 { /* 2 (LDID_SETUPTEMP) = setup temp dir */
138 "SetupTempDir",
139 NULL
140 },
141 { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
142 "UninstallDir",
143 NULL
144 },
145 { /* 4 (LDID_BACKUP) = backup dir */
146 "BackupDir",
147 NULL
148 },
149 { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
150 "SetupScratchDir",
151 NULL
152 },
153 { /* 6 -- not defined */
154 NULL,
155 NULL
156 },
157 { /* 7 -- not defined */
158 NULL,
159 NULL
160 },
161 { /* 8 -- not defined */
162 NULL,
163 NULL
164 },
165 { /* 9 -- not defined */
166 NULL,
167 NULL
168 },
169 { /* 10 (LDID_WIN) = windows dir */
170 "WinDir",
171 ""
172 },
173 { /* 11 (LDID_SYS) = system dir */
174 "SysDir",
175 NULL /* call GetSystemDirectory() instead */
176 },
177 { /* 12 (LDID_IOS) = IOSubSys dir */
178 NULL, /* FIXME: registry string ? */
179 "SYSTEM\\IOSUBSYS"
180 },
181 { /* 13 (LDID_CMD) = COMMAND dir */
182 NULL, /* FIXME: registry string ? */
183 "COMMAND"
184 },
185 { /* 14 (LDID_CPL) = control panel dir */
186 NULL,
187 ""
188 },
189 { /* 15 (LDID_PRINT) = windows printer dir */
190 NULL,
191 "SYSTEM" /* correct ?? */
192 },
193 { /* 16 (LDID_MAIL) = destination mail dir */
194 NULL,
195 ""
196 },
197 { /* 17 (LDID_INF) = INF dir */
198 "SetupScratchDir", /* correct ? */
199 "INF"
200 },
201 { /* 18 (LDID_HELP) = HELP dir */
202 NULL, /* ??? */
203 "HELP"
204 },
205 { /* 19 (LDID_WINADMIN) = Admin dir */
206 "WinAdminDir",
207 ""
208 },
209 { /* 20 (LDID_FONTS) = Fonts dir */
210 NULL, /* ??? */
211 "FONTS"
212 },
213 { /* 21 (LDID_VIEWERS) = Viewers */
214 NULL, /* ??? */
215 "SYSTEM\\VIEWERS"
216 },
217 { /* 22 (LDID_VMM32) = VMM32 dir */
218 NULL, /* ??? */
219 "SYSTEM\\VMM32"
220 },
221 { /* 23 (LDID_COLOR) = ICM dir */
222 "ICMPath",
223 "SYSTEM\\COLOR"
224 },
225 { /* 24 (LDID_APPS) = root of boot drive ? */
226 "AppsDir",
227 "C:\\"
228 },
229 { /* 25 (LDID_SHARED) = shared dir */
230 "SharedDir",
231 ""
232 },
233 { /* 26 (LDID_WINBOOT) = Windows boot dir */
234 "WinBootDir",
235 ""
236 },
237 { /* 27 (LDID_MACHINE) = machine specific files */
238 "MachineDir",
239 NULL
240 },
241 { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
242 "HostWinBootDir",
243 NULL
244 },
245 { /* 29 -- not defined */
246 NULL,
247 NULL
248 },
249 { /* 30 (LDID_BOOT) = Root of boot drive */
250 "BootDir",
251 NULL
252 },
253 { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
254 "BootHost",
255 NULL
256 },
257 { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
258 "OldWinBootDir",
259 NULL
260 },
261 { /* 33 (LDID_OLD_WIN) = old win dir */
262 "OldWinDir",
263 NULL
264 }
265 /* the rest (34-38) isn't too interesting, so I'll forget about it */
266 };
267
268 /*
269 * LDD == Logical Device Descriptor
270 * LDID == Logical Device ID
271 *
272 * The whole LDD/LDID business might go into a separate file named
273 * ldd.c.
274 * At the moment I don't know what the hell these functions are really doing.
275 * That's why I added reporting stubs.
276 * The only thing I do know is that I need them for the LDD/LDID infrastructure.
277 * That's why I implemented them in a way that's suitable for my purpose.
278 */
279 static LDD_LIST *pFirstLDD = NULL;
280
281 static BOOL std_LDDs_done = FALSE;
282
283 void SETUPX_CreateStandardLDDs(void)
284 {
285 HKEY hKey = 0;
286 WORD n;
287 DWORD type, len;
288 LOGDISKDESC_S ldd;
289 char buffer[MAX_PATH];
290
291 /* has to be here, otherwise loop */
292 std_LDDs_done = TRUE;
293
294 RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
295
296 for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
297 {
298 buffer[0] = '\0';
299
300 len = MAX_PATH;
301 if ( (hKey) && (LDID_Data[n].RegValName)
302 && (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
303 NULL, &type, (LPBYTE)buffer, &len) == ERROR_SUCCESS)
304 && (type == REG_SZ) )
305 {
306 TRACE("found value '%s' for LDID %d\n", buffer, n);
307 }
308 else
309 switch(n)
310 {
311 case LDID_SRCPATH:
312 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
313 strcpy(buffer, "X:\\FIXME");
314 break;
315 case LDID_SYS:
316 GetSystemDirectoryA(buffer, MAX_PATH);
317 break;
318 case LDID_APPS:
319 case LDID_MACHINE:
320 case LDID_HOST_WINBOOT:
321 case LDID_BOOT:
322 case LDID_BOOT_HOST:
323 strcpy(buffer, "C:\\");
324 break;
325 default:
326 if (LDID_Data[n].StdString)
327 {
328 DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
329 LPSTR p;
330 p = buffer + len;
331 *p++ = '\\';
332 strcpy(p, LDID_Data[n].StdString);
333 }
334 break;
335 }
336 if (buffer[0])
337 {
338 INIT_LDD(ldd, n);
339 ldd.pszPath = buffer;
340 TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
341 CtlSetLdd16(&ldd);
342 }
343 }
344 if (hKey) RegCloseKey(hKey);
345 }
346
347 /***********************************************************************
348 * CtlDelLdd (SETUPX.37)
349 *
350 * RETURN
351 * ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
352 */
353 RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
354 {
355 LDD_LIST *pCurr, *pPrev = NULL;
356
357 TRACE("(%d)\n", ldid);
358
359 if (!std_LDDs_done)
360 SETUPX_CreateStandardLDDs();
361
362 if (ldid < LDID_ASSIGN_START)
363 return ERR_VCP_LDDINVALID;
364
365 pCurr = pFirstLDD;
366 /* search until we find the appropriate LDD or hit the end */
367 while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
368 {
369 pPrev = pCurr;
370 pCurr = pCurr->next;
371 }
372 if ( (pCurr == NULL) /* hit end of list */
373 || (ldid != pCurr->pldd->ldid) )
374 return ERR_VCP_LDDFIND; /* correct ? */
375
376 /* ok, found our victim: eliminate it */
377
378 if (pPrev)
379 pPrev->next = pCurr->next;
380
381 if (pCurr == pFirstLDD)
382 pFirstLDD = NULL;
383 HeapFree(GetProcessHeap(), 0, pCurr);
384
385 return OK;
386 }
387
388 /***********************************************************************
389 * CtlDelLdd (SETUPX.37)
390 */
391 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
392 {
393 FIXME("(%d); - please report this!\n", ldid);
394 return SETUPX_DelLdd(ldid);
395 }
396
397 /***********************************************************************
398 * CtlFindLdd (SETUPX.35)
399 *
400 * doesn't check pldd ptr validity: crash (W98SE)
401 *
402 * RETURN
403 * ERR_VCP_LDDINVALID if pldd->cbSize != structsize
404 * 1 in all other cases ??
405 *
406 */
407 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
408 {
409 LDD_LIST *pCurr, *pPrev = NULL;
410
411 TRACE("(%p)\n", pldd);
412
413 if (!std_LDDs_done)
414 SETUPX_CreateStandardLDDs();
415
416 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
417 return ERR_VCP_LDDINVALID;
418
419 pCurr = pFirstLDD;
420 /* search until we find the appropriate LDD or hit the end */
421 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
422 {
423 pPrev = pCurr;
424 pCurr = pCurr->next;
425 }
426 if ( (pCurr == NULL) /* hit end of list */
427 || (pldd->ldid != pCurr->pldd->ldid) )
428 return ERR_VCP_LDDFIND; /* correct ? */
429
430 memcpy(pldd, pCurr->pldd, pldd->cbSize);
431 /* hmm, we probably ought to strcpy() the string ptrs here */
432
433 return 1; /* what is this ?? */
434 }
435
436 /***********************************************************************
437 * CtlSetLdd (SETUPX.33)
438 *
439 * Set an LDD entry.
440 *
441 * RETURN
442 * ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
443 *
444 */
445 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
446 {
447 LDD_LIST *pCurr, *pPrev = NULL;
448 LPLOGDISKDESC pCurrLDD;
449 HANDLE heap;
450 BOOL is_new = FALSE;
451
452 TRACE("(%p)\n", pldd);
453
454 if (!std_LDDs_done)
455 SETUPX_CreateStandardLDDs();
456
457 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
458 return ERR_VCP_LDDINVALID;
459
460 heap = GetProcessHeap();
461 pCurr = pFirstLDD;
462 /* search until we find the appropriate LDD or hit the end */
463 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
464 {
465 pPrev = pCurr;
466 pCurr = pCurr->next;
467 }
468 if (!pCurr || pldd->ldid != pCurr->pldd->ldid)
469 {
470 is_new = TRUE;
471 pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
472 pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
473 pCurr->next = NULL;
474 pCurrLDD = pCurr->pldd;
475 }
476 else
477 {
478 pCurrLDD = pCurr->pldd;
479 HeapFree(heap, 0, pCurrLDD->pszPath);
480 HeapFree(heap, 0, pCurrLDD->pszVolLabel);
481 HeapFree(heap, 0, pCurrLDD->pszDiskName);
482 }
483
484 memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
485
486 if (pldd->pszPath)
487 {
488 pCurrLDD->pszPath = HeapAlloc( heap, 0, strlen(pldd->pszPath)+1 );
489 strcpy( pCurrLDD->pszPath, pldd->pszPath );
490 }
491 if (pldd->pszVolLabel)
492 {
493 pCurrLDD->pszVolLabel = HeapAlloc( heap, 0, strlen(pldd->pszVolLabel)+1 );
494 strcpy( pCurrLDD->pszVolLabel, pldd->pszVolLabel );
495 }
496 if (pldd->pszDiskName)
497 {
498 pCurrLDD->pszDiskName = HeapAlloc( heap, 0, strlen(pldd->pszDiskName)+1 );
499 strcpy( pCurrLDD->pszDiskName, pldd->pszDiskName );
500 }
501
502 if (is_new) /* link into list */
503 {
504 if (pPrev)
505 {
506 pCurr->next = pPrev->next;
507 pPrev->next = pCurr;
508 }
509 if (!pFirstLDD)
510 pFirstLDD = pCurr;
511 }
512
513 return OK;
514 }
515
516
517 /***********************************************************************
518 * CtlAddLdd (SETUPX.36)
519 *
520 * doesn't check pldd ptr validity: crash (W98SE)
521 *
522 */
523 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
524 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
525 {
526 pldd->ldid = ldid_to_add++;
527 return CtlSetLdd16(pldd);
528 }
529
530 /***********************************************************************
531 * CtlGetLdd (SETUPX.34)
532 *
533 * doesn't check pldd ptr validity: crash (W98SE)
534 * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
535 *
536 * RETURN
537 * ERR_VCP_LDDINVALID if pldd->cbSize != structsize
538 *
539 */
540 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
541 {
542 LDD_LIST *pCurr, *pPrev = NULL;
543
544 if (!std_LDDs_done)
545 SETUPX_CreateStandardLDDs();
546
547 if (pldd->cbSize != sizeof(LOGDISKDESC_S))
548 return ERR_VCP_LDDINVALID;
549
550 pCurr = pFirstLDD;
551 /* search until we find the appropriate LDD or hit the end */
552 while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
553 {
554 pPrev = pCurr;
555 pCurr = pCurr->next;
556 }
557 if (pCurr == NULL) /* hit end of list */
558 return ERR_VCP_LDDFIND; /* correct ? */
559
560 memcpy(pldd, pCurr->pldd, pldd->cbSize);
561 /* hmm, we probably ought to strcpy() the string ptrs here */
562
563 return OK;
564 }
565
566 /**********************************************************************/
567
568 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
569 {
570 FIXME("(%p); - please report this!\n", pldd);
571 return SETUPX_GetLdd(pldd);
572 }
573
574 /***********************************************************************
575 * CtlGetLddPath (SETUPX.38)
576 *
577 * Gets the path of an LDD.
578 * No crash if szPath == NULL.
579 * szPath has to be at least MAX_PATH_LEN bytes long.
580 * RETURN
581 * ERR_VCP_LDDUNINIT if LDD for LDID not found.
582 */
583 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
584 {
585 TRACE("(%d, %p);\n", ldid, szPath);
586
587 if (szPath)
588 {
589 LOGDISKDESC_S ldd;
590 INIT_LDD(ldd, ldid);
591 if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
592 return ERR_VCP_LDDUNINIT;
593 SETUPX_GetLdd(&ldd);
594 strcpy(szPath, ldd.pszPath);
595 TRACE("ret '%s' for LDID %d\n", szPath, ldid);
596 }
597 return OK;
598 }
599
600 /***********************************************************************
601 * CtlSetLddPath (SETUPX.508)
602 *
603 * Sets the path of an LDD.
604 * Creates LDD for LDID if not existing yet.
605 */
606 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
607 {
608 LOGDISKDESC_S ldd;
609 TRACE("(%d, '%s');\n", ldid, szPath);
610
611 SetupSetDirectoryIdA( 0, ldid, szPath );
612 INIT_LDD(ldd, ldid);
613 ldd.pszPath = szPath;
614 return CtlSetLdd16(&ldd);
615 }