c14fd64bdf29bad116704fac77da1a2ca58e8824
[reactos.git] / reactos / boot / freeldr / freeldr / windows / wlregistry.c
1 /*
2 * PROJECT: EFI Windows Loader
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: freeldr/winldr/wlregistry.c
5 * PURPOSE: Registry support functions
6 * PROGRAMMERS: Aleksey Bragin (aleksey@reactos.org)
7 */
8
9 /* INCLUDES ***************************************************************/
10
11 #include <freeldr.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* FUNCTIONS **************************************************************/
17
18 BOOLEAN
19 WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
20 IN LPCSTR DirectoryPath,
21 IN LPCSTR HiveName)
22 {
23 PFILE FileHandle;
24 CHAR FullHiveName[256];
25 BOOLEAN Status;
26 ULONG HiveFileSize;
27 ULONG_PTR HiveDataPhysical;
28 PVOID HiveDataVirtual;
29
30 /* Concatenate path and filename to get the full name */
31 strcpy(FullHiveName, DirectoryPath);
32 strcat(FullHiveName, HiveName);
33 //Print(L"Loading %s...\n", FullHiveName);
34 FileHandle = FsOpenFile(FullHiveName);
35
36 if (FileHandle == NULL)
37 {
38 UiMessageBox("Opening hive file failed!");
39 return FALSE;
40 }
41
42 /* Get the file length */
43 HiveFileSize = FsGetFileSize(FileHandle);
44
45 if (HiveFileSize == 0)
46 {
47 FsCloseFile(FileHandle);
48 UiMessageBox("Hive file has 0 size!");
49 return FALSE;
50 }
51
52 /* Round up the size to page boundary and alloc memory */
53 HiveDataPhysical = (ULONG_PTR)MmAllocateMemory(
54 MM_SIZE_TO_PAGES(HiveFileSize + MM_PAGE_SIZE - 1) << MM_PAGE_SHIFT);
55
56 if (HiveDataPhysical == 0)
57 {
58 FsCloseFile(FileHandle);
59 UiMessageBox("Unable to alloc memory for a hive!");
60 return FALSE;
61 }
62
63 /* Convert address to virtual */
64 HiveDataVirtual = (PVOID)(KSEG0_BASE | HiveDataPhysical);
65
66 /* Fill LoaderBlock's entries */
67 LoaderBlock->RegistryLength = HiveFileSize;
68 LoaderBlock->RegistryBase = HiveDataVirtual;
69
70 /* Finally read from file to the memory */
71 Status = FsReadFile(FileHandle, HiveFileSize, NULL, (PVOID)HiveDataPhysical);
72 FsCloseFile(FileHandle);
73 if (!Status)
74 {
75 UiMessageBox("Unable to read from hive file!");
76 return FALSE;
77 }
78
79 return TRUE;
80 }
81
82
83 BOOLEAN WinLdrLoadAndScanSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
84 IN LPCSTR DirectoryPath)
85 {
86 CHAR SearchPath[1024];
87 BOOLEAN Status;
88
89 // There is a simple logic here: try to load usual hive (system), if it
90 // fails, then give system.alt a try, and finally try a system.sav
91
92 // FIXME: For now we only try system
93 strcpy(SearchPath, DirectoryPath);
94 strcat(SearchPath, "SYSTEM32\\CONFIG\\");
95 Status = WinLdrLoadSystemHive(LoaderBlock, SearchPath, "SYSTEM");
96
97 // Fail if failed...
98 if (!Status)
99 return FALSE;
100
101
102
103 return TRUE;
104 }