[SETUPLIB][USETUP] Add support for setup error handling.
[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 error handling */
679 pSetupData->LastErrorNumber = ERROR_SUCCESS;
680 pSetupData->ErrorRoutine = NULL;
681
682 /* Initialize global unicode strings */
683 RtlInitUnicodeString(&pSetupData->SourcePath, NULL);
684 RtlInitUnicodeString(&pSetupData->SourceRootPath, NULL);
685 RtlInitUnicodeString(&pSetupData->SourceRootDir, NULL);
686 RtlInitUnicodeString(&pSetupData->DestinationArcPath, NULL);
687 RtlInitUnicodeString(&pSetupData->DestinationPath, NULL);
688 RtlInitUnicodeString(&pSetupData->DestinationRootPath, NULL);
689 RtlInitUnicodeString(&pSetupData->SystemRootPath, NULL);
690
691 // FIXME: This is only temporary!! Must be removed later!
692 /***/RtlInitUnicodeString(&pSetupData->InstallPath, NULL);/***/
693
694 //
695 // TODO: Load and start SetupDD, and ask it for the information
696 //
697
698 return ERROR_SUCCESS;
699 }
700 else
701 if (InitPhase == 1)
702 {
703 ERROR_NUMBER Error;
704 NTSTATUS Status;
705
706 /* Get the source path and source root path */
707 //
708 // NOTE: Sometimes the source path may not be in SystemRoot !!
709 // (and this is the case when using the 1st-stage GUI setup!)
710 //
711 Status = GetSourcePaths(&pSetupData->SourcePath,
712 &pSetupData->SourceRootPath,
713 &pSetupData->SourceRootDir);
714 if (!NT_SUCCESS(Status))
715 {
716 DPRINT1("GetSourcePaths() failed (Status 0x%08lx)", Status);
717 return ERROR_NO_SOURCE_DRIVE;
718 }
719 /*
720 * Example of output:
721 * SourcePath: '\Device\CdRom0\I386'
722 * SourceRootPath: '\Device\CdRom0'
723 * SourceRootDir: '\I386'
724 */
725 DPRINT1("SourcePath (1): '%wZ'\n", &pSetupData->SourcePath);
726 DPRINT1("SourceRootPath (1): '%wZ'\n", &pSetupData->SourceRootPath);
727 DPRINT1("SourceRootDir (1): '%wZ'\n", &pSetupData->SourceRootDir);
728
729 /* Load 'txtsetup.sif' from the installation media */
730 Error = LoadSetupInf(pSetupData);
731 if (Error != ERROR_SUCCESS)
732 {
733 DPRINT1("LoadSetupInf() failed (Error 0x%lx)", Error);
734 return Error;
735 }
736 DPRINT1("SourcePath (2): '%wZ'\n", &pSetupData->SourcePath);
737 DPRINT1("SourceRootPath (2): '%wZ'\n", &pSetupData->SourceRootPath);
738 DPRINT1("SourceRootDir (2): '%wZ'\n", &pSetupData->SourceRootDir);
739
740 return ERROR_SUCCESS;
741 }
742
743 return ERROR_SUCCESS;
744 }
745
746 VOID
747 FinishSetup(
748 IN OUT PUSETUP_DATA pSetupData)
749 {
750 /* Destroy the computer settings list */
751 if (pSetupData->ComputerList != NULL)
752 {
753 DestroyGenericList(pSetupData->ComputerList, TRUE);
754 pSetupData->ComputerList = NULL;
755 }
756
757 /* Destroy the display settings list */
758 if (pSetupData->DisplayList != NULL)
759 {
760 DestroyGenericList(pSetupData->DisplayList, TRUE);
761 pSetupData->DisplayList = NULL;
762 }
763
764 /* Destroy the keyboard settings list */
765 if (pSetupData->KeyboardList != NULL)
766 {
767 DestroyGenericList(pSetupData->KeyboardList, TRUE);
768 pSetupData->KeyboardList = NULL;
769 }
770
771 /* Destroy the keyboard layout list */
772 if (pSetupData->LayoutList != NULL)
773 {
774 DestroyGenericList(pSetupData->LayoutList, TRUE);
775 pSetupData->LayoutList = NULL;
776 }
777
778 /* Destroy the languages list */
779 if (pSetupData->LanguageList != NULL)
780 {
781 DestroyGenericList(pSetupData->LanguageList, FALSE);
782 pSetupData->LanguageList = NULL;
783 }
784
785 /* Close the Setup INF */
786 SpInfCloseInfFile(pSetupData->SetupInf);
787 }
788
789 /*
790 * SIDEEFFECTS
791 * Calls RegInitializeRegistry
792 * Calls ImportRegistryFile
793 * Calls SetDefaultPagefile
794 * Calls SetMountedDeviceValues
795 */
796 ERROR_NUMBER
797 UpdateRegistry(
798 IN OUT PUSETUP_DATA pSetupData,
799 /**/IN BOOLEAN RepairUpdateFlag, /* HACK HACK! */
800 /**/IN PPARTLIST PartitionList, /* HACK HACK! */
801 /**/IN WCHAR DestinationDriveLetter, /* HACK HACK! */
802 /**/IN PCWSTR SelectedLanguageId, /* HACK HACK! */
803 IN PREGISTRY_STATUS_ROUTINE StatusRoutine OPTIONAL)
804 {
805 ERROR_NUMBER ErrorNumber;
806 NTSTATUS Status;
807 INFCONTEXT InfContext;
808 PCWSTR Action;
809 PCWSTR File;
810 PCWSTR Section;
811 BOOLEAN Success;
812 BOOLEAN ShouldRepairRegistry = FALSE;
813 BOOLEAN Delete;
814
815 if (RepairUpdateFlag)
816 {
817 DPRINT1("TODO: Updating / repairing the registry is not completely implemented yet!\n");
818
819 /* Verify the registry hives and check whether we need to update or repair any of them */
820 Status = VerifyRegistryHives(&pSetupData->DestinationPath, &ShouldRepairRegistry);
821 if (!NT_SUCCESS(Status))
822 {
823 DPRINT1("VerifyRegistryHives failed, Status 0x%08lx\n", Status);
824 ShouldRepairRegistry = FALSE;
825 }
826 if (!ShouldRepairRegistry)
827 DPRINT1("No need to repair the registry\n");
828 }
829
830 DoUpdate:
831 ErrorNumber = ERROR_SUCCESS;
832
833 /* Update the registry */
834 if (StatusRoutine) StatusRoutine(RegHiveUpdate);
835
836 /* Initialize the registry and setup the registry hives */
837 Status = RegInitializeRegistry(&pSetupData->DestinationPath);
838 if (!NT_SUCCESS(Status))
839 {
840 DPRINT1("RegInitializeRegistry() failed\n");
841 /********** HACK!!!!!!!!!!! **********/
842 if (Status == STATUS_NOT_IMPLEMENTED)
843 {
844 /* The hack was called, return its corresponding error */
845 return ERROR_INITIALIZE_REGISTRY;
846 }
847 else
848 /*************************************/
849 {
850 /* Something else failed */
851 return ERROR_CREATE_HIVE;
852 }
853 }
854
855 if (!RepairUpdateFlag || ShouldRepairRegistry)
856 {
857 /*
858 * We fully setup the hives, in case we are doing a fresh installation
859 * (RepairUpdateFlag == FALSE), or in case we are doing an update
860 * (RepairUpdateFlag == TRUE) BUT we have some registry hives to
861 * "repair" (aka. recreate: ShouldRepairRegistry == TRUE).
862 */
863
864 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Fresh", NULL, &InfContext); // Windows-compatible
865 if (!Success)
866 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Install", NULL, &InfContext); // ReactOS-specific
867
868 if (!Success)
869 {
870 DPRINT1("SpInfFindFirstLine() failed\n");
871 ErrorNumber = ERROR_FIND_REGISTRY;
872 goto Cleanup;
873 }
874 }
875 else // if (RepairUpdateFlag && !ShouldRepairRegistry)
876 {
877 /*
878 * In case we are doing an update (RepairUpdateFlag == TRUE) and
879 * NO registry hives need a repair (ShouldRepairRegistry == FALSE),
880 * we only update the hives.
881 */
882
883 Success = SpInfFindFirstLine(pSetupData->SetupInf, L"HiveInfs.Upgrade", NULL, &InfContext);
884 if (!Success)
885 {
886 /* Nothing to do for update! */
887 DPRINT1("No update needed for the registry!\n");
888 goto Cleanup;
889 }
890 }
891
892 do
893 {
894 INF_GetDataField(&InfContext, 0, &Action);
895 INF_GetDataField(&InfContext, 1, &File);
896 INF_GetDataField(&InfContext, 2, &Section);
897
898 DPRINT("Action: %S File: %S Section %S\n", Action, File, Section);
899
900 if (Action == NULL)
901 {
902 INF_FreeData(Action);
903 INF_FreeData(File);
904 INF_FreeData(Section);
905 break; // Hackfix
906 }
907
908 if (!_wcsicmp(Action, L"AddReg"))
909 Delete = FALSE;
910 else if (!_wcsicmp(Action, L"DelReg"))
911 Delete = TRUE;
912 else
913 {
914 DPRINT1("Unrecognized registry INF action '%S'\n", Action);
915 INF_FreeData(Action);
916 INF_FreeData(File);
917 INF_FreeData(Section);
918 continue;
919 }
920
921 INF_FreeData(Action);
922
923 if (StatusRoutine) StatusRoutine(ImportRegHive, File);
924
925 if (!ImportRegistryFile(pSetupData->SourcePath.Buffer,
926 File, Section,
927 pSetupData->LanguageId, Delete))
928 {
929 DPRINT1("Importing %S failed\n", File);
930 INF_FreeData(File);
931 INF_FreeData(Section);
932 ErrorNumber = ERROR_IMPORT_HIVE;
933 goto Cleanup;
934 }
935 } while (SpInfFindNextLine(&InfContext, &InfContext));
936
937 if (!RepairUpdateFlag || ShouldRepairRegistry)
938 {
939 /* See the explanation for this test above */
940
941 /* Update display registry settings */
942 if (StatusRoutine) StatusRoutine(DisplaySettingsUpdate);
943 if (!ProcessDisplayRegistry(pSetupData->SetupInf, pSetupData->DisplayList))
944 {
945 ErrorNumber = ERROR_UPDATE_DISPLAY_SETTINGS;
946 goto Cleanup;
947 }
948
949 /* Set the locale */
950 if (StatusRoutine) StatusRoutine(LocaleSettingsUpdate);
951 if (!ProcessLocaleRegistry(pSetupData->LanguageList))
952 {
953 ErrorNumber = ERROR_UPDATE_LOCALESETTINGS;
954 goto Cleanup;
955 }
956
957 /* Add keyboard layouts */
958 if (StatusRoutine) StatusRoutine(KeybLayouts);
959 if (!AddKeyboardLayouts(SelectedLanguageId))
960 {
961 ErrorNumber = ERROR_ADDING_KBLAYOUTS;
962 goto Cleanup;
963 }
964
965 /* Set GeoID */
966 if (!SetGeoID(MUIGetGeoID(SelectedLanguageId)))
967 {
968 ErrorNumber = ERROR_UPDATE_GEOID;
969 goto Cleanup;
970 }
971
972 if (!IsUnattendedSetup)
973 {
974 /* Update keyboard layout settings */
975 if (StatusRoutine) StatusRoutine(KeybSettingsUpdate);
976 if (!ProcessKeyboardLayoutRegistry(pSetupData->LayoutList, SelectedLanguageId))
977 {
978 ErrorNumber = ERROR_UPDATE_KBSETTINGS;
979 goto Cleanup;
980 }
981 }
982
983 /* Add codepage information to registry */
984 if (StatusRoutine) StatusRoutine(CodePageInfoUpdate);
985 if (!AddCodePage(SelectedLanguageId))
986 {
987 ErrorNumber = ERROR_ADDING_CODEPAGE;
988 goto Cleanup;
989 }
990
991 /* Set the default pagefile entry */
992 SetDefaultPagefile(DestinationDriveLetter);
993
994 /* Update the mounted devices list */
995 // FIXME: This should technically be done by mountmgr (if AutoMount is enabled)!
996 SetMountedDeviceValues(PartitionList);
997 }
998
999 Cleanup:
1000 //
1001 // TODO: Unload all the registry stuff, perform cleanup,
1002 // and copy the created hive files into .sav files.
1003 //
1004 RegCleanupRegistry(&pSetupData->DestinationPath);
1005
1006 /*
1007 * Check whether we were in update/repair mode but we were actually
1008 * repairing the registry hives. If so, we have finished repairing them,
1009 * and we now reset the flag and run the proper registry update.
1010 * Otherwise we have finished the registry update!
1011 */
1012 if (RepairUpdateFlag && ShouldRepairRegistry)
1013 {
1014 ShouldRepairRegistry = FALSE;
1015 goto DoUpdate;
1016 }
1017
1018 return ErrorNumber;
1019 }
1020
1021 /* EOF */