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