From b39e6ee51c5b646ba78da1d357ed0ec0df755d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Mon, 22 Sep 2025 23:07:58 +0200 Subject: [PATCH] [FREELDR] Improve RamDisk loading loop (#8399) CORE-9023 Do the seek to the beginning only once before starting the loop, but avoid doing it for each chunk read: we base ourselves on the fact that read operations automatically advance the internal read pointer (per the specs). --- boot/freeldr/freeldr/disk/ramdisk.c | 53 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/boot/freeldr/freeldr/disk/ramdisk.c b/boot/freeldr/freeldr/disk/ramdisk.c index e080a7e4000..7ac7caf67d8 100644 --- a/boot/freeldr/freeldr/disk/ramdisk.c +++ b/boot/freeldr/freeldr/disk/ramdisk.c @@ -170,49 +170,46 @@ RamDiskLoadVirtualFile( } /* - * Read it in chunks + * Read it in chunks, starting at the beginning */ - Percent = 0; - for (TotalRead = 0; TotalRead < RamDiskFileSize; TotalRead += ChunkSize) + Position.QuadPart = 0; + Status = ArcSeek(RamFileId, &Position, SeekAbsolute); + if (Status != ESUCCESS) + goto ReadFailure; + + for (TotalRead = 0, Percent = 0; + TotalRead < RamDiskFileSize; + TotalRead += ChunkSize, Percent += PercentPerChunk) { - /* Check if we're at the last chunk */ + /* If we are at the last chunk, read only what's remaining */ if ((RamDiskFileSize - TotalRead) < ChunkSize) - { - /* Only need the actual data required */ ChunkSize = (ULONG)(RamDiskFileSize - TotalRead); - } /* Update progress */ UiUpdateProgressBar(Percent, NULL); - Percent += PercentPerChunk; - /* Copy the contents */ - Position.QuadPart = TotalRead; - Status = ArcSeek(RamFileId, &Position, SeekAbsolute); - if (Status == ESUCCESS) - { - Status = ArcRead(RamFileId, - (PVOID)((ULONG_PTR)RamDiskBase + (ULONG_PTR)TotalRead), - ChunkSize, - &Count); - } - - /* Check for success */ + /* Copy the data */ + Status = ArcRead(RamFileId, + (PVOID)((ULONG_PTR)RamDiskBase + (ULONG_PTR)TotalRead), + ChunkSize, + &Count); if ((Status != ESUCCESS) || (Count != ChunkSize)) { - MmFreeMemory(RamDiskBase); - RamDiskBase = NULL; - RamDiskFileSize = 0; - ArcClose(RamFileId); - UiMessageBox("Failed to read RAM disk."); - return ((Status != ESUCCESS) ? Status : EIO); + Status = ((Status != ESUCCESS) ? Status : EIO); + goto ReadFailure; } } UiUpdateProgressBar(100, NULL); - ArcClose(RamFileId); - return ESUCCESS; + +ReadFailure: + MmFreeMemory(RamDiskBase); + RamDiskBase = NULL; + RamDiskFileSize = 0; + ArcClose(RamFileId); + UiMessageBox("Failed to read RAM disk."); + return Status; } ARC_STATUS