[FREELDR] Verbose error output for FS errors
[reactos.git] / boot / freeldr / freeldr / ntldr / wlregistry.c
index 40e297e..8c78e60 100644 (file)
@@ -9,6 +9,7 @@
 /* INCLUDES ***************************************************************/
 
 #include <freeldr.h>
+#include "winldr.h"
 #include "registry.h"
 
 #include <debug.h>
@@ -29,28 +30,30 @@ WinLdrScanRegistry(IN OUT PLIST_ENTRY BootDriverListHead,
 
 /* FUNCTIONS **************************************************************/
 
-BOOLEAN
-WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
-                     IN LPCSTR DirectoryPath,
-                     IN LPCSTR HiveName)
+static BOOLEAN
+WinLdrLoadSystemHive(
+    IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
+    IN PCSTR DirectoryPath,
+    IN PCSTR HiveName)
 {
     ULONG FileId;
-    CHAR FullHiveName[256];
+    CHAR FullHiveName[MAX_PATH];
     ARC_STATUS Status;
     FILEINFORMATION FileInfo;
     ULONG HiveFileSize;
     ULONG_PTR HiveDataPhysical;
     PVOID HiveDataVirtual;
     ULONG BytesRead;
-    LPCWSTR FsService;
+    PCWSTR FsService;
 
     /* Concatenate path and filename to get the full name */
     strcpy(FullHiveName, DirectoryPath);
     strcat(FullHiveName, HiveName);
-    //Print(L"Loading %s...\n", FullHiveName);
+
     Status = ArcOpen(FullHiveName, OpenReadOnly, &FileId);
     if (Status != ESUCCESS)
     {
+        WARN("Error while opening '%s', Status: %u\n", FullHiveName, Status);
         UiMessageBox("Opening hive file failed!");
         return FALSE;
     }
@@ -89,11 +92,12 @@ WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     if (Status != ESUCCESS)
     {
         ArcClose(FileId);
+        WARN("Error while reading '%s', Status: %u\n", FullHiveName, Status);
         UiMessageBox("Unable to read from hive file!");
         return FALSE;
     }
 
-    // Add boot filesystem driver to the list
+    /* Add boot filesystem driver to the list */
     FsService = FsGetServiceName(FileId);
     if (FsService)
     {
@@ -115,25 +119,40 @@ WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     return TRUE;
 }
 
-BOOLEAN WinLdrInitSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
-                             IN LPCSTR DirectoryPath)
+BOOLEAN
+WinLdrInitSystemHive(
+    IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
+    IN PCSTR SystemRoot,
+    IN BOOLEAN Setup)
 {
     CHAR SearchPath[1024];
+    PCSTR HiveName;
     BOOLEAN Success;
 
-    // There is a simple logic here: try to load usual hive (system), if it
-    // fails, then give system.alt a try, and finally try a system.sav
+    if (Setup)
+    {
+        strcpy(SearchPath, SystemRoot);
+        HiveName = "SETUPREG.HIV";
+    }
+    else
+    {
+        // There is a simple logic here: try to load usual hive (system), if it
+        // fails, then give system.alt a try, and finally try a system.sav
+
+        // FIXME: For now we only try system
+        strcpy(SearchPath, SystemRoot);
+        strcat(SearchPath, "system32\\config\\");
+        HiveName = "SYSTEM";
+    }
 
-    // FIXME: For now we only try system
-    strcpy(SearchPath, DirectoryPath);
-    strcat(SearchPath, "system32\\config\\");
-    Success = WinLdrLoadSystemHive(LoaderBlock, SearchPath, "SYSTEM");
+    TRACE("WinLdrInitSystemHive: loading hive %s%s\n", SearchPath, HiveName);
+    Success = WinLdrLoadSystemHive(LoaderBlock, SearchPath, HiveName);
 
-    // Fail if failed...
+    /* Fail if failed... */
     if (!Success)
         return FALSE;
 
-    // Import what was loaded
+    /* Import what was loaded */
     Success = RegImportBinaryHive(VaToPa(LoaderBlock->RegistryBase), LoaderBlock->RegistryLength);
     if (!Success)
     {
@@ -141,7 +160,7 @@ BOOLEAN WinLdrInitSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
         return FALSE;
     }
 
-    // Initialize the 'CurrentControlSet' link
+    /* Initialize the 'CurrentControlSet' link */
     if (RegInitCurrentControlSet(FALSE) != ERROR_SUCCESS)
     {
         UiMessageBox("Initializing CurrentControlSet link failed!");
@@ -158,10 +177,10 @@ BOOLEAN WinLdrScanSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     CHAR AnsiName[256], OemName[256], LangName[256];
     BOOLEAN Success;
 
-    // Scan registry and prepare boot drivers list
+    /* Scan registry and prepare boot drivers list */
     WinLdrScanRegistry(&LoaderBlock->BootDriverListHead, DirectoryPath);
 
-    // Get names of NLS files
+    /* Get names of NLS files */
     Success = WinLdrGetNLSNames(AnsiName, OemName, LangName);
     if (!Success)
     {
@@ -171,13 +190,15 @@ BOOLEAN WinLdrScanSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
 
     TRACE("NLS data %s %s %s\n", AnsiName, OemName, LangName);
 
-    // Load NLS data
+    /* Load NLS data */
     strcpy(SearchPath, DirectoryPath);
     strcat(SearchPath, "system32\\");
     Success = WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
     TRACE("NLS data loading %s\n", Success ? "successful" : "failed");
 
     /* TODO: Load OEM HAL font */
+    // In HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage,
+    // REG_SZ value "OEMHAL"
 
     return TRUE;
 }
@@ -207,7 +228,7 @@ WinLdrGetNLSNames(LPSTR AnsiName,
         return FALSE;
     }
 
-    /* get ANSI codepage */
+    /* Get ANSI codepage */
     BufferSize = sizeof(szIdBuffer);
     rc = RegQueryValue(hKey, L"ACP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
     if (rc != ERROR_SUCCESS)
@@ -226,7 +247,7 @@ WinLdrGetNLSNames(LPSTR AnsiName,
     }
     sprintf(AnsiName, "%S", NameBuffer);
 
-    /* get OEM codepage */
+    /* Get OEM codepage */
     BufferSize = sizeof(szIdBuffer);
     rc = RegQueryValue(hKey, L"OEMCP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
     if (rc != ERROR_SUCCESS)
@@ -245,7 +266,7 @@ WinLdrGetNLSNames(LPSTR AnsiName,
     }
     sprintf(OemName, "%S", NameBuffer);
 
-    /* open the language key */
+    /* Open the language key */
     rc = RegOpenKey(NULL,
         L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language",
         &hKey);
@@ -255,7 +276,7 @@ WinLdrGetNLSNames(LPSTR AnsiName,
         return FALSE;
     }
 
-    /* get the Unicode case table */
+    /* Get the Unicode case table */
     BufferSize = sizeof(szIdBuffer);
     rc = RegQueryValue(hKey, L"Default", NULL, (PUCHAR)szIdBuffer, &BufferSize);
     if (rc != ERROR_SUCCESS)
@@ -301,12 +322,14 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
         AnsiEqualsOem = TRUE;
 
     /* Open file with ANSI and store its size */
-    //Print(L"Loading %s...\n", Filename);
     strcpy(FileName, DirectoryPath);
     strcat(FileName, AnsiFileName);
     Status = ArcOpen(FileName, OpenReadOnly, &AnsiFileId);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while opening '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     Status = ArcGetFileInformation(AnsiFileId, &FileInfo);
     if (Status != ESUCCESS)
@@ -327,7 +350,10 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
         strcat(FileName, OemFileName);
         Status = ArcOpen(FileName, OpenReadOnly, &OemFileId);
         if (Status != ESUCCESS)
+        {
+            WARN("Error while opening '%s', Status: %u\n", FileName, Status);
             goto Failure;
+        }
 
         Status = ArcGetFileInformation(OemFileId, &FileInfo);
         if (Status != ESUCCESS)
@@ -343,7 +369,10 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     strcat(FileName, LanguageFileName);
     Status = ArcOpen(FileName, OpenReadOnly, &LanguageFileId);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while opening '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     Status = ArcGetFileInformation(LanguageFileId, &FileInfo);
     if (Status != ESUCCESS)
@@ -384,11 +413,17 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     strcat(FileName, AnsiFileName);
     Status = ArcOpen(FileName, OpenReadOnly, &AnsiFileId);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while opening '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     Status = ArcRead(AnsiFileId, VaToPa(LoaderBlock->NlsData->AnsiCodePageData), AnsiFileSize, &BytesRead);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while reading '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     ArcClose(AnsiFileId);
 
@@ -399,11 +434,17 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
         strcat(FileName, OemFileName);
         Status = ArcOpen(FileName, OpenReadOnly, &OemFileId);
         if (Status != ESUCCESS)
+        {
+            WARN("Error while opening '%s', Status: %u\n", FileName, Status);
             goto Failure;
+        }
 
         Status = ArcRead(OemFileId, VaToPa(LoaderBlock->NlsData->OemCodePageData), OemFileSize, &BytesRead);
         if (Status != ESUCCESS)
+        {
+            WARN("Error while reading '%s', Status: %u\n", FileName, Status);
             goto Failure;
+        }
 
         ArcClose(OemFileId);
     }
@@ -413,11 +454,17 @@ WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
     strcat(FileName, LanguageFileName);
     Status = ArcOpen(FileName, OpenReadOnly, &LanguageFileId);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while opening '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     Status = ArcRead(LanguageFileId, VaToPa(LoaderBlock->NlsData->UnicodeCodePageData), LanguageFileSize, &BytesRead);
     if (Status != ESUCCESS)
+    {
+        WARN("Error while reading '%s', Status: %u\n", FileName, Status);
         goto Failure;
+    }
 
     ArcClose(LanguageFileId);
 
@@ -567,15 +614,15 @@ WinLdrScanRegistry(IN OUT PLIST_ENTRY BootDriverListHead,
                     {
                         TRACE_CH(REACTOS, "ImagePath: not found\n");
                         TempImagePath[0] = 0;
-                        sprintf(ImagePath, "%s\\system32\\drivers\\%S.sys", DirectoryPath, ServiceName);
+                        RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%s\\system32\\drivers\\%S.sys", DirectoryPath, ServiceName);
                     }
                     else if (TempImagePath[0] != L'\\')
                     {
-                        sprintf(ImagePath, "%s%S", DirectoryPath, TempImagePath);
+                        RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%s%S", DirectoryPath, TempImagePath);
                     }
                     else
                     {
-                        sprintf(ImagePath, "%S", TempImagePath);
+                        RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%S", TempImagePath);
                         TRACE_CH(REACTOS, "ImagePath: '%s'\n", ImagePath);
                     }
 
@@ -647,15 +694,15 @@ WinLdrScanRegistry(IN OUT PLIST_ENTRY BootDriverListHead,
                 {
                     TRACE_CH(REACTOS, "ImagePath: not found\n");
                     TempImagePath[0] = 0;
-                    sprintf(ImagePath, "%ssystem32\\drivers\\%S.sys", DirectoryPath, ServiceName);
+                    RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%ssystem32\\drivers\\%S.sys", DirectoryPath, ServiceName);
                 }
                 else if (TempImagePath[0] != L'\\')
                 {
-                    sprintf(ImagePath, "%s%S", DirectoryPath, TempImagePath);
+                    RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%s%S", DirectoryPath, TempImagePath);
                 }
                 else
                 {
-                    sprintf(ImagePath, "%S", TempImagePath);
+                    RtlStringCbPrintfA(ImagePath, sizeof(ImagePath), "%S", TempImagePath);
                     TRACE_CH(REACTOS, "ImagePath: '%s'\n", ImagePath);
                 }
                 TRACE("  Adding boot driver: '%s'\n", ImagePath);
@@ -822,7 +869,7 @@ WinLdrAddDriverToList(LIST_ENTRY *BootDriverListHead,
     if (!NT_SUCCESS(Status))
         return FALSE;
 
-    // Insert entry into the list 
+    // Insert entry into the list
     if (!InsertInBootDriverList(BootDriverListHead, BootDriverEntry))
     {
         // It was already there, so delete our entry