[FREELDR] Advance the file pointers every time a read operation is performed, in accordance with the ARC specification.

This commit is contained in:
Hermès Bélusca-Maïto 2019-10-09 02:26:29 +02:00
parent db15c921e8
commit 7909284220
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
7 changed files with 41 additions and 39 deletions

View file

@ -157,8 +157,8 @@ DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
DISKCONTEXT* Context = FsGetDeviceSpecific(FileId); DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
UCHAR* Ptr = (UCHAR*)Buffer; UCHAR* Ptr = (UCHAR*)Buffer;
ULONG Length, TotalSectors, MaxSectors, ReadSectors; ULONG Length, TotalSectors, MaxSectors, ReadSectors;
BOOLEAN ret;
ULONGLONG SectorOffset; ULONGLONG SectorOffset;
BOOLEAN ret;
ASSERT(DiskReadBufferSize > 0); ASSERT(DiskReadBufferSize > 0);
@ -197,7 +197,8 @@ DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
TotalSectors -= ReadSectors; TotalSectors -= ReadSectors;
} }
*Count = (ULONG)(Ptr - (UCHAR*)Buffer); *Count = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)Buffer);
Context->SectorNumber = SectorOffset - Context->SectorOffset;
return (!ret) ? EIO : ESUCCESS; return (!ret) ? EIO : ESUCCESS;
} }

View file

@ -318,6 +318,7 @@ static ARC_STATUS DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
Buffer = (PUCHAR)Buffer + FullSectors * Context->SectorSize; Buffer = (PUCHAR)Buffer + FullSectors * Context->SectorSize;
N -= FullSectors * Context->SectorSize; N -= FullSectors * Context->SectorSize;
*Count += FullSectors * Context->SectorSize; *Count += FullSectors * Context->SectorSize;
Context->SectorNumber += FullSectors;
Lba += FullSectors; Lba += FullSectors;
} }
@ -364,6 +365,7 @@ static ARC_STATUS DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
} }
RtlCopyMemory(Buffer, Sector, N); RtlCopyMemory(Buffer, Sector, N);
*Count += N; *Count += N;
/* Context->SectorNumber remains untouched (incomplete sector read) */
ExFreePool(Sector); ExFreePool(Sector);
} }
@ -399,7 +401,8 @@ static ARC_STATUS DiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekM
return ESUCCESS; return ESUCCESS;
} }
static const DEVVTBL DiskVtbl = { static const DEVVTBL DiskVtbl =
{
DiskClose, DiskClose,
DiskGetFileInformation, DiskGetFileInformation,
DiskOpen, DiskOpen,

View file

@ -1201,6 +1201,7 @@ ARC_STATUS BtrFsRead(ULONG FileId, VOID *Buffer, ULONG Size, ULONG *BytesRead)
return ENOENT; return ENOENT;
} }
phandle->position += rd;
*BytesRead = rd; *BytesRead = rd;
return ESUCCESS; return ESUCCESS;
} }

View file

@ -386,9 +386,8 @@ BOOLEAN Ext2ReadFileBig(PEXT2_FILE_INFO Ext2FileInfo, ULONGLONG BytesToRead, ULO
} }
// //
// If they are trying to read past the // If the user is trying to read past the end of
// end of the file then return success // the file then return success with BytesRead == 0.
// with BytesRead == 0
// //
if (Ext2FileInfo->FilePointer >= Ext2FileInfo->FileSize) if (Ext2FileInfo->FilePointer >= Ext2FileInfo->FileSize)
{ {
@ -396,8 +395,8 @@ BOOLEAN Ext2ReadFileBig(PEXT2_FILE_INFO Ext2FileInfo, ULONGLONG BytesToRead, ULO
} }
// //
// If they are trying to read more than there is to read // If the user is trying to read more than there is to read
// then adjust the amount to read // then adjust the amount to read.
// //
if ((Ext2FileInfo->FilePointer + BytesToRead) > Ext2FileInfo->FileSize) if ((Ext2FileInfo->FilePointer + BytesToRead) > Ext2FileInfo->FileSize)
{ {
@ -418,6 +417,7 @@ BOOLEAN Ext2ReadFileBig(PEXT2_FILE_INFO Ext2FileInfo, ULONGLONG BytesToRead, ULO
{ {
*BytesRead = BytesToRead; *BytesRead = BytesToRead;
} }
// Ext2FileInfo->FilePointer += BytesToRead;
return TRUE; return TRUE;
} }

View file

@ -1206,9 +1206,8 @@ BOOLEAN FatReadFile(PFAT_FILE_INFO FatFileInfo, ULONG BytesToRead, ULONG* BytesR
} }
// //
// If they are trying to read past the // If the user is trying to read past the end of
// end of the file then return success // the file then return success with BytesRead == 0.
// with BytesRead == 0
// //
if (FatFileInfo->FilePointer >= FatFileInfo->FileSize) if (FatFileInfo->FilePointer >= FatFileInfo->FileSize)
{ {
@ -1216,8 +1215,8 @@ BOOLEAN FatReadFile(PFAT_FILE_INFO FatFileInfo, ULONG BytesToRead, ULONG* BytesR
} }
// //
// If they are trying to read more than there is to read // If the user is trying to read more than there is to read
// then adjust the amount to read // then adjust the amount to read.
// //
if ((FatFileInfo->FilePointer + BytesToRead) > FatFileInfo->FileSize) if ((FatFileInfo->FilePointer + BytesToRead) > FatFileInfo->FileSize)
{ {
@ -1489,7 +1488,7 @@ ARC_STATUS FatSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
case SeekAbsolute: case SeekAbsolute:
break; break;
case SeekRelative: case SeekRelative:
NewPosition.QuadPart += (UINT64)FileHandle->FilePointer; NewPosition.QuadPart += (ULONGLONG)FileHandle->FilePointer;
break; break;
default: default:
ASSERT(FALSE); ASSERT(FALSE);

View file

@ -75,7 +75,7 @@ static BOOLEAN IsoSearchDirectoryBufferForFile(PVOID DirectoryBuffer, ULONG Dire
IsoFileInfoPointer->FileStart = Record->ExtentLocationL; IsoFileInfoPointer->FileStart = Record->ExtentLocationL;
IsoFileInfoPointer->FileSize = Record->DataLengthL; IsoFileInfoPointer->FileSize = Record->DataLengthL;
IsoFileInfoPointer->FilePointer = 0; IsoFileInfoPointer->FilePointer = 0;
IsoFileInfoPointer->Directory = (Record->FileFlags & 0x02)?TRUE:FALSE; IsoFileInfoPointer->Directory = !!(Record->FileFlags & 0x02);
return TRUE; return TRUE;
} }
@ -292,17 +292,16 @@ ARC_STATUS IsoOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count) ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
{ {
ARC_STATUS Status;
PISO_FILE_INFO FileHandle = FsGetDeviceSpecific(FileId); PISO_FILE_INFO FileHandle = FsGetDeviceSpecific(FileId);
UCHAR SectorBuffer[SECTORSIZE]; UCHAR SectorBuffer[SECTORSIZE];
LARGE_INTEGER Position; LARGE_INTEGER Position;
ULONG DeviceId; ULONG DeviceId;
ULONG FilePointer; ULONG SectorNumber;
ULONG SectorNumber; ULONG OffsetInSector;
ULONG OffsetInSector; ULONG LengthInSector;
ULONG LengthInSector; ULONG NumberOfSectors;
ULONG NumberOfSectors;
ULONG BytesRead; ULONG BytesRead;
ARC_STATUS Status;
TRACE("IsoRead() Buffer = %p, N = %lu\n", Buffer, N); TRACE("IsoRead() Buffer = %p, N = %lu\n", Buffer, N);
@ -310,23 +309,21 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
*Count = 0; *Count = 0;
// //
// If they are trying to read past the // If the user is trying to read past the end of
// end of the file then return success // the file then return success with Count == 0.
// with Count == 0
// //
FilePointer = FileHandle->FilePointer; if (FileHandle->FilePointer >= FileHandle->FileSize)
if (FilePointer >= FileHandle->FileSize)
{ {
return ESUCCESS; return ESUCCESS;
} }
// //
// If they are trying to read more than there is to read // If the user is trying to read more than there is to read
// then adjust the amount to read // then adjust the amount to read.
// //
if (FilePointer + N > FileHandle->FileSize) if (FileHandle->FilePointer + N > FileHandle->FileSize)
{ {
N = FileHandle->FileSize - FilePointer; N = FileHandle->FileSize - FileHandle->FilePointer;
} }
// //
@ -362,14 +359,14 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
// Only do the first read if we // Only do the first read if we
// aren't aligned on a cluster boundary // aren't aligned on a cluster boundary
// //
if (FilePointer % SECTORSIZE) if (FileHandle->FilePointer % SECTORSIZE)
{ {
// //
// Do the math for our first read // Do the math for our first read
// //
SectorNumber = FileHandle->FileStart + (FilePointer / SECTORSIZE); SectorNumber = FileHandle->FileStart + (FileHandle->FilePointer / SECTORSIZE);
OffsetInSector = FilePointer % SECTORSIZE; OffsetInSector = FileHandle->FilePointer % SECTORSIZE;
LengthInSector = (N > (SECTORSIZE - OffsetInSector)) ? (SECTORSIZE - OffsetInSector) : N; LengthInSector = min(N, SECTORSIZE - OffsetInSector);
// //
// Now do the read and update Count, N, FilePointer, & Buffer // Now do the read and update Count, N, FilePointer, & Buffer
@ -389,7 +386,7 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
RtlCopyMemory(Buffer, SectorBuffer + OffsetInSector, LengthInSector); RtlCopyMemory(Buffer, SectorBuffer + OffsetInSector, LengthInSector);
*Count += LengthInSector; *Count += LengthInSector;
N -= LengthInSector; N -= LengthInSector;
FilePointer += LengthInSector; FileHandle->FilePointer += LengthInSector;
Buffer = (PVOID)((ULONG_PTR)Buffer + LengthInSector); Buffer = (PVOID)((ULONG_PTR)Buffer + LengthInSector);
} }
@ -403,7 +400,7 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
// //
NumberOfSectors = (N / SECTORSIZE); NumberOfSectors = (N / SECTORSIZE);
SectorNumber = FileHandle->FileStart + (FilePointer / SECTORSIZE); SectorNumber = FileHandle->FileStart + (FileHandle->FilePointer / SECTORSIZE);
// //
// Now do the read and update Count, N, FilePointer, & Buffer // Now do the read and update Count, N, FilePointer, & Buffer
@ -423,7 +420,7 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
*Count += NumberOfSectors * SECTORSIZE; *Count += NumberOfSectors * SECTORSIZE;
N -= NumberOfSectors * SECTORSIZE; N -= NumberOfSectors * SECTORSIZE;
FilePointer += NumberOfSectors * SECTORSIZE; FileHandle->FilePointer += NumberOfSectors * SECTORSIZE;
Buffer = (PVOID)((ULONG_PTR)Buffer + NumberOfSectors * SECTORSIZE); Buffer = (PVOID)((ULONG_PTR)Buffer + NumberOfSectors * SECTORSIZE);
} }
@ -432,7 +429,7 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
// //
if (N > 0) if (N > 0)
{ {
SectorNumber = FileHandle->FileStart + (FilePointer / SECTORSIZE); SectorNumber = FileHandle->FileStart + (FileHandle->FilePointer / SECTORSIZE);
// //
// Now do the read and update Count, N, FilePointer, & Buffer // Now do the read and update Count, N, FilePointer, & Buffer
@ -451,7 +448,7 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
} }
RtlCopyMemory(Buffer, SectorBuffer, N); RtlCopyMemory(Buffer, SectorBuffer, N);
*Count += N; *Count += N;
FilePointer += N; FileHandle->FilePointer += N;
} }
TRACE("IsoRead() done\n"); TRACE("IsoRead() done\n");

View file

@ -832,6 +832,7 @@ ARC_STATUS NtfsRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
// Read file // Read file
// //
BytesRead64 = NtfsReadAttribute(FileHandle->Volume, FileHandle->DataContext, FileHandle->Offset, Buffer, N); BytesRead64 = NtfsReadAttribute(FileHandle->Volume, FileHandle->DataContext, FileHandle->Offset, Buffer, N);
FileHandle->Offset += BytesRead64;
*Count = (ULONG)BytesRead64; *Count = (ULONG)BytesRead64;
// //