ReactOS  0.4.11-dev-195-gef016bf
bldrsup.c File Reference
#include "precomp.h"
#include "bldrsup.h"
#include "filesup.h"
#include "inicache.h"
#include <debug.h>
Include dependency graph for bldrsup.c:

Go to the source code of this file.

Classes

struct  _NTOS_BOOT_LOADER_FILES
 

Macros

#define NDEBUG
 

Typedefs

typedef struct
_NTOS_BOOT_LOADER_FILES 
NTOS_BOOT_LOADER_FILES
 
typedef struct
_NTOS_BOOT_LOADER_FILES
PNTOS_BOOT_LOADER_FILES
 

Functions

NTSTATUS FindNTOSBootLoader (IN HANDLE PartitionHandle, IN NTOS_BOOT_LOADER_TYPE Type, OUT PULONG Version OPTIONAL)
 
static NTSTATUS FreeLdrEnumerateBootEntries (IN PCHAR FileBuffer, IN ULONG FileLength, IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine, IN PVOID Parameter OPTIONAL)
 
static NTSTATUS NtLdrEnumerateBootEntries (IN PCHAR FileBuffer, IN ULONG FileLength, IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine, IN PVOID Parameter OPTIONAL)
 
NTSTATUS EnumerateNTOSBootEntries (IN HANDLE PartitionHandle, IN NTOS_BOOT_LOADER_TYPE Type, IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine, IN PVOID Parameter OPTIONAL)
 

Variables

NTOS_BOOT_LOADER_FILES NtosBootLoaders []
 

Macro Definition Documentation

#define NDEBUG

Definition at line 19 of file bldrsup.c.

Typedef Documentation

Function Documentation

NTSTATUS EnumerateNTOSBootEntries ( IN HANDLE  PartitionHandle,
IN NTOS_BOOT_LOADER_TYPE  Type,
IN PENUM_BOOT_ENTRIES_ROUTINE  EnumBootEntriesRoutine,
IN PVOID Parameter  OPTIONAL 
)

Definition at line 293 of file bldrsup.c.

Referenced by FindNTOSInstallations().

299 {
302  HANDLE SectionHandle;
303  // SIZE_T ViewSize;
304  ULONG FileSize;
305  PVOID ViewBase;
306 
307  /*
308  * NOTE: Currently we open & map the loader configuration file without
309  * further tests. It's OK as long as we only deal with FreeLdr's freeldr.ini
310  * and NTLDR's boot.ini files. But as soon as we'll implement support for
311  * BOOTMGR detection, the "configuration file" will be the BCD registry
312  * hive and then, we'll have instead to mount the hive & open it.
313  */
314 
315  /* Check whether the loader configuration file exists */
316  Status = OpenAndMapFile(PartitionHandle, NtosBootLoaders[Type].LoaderConfigurationFile,
317  &FileHandle, &SectionHandle, &ViewBase, &FileSize, FALSE);
318  if (!NT_SUCCESS(Status))
319  {
320  /* The loader does not exist, continue with another one */
321  // FIXME: Consider it might be optional??
322  // DPRINT1("Loader configuration file '%S' does not exist, continue with another one...\n", NtosBootLoaders[Type].LoaderConfigurationFile);
323  DPRINT1("Loader configuration file '%S' does not exist\n", NtosBootLoaders[Type].LoaderConfigurationFile);
324  return Status;
325  }
326 
327  /* The loader configuration file exists, interpret it to find valid installations */
328  switch (NtosBootLoaders[Type].Type)
329  {
330  case FreeLdr:
331  Status = FreeLdrEnumerateBootEntries(ViewBase, FileSize, /* Flags, */
332  EnumBootEntriesRoutine, Parameter);
333  break;
334 
335  case NtLdr:
336  Status = NtLdrEnumerateBootEntries(ViewBase, FileSize, /* Flags, */
337  EnumBootEntriesRoutine, Parameter);
338  break;
339 
340  default:
341  DPRINT1("Loader type %d is currently unsupported!\n", NtosBootLoaders[Type].Type);
342  Status = STATUS_SUCCESS;
343  }
344 
345  /* Finally, unmap and close the file */
346  UnMapFile(SectionHandle, ViewBase);
347  NtClose(FileHandle);
348 
349  return Status;
350 }
DWORD *typedef PVOID
Definition: winlogon.h:61
Type
Definition: Type.h:6
static SIZE_T FileSize
Definition: cabinet.c:52
_In_ PVOID Parameter
Definition: ldrtypes.h:239
Definition: bldrsup.h:16
#define FALSE
Definition: types.h:117
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
NTSTATUS OpenAndMapFile(IN HANDLE RootDirectory OPTIONAL, IN PCWSTR PathNameToFile, OUT PHANDLE FileHandle, OUT PHANDLE SectionHandle, OUT PVOID *BaseAddress, OUT PULONG FileSize OPTIONAL, IN BOOLEAN ReadWriteAccess)
Definition: filesup.c:307
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3393
Status
Definition: gdiplustypes.h:24
static HANDLE FileHandle
Definition: cabinet.c:48
DWORD *typedef HANDLE
Definition: winlogon.h:61
BOOLEAN UnMapFile(IN HANDLE SectionHandle, IN PVOID BaseAddress)
Definition: filesup.c:424
#define DPRINT1
Definition: precomp.h:8
static NTSTATUS NtLdrEnumerateBootEntries(IN PCHAR FileBuffer, IN ULONG FileLength, IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine, IN PVOID Parameter OPTIONAL)
Definition: bldrsup.c:215
NTOS_BOOT_LOADER_FILES NtosBootLoaders[]
Definition: bldrsup.c:35
unsigned int ULONG
Definition: retypes.h:1
static NTSTATUS FreeLdrEnumerateBootEntries(IN PCHAR FileBuffer, IN ULONG FileLength, IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine, IN PVOID Parameter OPTIONAL)
Definition: bldrsup.c:107
return STATUS_SUCCESS
Definition: btrfs.c:2710
NTSTATUS FindNTOSBootLoader ( IN HANDLE  PartitionHandle,
IN NTOS_BOOT_LOADER_TYPE  Type,
OUT PULONG Version  OPTIONAL 
)

Definition at line 55 of file bldrsup.c.

Referenced by FindNTOSInstallations().

60 {
61  // UINT i;
62 
63  if (Type >= BldrTypeMax)
65 
66  // FIXME: Unused for now, but should be used later!!
67  *Version = 0;
68  // TODO: Check for BLDR version ONLY if Version != NULL
69 
70  /* Check whether the loader executable exists */
71  if (!DoesFileExist(PartitionHandle, NtosBootLoaders[Type].LoaderExecutable))
72  {
73  /* The loader does not exist, continue with another one */
74  // DPRINT1("Loader executable '%S' does not exist, continue with another one...\n", NtosBootLoaders[Type].LoaderExecutable);
75  DPRINT1("Loader executable '%S' does not exist\n", NtosBootLoaders[Type].LoaderExecutable);
76  return STATUS_NOT_FOUND;
77  }
78 
79  /* Check whether the loader configuration file exists */
80  if (!DoesFileExist(PartitionHandle, NtosBootLoaders[Type].LoaderConfigurationFile))
81  {
82  /* The loader does not exist, continue with another one */
83  // FIXME: Consider it might be optional??
84  // DPRINT1("Loader configuration file '%S' does not exist, continue with another one...\n", NtosBootLoaders[Type].LoaderConfigurationFile);
85  DPRINT1("Loader configuration file '%S' does not exist\n", NtosBootLoaders[Type].LoaderConfigurationFile);
86  return STATUS_NOT_FOUND;
87  }
88 
89 #if 0
90  /* Check whether the loader configuration file exists */
91  Status = OpenAndMapFile(PartitionHandle, NtosBootLoaders[Type].LoaderConfigurationFile,
92  &FileHandle, &SectionHandle, &ViewBase, &FileSize, FALSE);
93  if (!NT_SUCCESS(Status))
94  {
95  /* The loader does not exist, continue with another one */
96  // FIXME: Consider it might be optional??
97  DPRINT1("Loader configuration file '%S' does not exist, continue with another one...\n", NtosBootLoaders[Type].LoaderConfigurationFile);
98  return STATUS_NOT_FOUND;
99  }
100 #endif
101 
102  return STATUS_SUCCESS;
103 }
IN PVOID IN PVOID IN USHORT Version
Definition: pci.h:359
Type
Definition: Type.h:6
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
static SIZE_T FileSize
Definition: cabinet.c:52
#define FALSE
Definition: types.h:117
#define STATUS_NOT_FOUND
Definition: shellext.h:55
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
NTSTATUS OpenAndMapFile(IN HANDLE RootDirectory OPTIONAL, IN PCWSTR PathNameToFile, OUT PHANDLE FileHandle, OUT PHANDLE SectionHandle, OUT PVOID *BaseAddress, OUT PULONG FileSize OPTIONAL, IN BOOLEAN ReadWriteAccess)
Definition: filesup.c:307
Status
Definition: gdiplustypes.h:24
static HANDLE FileHandle
Definition: cabinet.c:48
BOOLEAN DoesFileExist(IN HANDLE RootDirectory OPTIONAL, IN PCWSTR PathNameToFile)
Definition: filesup.c:163
#define DPRINT1
Definition: precomp.h:8
NTOS_BOOT_LOADER_FILES NtosBootLoaders[]
Definition: bldrsup.c:35
return STATUS_SUCCESS
Definition: btrfs.c:2710
static NTSTATUS FreeLdrEnumerateBootEntries ( IN PCHAR  FileBuffer,
IN ULONG  FileLength,
IN PENUM_BOOT_ENTRIES_ROUTINE  EnumBootEntriesRoutine,
IN PVOID Parameter  OPTIONAL 
)
static

Definition at line 107 of file bldrsup.c.

Referenced by EnumerateNTOSBootEntries().

113 {
115  PINICACHE IniCache;
116  PINICACHEITERATOR Iterator;
117  PINICACHESECTION IniSection, OsIniSection;
118  PWCHAR SectionName, KeyData;
119 NTOS_BOOT_ENTRY xxBootEntry;
120  PNTOS_BOOT_ENTRY BootEntry = &xxBootEntry;
121  UNICODE_STRING InstallName;
122 
123  /* Open an *existing* FreeLdr.ini configuration file */
124  Status = IniCacheLoadFromMemory(&IniCache, FileBuffer, FileLength, FALSE);
125  if (!NT_SUCCESS(Status))
126  return Status;
127 
128  /* Get the "Operating Systems" section */
129  IniSection = IniCacheGetSection(IniCache, L"Operating Systems");
130  if (IniSection == NULL)
131  {
132  IniCacheDestroy(IniCache);
133  return STATUS_UNSUCCESSFUL;
134  }
135 
136  /* Enumerate all the valid installations */
137  Iterator = IniCacheFindFirstValue(IniSection, &SectionName, &KeyData);
138  if (!Iterator) goto Quit;
139  do
140  {
141  // FIXME: Poor-man quotes removal (improvement over bootsup.c:UpdateFreeLoaderIni).
142  if (KeyData[0] == L'"')
143  {
144  /* Quoted name, copy up to the closing quote */
145  PWCHAR Begin = &KeyData[1];
146  PWCHAR End = wcschr(Begin, L'"');
147  if (!End)
148  End = Begin + wcslen(Begin);
149  RtlInitEmptyUnicodeString(&InstallName, Begin, (ULONG_PTR)End - (ULONG_PTR)Begin);
150  InstallName.Length = InstallName.MaximumLength;
151  }
152  else
153  {
154  /* Non-quoted name, copy everything */
155  RtlInitUnicodeString(&InstallName, KeyData);
156  }
157 
158  DPRINT1("Boot entry '%wZ' in OS section '%S'\n", &InstallName, SectionName);
159 
160  /* Search for an existing ReactOS entry */
161  OsIniSection = IniCacheGetSection(IniCache, SectionName);
162  if (!OsIniSection)
163  continue;
164 
165  /* Check for supported boot type "Windows2003" */
166  Status = IniCacheGetKey(OsIniSection, L"BootType", &KeyData);
167  if (NT_SUCCESS(Status))
168  {
169  // TODO: What to do with "Windows" ; "WindowsNT40" ; "ReactOSSetup" ?
170  if ((KeyData == NULL) ||
171  ( (_wcsicmp(KeyData, L"Windows2003") != 0) &&
172  (_wcsicmp(KeyData, L"\"Windows2003\"") != 0) ))
173  {
174  /* This is not a ReactOS entry */
175  continue;
176  }
177  }
178  else
179  {
180  /* Certainly not a ReactOS installation */
181  continue;
182  }
183 
184  /* BootType is Windows2003. Now check its SystemPath. */
185  Status = IniCacheGetKey(OsIniSection, L"SystemPath", &KeyData);
186  if (!NT_SUCCESS(Status))
187  {
188  DPRINT1(" A Win2k3 install '%wZ' without an ARC path?!\n", &InstallName);
189  continue;
190  }
191 
192  DPRINT1(" Found a candidate Win2k3 install '%wZ' with ARC path '%S'\n", &InstallName, KeyData);
193  // KeyData == SystemRoot;
194 
195  BootEntry->FriendlyName = &InstallName;
196  BootEntry->OsLoadPath = KeyData;
197  /* Unused stuff (for now...) */
198  BootEntry->BootFilePath = NULL;
199  BootEntry->OsOptions = NULL;
200  BootEntry->OsLoadOptions = NULL;
201 
202  Status = EnumBootEntriesRoutine(FreeLdr, BootEntry, Parameter);
203  // TODO: Stop enumeration if !NT_SUCCESS(Status);
204  }
205  while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
206 
207  IniCacheFindClose(Iterator);
208 
209 Quit:
210  IniCacheDestroy(IniCache);
211  return STATUS_SUCCESS;
212 }
PCWSTR OsLoadPath
Definition: bldrsup.h:33
_Out_ PNDIS_HANDLE _Out_ PUINT FileLength
Definition: ndis.h:3209
USHORT MaximumLength
Definition: env_spec_w32.h:370
VOID IniCacheDestroy(PINICACHE Cache)
Definition: inicache.c:666
NTSTATUS IniCacheLoadFromMemory(PINICACHE *Cache, PCHAR FileBuffer, ULONG FileLength, BOOLEAN String)
Definition: inicache.c:452
PINICACHESECTION IniCacheGetSection(PINICACHE Cache, PWCHAR Name)
Definition: inicache.c:683
uint16_t * PWCHAR
Definition: typedefs.h:54
_In_ PVOID Parameter
Definition: ldrtypes.h:239
BOOLEAN IniCacheFindNextValue(PINICACHEITERATOR Iterator, PWCHAR *KeyName, PWCHAR *KeyData)
Definition: inicache.c:785
static PUCHAR FileBuffer
Definition: cabinet.c:50
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS IniCacheGetKey(PINICACHESECTION Section, PWCHAR KeyName, PWCHAR *KeyData)
Definition: inicache.c:716
LONG NTSTATUS
Definition: precomp.h:26
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
VOID IniCacheFindClose(PINICACHEITERATOR Iterator)
Definition: inicache.c:815
static const WCHAR L[]
Definition: oid.c:1087
PUNICODE_STRING FriendlyName
Definition: bldrsup.h:31
PCWSTR OsOptions
Definition: bldrsup.h:34
Status
Definition: gdiplustypes.h:24
Definition: bldrsup.h:25
#define DPRINT1
Definition: precomp.h:8
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
return STATUS_SUCCESS
Definition: btrfs.c:2710
PCWSTR OsLoadOptions
Definition: bldrsup.h:35
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
PCWSTR BootFilePath
Definition: bldrsup.h:32
PINICACHEITERATOR IniCacheFindFirstValue(PINICACHESECTION Section, PWCHAR *KeyName, PWCHAR *KeyData)
Definition: inicache.c:744
static NTSTATUS NtLdrEnumerateBootEntries ( IN PCHAR  FileBuffer,
IN ULONG  FileLength,
IN PENUM_BOOT_ENTRIES_ROUTINE  EnumBootEntriesRoutine,
IN PVOID Parameter  OPTIONAL 
)
static

Definition at line 215 of file bldrsup.c.

Referenced by EnumerateNTOSBootEntries().

221 {
223  PINICACHE IniCache;
224  PINICACHEITERATOR Iterator;
225  PINICACHESECTION IniSection;
226  PWCHAR SectionName, KeyData;
227 NTOS_BOOT_ENTRY xxBootEntry;
228  PNTOS_BOOT_ENTRY BootEntry = &xxBootEntry;
229  UNICODE_STRING InstallName;
230 
231  /* Open an *existing* boot.ini configuration file */
232  Status = IniCacheLoadFromMemory(&IniCache, FileBuffer, FileLength, FALSE);
233  if (!NT_SUCCESS(Status))
234  return Status;
235 
236  /* Get the "Operating Systems" section */
237  IniSection = IniCacheGetSection(IniCache, L"operating systems");
238  if (IniSection == NULL)
239  {
240  IniCacheDestroy(IniCache);
241  return STATUS_UNSUCCESSFUL;
242  }
243 
244  /* Enumerate all the valid installations */
245  Iterator = IniCacheFindFirstValue(IniSection, &SectionName, &KeyData);
246  if (!Iterator) goto Quit;
247  do
248  {
249  // FIXME: Poor-man quotes removal (improvement over bootsup.c:UpdateFreeLoaderIni).
250  if (KeyData[0] == L'"')
251  {
252  /* Quoted name, copy up to the closing quote */
253  PWCHAR Begin = &KeyData[1];
254  PWCHAR End = wcschr(Begin, L'"');
255  if (!End)
256  End = Begin + wcslen(Begin);
257  RtlInitEmptyUnicodeString(&InstallName, Begin, (ULONG_PTR)End - (ULONG_PTR)Begin);
258  InstallName.Length = InstallName.MaximumLength;
259  }
260  else
261  {
262  /* Non-quoted name, copy everything */
263  RtlInitUnicodeString(&InstallName, KeyData);
264  }
265 
266  DPRINT1("Boot entry '%wZ' in OS section '%S'\n", &InstallName, SectionName);
267 
268  DPRINT1(" Found a Win2k3 install '%wZ' with ARC path '%S'\n", &InstallName, SectionName);
269  // SectionName == SystemRoot;
270 
271  BootEntry->FriendlyName = &InstallName;
272  BootEntry->OsLoadPath = SectionName;
273  /* Unused stuff (for now...) */
274  BootEntry->BootFilePath = NULL;
275  BootEntry->OsOptions = NULL;
276  BootEntry->OsLoadOptions = NULL;
277 
278  Status = EnumBootEntriesRoutine(NtLdr, BootEntry, Parameter);
279  // TODO: Stop enumeration if !NT_SUCCESS(Status);
280  }
281  while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
282 
283  IniCacheFindClose(Iterator);
284 
285 Quit:
286  IniCacheDestroy(IniCache);
287  return STATUS_SUCCESS;
288 }
PCWSTR OsLoadPath
Definition: bldrsup.h:33
_Out_ PNDIS_HANDLE _Out_ PUINT FileLength
Definition: ndis.h:3209
USHORT MaximumLength
Definition: env_spec_w32.h:370
VOID IniCacheDestroy(PINICACHE Cache)
Definition: inicache.c:666
NTSTATUS IniCacheLoadFromMemory(PINICACHE *Cache, PCHAR FileBuffer, ULONG FileLength, BOOLEAN String)
Definition: inicache.c:452
PINICACHESECTION IniCacheGetSection(PINICACHE Cache, PWCHAR Name)
Definition: inicache.c:683
uint16_t * PWCHAR
Definition: typedefs.h:54
_In_ PVOID Parameter
Definition: ldrtypes.h:239
BOOLEAN IniCacheFindNextValue(PINICACHEITERATOR Iterator, PWCHAR *KeyName, PWCHAR *KeyData)
Definition: inicache.c:785
static PUCHAR FileBuffer
Definition: cabinet.c:50
Definition: bldrsup.h:16
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
LONG NTSTATUS
Definition: precomp.h:26
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
VOID IniCacheFindClose(PINICACHEITERATOR Iterator)
Definition: inicache.c:815
static const WCHAR L[]
Definition: oid.c:1087
PUNICODE_STRING FriendlyName
Definition: bldrsup.h:31
PCWSTR OsOptions
Definition: bldrsup.h:34
Status
Definition: gdiplustypes.h:24
Definition: bldrsup.h:25
#define DPRINT1
Definition: precomp.h:8
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
return STATUS_SUCCESS
Definition: btrfs.c:2710
PCWSTR OsLoadOptions
Definition: bldrsup.h:35
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PCWSTR BootFilePath
Definition: bldrsup.h:32
PINICACHEITERATOR IniCacheFindFirstValue(PINICACHESECTION Section, PWCHAR *KeyName, PWCHAR *KeyData)
Definition: inicache.c:744

Variable Documentation

NTOS_BOOT_LOADER_FILES NtosBootLoaders[]
Initial value:
=
{
{FreeLdr, L"freeldr.sys", L"freeldr.ini"},
{NtLdr , L"ntldr" , L"boot.ini"},
}
Definition: bldrsup.h:16
static const WCHAR L[]
Definition: oid.c:1087

Definition at line 35 of file bldrsup.c.