Sync trunk head (r41026)
[reactos.git] / reactos / boot / freeldr / freeldr / windows / peloader.c
index 043cb69..bbd2770 100644 (file)
@@ -255,39 +255,37 @@ WinLdrLoadImage(IN PCHAR FileName,
                 TYPE_OF_MEMORY MemoryType,
                 OUT PVOID *ImageBasePA)
 {
-       PFILE FileHandle;
+       ULONG FileId;
        PVOID PhysicalBase;
        PVOID VirtualBase = NULL;
        UCHAR HeadersBuffer[SECTOR_SIZE * 2];
        PIMAGE_NT_HEADERS NtHeaders;
        PIMAGE_SECTION_HEADER SectionHeader;
        ULONG VirtualSize, SizeOfRawData, NumberOfSections;
-       BOOLEAN Status;
+       LONG Status;
+       LARGE_INTEGER Position;
        ULONG i, BytesRead;
 
        CHAR ProgressString[256];
 
        /* Inform user we are loading files */
-       sprintf(ProgressString, "Loading %s...", FileName);
+       sprintf(ProgressString, "Loading %s...", strchr(FileName, '\\') + 1);
        UiDrawProgressBarCenter(1, 100, ProgressString);
 
        /* Open the image file */
-       FileHandle = FsOpenFile(FileName);
-
-       if (FileHandle == NULL)
+       Status = ArcOpen(FileName, OpenReadOnly, &FileId);
+       if (Status != ESUCCESS)
        {
-               //Print(L"Can not open the file %s\n",FileName);
                UiMessageBox("Can not open the file");
                return FALSE;
        }
 
        /* Load the first 2 sectors of the image so we can read the PE header */
-       Status = FsReadFile(FileHandle, SECTOR_SIZE * 2, NULL, HeadersBuffer);
-       if (!Status)
+       Status = ArcRead(FileId, HeadersBuffer, SECTOR_SIZE * 2, &BytesRead);
+       if (Status != ESUCCESS)
        {
-               //Print(L"Error reading from file %s\n", FileName);
                UiMessageBox("Error reading from file");
-               FsCloseFile(FileHandle);
+               ArcClose(FileId);
                return FALSE;
        }
 
@@ -298,7 +296,7 @@ WinLdrLoadImage(IN PCHAR FileName,
        {
                //Print(L"Error - no NT header found in %s\n", FileName);
                UiMessageBox("Error - no NT header found");
-               FsCloseFile(FileHandle);
+               ArcClose(FileId);
                return FALSE;
        }
 
@@ -307,7 +305,7 @@ WinLdrLoadImage(IN PCHAR FileName,
        {
                //Print(L"Not an executable image %s\n", FileName);
                UiMessageBox("Not an executable image");
-               FsCloseFile(FileHandle);
+               ArcClose(FileId);
                return FALSE;
        }
 
@@ -329,7 +327,7 @@ WinLdrLoadImage(IN PCHAR FileName,
                {
                        //Print(L"Failed to alloc pages for image %s\n", FileName);
                        UiMessageBox("Failed to alloc pages for image");
-                       FsCloseFile(FileHandle);
+                       ArcClose(FileId);
                        return FALSE;
                }
        }
@@ -340,15 +338,22 @@ WinLdrLoadImage(IN PCHAR FileName,
        DPRINTM(DPRINT_PELOADER, "Base PA: 0x%X, VA: 0x%X\n", PhysicalBase, VirtualBase);
 
        /* Set to 0 position and fully load the file image */
-       FsSetFilePointer(FileHandle, 0);
+       Position.HighPart = Position.LowPart = 0;
+       Status = ArcSeek(FileId, &Position, SeekAbsolute);
+       if (Status != ESUCCESS)
+       {
+               UiMessageBox("Error seeking to start of file");
+               ArcClose(FileId);
+               return FALSE;
+       }
 
-       Status = FsReadFile(FileHandle, NtHeaders->OptionalHeader.SizeOfHeaders, NULL, PhysicalBase);
+       Status = ArcRead(FileId, PhysicalBase, NtHeaders->OptionalHeader.SizeOfHeaders, &BytesRead);
 
-       if (!Status)
+       if (Status != ESUCCESS)
        {
                //Print(L"Error reading headers %s\n", FileName);
                UiMessageBox("Error reading headers");
-               FsCloseFile(FileHandle);
+               ArcClose(FileId);
                return FALSE;
        }
 
@@ -388,14 +393,15 @@ WinLdrLoadImage(IN PCHAR FileName,
                if (SizeOfRawData != 0)
                {
                        /* Seek to the correct position */
-                       FsSetFilePointer(FileHandle, SectionHeader->PointerToRawData);
+                       Position.LowPart = SectionHeader->PointerToRawData;
+                       Status = ArcSeek(FileId, &Position, SeekAbsolute);
 
                        DPRINTM(DPRINT_PELOADER, "SH->VA: 0x%X\n", SectionHeader->VirtualAddress);
 
                        /* Read this section from the file, size = SizeOfRawData */
-                       Status = FsReadFile(FileHandle, SizeOfRawData, &BytesRead, (PUCHAR)PhysicalBase + SectionHeader->VirtualAddress);
+                       Status = ArcRead(FileId, (PUCHAR)PhysicalBase + SectionHeader->VirtualAddress, SizeOfRawData, &BytesRead);
 
-                       if (!Status && (BytesRead == 0))
+                       if (Status != ESUCCESS)
                        {
                                DPRINTM(DPRINT_PELOADER, "WinLdrLoadImage(): Error reading section from file!\n");
                                break;
@@ -413,10 +419,10 @@ WinLdrLoadImage(IN PCHAR FileName,
        }
 
        /* We are done with the file - close it */
-       FsCloseFile(FileHandle);
+       ArcClose(FileId);
 
        /* If loading failed - return right now */
-       if (!Status)
+       if (Status != ESUCCESS)
                return FALSE;
 
 
@@ -425,7 +431,7 @@ WinLdrLoadImage(IN PCHAR FileName,
        {
                DPRINTM(DPRINT_PELOADER, "Relocating %p -> %p\n",
                        NtHeaders->OptionalHeader.ImageBase, VirtualBase);
-               Status = (BOOLEAN)LdrRelocateImageWithBias(PhysicalBase,
+               return (BOOLEAN)LdrRelocateImageWithBias(PhysicalBase,
                        (ULONG_PTR)VirtualBase - (ULONG_PTR)PhysicalBase,
                        "FreeLdr",
                        TRUE,
@@ -433,7 +439,7 @@ WinLdrLoadImage(IN PCHAR FileName,
                        FALSE);
        }
 
-       return Status;
+       return TRUE;
 }
 
 /* PRIVATE FUNCTIONS *******************************************************/