Skip to content

Commit bee8e81

Browse files
committed
[FREELDR] Move IoReadPartitionTable to ntoskrnl.c
1 parent 77ba2d4 commit bee8e81

File tree

2 files changed

+152
-159
lines changed

2 files changed

+152
-159
lines changed

boot/freeldr/freeldr/arch/i386/ntoskrnl.c

Lines changed: 152 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <freeldr.h>
1212
#include <ntoskrnl.h>
1313

14+
#ifndef UNIMPLEMENTED
15+
#define UNIMPLEMENTED ASSERT(FALSE)
16+
#endif
17+
1418
/* FUNCTIONS *****************************************************************/
1519

1620
VOID
@@ -52,17 +56,154 @@ IoSetPartitionInformation(
5256
return STATUS_NOT_IMPLEMENTED;
5357
}
5458

55-
/*
56-
* NTSTATUS
57-
* FASTCALL
58-
* IoReadPartitionTable(
59-
* IN PDEVICE_OBJECT DeviceObject,
60-
* IN ULONG SectorSize,
61-
* IN BOOLEAN ReturnRecognizedPartitions,
62-
* OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer);
63-
*
64-
* See boot/freeldr/freeldr/disk/partition.c
65-
*/
59+
#ifndef _M_AMD64
60+
NTSTATUS
61+
NTAPI
62+
IopReadBootRecord(
63+
IN PDEVICE_OBJECT DeviceObject,
64+
IN ULONGLONG LogicalSectorNumber,
65+
IN ULONG SectorSize,
66+
OUT PMASTER_BOOT_RECORD BootRecord)
67+
{
68+
ULONG_PTR FileId = (ULONG_PTR)DeviceObject;
69+
LARGE_INTEGER Position;
70+
ULONG BytesRead;
71+
ARC_STATUS Status;
72+
73+
Position.QuadPart = LogicalSectorNumber * SectorSize;
74+
Status = ArcSeek(FileId, &Position, SeekAbsolute);
75+
if (Status != ESUCCESS)
76+
return STATUS_IO_DEVICE_ERROR;
77+
78+
Status = ArcRead(FileId, BootRecord, SectorSize, &BytesRead);
79+
if (Status != ESUCCESS || BytesRead != SectorSize)
80+
return STATUS_IO_DEVICE_ERROR;
81+
82+
return STATUS_SUCCESS;
83+
}
84+
85+
BOOLEAN
86+
NTAPI
87+
IopCopyPartitionRecord(
88+
IN BOOLEAN ReturnRecognizedPartitions,
89+
IN ULONG SectorSize,
90+
IN PPARTITION_TABLE_ENTRY PartitionTableEntry,
91+
OUT PARTITION_INFORMATION *PartitionEntry)
92+
{
93+
BOOLEAN IsRecognized;
94+
95+
IsRecognized = TRUE; /* FIXME */
96+
if (!IsRecognized && ReturnRecognizedPartitions)
97+
return FALSE;
98+
99+
PartitionEntry->StartingOffset.QuadPart = (ULONGLONG)PartitionTableEntry->SectorCountBeforePartition * SectorSize;
100+
PartitionEntry->PartitionLength.QuadPart = (ULONGLONG)PartitionTableEntry->PartitionSectorCount * SectorSize;
101+
PartitionEntry->HiddenSectors = 0;
102+
PartitionEntry->PartitionNumber = 0; /* Will be filled later */
103+
PartitionEntry->PartitionType = PartitionTableEntry->SystemIndicator;
104+
PartitionEntry->BootIndicator = (PartitionTableEntry->BootIndicator & 0x80) ? TRUE : FALSE;
105+
PartitionEntry->RecognizedPartition = IsRecognized;
106+
PartitionEntry->RewritePartition = FALSE;
107+
108+
return TRUE;
109+
}
110+
111+
NTSTATUS
112+
FASTCALL
113+
IoReadPartitionTable(
114+
IN PDEVICE_OBJECT DeviceObject,
115+
IN ULONG SectorSize,
116+
IN BOOLEAN ReturnRecognizedPartitions,
117+
OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer)
118+
{
119+
NTSTATUS Status;
120+
PMASTER_BOOT_RECORD MasterBootRecord;
121+
PDRIVE_LAYOUT_INFORMATION Partitions;
122+
ULONG NbPartitions, i, Size;
123+
124+
*PartitionBuffer = NULL;
125+
126+
if (SectorSize < sizeof(MASTER_BOOT_RECORD))
127+
return STATUS_NOT_SUPPORTED;
128+
129+
MasterBootRecord = ExAllocatePool(NonPagedPool, SectorSize);
130+
if (!MasterBootRecord)
131+
return STATUS_NO_MEMORY;
132+
133+
/* Read disk MBR */
134+
Status = IopReadBootRecord(DeviceObject, 0, SectorSize, MasterBootRecord);
135+
if (!NT_SUCCESS(Status))
136+
{
137+
ExFreePool(MasterBootRecord);
138+
return Status;
139+
}
140+
141+
/* Check validity of boot record */
142+
if (MasterBootRecord->MasterBootRecordMagic != 0xaa55)
143+
{
144+
ExFreePool(MasterBootRecord);
145+
return STATUS_NOT_SUPPORTED;
146+
}
147+
148+
/* Count number of partitions */
149+
NbPartitions = 0;
150+
for (i = 0; i < 4; i++)
151+
{
152+
NbPartitions++;
153+
154+
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
155+
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
156+
{
157+
/* FIXME: unhandled case; count number of partitions */
158+
UNIMPLEMENTED;
159+
}
160+
}
161+
162+
if (NbPartitions == 0)
163+
{
164+
ExFreePool(MasterBootRecord);
165+
return STATUS_NOT_SUPPORTED;
166+
}
167+
168+
/* Allocation space to store partitions */
169+
Size = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION, PartitionEntry) +
170+
NbPartitions * sizeof(PARTITION_INFORMATION);
171+
Partitions = ExAllocatePool(NonPagedPool, Size);
172+
if (!Partitions)
173+
{
174+
ExFreePool(MasterBootRecord);
175+
return STATUS_NO_MEMORY;
176+
}
177+
178+
/* Count number of partitions */
179+
NbPartitions = 0;
180+
for (i = 0; i < 4; i++)
181+
{
182+
if (IopCopyPartitionRecord(ReturnRecognizedPartitions,
183+
SectorSize,
184+
&MasterBootRecord->PartitionTable[i],
185+
&Partitions->PartitionEntry[NbPartitions]))
186+
{
187+
Partitions->PartitionEntry[NbPartitions].PartitionNumber = NbPartitions + 1;
188+
NbPartitions++;
189+
}
190+
191+
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
192+
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
193+
{
194+
/* FIXME: unhandled case; copy partitions */
195+
UNIMPLEMENTED;
196+
}
197+
}
198+
199+
Partitions->PartitionCount = NbPartitions;
200+
Partitions->Signature = MasterBootRecord->Signature;
201+
ExFreePool(MasterBootRecord);
202+
203+
*PartitionBuffer = Partitions;
204+
return STATUS_SUCCESS;
205+
}
206+
#endif // _M_AMD64
66207

67208
NTSTATUS
68209
FASTCALL

boot/freeldr/freeldr/disk/partition.c

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -438,152 +438,4 @@ DiskGetPartitionEntry(
438438
return FALSE;
439439
}
440440

441-
#ifndef _M_AMD64
442-
NTSTATUS
443-
NTAPI
444-
IopReadBootRecord(
445-
IN PDEVICE_OBJECT DeviceObject,
446-
IN ULONGLONG LogicalSectorNumber,
447-
IN ULONG SectorSize,
448-
OUT PMASTER_BOOT_RECORD BootRecord)
449-
{
450-
ULONG_PTR FileId = (ULONG_PTR)DeviceObject;
451-
LARGE_INTEGER Position;
452-
ULONG BytesRead;
453-
ARC_STATUS Status;
454-
455-
Position.QuadPart = LogicalSectorNumber * SectorSize;
456-
Status = ArcSeek(FileId, &Position, SeekAbsolute);
457-
if (Status != ESUCCESS)
458-
return STATUS_IO_DEVICE_ERROR;
459-
460-
Status = ArcRead(FileId, BootRecord, SectorSize, &BytesRead);
461-
if (Status != ESUCCESS || BytesRead != SectorSize)
462-
return STATUS_IO_DEVICE_ERROR;
463-
464-
return STATUS_SUCCESS;
465-
}
466-
467-
BOOLEAN
468-
NTAPI
469-
IopCopyPartitionRecord(
470-
IN BOOLEAN ReturnRecognizedPartitions,
471-
IN ULONG SectorSize,
472-
IN PPARTITION_TABLE_ENTRY PartitionTableEntry,
473-
OUT PARTITION_INFORMATION *PartitionEntry)
474-
{
475-
BOOLEAN IsRecognized;
476-
477-
IsRecognized = TRUE; /* FIXME */
478-
if (!IsRecognized && ReturnRecognizedPartitions)
479-
return FALSE;
480-
481-
PartitionEntry->StartingOffset.QuadPart = (ULONGLONG)PartitionTableEntry->SectorCountBeforePartition * SectorSize;
482-
PartitionEntry->PartitionLength.QuadPart = (ULONGLONG)PartitionTableEntry->PartitionSectorCount * SectorSize;
483-
PartitionEntry->HiddenSectors = 0;
484-
PartitionEntry->PartitionNumber = 0; /* Will be filled later */
485-
PartitionEntry->PartitionType = PartitionTableEntry->SystemIndicator;
486-
PartitionEntry->BootIndicator = (PartitionTableEntry->BootIndicator & 0x80) ? TRUE : FALSE;
487-
PartitionEntry->RecognizedPartition = IsRecognized;
488-
PartitionEntry->RewritePartition = FALSE;
489-
490-
return TRUE;
491-
}
492-
493-
NTSTATUS
494-
FASTCALL
495-
IoReadPartitionTable(
496-
IN PDEVICE_OBJECT DeviceObject,
497-
IN ULONG SectorSize,
498-
IN BOOLEAN ReturnRecognizedPartitions,
499-
OUT PDRIVE_LAYOUT_INFORMATION *PartitionBuffer)
500-
{
501-
NTSTATUS Status;
502-
PMASTER_BOOT_RECORD MasterBootRecord;
503-
PDRIVE_LAYOUT_INFORMATION Partitions;
504-
ULONG NbPartitions, i, Size;
505-
506-
*PartitionBuffer = NULL;
507-
508-
if (SectorSize < sizeof(MASTER_BOOT_RECORD))
509-
return STATUS_NOT_SUPPORTED;
510-
511-
MasterBootRecord = ExAllocatePool(NonPagedPool, SectorSize);
512-
if (!MasterBootRecord)
513-
return STATUS_NO_MEMORY;
514-
515-
/* Read disk MBR */
516-
Status = IopReadBootRecord(DeviceObject, 0, SectorSize, MasterBootRecord);
517-
if (!NT_SUCCESS(Status))
518-
{
519-
ExFreePool(MasterBootRecord);
520-
return Status;
521-
}
522-
523-
/* Check validity of boot record */
524-
if (MasterBootRecord->MasterBootRecordMagic != 0xaa55)
525-
{
526-
ExFreePool(MasterBootRecord);
527-
return STATUS_NOT_SUPPORTED;
528-
}
529-
530-
/* Count number of partitions */
531-
NbPartitions = 0;
532-
for (i = 0; i < 4; i++)
533-
{
534-
NbPartitions++;
535-
536-
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
537-
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
538-
{
539-
/* FIXME: unhandled case; count number of partitions */
540-
UNIMPLEMENTED;
541-
}
542-
}
543-
544-
if (NbPartitions == 0)
545-
{
546-
ExFreePool(MasterBootRecord);
547-
return STATUS_NOT_SUPPORTED;
548-
}
549-
550-
/* Allocation space to store partitions */
551-
Size = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION, PartitionEntry) +
552-
NbPartitions * sizeof(PARTITION_INFORMATION);
553-
Partitions = ExAllocatePool(NonPagedPool, Size);
554-
if (!Partitions)
555-
{
556-
ExFreePool(MasterBootRecord);
557-
return STATUS_NO_MEMORY;
558-
}
559-
560-
/* Count number of partitions */
561-
NbPartitions = 0;
562-
for (i = 0; i < 4; i++)
563-
{
564-
if (IopCopyPartitionRecord(ReturnRecognizedPartitions,
565-
SectorSize,
566-
&MasterBootRecord->PartitionTable[i],
567-
&Partitions->PartitionEntry[NbPartitions]))
568-
{
569-
Partitions->PartitionEntry[NbPartitions].PartitionNumber = NbPartitions + 1;
570-
NbPartitions++;
571-
}
572-
573-
if (MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_EXTENDED ||
574-
MasterBootRecord->PartitionTable[i].SystemIndicator == PARTITION_XINT13_EXTENDED)
575-
{
576-
/* FIXME: unhandled case; copy partitions */
577-
UNIMPLEMENTED;
578-
}
579-
}
580-
581-
Partitions->PartitionCount = NbPartitions;
582-
Partitions->Signature = MasterBootRecord->Signature;
583-
ExFreePool(MasterBootRecord);
584-
585-
*PartitionBuffer = Partitions;
586-
return STATUS_SUCCESS;
587-
}
588-
#endif // _M_AMD64
589441
#endif

0 commit comments

Comments
 (0)