[FREELDR] Move IoReadPartitionTable to ntoskrnl.c

This commit is contained in:
Timo Kreuzer 2024-10-05 12:38:17 +02:00
parent 77ba2d496b
commit bee8e81d5a
2 changed files with 152 additions and 159 deletions

View file

@ -11,6 +11,10 @@
#include <freeldr.h>
#include <ntoskrnl.h>
#ifndef UNIMPLEMENTED
#define UNIMPLEMENTED ASSERT(FALSE)
#endif
/* FUNCTIONS *****************************************************************/
VOID
@ -52,17 +56,154 @@ IoSetPartitionInformation(
return STATUS_NOT_IMPLEMENTED;
}
/*
* NTSTATUS
* FASTCALL
* IoReadPartitionTable(
* IN PDEVICE_OBJECT DeviceObject,
* IN ULONG SectorSize,
* IN BOOLEAN ReturnRecognizedPartitions,
* OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer);
*
* See boot/freeldr/freeldr/disk/partition.c
*/
#ifndef _M_AMD64
NTSTATUS
NTAPI
IopReadBootRecord(
IN PDEVICE_OBJECT DeviceObject,
IN ULONGLONG LogicalSectorNumber,
IN ULONG SectorSize,
OUT PMASTER_BOOT_RECORD BootRecord)
{
ULONG_PTR FileId = (ULONG_PTR)DeviceObject;
LARGE_INTEGER Position;
ULONG BytesRead;
ARC_STATUS Status;
Position.QuadPart = LogicalSectorNumber * SectorSize;
Status = ArcSeek(FileId, &Position, SeekAbsolute);
if (Status != ESUCCESS)
return STATUS_IO_DEVICE_ERROR;
Status = ArcRead(FileId, BootRecord, SectorSize, &BytesRead);
if (Status != ESUCCESS || BytesRead != SectorSize)
return STATUS_IO_DEVICE_ERROR;
return STATUS_SUCCESS;
}
BOOLEAN
NTAPI
IopCopyPartitionRecord(
IN BOOLEAN ReturnRecognizedPartitions,
IN ULONG SectorSize,
IN PPARTITION_TABLE_ENTRY PartitionTableEntry,
OUT PARTITION_INFORMATION *PartitionEntry)
{
BOOLEAN IsRecognized;
IsRecognized = TRUE; /* FIXME */
if (!IsRecognized && ReturnRecognizedPartitions)
return FALSE;
PartitionEntry->StartingOffset.QuadPart = (ULONGLONG)PartitionTableEntry->SectorCountBeforePartition * SectorSize;
PartitionEntry->PartitionLength.QuadPart = (ULONGLONG)PartitionTableEntry->PartitionSectorCount * SectorSize;
PartitionEntry->HiddenSectors = 0;
PartitionEntry->PartitionNumber = 0; /* Will be filled later */
PartitionEntry->PartitionType = PartitionTableEntry->SystemIndicator;
PartitionEntry->BootIndicator = (PartitionTableEntry->BootIndicator & 0x80) ? TRUE : FALSE;
PartitionEntry->RecognizedPartition = IsRecognized;
PartitionEntry->RewritePartition = FALSE;
return TRUE;
}
NTSTATUS
FASTCALL
IoReadPartitionTable(
IN PDEVICE_OBJECT DeviceObject,
IN ULONG SectorSize,
IN BOOLEAN ReturnRecognizedPartitions,
OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer)
{
NTSTATUS Status;
PMASTER_BOOT_RECORD MasterBootRecord;
PDRIVE_LAYOUT_INFORMATION Partitions;
ULONG NbPartitions, i, Size;
*PartitionBuffer = NULL;
if (SectorSize < sizeof(MASTER_BOOT_RECORD))
return STATUS_NOT_SUPPORTED;
MasterBootRecord = ExAllocatePool(NonPagedPool, SectorSize);
if (!MasterBootRecord)
return STATUS_NO_MEMORY;
/* Read disk MBR */
Status = IopReadBootRecord(DeviceObject, 0, SectorSize, MasterBootRecord);
if (!NT_SUCCESS(Status))
{
ExFreePool(MasterBootRecord);
return Status;
}
/* Check validity of boot record */
if (MasterBootRecord->MasterBootRecordMagic != 0xaa55)
{
ExFreePool(MasterBootRecord);
return STATUS_NOT_SUPPORTED;
}
/* Count number of partitions */
NbPartitions = 0;
for (i = 0; i < 4; i++)
{
NbPartitions++;
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
{
/* FIXME: unhandled case; count number of partitions */
UNIMPLEMENTED;
}
}
if (NbPartitions == 0)
{
ExFreePool(MasterBootRecord);
return STATUS_NOT_SUPPORTED;
}
/* Allocation space to store partitions */
Size = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION, PartitionEntry) +
NbPartitions * sizeof(PARTITION_INFORMATION);
Partitions = ExAllocatePool(NonPagedPool, Size);
if (!Partitions)
{
ExFreePool(MasterBootRecord);
return STATUS_NO_MEMORY;
}
/* Count number of partitions */
NbPartitions = 0;
for (i = 0; i < 4; i++)
{
if (IopCopyPartitionRecord(ReturnRecognizedPartitions,
SectorSize,
&MasterBootRecord->PartitionTable[i],
&Partitions->PartitionEntry[NbPartitions]))
{
Partitions->PartitionEntry[NbPartitions].PartitionNumber = NbPartitions + 1;
NbPartitions++;
}
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
{
/* FIXME: unhandled case; copy partitions */
UNIMPLEMENTED;
}
}
Partitions->PartitionCount = NbPartitions;
Partitions->Signature = MasterBootRecord->Signature;
ExFreePool(MasterBootRecord);
*PartitionBuffer = Partitions;
return STATUS_SUCCESS;
}
#endif // _M_AMD64
NTSTATUS
FASTCALL

View file

@ -438,152 +438,4 @@ DiskGetPartitionEntry(
return FALSE;
}
#ifndef _M_AMD64
NTSTATUS
NTAPI
IopReadBootRecord(
IN PDEVICE_OBJECT DeviceObject,
IN ULONGLONG LogicalSectorNumber,
IN ULONG SectorSize,
OUT PMASTER_BOOT_RECORD BootRecord)
{
ULONG_PTR FileId = (ULONG_PTR)DeviceObject;
LARGE_INTEGER Position;
ULONG BytesRead;
ARC_STATUS Status;
Position.QuadPart = LogicalSectorNumber * SectorSize;
Status = ArcSeek(FileId, &Position, SeekAbsolute);
if (Status != ESUCCESS)
return STATUS_IO_DEVICE_ERROR;
Status = ArcRead(FileId, BootRecord, SectorSize, &BytesRead);
if (Status != ESUCCESS || BytesRead != SectorSize)
return STATUS_IO_DEVICE_ERROR;
return STATUS_SUCCESS;
}
BOOLEAN
NTAPI
IopCopyPartitionRecord(
IN BOOLEAN ReturnRecognizedPartitions,
IN ULONG SectorSize,
IN PPARTITION_TABLE_ENTRY PartitionTableEntry,
OUT PARTITION_INFORMATION *PartitionEntry)
{
BOOLEAN IsRecognized;
IsRecognized = TRUE; /* FIXME */
if (!IsRecognized && ReturnRecognizedPartitions)
return FALSE;
PartitionEntry->StartingOffset.QuadPart = (ULONGLONG)PartitionTableEntry->SectorCountBeforePartition * SectorSize;
PartitionEntry->PartitionLength.QuadPart = (ULONGLONG)PartitionTableEntry->PartitionSectorCount * SectorSize;
PartitionEntry->HiddenSectors = 0;
PartitionEntry->PartitionNumber = 0; /* Will be filled later */
PartitionEntry->PartitionType = PartitionTableEntry->SystemIndicator;
PartitionEntry->BootIndicator = (PartitionTableEntry->BootIndicator & 0x80) ? TRUE : FALSE;
PartitionEntry->RecognizedPartition = IsRecognized;
PartitionEntry->RewritePartition = FALSE;
return TRUE;
}
NTSTATUS
FASTCALL
IoReadPartitionTable(
IN PDEVICE_OBJECT DeviceObject,
IN ULONG SectorSize,
IN BOOLEAN ReturnRecognizedPartitions,
OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer)
{
NTSTATUS Status;
PMASTER_BOOT_RECORD MasterBootRecord;
PDRIVE_LAYOUT_INFORMATION Partitions;
ULONG NbPartitions, i, Size;
*PartitionBuffer = NULL;
if (SectorSize < sizeof(MASTER_BOOT_RECORD))
return STATUS_NOT_SUPPORTED;
MasterBootRecord = ExAllocatePool(NonPagedPool, SectorSize);
if (!MasterBootRecord)
return STATUS_NO_MEMORY;
/* Read disk MBR */
Status = IopReadBootRecord(DeviceObject, 0, SectorSize, MasterBootRecord);
if (!NT_SUCCESS(Status))
{
ExFreePool(MasterBootRecord);
return Status;
}
/* Check validity of boot record */
if (MasterBootRecord->MasterBootRecordMagic != 0xaa55)
{
ExFreePool(MasterBootRecord);
return STATUS_NOT_SUPPORTED;
}
/* Count number of partitions */
NbPartitions = 0;
for (i = 0; i < 4; i++)
{
NbPartitions++;
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
{
/* FIXME: unhandled case; count number of partitions */
UNIMPLEMENTED;
}
}
if (NbPartitions == 0)
{
ExFreePool(MasterBootRecord);
return STATUS_NOT_SUPPORTED;
}
/* Allocation space to store partitions */
Size = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION, PartitionEntry) +
NbPartitions * sizeof(PARTITION_INFORMATION);
Partitions = ExAllocatePool(NonPagedPool, Size);
if (!Partitions)
{
ExFreePool(MasterBootRecord);
return STATUS_NO_MEMORY;
}
/* Count number of partitions */
NbPartitions = 0;
for (i = 0; i < 4; i++)
{
if (IopCopyPartitionRecord(ReturnRecognizedPartitions,
SectorSize,
&MasterBootRecord->PartitionTable[i],
&Partitions->PartitionEntry[NbPartitions]))
{
Partitions->PartitionEntry[NbPartitions].PartitionNumber = NbPartitions + 1;
NbPartitions++;
}
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
{
/* FIXME: unhandled case; copy partitions */
UNIMPLEMENTED;
}
}
Partitions->PartitionCount = NbPartitions;
Partitions->Signature = MasterBootRecord->Signature;
ExFreePool(MasterBootRecord);
*PartitionBuffer = Partitions;
return STATUS_SUCCESS;
}
#endif // _M_AMD64
#endif