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)
10 /* INCLUDES *****************************************************************/
23 /* GLOBALS ******************************************************************/
25 /* FUNCTIONS ****************************************************************/
29 IN OUT PUSETUP_DATA pSetupData
)
36 WCHAR UnattendInfPath
[MAX_PATH
];
38 CombinePaths(UnattendInfPath
, ARRAYSIZE(UnattendInfPath
), 2,
39 pSetupData
->SourcePath
.Buffer
, L
"unattend.inf");
41 DPRINT("UnattendInf path: '%S'\n", UnattendInfPath
);
43 if (DoesFileExist(NULL
, UnattendInfPath
) == FALSE
)
45 DPRINT("Does not exist: %S\n", UnattendInfPath
);
49 /* Load 'unattend.inf' from installation media */
50 UnattendInf
= SetupOpenInfFileExW(UnattendInfPath
,
53 pSetupData
->LanguageId
,
56 if (UnattendInf
== INVALID_HANDLE_VALUE
)
58 DPRINT("SetupOpenInfFileExW() failed\n");
62 /* Open 'Unattend' section */
63 if (!SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"Signature", &Context
))
65 DPRINT("SetupFindFirstLineW() failed for section 'Unattend'\n");
69 /* Get pointer 'Signature' key */
70 if (!INF_GetData(&Context
, NULL
, &Value
))
72 DPRINT("INF_GetData() failed for key 'Signature'\n");
76 /* Check 'Signature' string */
77 if (_wcsicmp(Value
, L
"$ReactOS$") != 0)
79 DPRINT("Signature not $ReactOS$\n");
86 /* Check if Unattend setup is enabled */
87 if (!SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"UnattendSetupEnabled", &Context
))
89 DPRINT("Can't find key 'UnattendSetupEnabled'\n");
93 if (!INF_GetData(&Context
, NULL
, &Value
))
95 DPRINT("Can't read key 'UnattendSetupEnabled'\n");
99 if (_wcsicmp(Value
, L
"yes") != 0)
101 DPRINT("Unattend setup is disabled by 'UnattendSetupEnabled' key!\n");
108 /* Search for 'DestinationDiskNumber' in the 'Unattend' section */
109 if (!SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"DestinationDiskNumber", &Context
))
111 DPRINT("SetupFindFirstLine() failed for key 'DestinationDiskNumber'\n");
115 if (!SetupGetIntField(&Context
, 1, &IntValue
))
117 DPRINT("SetupGetIntField() failed for key 'DestinationDiskNumber'\n");
121 pSetupData
->DestinationDiskNumber
= (LONG
)IntValue
;
123 /* Search for 'DestinationPartitionNumber' in the 'Unattend' section */
124 if (!SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"DestinationPartitionNumber", &Context
))
126 DPRINT("SetupFindFirstLine() failed for key 'DestinationPartitionNumber'\n");
130 if (!SetupGetIntField(&Context
, 1, &IntValue
))
132 DPRINT("SetupGetIntField() failed for key 'DestinationPartitionNumber'\n");
136 pSetupData
->DestinationPartitionNumber
= (LONG
)IntValue
;
138 /* Search for 'InstallationDirectory' in the 'Unattend' section (optional) */
139 if (SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"InstallationDirectory", &Context
))
141 /* Get pointer 'InstallationDirectory' key */
142 if (!INF_GetData(&Context
, NULL
, &Value
))
144 DPRINT("INF_GetData() failed for key 'InstallationDirectory'\n");
148 RtlStringCchCopyW(pSetupData
->InstallationDirectory
,
149 ARRAYSIZE(pSetupData
->InstallationDirectory
),
155 IsUnattendedSetup
= TRUE
;
156 DPRINT("Running unattended setup\n");
158 /* Search for 'MBRInstallType' in the 'Unattend' section */
159 pSetupData
->MBRInstallType
= -1;
160 if (SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"MBRInstallType", &Context
))
162 if (SetupGetIntField(&Context
, 1, &IntValue
))
164 pSetupData
->MBRInstallType
= IntValue
;
168 /* Search for 'FormatPartition' in the 'Unattend' section */
169 pSetupData
->FormatPartition
= 0;
170 if (SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"FormatPartition", &Context
))
172 if (SetupGetIntField(&Context
, 1, &IntValue
))
174 pSetupData
->FormatPartition
= IntValue
;
178 pSetupData
->AutoPartition
= 0;
179 if (SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"AutoPartition", &Context
))
181 if (SetupGetIntField(&Context
, 1, &IntValue
))
183 pSetupData
->AutoPartition
= IntValue
;
187 /* Search for LocaleID in the 'Unattend' section */
188 if (SetupFindFirstLineW(UnattendInf
, L
"Unattend", L
"LocaleID", &Context
))
190 if (INF_GetData(&Context
, NULL
, &Value
))
192 LONG Id
= wcstol(Value
, NULL
, 16);
193 RtlStringCchPrintfW(pSetupData
->LocaleID
,
194 ARRAYSIZE(pSetupData
->LocaleID
),
201 SetupCloseInfFile(UnattendInf
);
206 IN OUT PUSETUP_DATA pSetupData
)
212 PINICACHE UnattendCache
;
213 PINICACHEITERATOR Iterator
;
215 // PCWSTR CrLf = L"\r\n";
217 HANDLE FileHandle
, UnattendFileHandle
, SectionHandle
;
218 FILE_STANDARD_INFORMATION FileInfo
;
221 UNICODE_STRING FileName
;
222 OBJECT_ATTRIBUTES ObjectAttributes
;
223 IO_STATUS_BLOCK IoStatusBlock
;
226 PINICACHESECTION IniSection
;
227 WCHAR PathBuffer
[MAX_PATH
];
228 WCHAR UnattendInfPath
[MAX_PATH
];
230 /* Create a $winnt$.inf file with default entries */
231 IniCache
= IniCacheCreate();
235 IniSection
= IniCacheAppendSection(IniCache
, L
"SetupParams");
238 /* Key "skipmissingfiles" */
239 // RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
240 // L"\"%s\"", L"WinNt5.2");
241 // IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
242 // L"Version", PathBuffer);
245 IniSection
= IniCacheAppendSection(IniCache
, L
"Data");
248 RtlStringCchPrintfW(PathBuffer
, ARRAYSIZE(PathBuffer
),
249 L
"\"%s\"", IsUnattendedSetup
? L
"yes" : L
"no");
250 IniCacheInsertKey(IniSection
, NULL
, INSERT_LAST
,
251 L
"UnattendedInstall", PathBuffer
);
253 // "floppylessbootpath" (yes/no)
255 RtlStringCchPrintfW(PathBuffer
, ARRAYSIZE(PathBuffer
),
256 L
"\"%s\"", L
"winnt");
257 IniCacheInsertKey(IniSection
, NULL
, INSERT_LAST
,
258 L
"ProductType", PathBuffer
);
260 RtlStringCchPrintfW(PathBuffer
, ARRAYSIZE(PathBuffer
),
261 L
"\"%s\\\"", pSetupData
->SourceRootPath
.Buffer
);
262 IniCacheInsertKey(IniSection
, NULL
, INSERT_LAST
,
263 L
"SourcePath", PathBuffer
);
265 // "floppyless" ("0")
270 /* TODO: Append the standard unattend.inf file */
271 CombinePaths(UnattendInfPath
, ARRAYSIZE(UnattendInfPath
), 2,
272 pSetupData
->SourcePath
.Buffer
, L
"unattend.inf");
273 if (DoesFileExist(NULL
, UnattendInfPath
) == FALSE
)
275 DPRINT("Does not exist: %S\n", UnattendInfPath
);
279 Status
= IniCacheLoad(&UnattendCache
, UnattendInfPath
, FALSE
);
280 if (!NT_SUCCESS(Status
))
282 DPRINT1("Cannot load %S as an INI file!\n", UnattendInfPath
);
286 IniCacheDestroy(UnattendCache
);
289 CombinePaths(PathBuffer
, ARRAYSIZE(PathBuffer
), 2,
290 pSetupData
->DestinationPath
.Buffer
, L
"System32\\$winnt$.inf");
291 IniCacheSave(IniCache
, PathBuffer
);
292 IniCacheDestroy(IniCache
);
296 CombinePaths(PathBuffer
, ARRAYSIZE(PathBuffer
), 2,
297 pSetupData
->DestinationPath
.Buffer
, L
"System32\\$winnt$.inf");
298 IniCacheSave(IniCache
, PathBuffer
);
299 IniCacheDestroy(IniCache
);
301 /* TODO: Append the standard unattend.inf file */
302 CombinePaths(UnattendInfPath
, ARRAYSIZE(UnattendInfPath
), 2,
303 pSetupData
->SourcePath
.Buffer
, L
"unattend.inf");
304 if (DoesFileExist(NULL
, UnattendInfPath
) == FALSE
)
306 DPRINT("Does not exist: %S\n", UnattendInfPath
);
310 RtlInitUnicodeString(&FileName
, PathBuffer
);
311 InitializeObjectAttributes(&ObjectAttributes
,
313 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
316 Status
= NtOpenFile(&FileHandle
,
317 FILE_APPEND_DATA
| SYNCHRONIZE
,
321 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
322 if (!NT_SUCCESS(Status
))
324 DPRINT1("Cannot load %S as an INI file!\n", PathBuffer
);
328 /* Query the file size */
329 Status
= NtQueryInformationFile(FileHandle
,
333 FileStandardInformation
);
334 if (!NT_SUCCESS(Status
))
336 DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status
);
337 FileInfo
.EndOfFile
.QuadPart
= 0ULL;
340 Status
= OpenAndMapFile(NULL
,
347 if (!NT_SUCCESS(Status
))
349 DPRINT1("Cannot load %S !\n", UnattendInfPath
);
354 /* Write to the INI file */
357 Status
= NtWriteFile(FileHandle
,
363 2 * sizeof(CHAR
), // 2 * sizeof(WCHAR),
367 Status
= NtWriteFile(FileHandle
,
376 if (!NT_SUCCESS(Status
))
378 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
381 /* Finally, unmap and close the file */
382 UnMapAndCloseFile(UnattendFileHandle
, SectionHandle
, ViewBase
);
390 OUT PUNICODE_STRING SourcePath
,
391 OUT PUNICODE_STRING SourceRootPath
,
392 OUT PUNICODE_STRING SourceRootDir
)
396 OBJECT_ATTRIBUTES ObjectAttributes
;
397 UCHAR ImageFileBuffer
[sizeof(UNICODE_STRING
) + MAX_PATH
* sizeof(WCHAR
)];
398 PUNICODE_STRING InstallSourcePath
= (PUNICODE_STRING
)&ImageFileBuffer
;
399 WCHAR SystemRootBuffer
[MAX_PATH
] = L
"";
400 UNICODE_STRING SystemRootPath
= RTL_CONSTANT_STRING(L
"\\SystemRoot");
404 /* Determine the installation source path via the full path of the installer */
405 RtlInitEmptyUnicodeString(InstallSourcePath
,
406 (PWSTR
)((ULONG_PTR
)ImageFileBuffer
+ sizeof(UNICODE_STRING
)),
407 sizeof(ImageFileBuffer
) - sizeof(UNICODE_STRING
)
408 /* Reserve space for a NULL terminator */ - sizeof(UNICODE_NULL
));
409 BufferSize
= sizeof(ImageFileBuffer
);
410 Status
= NtQueryInformationProcess(NtCurrentProcess(),
411 ProcessImageFileName
,
415 // STATUS_INFO_LENGTH_MISMATCH or STATUS_BUFFER_TOO_SMALL ?
416 if (!NT_SUCCESS(Status
))
419 /* Manually NULL-terminate */
420 InstallSourcePath
->Buffer
[InstallSourcePath
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
422 /* Strip the trailing file name */
423 Ptr
= wcsrchr(InstallSourcePath
->Buffer
, OBJ_NAME_PATH_SEPARATOR
);
426 InstallSourcePath
->Length
= wcslen(InstallSourcePath
->Buffer
) * sizeof(WCHAR
);
430 * Now resolve the full path to \SystemRoot. In case it prefixes
431 * the installation source path determined from the full path of
432 * the installer, we use instead the resolved \SystemRoot as the
433 * installation source path.
434 * Otherwise, we use instead the path from the full installer path.
437 InitializeObjectAttributes(&ObjectAttributes
,
439 OBJ_CASE_INSENSITIVE
,
443 Status
= NtOpenSymbolicLinkObject(&Handle
,
446 if (!NT_SUCCESS(Status
))
449 * We failed at opening the \SystemRoot link (usually due to wrong
450 * access rights). Do not consider this as a fatal error, but use
451 * instead the image file path as the installation source path.
453 DPRINT1("NtOpenSymbolicLinkObject(%wZ) failed with Status 0x%08lx\n",
454 &SystemRootPath
, Status
);
458 RtlInitEmptyUnicodeString(&SystemRootPath
,
460 sizeof(SystemRootBuffer
));
462 Status
= NtQuerySymbolicLinkObject(Handle
,
467 if (!NT_SUCCESS(Status
))
468 return Status
; // Unexpected error
470 /* Check whether the resolved \SystemRoot is a prefix of the image file path */
471 if (RtlPrefixUnicodeString(&SystemRootPath
, InstallSourcePath
, TRUE
))
473 /* Yes it is, so we use instead SystemRoot as the installation source path */
474 InstallSourcePath
= &SystemRootPath
;
480 * Retrieve the different source path components
482 RtlCreateUnicodeString(SourcePath
, InstallSourcePath
->Buffer
);
484 /* Strip trailing directory */
485 Ptr
= wcsrchr(InstallSourcePath
->Buffer
, OBJ_NAME_PATH_SEPARATOR
);
488 RtlCreateUnicodeString(SourceRootDir
, Ptr
);
493 RtlCreateUnicodeString(SourceRootDir
, L
"");
496 RtlCreateUnicodeString(SourceRootPath
, InstallSourcePath
->Buffer
);
498 return STATUS_SUCCESS
;
504 IN OUT PUSETUP_DATA pSetupData
)
510 WCHAR FileNameBuffer
[MAX_PATH
];
512 CombinePaths(FileNameBuffer
, ARRAYSIZE(FileNameBuffer
), 2,
513 pSetupData
->SourcePath
.Buffer
, L
"txtsetup.sif");
515 DPRINT("SetupInf path: '%S'\n", FileNameBuffer
);
517 *SetupInf
= SetupOpenInfFileExW(FileNameBuffer
,
519 INF_STYLE_WIN4
| INF_STYLE_OLDNT
,
520 pSetupData
->LanguageId
,
523 if (*SetupInf
== INVALID_HANDLE_VALUE
)
524 return ERROR_LOAD_TXTSETUPSIF
;
526 /* Open 'Version' section */
527 if (!SetupFindFirstLineW(*SetupInf
, L
"Version", L
"Signature", &Context
))
528 return ERROR_CORRUPT_TXTSETUPSIF
;
530 /* Get pointer 'Signature' key */
531 if (!INF_GetData(&Context
, NULL
, &Value
))
532 return ERROR_CORRUPT_TXTSETUPSIF
;
534 /* Check 'Signature' string */
535 if (_wcsicmp(Value
, L
"$ReactOS$") != 0)
538 return ERROR_SIGNATURE_TXTSETUPSIF
;
543 /* Open 'DiskSpaceRequirements' section */
544 if (!SetupFindFirstLineW(*SetupInf
, L
"DiskSpaceRequirements", L
"FreeSysPartDiskSpace", &Context
))
545 return ERROR_CORRUPT_TXTSETUPSIF
;
547 pSetupData
->RequiredPartitionDiskSpace
= ~0;
549 /* Get the 'FreeSysPartDiskSpace' value */
550 if (!SetupGetIntField(&Context
, 1, &IntValue
))
551 return ERROR_CORRUPT_TXTSETUPSIF
;
553 pSetupData
->RequiredPartitionDiskSpace
= (ULONG
)IntValue
;
556 // TODO: Support "SetupSourceDevice" and "SetupSourcePath" in txtsetup.sif
560 /* Search for 'DefaultPath' in the 'SetupData' section */
561 if (SetupFindFirstLineW(*SetupInf
, L
"SetupData", L
"DefaultPath", &Context
))
563 /* Get pointer 'DefaultPath' key */
564 if (!INF_GetData(&Context
, NULL
, &Value
))
565 return ERROR_CORRUPT_TXTSETUPSIF
;
567 RtlStringCchCopyW(pSetupData
->InstallationDirectory
,
568 ARRAYSIZE(pSetupData
->InstallationDirectory
),
574 return ERROR_SUCCESS
;