https://git.reactos.org/?p=reactos.git;a=commitdiff;h=5904361a54d5594a902d81eb6f78469818d79435

commit 5904361a54d5594a902d81eb6f78469818d79435
Author:     Hervé Poussineau <[email protected]>
AuthorDate: Wed Jan 27 23:41:49 2021 +0100
Commit:     Hervé Poussineau <[email protected]>
CommitDate: Thu Jan 28 20:45:44 2021 +0100

    [FREELDR] In PE loader, use cache to not load twice the start of the file
    
    This is especially important in PXE boot, where a seek backwards requires 
reloading the file from start.
---
 boot/freeldr/freeldr/lib/peloader.c | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/boot/freeldr/freeldr/lib/peloader.c 
b/boot/freeldr/freeldr/lib/peloader.c
index 2c981cefe91..340eba1d2c0 100644
--- a/boot/freeldr/freeldr/lib/peloader.c
+++ b/boot/freeldr/freeldr/lib/peloader.c
@@ -781,24 +781,19 @@ PeLdrLoadImage(
 
     TRACE("Base PA: 0x%X, VA: 0x%X\n", PhysicalBase, VirtualBase);
 
-    /* Set to 0 position and fully load the file image */
-    Position.QuadPart = 0;
-    Status = ArcSeek(FileId, &Position, SeekAbsolute);
-    if (Status != ESUCCESS)
-    {
-        ERR("ArcSeek(File: '%s') failed. Status: 0x%lx\n", FileName, Status);
-        UiMessageBox("Error seeking the start of a file.");
-        ArcClose(FileId);
-        return FALSE;
-    }
-
-    Status = ArcRead(FileId, PhysicalBase, 
NtHeaders->OptionalHeader.SizeOfHeaders, &BytesRead);
-    if (Status != ESUCCESS)
+    /* Copy headers from already read data */
+    RtlCopyMemory(PhysicalBase, HeadersBuffer, 
min(NtHeaders->OptionalHeader.SizeOfHeaders, sizeof(HeadersBuffer)));
+    /* If headers are quite big, request next bytes from file */
+    if (NtHeaders->OptionalHeader.SizeOfHeaders > sizeof(HeadersBuffer))
     {
-        ERR("ArcRead(File: '%s') failed. Status: %u\n", FileName, Status);
-        UiMessageBox("Error reading headers.");
-        ArcClose(FileId);
-        return FALSE;
+        Status = ArcRead(FileId, (PUCHAR)PhysicalBase + sizeof(HeadersBuffer), 
NtHeaders->OptionalHeader.SizeOfHeaders - sizeof(HeadersBuffer), &BytesRead);
+        if (Status != ESUCCESS)
+        {
+            ERR("ArcRead(File: '%s') failed. Status: %u\n", FileName, Status);
+            UiMessageBox("Error reading headers.");
+            ArcClose(FileId);
+            return FALSE;
+        }
     }
 
     /*
@@ -843,7 +838,7 @@ PeLdrLoadImage(
         if (SizeOfRawData != 0)
         {
             /* Seek to the correct position */
-            Position.LowPart = SectionHeader->PointerToRawData;
+            Position.QuadPart = SectionHeader->PointerToRawData;
             Status = ArcSeek(FileId, &Position, SeekAbsolute);
 
             TRACE("SH->VA: 0x%X\n", SectionHeader->VirtualAddress);

Reply via email to