[FREELDR] Diverse enhancements.
[reactos.git] / boot / freeldr / freeldr / disk / ramdisk.c
index 19eaa78..c9a0963 100644 (file)
@@ -10,7 +10,7 @@
 /* INCLUDES *******************************************************************/
 
 #include <freeldr.h>
-#define NDEBUG
+
 #include <debug.h>
 
 /* GLOBALS ********************************************************************/
@@ -21,7 +21,7 @@ ULONG gRamDiskOffset;
 
 /* FUNCTIONS ******************************************************************/
 
-static LONG RamDiskClose(ULONG FileId)
+static ARC_STATUS RamDiskClose(ULONG FileId)
 {
     //
     // Nothing to do
@@ -29,19 +29,19 @@ static LONG RamDiskClose(ULONG FileId)
     return ESUCCESS;
 }
 
-static LONG RamDiskGetFileInformation(ULONG FileId, FILEINFORMATION* Information)
+static ARC_STATUS RamDiskGetFileInformation(ULONG FileId, FILEINFORMATION* Information)
 {
     //
     // Give current seek offset and ram disk size to caller
     //
-    RtlZeroMemory(Information, sizeof(FILEINFORMATION));
+    RtlZeroMemory(Information, sizeof(*Information));
     Information->EndingAddress.LowPart = gRamDiskSize;
     Information->CurrentAddress.LowPart = gRamDiskOffset;
 
     return ESUCCESS;
 }
 
-static LONG RamDiskOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
+static ARC_STATUS RamDiskOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
 {
     //
     // Always return success, as contents are already in memory
@@ -49,7 +49,7 @@ static LONG RamDiskOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
     return ESUCCESS;
 }
 
-static LONG RamDiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
+static ARC_STATUS RamDiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
 {
     PVOID StartAddress;
 
@@ -76,7 +76,7 @@ static LONG RamDiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
     return ESUCCESS;
 }
 
-static LONG RamDiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
+static ARC_STATUS RamDiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
 {
     //
     // Only accept absolute mode now
@@ -112,123 +112,126 @@ VOID
 NTAPI
 RamDiskInitialize(VOID)
 {
-    /* Setup the RAMDISK device */
+    /* Register the RAMDISK device */
     FsRegisterDevice("ramdisk(0)", &RamDiskVtbl);
 }
 
-VOID
+BOOLEAN
 NTAPI
 RamDiskLoadVirtualFile(IN PCHAR FileName)
 {
-    ULONG RamFile;
+    ULONG RamFileId;
     ULONG TotalRead, ChunkSize, Count;
-    PCHAR MsgBuffer = "Loading ramdisk...";
+    PCHAR MsgBuffer = "Loading RamDisk...";
     ULONG PercentPerChunk, Percent;
     FILEINFORMATION Information;
     LARGE_INTEGER Position;
-    LONG ret;
+    ARC_STATUS Status;
 
     //
     // Display progress
     //
+    UiDrawBackdrop();
     UiDrawProgressBarCenter(1, 100, MsgBuffer);
 
     //
     // Try opening the ramdisk file
     //
-    ret = ArcOpen(FileName, OpenReadOnly, &RamFile);
-    if (ret == ESUCCESS)
+    RamFileId = FsOpenFile(FileName);
+    if (!RamFileId)
+        return FALSE;
+
+    //
+    // Get the file size
+    //
+    Status = ArcGetFileInformation(RamFileId, &Information);
+    if (Status != ESUCCESS)
+    {
+        ArcClose(RamFileId);
+        return FALSE;
+    }
+
+    //
+    // For now, limit RAM disks to 4GB
+    //
+    if (Information.EndingAddress.HighPart != 0)
+    {
+        UiMessageBox("RAM disk too big.");
+        ArcClose(RamFileId);
+        return FALSE;
+    }
+    gRamDiskSize = Information.EndingAddress.LowPart;
+
+    //
+    // Allocate memory for it
+    //
+    ChunkSize = 8 * 1024 * 1024;
+    if (gRamDiskSize < ChunkSize)
+        Percent = PercentPerChunk = 0;
+    else
+        Percent = PercentPerChunk = 100 / (gRamDiskSize / ChunkSize);
+    gRamDiskBase = MmAllocateMemoryWithType(gRamDiskSize, LoaderXIPRom);
+    if (!gRamDiskBase)
+    {
+        UiMessageBox("Failed to allocate memory for RAM disk.");
+        ArcClose(RamFileId);
+        return FALSE;
+    }
+
+    //
+    // Read it in chunks
+    //
+    for (TotalRead = 0; TotalRead < gRamDiskSize; TotalRead += ChunkSize)
     {
         //
-        // Get the file size
+        // Check if we're at the last chunk
         //
-        ret = ArcGetFileInformation(RamFile, &Information);
-        if (ret != ESUCCESS)
+        if ((gRamDiskSize - TotalRead) < ChunkSize)
         {
-            ArcClose(RamFile);
-            return;
+            //
+            // Only need the actual data required
+            //
+            ChunkSize = gRamDiskSize - TotalRead;
         }
 
         //
-        // For now, limit RAM disks to 4GB
+        // Draw progress
         //
-        if (Information.EndingAddress.HighPart != 0)
-        {
-            UiMessageBox("RAM disk too big\n");
-            ArcClose(RamFile);
-            return;
-        }
-        gRamDiskSize = Information.EndingAddress.LowPart;
+        UiDrawProgressBarCenter(Percent, 100, MsgBuffer);
+        Percent += PercentPerChunk;
 
         //
-        // Allocate memory for it
+        // Copy the contents
         //
-        ChunkSize = 8 * 1024 * 1024;
-        if (gRamDiskSize < ChunkSize)
-            Percent = PercentPerChunk = 0;
-        else
-            Percent = PercentPerChunk = 100 / (gRamDiskSize / ChunkSize);
-        gRamDiskBase = MmAllocateMemoryWithType(gRamDiskSize, LoaderXIPRom);
-        if (!gRamDiskBase)
+        Position.HighPart = 0;
+        Position.LowPart = TotalRead;
+        Status = ArcSeek(RamFileId, &Position, SeekAbsolute);
+        if (Status == ESUCCESS)
         {
-            UiMessageBox("Failed to allocate memory for RAM disk\n");
-            ArcClose(RamFile);
-            return;
+            Status = ArcRead(RamFileId,
+                             (PVOID)((ULONG_PTR)gRamDiskBase + TotalRead),
+                             ChunkSize,
+                             &Count);
         }
 
         //
-        // Read it in chunks
+        // Check for success
         //
-        for (TotalRead = 0; TotalRead < gRamDiskSize; TotalRead += ChunkSize)
+        if (Status != ESUCCESS || Count != ChunkSize)
         {
-            //
-            // Check if we're at the last chunk
-            //
-            if ((gRamDiskSize - TotalRead) < ChunkSize)
-            {
-                //
-                // Only need the actual data required
-                //
-                ChunkSize = gRamDiskSize - TotalRead;
-            }
-
-            //
-            // Draw progress
-            //
-            UiDrawProgressBarCenter(Percent, 100, MsgBuffer);
-            Percent += PercentPerChunk;
-
-            //
-            // Copy the contents
-            //
-            Position.HighPart = 0;
-            Position.LowPart = TotalRead;
-            ret = ArcSeek(RamFile, &Position, SeekAbsolute);
-            if (ret == ESUCCESS)
-            {
-                ret = ArcRead(RamFile,
-                              (PVOID)((ULONG_PTR)gRamDiskBase + TotalRead),
-                              ChunkSize,
-                              &Count);
-            }
-
-            //
-            // Check for success
-            //
-            if (ret != ESUCCESS || Count != ChunkSize)
-            {
-                MmFreeMemory(gRamDiskBase);
-                gRamDiskBase = NULL;
-                gRamDiskSize = 0;
-                ArcClose(RamFile);
-                UiMessageBox("Failed to read ramdisk\n");
-                return;
-            }
+            MmFreeMemory(gRamDiskBase);
+            gRamDiskBase = NULL;
+            gRamDiskSize = 0;
+            ArcClose(RamFileId);
+            UiMessageBox("Failed to read RAM disk.");
+            return FALSE;
         }
+    }
 
-        ArcClose(RamFile);
+    ArcClose(RamFileId);
 
-        // Register a new device for the ramdisk
-        FsRegisterDevice("ramdisk(0)", &RamDiskVtbl);
-    }
+    /* Setup the RAMDISK device */
+    RamDiskInitialize();
+
+    return TRUE;
 }