[SETUPLIB][USETUP] Minor improvements.
[reactos.git] / base / setup / lib / setuplib.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Setup Library
4 * FILE: base/setup/lib/setuplib.c
5 * PURPOSE: Setup Library - Main initialization helpers
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "precomp.h"
13 #include "filesup.h"
14 #include "infsupp.h"
15 #include "inicache.h"
16
17 #include "setuplib.h"
18
19 #define NDEBUG
20 #include <debug.h>
21
22
23 /* GLOBALS ******************************************************************/
24
25 /* FUNCTIONS ****************************************************************/
26
27 VOID
28 CheckUnattendedSetup(
29 IN OUT PUSETUP_DATA pSetupData)
30 {
31 INFCONTEXT Context;
32 HINF UnattendInf;
33 UINT ErrorLine;
34 INT IntValue;
35 PCWSTR Value;
36 WCHAR UnattendInfPath[MAX_PATH];
37
38 CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2,
39 pSetupData->SourcePath.Buffer, L"unattend.inf");
40
41 DPRINT("UnattendInf path: '%S'\n", UnattendInfPath);
42
43 if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
44 {
45 DPRINT("Does not exist: %S\n", UnattendInfPath);
46 return;
47 }
48
49 /* Load 'unattend.inf' from installation media */
50 UnattendInf = SpInfOpenInfFile(UnattendInfPath,
51 NULL,
52 INF_STYLE_OLDNT,
53 pSetupData->LanguageId,
54 &ErrorLine);
55 if (UnattendInf == INVALID_HANDLE_VALUE)
56 {
57 DPRINT("SpInfOpenInfFile() failed\n");
58 return;
59 }
60
61 /* Open 'Unattend' section */
62 if (!SpInfFindFirstLine(UnattendInf, L"Unattend", L"Signature", &Context))
63 {
64 DPRINT("SpInfFindFirstLine() failed for section 'Unattend'\n");
65 goto Quit;
66 }
67
68 /* Get pointer 'Signature' key */
69 if (!INF_GetData(&Context, NULL, &Value))
70 {
71 DPRINT("INF_GetData() failed for key 'Signature'\n");
72 goto Quit;
73 }
74
75 /* Check 'Signature' string */
76 if (_wcsicmp(Value, L"$ReactOS$") != 0)
77 {
78 DPRINT("Signature not $ReactOS$\n");
79 INF_FreeData(Value);
80 goto Quit;
81 }
82
83 INF_FreeData(Value);
84
85 /* Check if Unattend setup is enabled */
86 if (!SpInfFindFirstLine(UnattendInf, L"Unattend", L"UnattendSetupEnabled", &Context))
87 {
88 DPRINT("Can't find key 'UnattendSetupEnabled'\n");
89 goto Quit;
90 }
91
92 if (!INF_GetData(&Context, NULL, &Value))
93 {
94 DPRINT("Can't read key 'UnattendSetupEnabled'\n");
95 goto Quit;
96 }
97
98 if (_wcsicmp(Value, L"yes") != 0)
99 {
100 DPRINT("Unattend setup is disabled by 'UnattendSetupEnabled' key!\n");
101 INF_FreeData(Value);
102 goto Quit;
103 }
104
105 INF_FreeData(Value);
106
107 /* Search for 'DestinationDiskNumber' in the 'Unattend' section */
108 if (!SpInfFindFirstLine(UnattendInf, L"Unattend", L"DestinationDiskNumber", &Context))
109 {
110 DPRINT("SpInfFindFirstLine() failed for key 'DestinationDiskNumber'\n");
111 goto Quit;
112 }
113
114 if (!SpInfGetIntField(&Context, 1, &IntValue))
115 {
116 DPRINT("SpInfGetIntField() failed for key 'DestinationDiskNumber'\n");
117 goto Quit;
118 }
119
120 pSetupData->DestinationDiskNumber = (LONG)IntValue;
121
122 /* Search for 'DestinationPartitionNumber' in the 'Unattend' section */
123 if (!SpInfFindFirstLine(UnattendInf, L"Unattend", L"DestinationPartitionNumber", &Context))
124 {
125 DPRINT("SpInfFindFirstLine() failed for key 'DestinationPartitionNumber'\n");
126 goto Quit;
127 }
128
129 if (!SpInfGetIntField(&Context, 1, &IntValue))
130 {
131 DPRINT("SpInfGetIntField() failed for key 'DestinationPartitionNumber'\n");
132 goto Quit;
133 }
134
135 pSetupData->DestinationPartitionNumber = (LONG)IntValue;
136
137 /* Search for 'InstallationDirectory' in the 'Unattend' section (optional) */
138 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"InstallationDirectory", &Context))
139 {
140 /* Get pointer 'InstallationDirectory' key */
141 if (!INF_GetData(&Context, NULL, &Value))
142 {
143 DPRINT("INF_GetData() failed for key 'InstallationDirectory'\n");
144 goto Quit;
145 }
146
147 RtlStringCchCopyW(pSetupData->InstallationDirectory,
148 ARRAYSIZE(pSetupData->InstallationDirectory),
149 Value);
150
151 INF_FreeData(Value);
152 }
153
154 IsUnattendedSetup = TRUE;
155 DPRINT("Running unattended setup\n");
156
157 /* Search for 'MBRInstallType' in the 'Unattend' section */
158 pSetupData->MBRInstallType = -1;
159 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"MBRInstallType", &Context))
160 {
161 if (SpInfGetIntField(&Context, 1, &IntValue))
162 {
163 pSetupData->MBRInstallType = IntValue;
164 }
165 }
166
167 /* Search for 'FormatPartition' in the 'Unattend' section */
168 pSetupData->FormatPartition = 0;
169 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"FormatPartition", &Context))
170 {
171 if (SpInfGetIntField(&Context, 1, &IntValue))
172 {
173 pSetupData->FormatPartition = IntValue;
174 }
175 }
176
177 pSetupData->AutoPartition = 0;
178 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"AutoPartition", &Context))
179 {
180 if (SpInfGetIntField(&Context, 1, &IntValue))
181 {
182 pSetupData->AutoPartition = IntValue;
183 }
184 }
185
186 /* Search for LocaleID in the 'Unattend' section */
187 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"LocaleID", &Context))
188 {
189 if (INF_GetData(&Context, NULL, &Value))
190 {
191 LONG Id = wcstol(Value, NULL, 16);
192 RtlStringCchPrintfW(pSetupData->LocaleID,
193 ARRAYSIZE(pSetupData->LocaleID),
194 L"%08lx", Id);
195 INF_FreeData(Value);
196 }
197 }
198
199 Quit:
200 SpInfCloseInfFile(UnattendInf);
201 }
202
203 VOID
204 InstallSetupInfFile(
205 IN OUT PUSETUP_DATA pSetupData)
206 {
207 NTSTATUS Status;
208 PINICACHE IniCache;
209
210 #if 0 // HACK FIXME!
211 PINICACHE UnattendCache;
212 PINICACHEITERATOR Iterator;
213 #else
214 // WCHAR CrLf[] = {L'\r', L'\n'};
215 CHAR CrLf[] = {'\r', '\n'};
216 HANDLE FileHandle, UnattendFileHandle, SectionHandle;
217 FILE_STANDARD_INFORMATION FileInfo;
218 ULONG FileSize;
219 PVOID ViewBase;
220 UNICODE_STRING FileName;
221 OBJECT_ATTRIBUTES ObjectAttributes;
222 IO_STATUS_BLOCK IoStatusBlock;
223 #endif
224
225 PINICACHESECTION IniSection;
226 WCHAR PathBuffer[MAX_PATH];
227 WCHAR UnattendInfPath[MAX_PATH];
228
229 /* Create a $winnt$.inf file with default entries */
230 IniCache = IniCacheCreate();
231 if (!IniCache)
232 return;
233
234 IniSection = IniCacheAppendSection(IniCache, L"SetupParams");
235 if (IniSection)
236 {
237 /* Key "skipmissingfiles" */
238 // RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
239 // L"\"%s\"", L"WinNt5.2");
240 // IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
241 // L"Version", PathBuffer);
242 }
243
244 IniSection = IniCacheAppendSection(IniCache, L"Data");
245 if (IniSection)
246 {
247 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
248 L"\"%s\"", IsUnattendedSetup ? L"yes" : L"no");
249 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
250 L"UnattendedInstall", PathBuffer);
251
252 // "floppylessbootpath" (yes/no)
253
254 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
255 L"\"%s\"", L"winnt");
256 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
257 L"ProductType", PathBuffer);
258
259 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
260 L"\"%s\\\"", pSetupData->SourceRootPath.Buffer);
261 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
262 L"SourcePath", PathBuffer);
263
264 // "floppyless" ("0")
265 }
266
267 #if 0
268
269 /* TODO: Append the standard unattend.inf file */
270 CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2,
271 pSetupData->SourcePath.Buffer, L"unattend.inf");
272 if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
273 {
274 DPRINT("Does not exist: %S\n", UnattendInfPath);
275 goto Quit;
276 }
277
278 Status = IniCacheLoad(&UnattendCache, UnattendInfPath, FALSE);
279 if (!NT_SUCCESS(Status))
280 {
281 DPRINT1("Cannot load %S as an INI file!\n", UnattendInfPath);
282 goto Quit;
283 }
284
285 IniCacheDestroy(UnattendCache);
286
287 Quit:
288 CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
289 pSetupData->DestinationPath.Buffer, L"System32\\$winnt$.inf");
290 IniCacheSave(IniCache, PathBuffer);
291 IniCacheDestroy(IniCache);
292
293 #else
294
295 CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
296 pSetupData->DestinationPath.Buffer, L"System32\\$winnt$.inf");
297 IniCacheSave(IniCache, PathBuffer);
298 IniCacheDestroy(IniCache);
299
300 /* TODO: Append the standard unattend.inf file */
301 CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2,
302 pSetupData->SourcePath.Buffer, L"unattend.inf");
303 if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
304 {
305 DPRINT("Does not exist: %S\n", UnattendInfPath);
306 return;
307 }
308
309 RtlInitUnicodeString(&FileName, PathBuffer);
310 InitializeObjectAttributes(&ObjectAttributes,
311 &FileName,
312 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
313 NULL,
314 NULL);
315 Status = NtOpenFile(&FileHandle,
316 FILE_APPEND_DATA | SYNCHRONIZE,
317 &ObjectAttributes,
318 &IoStatusBlock,
319 FILE_SHARE_READ,
320 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
321 if (!NT_SUCCESS(Status))
322 {
323 DPRINT1("Cannot load %S as an INI file!\n", PathBuffer);
324 return;
325 }
326
327 /* Query the file size */
328 Status = NtQueryInformationFile(FileHandle,
329 &IoStatusBlock,
330 &FileInfo,
331 sizeof(FileInfo),
332 FileStandardInformation);
333 if (!NT_SUCCESS(Status))
334 {
335 DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
336 FileInfo.EndOfFile.QuadPart = 0ULL;
337 }
338
339 Status = OpenAndMapFile(NULL,
340 UnattendInfPath,
341 &UnattendFileHandle,
342 &SectionHandle,
343 &ViewBase,
344 &FileSize,
345 FALSE);
346 if (!NT_SUCCESS(Status))
347 {
348 DPRINT1("Cannot load %S !\n", UnattendInfPath);
349 NtClose(FileHandle);
350 return;
351 }
352
353 /* Write to the INI file */
354
355 /* "\r\n" */
356 Status = NtWriteFile(FileHandle,
357 NULL,
358 NULL,
359 NULL,
360 &IoStatusBlock,
361 (PVOID)CrLf,
362 sizeof(CrLf),
363 &FileInfo.EndOfFile,
364 NULL);
365
366 Status = NtWriteFile(FileHandle,
367 NULL,
368 NULL,
369 NULL,
370 &IoStatusBlock,
371 ViewBase,
372 FileSize,
373 NULL,
374 NULL);
375 if (!NT_SUCCESS(Status))
376 {
377 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
378 }
379
380 /* Finally, unmap and close the file */
381 UnMapAndCloseFile(UnattendFileHandle, SectionHandle, ViewBase);
382
383 NtClose(FileHandle);
384 #endif
385 }
386
387 NTSTATUS
388 GetSourcePaths(
389 OUT PUNICODE_STRING SourcePath,
390 OUT PUNICODE_STRING SourceRootPath,
391 OUT PUNICODE_STRING SourceRootDir)
392 {
393 NTSTATUS Status;
394 HANDLE LinkHandle;
395 OBJECT_ATTRIBUTES ObjectAttributes;
396 UCHAR ImageFileBuffer[sizeof(UNICODE_STRING) + MAX_PATH * sizeof(WCHAR)];
397 PUNICODE_STRING InstallSourcePath = (PUNICODE_STRING)&ImageFileBuffer;
398 WCHAR SystemRootBuffer[MAX_PATH] = L"";
399 UNICODE_STRING SystemRootPath = RTL_CONSTANT_STRING(L"\\SystemRoot");
400 ULONG BufferSize;
401 PWCHAR Ptr;
402
403 /* Determine the installation source path via the full path of the installer */
404 RtlInitEmptyUnicodeString(InstallSourcePath,
405 (PWSTR)((ULONG_PTR)ImageFileBuffer + sizeof(UNICODE_STRING)),
406 sizeof(ImageFileBuffer) - sizeof(UNICODE_STRING)
407 /* Reserve space for a NULL terminator */ - sizeof(UNICODE_NULL));
408 BufferSize = sizeof(ImageFileBuffer);
409 Status = NtQueryInformationProcess(NtCurrentProcess(),
410 ProcessImageFileName,
411 InstallSourcePath,
412 BufferSize,
413 NULL);
414 // STATUS_INFO_LENGTH_MISMATCH or STATUS_BUFFER_TOO_SMALL ?
415 if (!NT_SUCCESS(Status))
416 return Status;
417
418 /* Manually NULL-terminate */
419 InstallSourcePath->Buffer[InstallSourcePath->Length / sizeof(WCHAR)] = UNICODE_NULL;
420
421 /* Strip the trailing file name */
422 Ptr = wcsrchr(InstallSourcePath->Buffer, OBJ_NAME_PATH_SEPARATOR);
423 if (Ptr)
424 *Ptr = UNICODE_NULL;
425 InstallSourcePath->Length = wcslen(InstallSourcePath->Buffer) * sizeof(WCHAR);
426
427
428 /*
429 * Now resolve the full path to \SystemRoot. In case it prefixes
430 * the installation source path determined from the full path of
431 * the installer, we use instead the resolved \SystemRoot as the
432 * installation source path.
433 * Otherwise, we use instead the path from the full installer path.
434 */
435
436 InitializeObjectAttributes(&ObjectAttributes,
437 &SystemRootPath,
438 OBJ_CASE_INSENSITIVE,
439 NULL,
440 NULL);
441
442 Status = NtOpenSymbolicLinkObject(&LinkHandle,
443 SYMBOLIC_LINK_QUERY,
444 &ObjectAttributes);
445 if (!NT_SUCCESS(Status))
446 {
447 /*
448 * We failed at opening the \SystemRoot link (usually due to wrong
449 * access rights). Do not consider this as a fatal error, but use
450 * instead the image file path as the installation source path.
451 */
452 DPRINT1("NtOpenSymbolicLinkObject(%wZ) failed with Status 0x%08lx\n",
453 &SystemRootPath, Status);
454 goto InitPaths;
455 }
456
457 RtlInitEmptyUnicodeString(&SystemRootPath,
458 SystemRootBuffer,
459 sizeof(SystemRootBuffer));
460
461 /* Resolve the link and close its handle */
462 Status = NtQuerySymbolicLinkObject(LinkHandle,
463 &SystemRootPath,
464 &BufferSize);
465 NtClose(LinkHandle);
466
467 if (!NT_SUCCESS(Status))
468 return Status; // Unexpected error
469
470 /* Check whether the resolved \SystemRoot is a prefix of the image file path */
471 if (RtlPrefixUnicodeString(&SystemRootPath, InstallSourcePath, TRUE))
472 {
473 /* Yes it is, so we use instead SystemRoot as the installation source path */
474 InstallSourcePath = &SystemRootPath;
475 }
476
477
478 InitPaths:
479 /*
480 * Retrieve the different source path components
481 */
482 RtlCreateUnicodeString(SourcePath, InstallSourcePath->Buffer);
483
484 /* Strip trailing directory */
485 Ptr = wcsrchr(InstallSourcePath->Buffer, OBJ_NAME_PATH_SEPARATOR);
486 if (Ptr)
487 {
488 RtlCreateUnicodeString(SourceRootDir, Ptr);
489 *Ptr = UNICODE_NULL;
490 }
491 else
492 {
493 RtlCreateUnicodeString(SourceRootDir, L"");
494 }
495
496 RtlCreateUnicodeString(SourceRootPath, InstallSourcePath->Buffer);
497
498 return STATUS_SUCCESS;
499 }
500
501 ERROR_NUMBER
502 LoadSetupInf(
503 IN OUT PUSETUP_DATA pSetupData)
504 {
505 INFCONTEXT Context;
506 UINT ErrorLine;
507 INT IntValue;
508 PCWSTR Value;
509 WCHAR FileNameBuffer[MAX_PATH];
510
511 CombinePaths(FileNameBuffer, ARRAYSIZE(FileNameBuffer), 2,
512 pSetupData->SourcePath.Buffer, L"txtsetup.sif");
513
514 DPRINT("SetupInf path: '%S'\n", FileNameBuffer);
515
516 pSetupData->SetupInf =
517 SpInfOpenInfFile(FileNameBuffer,
518 NULL,
519 /* INF_STYLE_WIN4 | */ INF_STYLE_OLDNT,
520 pSetupData->LanguageId,
521 &ErrorLine);
522 if (pSetupData->SetupInf == INVALID_HANDLE_VALUE)
523 return ERROR_LOAD_TXTSETUPSIF;
524
525 /* Open 'Version' section */
526 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"Version", L"Signature", &Context))
527 return ERROR_CORRUPT_TXTSETUPSIF;
528
529 /* Get pointer 'Signature' key */
530 if (!INF_GetData(&Context, NULL, &Value))
531 return ERROR_CORRUPT_TXTSETUPSIF;
532
533 /* Check 'Signature' string */
534 if (_wcsicmp(Value, L"$ReactOS$") != 0)
535 {
536 INF_FreeData(Value);
537 return ERROR_SIGNATURE_TXTSETUPSIF;
538 }
539
540 INF_FreeData(Value);
541
542 /* Open 'DiskSpaceRequirements' section */
543 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"DiskSpaceRequirements", L"FreeSysPartDiskSpace", &Context))
544 return ERROR_CORRUPT_TXTSETUPSIF;
545
546 pSetupData->RequiredPartitionDiskSpace = ~0;
547
548 /* Get the 'FreeSysPartDiskSpace' value */
549 if (!SpInfGetIntField(&Context, 1, &IntValue))
550 return ERROR_CORRUPT_TXTSETUPSIF;
551
552 pSetupData->RequiredPartitionDiskSpace = (ULONG)IntValue;
553
554 //
555 // Support "SetupSourceDevice" and "SetupSourcePath" in txtsetup.sif
556 // See CORE-9023
557 // Support for that should also be added in setupldr.
558 //
559
560 /* Update the Setup Source paths */
561 if (SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"SetupSourceDevice", &Context))
562 {
563 /*
564 * Get optional pointer 'SetupSourceDevice' key, its presence
565 * will dictate whether we also need 'SetupSourcePath'.
566 */
567 if (INF_GetData(&Context, NULL, &Value))
568 {
569 /* Free the old source root path string and create the new one */
570 RtlFreeUnicodeString(&pSetupData->SourceRootPath);
571 RtlCreateUnicodeString(&pSetupData->SourceRootPath, Value);
572 INF_FreeData(Value);
573
574 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"SetupSourcePath", &Context))
575 {
576 /* The 'SetupSourcePath' value is mandatory! */
577 return ERROR_CORRUPT_TXTSETUPSIF;
578 }
579
580 /* Get pointer 'SetupSourcePath' key */
581 if (!INF_GetData(&Context, NULL, &Value))
582 {
583 /* The 'SetupSourcePath' value is mandatory! */
584 return ERROR_CORRUPT_TXTSETUPSIF;
585 }
586
587 /* Free the old source path string and create the new one */
588 RtlFreeUnicodeString(&pSetupData->SourceRootDir);
589 RtlCreateUnicodeString(&pSetupData->SourceRootDir, Value);
590 INF_FreeData(Value);
591 }
592 }
593
594 /* Search for 'DefaultPath' in the 'SetupData' section */
595 pSetupData->InstallationDirectory[0] = 0;
596 if (SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"DefaultPath", &Context))
597 {
598 /* Get pointer 'DefaultPath' key */
599 if (!INF_GetData(&Context, NULL, &Value))
600 return ERROR_CORRUPT_TXTSETUPSIF;
601
602 RtlStringCchCopyW(pSetupData->InstallationDirectory,
603 ARRAYSIZE(pSetupData->InstallationDirectory),
604 Value);
605
606 INF_FreeData(Value);
607 }
608
609 return ERROR_SUCCESS;
610 }
611
612 NTSTATUS
613 InitDestinationPaths(
614 IN OUT PUSETUP_DATA pSetupData,
615 IN PCWSTR InstallationDir,
616 IN PDISKENTRY DiskEntry, // FIXME: HACK!
617 IN PPARTENTRY PartEntry) // FIXME: HACK!
618 {
619 WCHAR PathBuffer[MAX_PATH];
620
621 //
622 // TODO: Check return status values of the functions!
623 //
624
625 /* Create 'pSetupData->DestinationRootPath' string */
626 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
627 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
628 L"\\Device\\Harddisk%lu\\Partition%lu\\",
629 DiskEntry->DiskNumber,
630 PartEntry->PartitionNumber);
631 RtlCreateUnicodeString(&pSetupData->DestinationRootPath, PathBuffer);
632 DPRINT("DestinationRootPath: %wZ\n", &pSetupData->DestinationRootPath);
633
634 // FIXME! Which variable to choose?
635 if (!InstallationDir)
636 InstallationDir = pSetupData->InstallationDirectory;
637
638 /** Equivalent of 'NTOS_INSTALLATION::SystemArcPath' **/
639 /* Create 'pSetupData->DestinationArcPath' */
640 RtlFreeUnicodeString(&pSetupData->DestinationArcPath);
641 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
642 L"multi(0)disk(0)rdisk(%lu)partition(%lu)\\",
643 DiskEntry->BiosDiskNumber,
644 PartEntry->PartitionNumber);
645 ConcatPaths(PathBuffer, ARRAYSIZE(PathBuffer), 1, InstallationDir);
646 RtlCreateUnicodeString(&pSetupData->DestinationArcPath, PathBuffer);
647
648 /** Equivalent of 'NTOS_INSTALLATION::SystemNtPath' **/
649 /* Create 'pSetupData->DestinationPath' string */
650 RtlFreeUnicodeString(&pSetupData->DestinationPath);
651 CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
652 pSetupData->DestinationRootPath.Buffer, InstallationDir);
653 RtlCreateUnicodeString(&pSetupData->DestinationPath, PathBuffer);
654
655 /** Equivalent of 'NTOS_INSTALLATION::PathComponent' **/
656 // FIXME: This is only temporary!! Must be removed later!
657 /***/RtlCreateUnicodeString(&pSetupData->InstallPath, InstallationDir);/***/
658
659 return STATUS_SUCCESS;
660 }
661
662 // NTSTATUS
663 ERROR_NUMBER
664 InitializeSetup(
665 IN OUT PUSETUP_DATA pSetupData,
666 IN ULONG InitPhase)
667 {
668 if (InitPhase == 0)
669 {
670 RtlZeroMemory(pSetupData, sizeof(*pSetupData));
671
672 // pSetupData->ComputerList = NULL;
673 // pSetupData->DisplayList = NULL;
674 // pSetupData->KeyboardList = NULL;
675 // pSetupData->LayoutList = NULL;
676 // pSetupData->LanguageList = NULL;
677
678 /* Initialize global unicode strings */
679 RtlInitUnicodeString(&pSetupData->SourcePath, NULL);
680 RtlInitUnicodeString(&pSetupData->SourceRootPath, NULL);
681 RtlInitUnicodeString(&pSetupData->SourceRootDir, NULL);
682 RtlInitUnicodeString(&pSetupData->DestinationArcPath, NULL);
683 RtlInitUnicodeString(&pSetupData->DestinationPath, NULL);
684 RtlInitUnicodeString(&pSetupData->DestinationRootPath, NULL);
685 RtlInitUnicodeString(&pSetupData->SystemRootPath, NULL);
686
687 // FIXME: This is only temporary!! Must be removed later!
688 /***/RtlInitUnicodeString(&pSetupData->InstallPath, NULL);/***/
689
690 //
691 // TODO: Load and start SetupDD, and ask it for the information
692 //
693
694 return ERROR_SUCCESS;
695 }
696 else
697 if (InitPhase == 1)
698 {
699 ERROR_NUMBER Error;
700 NTSTATUS Status;
701
702 /* Get the source path and source root path */
703 //
704 // NOTE: Sometimes the source path may not be in SystemRoot !!
705 // (and this is the case when using the 1st-stage GUI setup!)
706 //
707 Status = GetSourcePaths(&pSetupData->SourcePath,
708 &pSetupData->SourceRootPath,
709 &pSetupData->SourceRootDir);
710 if (!NT_SUCCESS(Status))
711 {
712 DPRINT1("GetSourcePaths() failed (Status 0x%08lx)", Status);
713 return ERROR_NO_SOURCE_DRIVE;
714 }
715 /*
716 * Example of output:
717 * SourcePath: '\Device\CdRom0\I386'
718 * SourceRootPath: '\Device\CdRom0'
719 * SourceRootDir: '\I386'
720 */
721 DPRINT1("SourcePath (1): '%wZ'\n", &pSetupData->SourcePath);
722 DPRINT1("SourceRootPath (1): '%wZ'\n", &pSetupData->SourceRootPath);
723 DPRINT1("SourceRootDir (1): '%wZ'\n", &pSetupData->SourceRootDir);
724
725 /* Load 'txtsetup.sif' from the installation media */
726 Error = LoadSetupInf(pSetupData);
727 if (Error != ERROR_SUCCESS)
728 {
729 DPRINT1("LoadSetupInf() failed (Error 0x%lx)", Error);
730 return Error;
731 }
732 DPRINT1("SourcePath (2): '%wZ'\n", &pSetupData->SourcePath);
733 DPRINT1("SourceRootPath (2): '%wZ'\n", &pSetupData->SourceRootPath);
734 DPRINT1("SourceRootDir (2): '%wZ'\n", &pSetupData->SourceRootDir);
735
736 return ERROR_SUCCESS;
737 }
738
739 return ERROR_SUCCESS;
740 }
741
742 VOID
743 FinishSetup(
744 IN OUT PUSETUP_DATA pSetupData)
745 {
746 /* Destroy the computer settings list */
747 if (pSetupData->ComputerList != NULL)
748 {
749 DestroyGenericList(pSetupData->ComputerList, TRUE);
750 pSetupData->ComputerList = NULL;
751 }
752
753 /* Destroy the display settings list */
754 if (pSetupData->DisplayList != NULL)
755 {
756 DestroyGenericList(pSetupData->DisplayList, TRUE);
757 pSetupData->DisplayList = NULL;
758 }
759
760 /* Destroy the keyboard settings list */
761 if (pSetupData->KeyboardList != NULL)
762 {
763 DestroyGenericList(pSetupData->KeyboardList, TRUE);
764 pSetupData->KeyboardList = NULL;
765 }
766
767 /* Destroy the keyboard layout list */
768 if (pSetupData->LayoutList != NULL)
769 {
770 DestroyGenericList(pSetupData->LayoutList, TRUE);
771 pSetupData->LayoutList = NULL;
772 }
773
774 /* Destroy the languages list */
775 if (pSetupData->LanguageList != NULL)
776 {
777 DestroyGenericList(pSetupData->LanguageList, FALSE);
778 pSetupData->LanguageList = NULL;
779 }
780
781 /* Close the Setup INF */
782 SpInfCloseInfFile(pSetupData->SetupInf);
783 }
784
785 /*
786 * SIDEEFFECTS
787 * Calls RegInitializeRegistry
788 * Calls ImportRegistryFile
789 * Calls SetDefaultPagefile
790 * Calls SetMountedDeviceValues
791 */
792 ERROR_NUMBER
793 UpdateRegistry(
794 IN OUT PUSETUP_DATA pSetupData,
795 /**/IN BOOLEAN RepairUpdateFlag, /* HACK HACK! */
796 /**/IN PPARTLIST PartitionList, /* HACK HACK! */
797 /**/IN WCHAR DestinationDriveLetter, /* HACK HACK! */
798 /**/IN PCWSTR SelectedLanguageId, /* HACK HACK! */
799 IN PREGISTRY_STATUS_ROUTINE StatusRoutine OPTIONAL)
800 {
801 ERROR_NUMBER ErrorNumber;
802 NTSTATUS Status;
803 INFCONTEXT InfContext;
804 PCWSTR Action;
805 PCWSTR File;
806 PCWSTR Section;
807 BOOLEAN Success;
808 BOOLEAN ShouldRepairRegistry = FALSE;
809 BOOLEAN Delete;
810
811 if (RepairUpdateFlag)
812 {
813 DPRINT1("TODO: Updating / repairing the registry is not completely implemented yet!\n");
814
815 /* Verify the registry hives and check whether we need to update or repair any of them */
816 Status = VerifyRegistryHives(&pSetupData->DestinationPath, &ShouldRepairRegistry);
817 if (!NT_SUCCESS(Status))
818 {
819 DPRINT1("VerifyRegistryHives failed, Status 0x%08lx\n", Status);
820 ShouldRepairRegistry = FALSE;
821 }
822 if (!ShouldRepairRegistry)
823 DPRINT1("No need to repair the registry\n");
824 }
825
826 DoUpdate:
827 ErrorNumber = ERROR_SUCCESS;
828
829 /* Update the registry */
830 if (StatusRoutine) StatusRoutine(RegHiveUpdate);
831
832 /* Initialize the registry and setup the registry hives */
833 Status = RegInitializeRegistry(&pSetupData->DestinationPath);
834 if (!NT_SUCCESS(Status))
835 {
836 DPRINT1("RegInitializeRegistry() failed\n");
837 /********** HACK!!!!!!!!!!! **********/
838 if (Status == STATUS_NOT_IMPLEMENTED)
839 {
840 /* The hack was called, return its corresponding error */
841 return ERROR_INITIALIZE_REGISTRY;
842 }
843 else
844 /*************************************/
845 {
846 /* Something else failed */
847 return ERROR_CREATE_HIVE;
848 }
849 }
850
851 if (!RepairUpdateFlag || ShouldRepairRegistry)
852 {
853 /*
854 * We fully setup the hives, in case we are doing a fresh installation
855 * (RepairUpdateFlag == FALSE), or in case we are doing an update
856 * (RepairUpdateFlag == TRUE) BUT we have some registry hives to
857 * "repair" (aka. recreate: ShouldRepairRegistry == TRUE).
858 */
859
860 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Fresh", NULL, &InfContext); // Windows-compatible
861 if (!Success)
862 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Install", NULL, &InfContext); // ReactOS-specific
863
864 if (!Success)
865 {
866 DPRINT1("SpInfFindFirstLine() failed\n");
867 ErrorNumber = ERROR_FIND_REGISTRY;
868 goto Cleanup;
869 }
870 }
871 else // if (RepairUpdateFlag && !ShouldRepairRegistry)
872 {
873 /*
874 * In case we are doing an update (RepairUpdateFlag == TRUE) and
875 * NO registry hives need a repair (ShouldRepairRegistry == FALSE),
876 * we only update the hives.
877 */
878
879 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Upgrade", NULL, &InfContext);
880 if (!Success)
881 {
882 /* Nothing to do for update! */
883 DPRINT1("No update needed for the registry!\n");
884 goto Cleanup;
885 }
886 }
887
888 do
889 {
890 INF_GetDataField(&InfContext, 0, &Action);
891 INF_GetDataField(&InfContext, 1, &File);
892 INF_GetDataField(&InfContext, 2, &Section);
893
894 DPRINT("Action: %S File: %S Section %S\n", Action, File, Section);
895
896 if (Action == NULL)
897 {
898 INF_FreeData(Action);
899 INF_FreeData(File);
900 INF_FreeData(Section);
901 break; // Hackfix
902 }
903
904 if (!_wcsicmp(Action, L"AddReg"))
905 Delete = FALSE;
906 else if (!_wcsicmp(Action, L"DelReg"))
907 Delete = TRUE;
908 else
909 {
910 DPRINT1("Unrecognized registry INF action '%S'\n", Action);
911 INF_FreeData(Action);
912 INF_FreeData(File);
913 INF_FreeData(Section);
914 continue;
915 }
916
917 INF_FreeData(Action);
918
919 if (StatusRoutine) StatusRoutine(ImportRegHive, File);
920
921 if (!ImportRegistryFile(pSetupData->SourcePath.Buffer,
922 File, Section,
923 pSetupData->LanguageId, Delete))
924 {
925 DPRINT1("Importing %S failed\n", File);
926 INF_FreeData(File);
927 INF_FreeData(Section);
928 ErrorNumber = ERROR_IMPORT_HIVE;
929 goto Cleanup;
930 }
931 } while (SpInfFindNextLine(&InfContext, &InfContext));
932
933 if (!RepairUpdateFlag || ShouldRepairRegistry)
934 {
935 /* See the explanation for this test above */
936
937 /* Update display registry settings */
938 if (StatusRoutine) StatusRoutine(DisplaySettingsUpdate);
939 if (!ProcessDisplayRegistry(pSetupData->SetupInf, pSetupData->DisplayList))
940 {
941 ErrorNumber = ERROR_UPDATE_DISPLAY_SETTINGS;
942 goto Cleanup;
943 }
944
945 /* Set the locale */
946 if (StatusRoutine) StatusRoutine(LocaleSettingsUpdate);
947 if (!ProcessLocaleRegistry(pSetupData->LanguageList))
948 {
949 ErrorNumber = ERROR_UPDATE_LOCALESETTINGS;
950 goto Cleanup;
951 }
952
953 /* Add keyboard layouts */
954 if (StatusRoutine) StatusRoutine(KeybLayouts);
955 if (!AddKeyboardLayouts(SelectedLanguageId))
956 {
957 ErrorNumber = ERROR_ADDING_KBLAYOUTS;
958 goto Cleanup;
959 }
960
961 /* Set GeoID */
962 if (!SetGeoID(MUIGetGeoID(SelectedLanguageId)))
963 {
964 ErrorNumber = ERROR_UPDATE_GEOID;
965 goto Cleanup;
966 }
967
968 if (!IsUnattendedSetup)
969 {
970 /* Update keyboard layout settings */
971 if (StatusRoutine) StatusRoutine(KeybSettingsUpdate);
972 if (!ProcessKeyboardLayoutRegistry(pSetupData->LayoutList, SelectedLanguageId))
973 {
974 ErrorNumber = ERROR_UPDATE_KBSETTINGS;
975 goto Cleanup;
976 }
977 }
978
979 /* Add codepage information to registry */
980 if (StatusRoutine) StatusRoutine(CodePageInfoUpdate);
981 if (!AddCodePage(SelectedLanguageId))
982 {
983 ErrorNumber = ERROR_ADDING_CODEPAGE;
984 goto Cleanup;
985 }
986
987 /* Set the default pagefile entry */
988 SetDefaultPagefile(DestinationDriveLetter);
989
990 /* Update the mounted devices list */
991 // FIXME: This should technically be done by mountmgr (if AutoMount is enabled)!
992 SetMountedDeviceValues(PartitionList);
993 }
994
995 Cleanup:
996 //
997 // TODO: Unload all the registry stuff, perform cleanup,
998 // and copy the created hive files into .sav files.
999 //
1000 RegCleanupRegistry(&pSetupData->DestinationPath);
1001
1002 /*
1003 * Check whether we were in update/repair mode but we were actually
1004 * repairing the registry hives. If so, we have finished repairing them,
1005 * and we now reset the flag and run the proper registry update.
1006 * Otherwise we have finished the registry update!
1007 */
1008 if (RepairUpdateFlag && ShouldRepairRegistry)
1009 {
1010 ShouldRepairRegistry = FALSE;
1011 goto DoUpdate;
1012 }
1013
1014 return ErrorNumber;
1015 }
1016
1017 /* EOF */