[FREELDR] Implement 'SeekRelative' mode for the *Seek() methods for disks & filesystems.

This commit is contained in:
Hermès Bélusca-Maïto 2019-09-18 02:42:56 +02:00
parent dd46d40fd2
commit e18e7b100c
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
7 changed files with 108 additions and 45 deletions

View file

@ -206,18 +206,29 @@ static ARC_STATUS
DiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
ULONGLONG SectorNumber;
LARGE_INTEGER NewPosition = *Position;
if (SeekMode != SeekAbsolute)
return EINVAL;
if (Position->LowPart & (Context->SectorSize - 1))
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += (Context->SectorNumber * Context->SectorSize);
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (NewPosition.QuadPart & (Context->SectorSize - 1))
return EINVAL;
SectorNumber = Position->QuadPart / Context->SectorSize;
if (SectorNumber >= Context->SectorCount)
/* Convert in number of sectors */
NewPosition.QuadPart /= Context->SectorSize;
if (NewPosition.QuadPart >= Context->SectorCount)
return EINVAL;
Context->SectorNumber = SectorNumber;
Context->SectorNumber = NewPosition.QuadPart;
return ESUCCESS;
}

View file

@ -373,18 +373,29 @@ static ARC_STATUS DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
static ARC_STATUS DiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
ULONGLONG SectorNumber;
LARGE_INTEGER NewPosition = *Position;
if (SeekMode != SeekAbsolute)
return EINVAL;
if (Position->QuadPart & (Context->SectorSize - 1))
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += (Context->SectorNumber * Context->SectorSize);
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (NewPosition.QuadPart & (Context->SectorSize - 1))
return EINVAL;
SectorNumber = Position->QuadPart / Context->SectorSize;
if (SectorNumber >= Context->SectorCount)
/* Convert in number of sectors */
NewPosition.QuadPart /= Context->SectorSize;
if (NewPosition.QuadPart >= Context->SectorCount)
return EINVAL;
Context->SectorNumber = SectorNumber;
Context->SectorNumber = NewPosition.QuadPart;
return ESUCCESS;
}

View file

@ -1201,15 +1201,24 @@ ARC_STATUS BtrFsRead(ULONG FileId, VOID *Buffer, ULONG Size, ULONG *BytesRead)
ARC_STATUS BtrFsSeek(ULONG FileId, LARGE_INTEGER *Position, SEEKMODE SeekMode)
{
pbtrfs_file_info phandle = FsGetDeviceSpecific(FileId);
LARGE_INTEGER NewPosition = *Position;
TRACE("BtrFsSeek %lu NewFilePointer = %llu\n", FileId, Position->QuadPart);
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += phandle->position;
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (SeekMode != SeekAbsolute)
return EINVAL;
if (Position->QuadPart >= phandle->inode.size)
if (NewPosition.QuadPart >= phandle->inode.size)
return EINVAL;
phandle->position = Position->QuadPart;
phandle->position = NewPosition.QuadPart;
return ESUCCESS;
}

View file

@ -1263,17 +1263,24 @@ ARC_STATUS Ext2Read(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
ARC_STATUS Ext2Seek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
PEXT2_FILE_INFO FileHandle = FsGetDeviceSpecific(FileId);
LARGE_INTEGER NewPosition = *Position;
TRACE("Ext2Seek() NewFilePointer = %lu\n", Position->LowPart);
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += FileHandle->FilePointer;
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (SeekMode != SeekAbsolute)
return EINVAL;
if (Position->HighPart != 0)
return EINVAL;
if (Position->LowPart >= FileHandle->FileSize)
if (NewPosition.QuadPart >= FileHandle->FileSize)
return EINVAL;
FileHandle->FilePointer = Position->LowPart;
FileHandle->FilePointer = NewPosition.QuadPart;
return ESUCCESS;
}

View file

@ -1485,17 +1485,26 @@ ARC_STATUS FatRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
ARC_STATUS FatSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
PFAT_FILE_INFO FileHandle = FsGetDeviceSpecific(FileId);
LARGE_INTEGER NewPosition = *Position;
TRACE("FatSeek() NewFilePointer = %lu\n", Position->LowPart);
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += (ULONGLONG)FileHandle->FilePointer;
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (SeekMode != SeekAbsolute)
if (NewPosition.HighPart != 0)
return EINVAL;
if (Position->HighPart != 0)
return EINVAL;
if (Position->LowPart >= FileHandle->FileSize)
if (NewPosition.LowPart >= FileHandle->FileSize)
return EINVAL;
FileHandle->FilePointer = Position->LowPart;
FileHandle->FilePointer = NewPosition.LowPart;
return ESUCCESS;
}

View file

@ -462,17 +462,26 @@ ARC_STATUS IsoRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
ARC_STATUS IsoSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
PISO_FILE_INFO FileHandle = FsGetDeviceSpecific(FileId);
LARGE_INTEGER NewPosition = *Position;
TRACE("IsoSeek() NewFilePointer = %lu\n", Position->LowPart);
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += (ULONGLONG)FileHandle->FilePointer;
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (SeekMode != SeekAbsolute)
if (NewPosition.HighPart != 0)
return EINVAL;
if (Position->HighPart != 0)
return EINVAL;
if (Position->LowPart >= FileHandle->FileSize)
if (NewPosition.LowPart >= FileHandle->FileSize)
return EINVAL;
FileHandle->FilePointer = Position->LowPart;
FileHandle->FilePointer = NewPosition.LowPart;
return ESUCCESS;
}

View file

@ -846,17 +846,24 @@ ARC_STATUS NtfsRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
ARC_STATUS NtfsSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
{
PNTFS_FILE_HANDLE FileHandle = FsGetDeviceSpecific(FileId);
LARGE_INTEGER NewPosition = *Position;
TRACE("NtfsSeek() NewFilePointer = %lu\n", Position->LowPart);
switch (SeekMode)
{
case SeekAbsolute:
break;
case SeekRelative:
NewPosition.QuadPart += FileHandle->Offset;
break;
default:
ASSERT(FALSE);
return EINVAL;
}
if (SeekMode != SeekAbsolute)
return EINVAL;
if (Position->HighPart != 0)
return EINVAL;
if (Position->LowPart >= (ULONG)NtfsGetAttributeSize(&FileHandle->DataContext->Record))
if (NewPosition.QuadPart >= NtfsGetAttributeSize(&FileHandle->DataContext->Record))
return EINVAL;
FileHandle->Offset = Position->LowPart;
FileHandle->Offset = NewPosition.QuadPart;
return ESUCCESS;
}