9e4d22ee372cd58d841b77f6f65ff59999cdb26f
[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 /* Search for FsType in the 'Unattend' section */
200 pSetupData->FsType = 0;
201 if (SpInfFindFirstLine(UnattendInf, L"Unattend", L"FsType", &Context))
202 {
203 if (SpInfGetIntField(&Context, 1, &IntValue))
204 {
205 pSetupData->FsType = IntValue;
206 }
207 }
208
209 Quit:
210 SpInfCloseInfFile(UnattendInf);
211 }
212
213 VOID
214 InstallSetupInfFile(
215 IN OUT PUSETUP_DATA pSetupData)
216 {
217 NTSTATUS Status;
218 PINICACHE IniCache;
219
220 #if 0 // HACK FIXME!
221 PINICACHE UnattendCache;
222 PINICACHEITERATOR Iterator;
223 #else
224 // WCHAR CrLf[] = {L'\r', L'\n'};
225 CHAR CrLf[] = {'\r', '\n'};
226 HANDLE FileHandle, UnattendFileHandle, SectionHandle;
227 FILE_STANDARD_INFORMATION FileInfo;
228 ULONG FileSize;
229 PVOID ViewBase;
230 UNICODE_STRING FileName;
231 OBJECT_ATTRIBUTES ObjectAttributes;
232 IO_STATUS_BLOCK IoStatusBlock;
233 #endif
234
235 PINICACHESECTION IniSection;
236 WCHAR PathBuffer[MAX_PATH];
237 WCHAR UnattendInfPath[MAX_PATH];
238
239 /* Create a $winnt$.inf file with default entries */
240 IniCache = IniCacheCreate();
241 if (!IniCache)
242 return;
243
244 IniSection = IniCacheAppendSection(IniCache, L"SetupParams");
245 if (IniSection)
246 {
247 /* Key "skipmissingfiles" */
248 // RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
249 // L"\"%s\"", L"WinNt5.2");
250 // IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
251 // L"Version", PathBuffer);
252 }
253
254 IniSection = IniCacheAppendSection(IniCache, L"Data");
255 if (IniSection)
256 {
257 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
258 L"\"%s\"", IsUnattendedSetup ? L"yes" : L"no");
259 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
260 L"UnattendedInstall", PathBuffer);
261
262 // "floppylessbootpath" (yes/no)
263
264 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
265 L"\"%s\"", L"winnt");
266 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
267 L"ProductType", PathBuffer);
268
269 RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
270 L"\"%s\\\"", pSetupData->SourceRootPath.Buffer);
271 IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
272 L"SourcePath", PathBuffer);
273
274 // "floppyless" ("0")
275 }
276
277 #if 0
278
279 /* TODO: Append the standard unattend.inf file */
280 CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2,
281 pSetupData->SourcePath.Buffer, L"unattend.inf");
282 if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
283 {
284 DPRINT("Does not exist: %S\n", UnattendInfPath);
285 goto Quit;
286 }
287
288 Status = IniCacheLoad(&UnattendCache, UnattendInfPath, FALSE);
289 if (!NT_SUCCESS(Status))
290 {
291 DPRINT1("Cannot load %S as an INI file!\n", UnattendInfPath);
292 goto Quit;
293 }
294
295 IniCacheDestroy(UnattendCache);
296
297 Quit:
298 CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
299 pSetupData->DestinationPath.Buffer, L"System32\\$winnt$.inf");
300 IniCacheSave(IniCache, PathBuffer);
301 IniCacheDestroy(IniCache);
302
303 #else
304
305 CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
306 pSetupData->DestinationPath.Buffer, L"System32\\$winnt$.inf");
307 IniCacheSave(IniCache, PathBuffer);
308 IniCacheDestroy(IniCache);
309
310 /* TODO: Append the standard unattend.inf file */
311 CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2,
312 pSetupData->SourcePath.Buffer, L"unattend.inf");
313 if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
314 {
315 DPRINT("Does not exist: %S\n", UnattendInfPath);
316 return;
317 }
318
319 RtlInitUnicodeString(&FileName, PathBuffer);
320 InitializeObjectAttributes(&ObjectAttributes,
321 &FileName,
322 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
323 NULL,
324 NULL);
325 Status = NtOpenFile(&FileHandle,
326 FILE_APPEND_DATA | SYNCHRONIZE,
327 &ObjectAttributes,
328 &IoStatusBlock,
329 FILE_SHARE_READ,
330 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
331 if (!NT_SUCCESS(Status))
332 {
333 DPRINT1("Cannot load %S as an INI file!\n", PathBuffer);
334 return;
335 }
336
337 /* Query the file size */
338 Status = NtQueryInformationFile(FileHandle,
339 &IoStatusBlock,
340 &FileInfo,
341 sizeof(FileInfo),
342 FileStandardInformation);
343 if (!NT_SUCCESS(Status))
344 {
345 DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
346 FileInfo.EndOfFile.QuadPart = 0ULL;
347 }
348
349 Status = OpenAndMapFile(NULL,
350 UnattendInfPath,
351 &UnattendFileHandle,
352 &SectionHandle,
353 &ViewBase,
354 &FileSize,
355 FALSE);
356 if (!NT_SUCCESS(Status))
357 {
358 DPRINT1("Cannot load %S !\n", UnattendInfPath);
359 NtClose(FileHandle);
360 return;
361 }
362
363 /* Write to the INI file */
364
365 /* "\r\n" */
366 Status = NtWriteFile(FileHandle,
367 NULL,
368 NULL,
369 NULL,
370 &IoStatusBlock,
371 (PVOID)CrLf,
372 sizeof(CrLf),
373 &FileInfo.EndOfFile,
374 NULL);
375
376 Status = NtWriteFile(FileHandle,
377 NULL,
378 NULL,
379 NULL,
380 &IoStatusBlock,
381 ViewBase,
382 FileSize,
383 NULL,
384 NULL);
385 if (!NT_SUCCESS(Status))
386 {
387 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
388 }
389
390 /* Finally, unmap and close the file */
391 UnMapAndCloseFile(UnattendFileHandle, SectionHandle, ViewBase);
392
393 NtClose(FileHandle);
394 #endif
395 }
396
397 NTSTATUS
398 GetSourcePaths(
399 OUT PUNICODE_STRING SourcePath,
400 OUT PUNICODE_STRING SourceRootPath,
401 OUT PUNICODE_STRING SourceRootDir)
402 {
403 NTSTATUS Status;
404 HANDLE LinkHandle;
405 OBJECT_ATTRIBUTES ObjectAttributes;
406 UCHAR ImageFileBuffer[sizeof(UNICODE_STRING) + MAX_PATH * sizeof(WCHAR)];
407 PUNICODE_STRING InstallSourcePath = (PUNICODE_STRING)&ImageFileBuffer;
408 WCHAR SystemRootBuffer[MAX_PATH] = L"";
409 UNICODE_STRING SystemRootPath = RTL_CONSTANT_STRING(L"\\SystemRoot");
410 ULONG BufferSize;
411 PWCHAR Ptr;
412
413 /* Determine the installation source path via the full path of the installer */
414 RtlInitEmptyUnicodeString(InstallSourcePath,
415 (PWSTR)((ULONG_PTR)ImageFileBuffer + sizeof(UNICODE_STRING)),
416 sizeof(ImageFileBuffer) - sizeof(UNICODE_STRING)
417 /* Reserve space for a NULL terminator */ - sizeof(UNICODE_NULL));
418 BufferSize = sizeof(ImageFileBuffer);
419 Status = NtQueryInformationProcess(NtCurrentProcess(),
420 ProcessImageFileName,
421 InstallSourcePath,
422 BufferSize,
423 NULL);
424 // STATUS_INFO_LENGTH_MISMATCH or STATUS_BUFFER_TOO_SMALL ?
425 if (!NT_SUCCESS(Status))
426 return Status;
427
428 /* Manually NULL-terminate */
429 InstallSourcePath->Buffer[InstallSourcePath->Length / sizeof(WCHAR)] = UNICODE_NULL;
430
431 /* Strip the trailing file name */
432 Ptr = wcsrchr(InstallSourcePath->Buffer, OBJ_NAME_PATH_SEPARATOR);
433 if (Ptr)
434 *Ptr = UNICODE_NULL;
435 InstallSourcePath->Length = wcslen(InstallSourcePath->Buffer) * sizeof(WCHAR);
436
437
438 /*
439 * Now resolve the full path to \SystemRoot. In case it prefixes
440 * the installation source path determined from the full path of
441 * the installer, we use instead the resolved \SystemRoot as the
442 * installation source path.
443 * Otherwise, we use instead the path from the full installer path.
444 */
445
446 InitializeObjectAttributes(&ObjectAttributes,
447 &SystemRootPath,
448 OBJ_CASE_INSENSITIVE,
449 NULL,
450 NULL);
451
452 Status = NtOpenSymbolicLinkObject(&LinkHandle,
453 SYMBOLIC_LINK_QUERY,
454 &ObjectAttributes);
455 if (!NT_SUCCESS(Status))
456 {
457 /*
458 * We failed at opening the \SystemRoot link (usually due to wrong
459 * access rights). Do not consider this as a fatal error, but use
460 * instead the image file path as the installation source path.
461 */
462 DPRINT1("NtOpenSymbolicLinkObject(%wZ) failed with Status 0x%08lx\n",
463 &SystemRootPath, Status);
464 goto InitPaths;
465 }
466
467 RtlInitEmptyUnicodeString(&SystemRootPath,
468 SystemRootBuffer,
469 sizeof(SystemRootBuffer));
470
471 /* Resolve the link and close its handle */
472 Status = NtQuerySymbolicLinkObject(LinkHandle,
473 &SystemRootPath,
474 &BufferSize);
475 NtClose(LinkHandle);
476
477 if (!NT_SUCCESS(Status))
478 return Status; // Unexpected error
479
480 /* Check whether the resolved \SystemRoot is a prefix of the image file path */
481 if (RtlPrefixUnicodeString(&SystemRootPath, InstallSourcePath, TRUE))
482 {
483 /* Yes it is, so we use instead SystemRoot as the installation source path */
484 InstallSourcePath = &SystemRootPath;
485 }
486
487
488 InitPaths:
489 /*
490 * Retrieve the different source path components
491 */
492 RtlCreateUnicodeString(SourcePath, InstallSourcePath->Buffer);
493
494 /* Strip trailing directory */
495 Ptr = wcsrchr(InstallSourcePath->Buffer, OBJ_NAME_PATH_SEPARATOR);
496 if (Ptr)
497 {
498 RtlCreateUnicodeString(SourceRootDir, Ptr);
499 *Ptr = UNICODE_NULL;
500 }
501 else
502 {
503 RtlCreateUnicodeString(SourceRootDir, L"");
504 }
505
506 RtlCreateUnicodeString(SourceRootPath, InstallSourcePath->Buffer);
507
508 return STATUS_SUCCESS;
509 }
510
511 ERROR_NUMBER
512 LoadSetupInf(
513 IN OUT PUSETUP_DATA pSetupData)
514 {
515 INFCONTEXT Context;
516 UINT ErrorLine;
517 INT IntValue;
518 PCWSTR Value;
519 WCHAR FileNameBuffer[MAX_PATH];
520
521 CombinePaths(FileNameBuffer, ARRAYSIZE(FileNameBuffer), 2,
522 pSetupData->SourcePath.Buffer, L"txtsetup.sif");
523
524 DPRINT("SetupInf path: '%S'\n", FileNameBuffer);
525
526 pSetupData->SetupInf =
527 SpInfOpenInfFile(FileNameBuffer,
528 NULL,
529 INF_STYLE_WIN4,
530 pSetupData->LanguageId,
531 &ErrorLine);
532 if (pSetupData->SetupInf == INVALID_HANDLE_VALUE)
533 return ERROR_LOAD_TXTSETUPSIF;
534
535 /* Open 'Version' section */
536 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"Version", L"Signature", &Context))
537 return ERROR_CORRUPT_TXTSETUPSIF;
538
539 /* Get pointer 'Signature' key */
540 if (!INF_GetData(&Context, NULL, &Value))
541 return ERROR_CORRUPT_TXTSETUPSIF;
542
543 /* Check 'Signature' string */
544 if (_wcsicmp(Value, L"$ReactOS$") != 0 &&
545 _wcsicmp(Value, L"$Windows NT$") != 0)
546 {
547 INF_FreeData(Value);
548 return ERROR_SIGNATURE_TXTSETUPSIF;
549 }
550
551 INF_FreeData(Value);
552
553 /* Open 'DiskSpaceRequirements' section */
554 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"DiskSpaceRequirements", L"FreeSysPartDiskSpace", &Context))
555 return ERROR_CORRUPT_TXTSETUPSIF;
556
557 pSetupData->RequiredPartitionDiskSpace = ~0;
558
559 /* Get the 'FreeSysPartDiskSpace' value */
560 if (!SpInfGetIntField(&Context, 1, &IntValue))
561 return ERROR_CORRUPT_TXTSETUPSIF;
562
563 pSetupData->RequiredPartitionDiskSpace = (ULONG)IntValue;
564
565 //
566 // Support "SetupSourceDevice" and "SetupSourcePath" in txtsetup.sif
567 // See CORE-9023
568 // Support for that should also be added in setupldr.
569 //
570
571 /* Update the Setup Source paths */
572 if (SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"SetupSourceDevice", &Context))
573 {
574 /*
575 * Get optional pointer 'SetupSourceDevice' key, its presence
576 * will dictate whether we also need 'SetupSourcePath'.
577 */
578 if (INF_GetData(&Context, NULL, &Value))
579 {
580 /* Free the old source root path string and create the new one */
581 RtlFreeUnicodeString(&pSetupData->SourceRootPath);
582 RtlCreateUnicodeString(&pSetupData->SourceRootPath, Value);
583 INF_FreeData(Value);
584
585 if (!SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"SetupSourcePath", &Context))
586 {
587 /* The 'SetupSourcePath' value is mandatory! */
588 return ERROR_CORRUPT_TXTSETUPSIF;
589 }
590
591 /* Get pointer 'SetupSourcePath' key */
592 if (!INF_GetData(&Context, NULL, &Value))
593 {
594 /* The 'SetupSourcePath' value is mandatory! */
595 return ERROR_CORRUPT_TXTSETUPSIF;
596 }
597
598 /* Free the old source path string and create the new one */
599 RtlFreeUnicodeString(&pSetupData->SourceRootDir);
600 RtlCreateUnicodeString(&pSetupData->SourceRootDir, Value);
601 INF_FreeData(Value);
602 }
603 }
604
605 /* Search for 'DefaultPath' in the 'SetupData' section */
606 pSetupData->InstallationDirectory[0] = 0;
607 if (SpInfFindFirstLine(pSetupData->SetupInf, L"SetupData", L"DefaultPath", &Context))
608 {
609 /* Get pointer 'DefaultPath' key */
610 if (!INF_GetData(&Context, NULL, &Value))
611 return ERROR_CORRUPT_TXTSETUPSIF;
612
613 RtlStringCchCopyW(pSetupData->InstallationDirectory,
614 ARRAYSIZE(pSetupData->InstallationDirectory),
615 Value);
616
617 INF_FreeData(Value);
618 }
619
620 return ERROR_SUCCESS;
621 }
622
623 NTSTATUS
624 InitDestinationPaths(
625 IN OUT PUSETUP_DATA pSetupData,
626 IN PCWSTR InstallationDir,
627 IN PDISKENTRY DiskEntry, // FIXME: HACK!
628 IN PPARTENTRY PartEntry) // FIXME: HACK!
629 {
630 WCHAR PathBuffer[MAX_PATH];
631 NTSTATUS Status;
632
633 /* Create 'pSetupData->DestinationRootPath' string */
634 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
635 Status = RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
636 L"\\Device\\Harddisk%lu\\Partition%lu\\",
637 DiskEntry->DiskNumber,
638 PartEntry->PartitionNumber);
639
640 if (!NT_SUCCESS(Status))
641 {
642 DPRINT1("RtlStringCchPrintfW() failed with status 0x%08lx\n", Status);
643 return Status;
644 }
645
646 Status = RtlCreateUnicodeString(&pSetupData->DestinationRootPath, PathBuffer) ? STATUS_SUCCESS : STATUS_NO_MEMORY;
647
648 if (!NT_SUCCESS(Status))
649 {
650 DPRINT1("RtlCreateUnicodeString() failed with status 0x%08lx\n", Status);
651 return Status;
652 }
653
654 DPRINT("DestinationRootPath: %wZ\n", &pSetupData->DestinationRootPath);
655
656 // FIXME! Which variable to choose?
657 if (!InstallationDir)
658 InstallationDir = pSetupData->InstallationDirectory;
659
660 /** Equivalent of 'NTOS_INSTALLATION::SystemArcPath' **/
661 /* Create 'pSetupData->DestinationArcPath' */
662 RtlFreeUnicodeString(&pSetupData->DestinationArcPath);
663 Status = RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
664 L"multi(0)disk(0)rdisk(%lu)partition(%lu)\\",
665 DiskEntry->BiosDiskNumber,
666 PartEntry->OnDiskPartitionNumber);
667
668 if (!NT_SUCCESS(Status))
669 {
670 DPRINT1("RtlStringCchPrintfW() failed with status 0x%08lx\n", Status);
671 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
672 return Status;
673 }
674
675 Status = ConcatPaths(PathBuffer, ARRAYSIZE(PathBuffer), 1, InstallationDir);
676
677 if (!NT_SUCCESS(Status))
678 {
679 DPRINT1("ConcatPaths() failed with status 0x%08lx\n", Status);
680 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
681 return Status;
682 }
683
684 Status = RtlCreateUnicodeString(&pSetupData->DestinationArcPath, PathBuffer) ? STATUS_SUCCESS : STATUS_NO_MEMORY;
685
686 if (!NT_SUCCESS(Status))
687 {
688 DPRINT1("RtlCreateUnicodeString() failed with status 0x%08lx\n", Status);
689 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
690 return Status;
691 }
692
693 /** Equivalent of 'NTOS_INSTALLATION::SystemNtPath' **/
694 /* Create 'pSetupData->DestinationPath' string */
695 RtlFreeUnicodeString(&pSetupData->DestinationPath);
696 Status = CombinePaths(PathBuffer, ARRAYSIZE(PathBuffer), 2,
697 pSetupData->DestinationRootPath.Buffer, InstallationDir);
698
699 if (!NT_SUCCESS(Status))
700 {
701 DPRINT1("CombinePaths() failed with status 0x%08lx\n", Status);
702 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
703 RtlFreeUnicodeString(&pSetupData->DestinationArcPath);
704 return Status;
705 }
706
707 Status = RtlCreateUnicodeString(&pSetupData->DestinationPath, PathBuffer) ? STATUS_SUCCESS : STATUS_NO_MEMORY;
708
709 if (!NT_SUCCESS(Status))
710 {
711 DPRINT1("RtlCreateUnicodeString() failed with status 0x%08lx\n", Status);
712 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
713 RtlFreeUnicodeString(&pSetupData->DestinationArcPath);
714 return Status;
715 }
716
717 /** Equivalent of 'NTOS_INSTALLATION::PathComponent' **/
718 // FIXME: This is only temporary!! Must be removed later!
719 Status = RtlCreateUnicodeString(&pSetupData->InstallPath, InstallationDir) ? STATUS_SUCCESS : STATUS_NO_MEMORY;
720
721 if (!NT_SUCCESS(Status))
722 {
723 DPRINT1("RtlCreateUnicodeString() failed with status 0x%08lx\n", Status);
724 RtlFreeUnicodeString(&pSetupData->DestinationRootPath);
725 RtlFreeUnicodeString(&pSetupData->DestinationArcPath);
726 RtlFreeUnicodeString(&pSetupData->DestinationPath);
727 return Status;
728 }
729
730 return STATUS_SUCCESS;
731 }
732
733 // NTSTATUS
734 ERROR_NUMBER
735 InitializeSetup(
736 IN OUT PUSETUP_DATA pSetupData,
737 IN ULONG InitPhase)
738 {
739 if (InitPhase == 0)
740 {
741 RtlZeroMemory(pSetupData, sizeof(*pSetupData));
742
743 // pSetupData->ComputerList = NULL;
744 // pSetupData->DisplayList = NULL;
745 // pSetupData->KeyboardList = NULL;
746 // pSetupData->LayoutList = NULL;
747 // pSetupData->LanguageList = NULL;
748
749 /* Initialize error handling */
750 pSetupData->LastErrorNumber = ERROR_SUCCESS;
751 pSetupData->ErrorRoutine = NULL;
752
753 /* Initialize global unicode strings */
754 RtlInitUnicodeString(&pSetupData->SourcePath, NULL);
755 RtlInitUnicodeString(&pSetupData->SourceRootPath, NULL);
756 RtlInitUnicodeString(&pSetupData->SourceRootDir, NULL);
757 RtlInitUnicodeString(&pSetupData->DestinationArcPath, NULL);
758 RtlInitUnicodeString(&pSetupData->DestinationPath, NULL);
759 RtlInitUnicodeString(&pSetupData->DestinationRootPath, NULL);
760 RtlInitUnicodeString(&pSetupData->SystemRootPath, NULL);
761
762 // FIXME: This is only temporary!! Must be removed later!
763 /***/RtlInitUnicodeString(&pSetupData->InstallPath, NULL);/***/
764
765 //
766 // TODO: Load and start SetupDD, and ask it for the information
767 //
768
769 return ERROR_SUCCESS;
770 }
771 else
772 if (InitPhase == 1)
773 {
774 ERROR_NUMBER Error;
775 NTSTATUS Status;
776
777 /* Get the source path and source root path */
778 //
779 // NOTE: Sometimes the source path may not be in SystemRoot !!
780 // (and this is the case when using the 1st-stage GUI setup!)
781 //
782 Status = GetSourcePaths(&pSetupData->SourcePath,
783 &pSetupData->SourceRootPath,
784 &pSetupData->SourceRootDir);
785 if (!NT_SUCCESS(Status))
786 {
787 DPRINT1("GetSourcePaths() failed (Status 0x%08lx)", Status);
788 return ERROR_NO_SOURCE_DRIVE;
789 }
790 /*
791 * Example of output:
792 * SourcePath: '\Device\CdRom0\I386'
793 * SourceRootPath: '\Device\CdRom0'
794 * SourceRootDir: '\I386'
795 */
796 DPRINT1("SourcePath (1): '%wZ'\n", &pSetupData->SourcePath);
797 DPRINT1("SourceRootPath (1): '%wZ'\n", &pSetupData->SourceRootPath);
798 DPRINT1("SourceRootDir (1): '%wZ'\n", &pSetupData->SourceRootDir);
799
800 /* Load 'txtsetup.sif' from the installation media */
801 Error = LoadSetupInf(pSetupData);
802 if (Error != ERROR_SUCCESS)
803 {
804 DPRINT1("LoadSetupInf() failed (Error 0x%lx)", Error);
805 return Error;
806 }
807 DPRINT1("SourcePath (2): '%wZ'\n", &pSetupData->SourcePath);
808 DPRINT1("SourceRootPath (2): '%wZ'\n", &pSetupData->SourceRootPath);
809 DPRINT1("SourceRootDir (2): '%wZ'\n", &pSetupData->SourceRootDir);
810
811 return ERROR_SUCCESS;
812 }
813
814 return ERROR_SUCCESS;
815 }
816
817 VOID
818 FinishSetup(
819 IN OUT PUSETUP_DATA pSetupData)
820 {
821 /* Destroy the computer settings list */
822 if (pSetupData->ComputerList != NULL)
823 {
824 DestroyGenericList(pSetupData->ComputerList, TRUE);
825 pSetupData->ComputerList = NULL;
826 }
827
828 /* Destroy the display settings list */
829 if (pSetupData->DisplayList != NULL)
830 {
831 DestroyGenericList(pSetupData->DisplayList, TRUE);
832 pSetupData->DisplayList = NULL;
833 }
834
835 /* Destroy the keyboard settings list */
836 if (pSetupData->KeyboardList != NULL)
837 {
838 DestroyGenericList(pSetupData->KeyboardList, TRUE);
839 pSetupData->KeyboardList = NULL;
840 }
841
842 /* Destroy the keyboard layout list */
843 if (pSetupData->LayoutList != NULL)
844 {
845 DestroyGenericList(pSetupData->LayoutList, TRUE);
846 pSetupData->LayoutList = NULL;
847 }
848
849 /* Destroy the languages list */
850 if (pSetupData->LanguageList != NULL)
851 {
852 DestroyGenericList(pSetupData->LanguageList, FALSE);
853 pSetupData->LanguageList = NULL;
854 }
855
856 /* Close the Setup INF */
857 SpInfCloseInfFile(pSetupData->SetupInf);
858 }
859
860 /*
861 * SIDEEFFECTS
862 * Calls RegInitializeRegistry
863 * Calls ImportRegistryFile
864 * Calls SetDefaultPagefile
865 * Calls SetMountedDeviceValues
866 */
867 ERROR_NUMBER
868 UpdateRegistry(
869 IN OUT PUSETUP_DATA pSetupData,
870 /**/IN BOOLEAN RepairUpdateFlag, /* HACK HACK! */
871 /**/IN PPARTLIST PartitionList, /* HACK HACK! */
872 /**/IN WCHAR DestinationDriveLetter, /* HACK HACK! */
873 /**/IN PCWSTR SelectedLanguageId, /* HACK HACK! */
874 IN PREGISTRY_STATUS_ROUTINE StatusRoutine OPTIONAL)
875 {
876 ERROR_NUMBER ErrorNumber;
877 NTSTATUS Status;
878 INFCONTEXT InfContext;
879 PCWSTR Action;
880 PCWSTR File;
881 PCWSTR Section;
882 BOOLEAN Success;
883 BOOLEAN ShouldRepairRegistry = FALSE;
884 BOOLEAN Delete;
885
886 if (RepairUpdateFlag)
887 {
888 DPRINT1("TODO: Updating / repairing the registry is not completely implemented yet!\n");
889
890 /* Verify the registry hives and check whether we need to update or repair any of them */
891 Status = VerifyRegistryHives(&pSetupData->DestinationPath, &ShouldRepairRegistry);
892 if (!NT_SUCCESS(Status))
893 {
894 DPRINT1("VerifyRegistryHives failed, Status 0x%08lx\n", Status);
895 ShouldRepairRegistry = FALSE;
896 }
897 if (!ShouldRepairRegistry)
898 DPRINT1("No need to repair the registry\n");
899 }
900
901 DoUpdate:
902 ErrorNumber = ERROR_SUCCESS;
903
904 /* Update the registry */
905 if (StatusRoutine) StatusRoutine(RegHiveUpdate);
906
907 /* Initialize the registry and setup the registry hives */
908 Status = RegInitializeRegistry(&pSetupData->DestinationPath);
909 if (!NT_SUCCESS(Status))
910 {
911 DPRINT1("RegInitializeRegistry() failed\n");
912 /********** HACK!!!!!!!!!!! **********/
913 if (Status == STATUS_NOT_IMPLEMENTED)
914 {
915 /* The hack was called, return its corresponding error */
916 return ERROR_INITIALIZE_REGISTRY;
917 }
918 else
919 /*************************************/
920 {
921 /* Something else failed */
922 return ERROR_CREATE_HIVE;
923 }
924 }
925
926 if (!RepairUpdateFlag || ShouldRepairRegistry)
927 {
928 /*
929 * We fully setup the hives, in case we are doing a fresh installation
930 * (RepairUpdateFlag == FALSE), or in case we are doing an update
931 * (RepairUpdateFlag == TRUE) BUT we have some registry hives to
932 * "repair" (aka. recreate: ShouldRepairRegistry == TRUE).
933 */
934
935 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Fresh", NULL, &InfContext); // Windows-compatible
936 if (!Success)
937 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Install", NULL, &InfContext); // ReactOS-specific
938
939 if (!Success)
940 {
941 DPRINT1("SpInfFindFirstLine() failed\n");
942 ErrorNumber = ERROR_FIND_REGISTRY;
943 goto Cleanup;
944 }
945 }
946 else // if (RepairUpdateFlag && !ShouldRepairRegistry)
947 {
948 /*
949 * In case we are doing an update (RepairUpdateFlag == TRUE) and
950 * NO registry hives need a repair (ShouldRepairRegistry == FALSE),
951 * we only update the hives.
952 */
953
954 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Upgrade", NULL, &InfContext);
955 if (!Success)
956 {
957 /* Nothing to do for update! */
958 DPRINT1("No update needed for the registry!\n");
959 goto Cleanup;
960 }
961 }
962
963 do
964 {
965 INF_GetDataField(&InfContext, 0, &Action);
966 INF_GetDataField(&InfContext, 1, &File);
967 INF_GetDataField(&InfContext, 2, &Section);
968
969 DPRINT("Action: %S File: %S Section %S\n", Action, File, Section);
970
971 if (Action == NULL)
972 {
973 INF_FreeData(Action);
974 INF_FreeData(File);
975 INF_FreeData(Section);
976 break; // Hackfix
977 }
978
979 if (!_wcsicmp(Action, L"AddReg"))
980 Delete = FALSE;
981 else if (!_wcsicmp(Action, L"DelReg"))
982 Delete = TRUE;
983 else
984 {
985 DPRINT1("Unrecognized registry INF action '%S'\n", Action);
986 INF_FreeData(Action);
987 INF_FreeData(File);
988 INF_FreeData(Section);
989 continue;
990 }
991
992 INF_FreeData(Action);
993
994 if (StatusRoutine) StatusRoutine(ImportRegHive, File);
995
996 if (!ImportRegistryFile(pSetupData->SourcePath.Buffer,
997 File, Section,
998 pSetupData->LanguageId, Delete))
999 {
1000 DPRINT1("Importing %S failed\n", File);
1001 INF_FreeData(File);
1002 INF_FreeData(Section);
1003 ErrorNumber = ERROR_IMPORT_HIVE;
1004 goto Cleanup;
1005 }
1006 } while (SpInfFindNextLine(&InfContext, &InfContext));
1007
1008 if (!RepairUpdateFlag || ShouldRepairRegistry)
1009 {
1010 /* See the explanation for this test above */
1011
1012 /* Update display registry settings */
1013 if (StatusRoutine) StatusRoutine(DisplaySettingsUpdate);
1014 if (!ProcessDisplayRegistry(pSetupData->SetupInf, pSetupData->DisplayList))
1015 {
1016 ErrorNumber = ERROR_UPDATE_DISPLAY_SETTINGS;
1017 goto Cleanup;
1018 }
1019
1020 /* Set the locale */
1021 if (StatusRoutine) StatusRoutine(LocaleSettingsUpdate);
1022 if (!ProcessLocaleRegistry(pSetupData->LanguageList))
1023 {
1024 ErrorNumber = ERROR_UPDATE_LOCALESETTINGS;
1025 goto Cleanup;
1026 }
1027
1028 /* Add keyboard layouts */
1029 if (StatusRoutine) StatusRoutine(KeybLayouts);
1030 if (!AddKeyboardLayouts(SelectedLanguageId))
1031 {
1032 ErrorNumber = ERROR_ADDING_KBLAYOUTS;
1033 goto Cleanup;
1034 }
1035
1036 /* Set GeoID */
1037 if (!SetGeoID(MUIGetGeoID(SelectedLanguageId)))
1038 {
1039 ErrorNumber = ERROR_UPDATE_GEOID;
1040 goto Cleanup;
1041 }
1042
1043 if (!IsUnattendedSetup)
1044 {
1045 /* Update keyboard layout settings */
1046 if (StatusRoutine) StatusRoutine(KeybSettingsUpdate);
1047 if (!ProcessKeyboardLayoutRegistry(pSetupData->LayoutList, SelectedLanguageId))
1048 {
1049 ErrorNumber = ERROR_UPDATE_KBSETTINGS;
1050 goto Cleanup;
1051 }
1052 }
1053
1054 /* Add codepage information to registry */
1055 if (StatusRoutine) StatusRoutine(CodePageInfoUpdate);
1056 if (!AddCodePage(SelectedLanguageId))
1057 {
1058 ErrorNumber = ERROR_ADDING_CODEPAGE;
1059 goto Cleanup;
1060 }
1061
1062 /* Set the default pagefile entry */
1063 SetDefaultPagefile(DestinationDriveLetter);
1064
1065 /* Update the mounted devices list */
1066 // FIXME: This should technically be done by mountmgr (if AutoMount is enabled)!
1067 SetMountedDeviceValues(PartitionList);
1068 }
1069
1070 Cleanup:
1071 //
1072 // TODO: Unload all the registry stuff, perform cleanup,
1073 // and copy the created hive files into .sav files.
1074 //
1075 RegCleanupRegistry(&pSetupData->DestinationPath);
1076
1077 /*
1078 * Check whether we were in update/repair mode but we were actually
1079 * repairing the registry hives. If so, we have finished repairing them,
1080 * and we now reset the flag and run the proper registry update.
1081 * Otherwise we have finished the registry update!
1082 */
1083 if (RepairUpdateFlag && ShouldRepairRegistry)
1084 {
1085 ShouldRepairRegistry = FALSE;
1086 goto DoUpdate;
1087 }
1088
1089 return ErrorNumber;
1090 }
1091
1092 /* EOF */