/*
* ReactOS kernel
- * Copyright (C) 2002, 2003 ReactOS Team
+ * Copyright (C) 2002, 2003, 2004, 2005 ReactOS Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-/* $Id: partlist.c,v 1.15 2003/08/06 16:37:46 ekohl Exp $
+/* $Id$
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS text-mode setup
* FILE: subsys/system/usetup/partlist.c
* PURPOSE: Partition list functions
* PROGRAMMER: Eric Kohl
* Casper S. Hornstrup (chorns@users.sourceforge.net)
+ * Hartmut Birr
*/
-#include <ddk/ntddk.h>
-#include <ddk/ntddscsi.h>
-
-#include <ntdll/rtl.h>
-
-#include <ntos/minmax.h>
-
-#include "usetup.h"
-#include "console.h"
-#include "partlist.h"
-#include "drivesup.h"
+#include <usetup.h>
+#define NDEBUG
+#include <debug.h>
/* FUNCTIONS ****************************************************************/
static VOID
-GetDriverName(PDISKENTRY DiskEntry)
+GetDriverName (PDISKENTRY DiskEntry)
{
RTL_QUERY_REGISTRY_TABLE QueryTable[2];
WCHAR KeyName[32];
NTSTATUS Status;
-#if 0
- RtlCreateUnicodeString (&DiskEntry->DriverName,
- L"atapi");
-#endif
-
- RtlInitUnicodeString(&DiskEntry->DriverName,
- NULL);
+ RtlInitUnicodeString (&DiskEntry->DriverName,
+ NULL);
- swprintf(KeyName,
- L"\\Scsi\\Scsi Port %lu",
- DiskEntry->Port);
+ swprintf (KeyName,
+ L"\\Scsi\\Scsi Port %lu",
+ DiskEntry->Port);
- RtlZeroMemory(&QueryTable,
- sizeof(QueryTable));
+ RtlZeroMemory (&QueryTable,
+ sizeof(QueryTable));
QueryTable[0].Name = L"Driver";
QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
QueryTable[0].EntryContext = &DiskEntry->DriverName;
- Status = RtlQueryRegistryValues(RTL_REGISTRY_DEVICEMAP,
- KeyName,
- QueryTable,
- NULL,
- NULL);
- if (!NT_SUCCESS(Status))
+ Status = RtlQueryRegistryValues (RTL_REGISTRY_DEVICEMAP,
+ KeyName,
+ QueryTable,
+ NULL,
+ NULL);
+ if (!NT_SUCCESS (Status))
+ {
+ DPRINT1 ("RtlQueryRegistryValues() failed (Status %lx)\n", Status);
+ }
+}
+
+
+static VOID
+AssignDriverLetters (PPARTLIST List)
+{
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+ PLIST_ENTRY Entry1;
+ PLIST_ENTRY Entry2;
+ CHAR Letter;
+
+ Letter = 'C';
+
+ /* Assign drive letters to primary partitions */
+ Entry1 = List->DiskListHead.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1, DISKENTRY, ListEntry);
+
+ if (!IsListEmpty (&DiskEntry->PartListHead))
+ {
+ PartEntry = CONTAINING_RECORD (DiskEntry->PartListHead.Flink,
+ PARTENTRY,
+ ListEntry);
+
+ PartEntry->DriveLetter = 0;
+
+ if (PartEntry->Unpartitioned == FALSE &&
+ !IsContainerPartition (PartEntry->PartInfo[0].PartitionType))
+ {
+ if (IsRecognizedPartition (PartEntry->PartInfo[0].PartitionType) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_ENTRY_UNUSED &&
+ PartEntry->PartInfo[0].PartitionLength.QuadPart != 0LL))
+ {
+ if (Letter <= 'Z')
+ {
+ PartEntry->DriveLetter = Letter;
+ Letter++;
+ }
+ }
+ }
+ }
+
+ Entry1 = Entry1->Flink;
+ }
+
+
+ /* Assign drive letters to logical drives */
+ Entry1 = List->DiskListHead.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1, DISKENTRY, ListEntry);
+
+ Entry2 = DiskEntry->PartListHead.Flink;
+ if (Entry2 != &DiskEntry->PartListHead)
+ {
+ Entry2 = Entry2->Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2,
+ PARTENTRY,
+ ListEntry);
+
+ PartEntry->DriveLetter = 0;
+
+ if (PartEntry->Unpartitioned == FALSE &&
+ !IsContainerPartition (PartEntry->PartInfo[0].PartitionType))
+ {
+ if (IsRecognizedPartition (PartEntry->PartInfo[0].PartitionType) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_ENTRY_UNUSED &&
+ PartEntry->PartInfo[0].PartitionLength.QuadPart != 0LL))
+ {
+ if (Letter <= 'Z')
+ {
+ PartEntry->DriveLetter = Letter;
+ Letter++;
+ }
+ }
+ }
+
+ Entry2 = Entry2->Flink;
+ }
+ }
+
+ Entry1 = Entry1->Flink;
+ }
+}
+
+
+static VOID
+UpdatePartitionNumbers (PDISKENTRY DiskEntry)
+{
+ PPARTENTRY PartEntry;
+ PLIST_ENTRY Entry;
+ ULONG PartNumber;
+ ULONG i;
+
+ PartNumber = 1;
+ Entry = DiskEntry->PartListHead.Flink;
+ while (Entry != &DiskEntry->PartListHead)
{
- DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status);
+ PartEntry = CONTAINING_RECORD (Entry,
+ PARTENTRY,
+ ListEntry);
+
+ if (PartEntry->Unpartitioned == TRUE)
+ {
+ for (i = 0; i < 4; i++)
+ {
+ PartEntry->PartInfo[i].PartitionNumber = 0;
+ }
+ }
+ else
+ {
+ for (i = 0; i < 4; i++)
+ {
+ if (IsContainerPartition (PartEntry->PartInfo[i].PartitionType))
+ {
+ PartEntry->PartInfo[i].PartitionNumber = 0;
+ }
+ else if (PartEntry->PartInfo[i].PartitionType == PARTITION_ENTRY_UNUSED &&
+ PartEntry->PartInfo[i].PartitionLength.QuadPart == 0ULL)
+ {
+ PartEntry->PartInfo[i].PartitionNumber = 0;
+ }
+ else
+ {
+ PartEntry->PartInfo[i].PartitionNumber = PartNumber;
+ PartNumber++;
+ }
+ }
+ }
+
+ Entry = Entry->Flink;
}
}
for (i = 0; i < LayoutBuffer->PartitionCount; i += 4)
{
+ for (j = 0; j < 4; j++)
+ {
+ if (LayoutBuffer->PartitionEntry[j].PartitionType != PARTITION_ENTRY_UNUSED ||
+ LayoutBuffer->PartitionEntry[j].PartitionLength.QuadPart != 0ULL)
+ {
+ break;
+ }
+ }
+ if (j >= 4)
+ {
+ continue;
+ }
+
PartEntry = (PPARTENTRY)RtlAllocateHeap (ProcessHeap,
0,
sizeof(PARTENTRY));
RtlZeroMemory (PartEntry,
sizeof(PARTENTRY));
- PartEntry->DriveLetter = GetDriveLetter(DiskNumber,
- LayoutBuffer->PartitionEntry[i].PartitionNumber);
-
PartEntry->Unpartitioned = FALSE;
for (j = 0; j < 4; j++)
sizeof(PARTITION_INFORMATION));
}
+ if (IsContainerPartition(PartEntry->PartInfo[0].PartitionType))
+ {
+ PartEntry->FormatState = Unformatted;
+ }
+ else if ((PartEntry->PartInfo[0].PartitionType == PARTITION_FAT_12) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_FAT_16) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_HUGE) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_XINT13) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_FAT32) ||
+ (PartEntry->PartInfo[0].PartitionType == PARTITION_FAT32_XINT13))
+ {
+#if 0
+ if (CheckFatFormat())
+ {
+ PartEntry->FormatState = Preformatted;
+ }
+ else
+ {
+ PartEntry->FormatState = Unformatted;
+ }
+#endif
+ PartEntry->FormatState = Preformatted;
+ }
+ else if (PartEntry->PartInfo[0].PartitionType == PARTITION_IFS)
+ {
+#if 0
+ if (CheckNtfsFormat())
+ {
+ PartEntry->FormatState = Preformatted;
+ }
+ else if (CheckHpfsFormat())
+ {
+ PartEntry->FormatState = Preformatted;
+ }
+ else
+ {
+ PartEntry->FormatState = Unformatted;
+ }
+#endif
+ PartEntry->FormatState = Preformatted;
+ }
+ else
+ {
+ PartEntry->FormatState = Unknown;
+ }
+
InsertTailList (&DiskEntry->PartListHead,
&PartEntry->ListEntry);
}
PartEntry->UnpartitionedOffset = 0ULL;
PartEntry->UnpartitionedLength = DiskEntry->DiskSize;
+ PartEntry->FormatState = Unformatted;
+
InsertTailList (&DiskEntry->PartListHead,
&PartEntry->ListEntry);
}
for (j = 0; j < 4; j++)
{
- if ((!IsContainerPartition(PartEntry->PartInfo[j].PartitionType)) &&
+ if ((!IsContainerPartition (PartEntry->PartInfo[j].PartitionType)) &&
(PartEntry->PartInfo[j].PartitionType != PARTITION_ENTRY_UNUSED ||
PartEntry->PartInfo[j].PartitionLength.QuadPart != 0LL))
{
if (j == 0)
NewPartEntry->UnpartitionedLength -= DiskEntry->TrackSize;
+ NewPartEntry->FormatState = Unformatted;
+
/* Insert the table into the list */
InsertTailList (&PartEntry->ListEntry,
&NewPartEntry->ListEntry);
/* Check for trailing unpartitioned disk space */
if (DiskEntry->DiskSize > (LastStartingOffset + LastPartitionLength))
{
-#if 0
- LastUnusedPartitionLength =
- DiskEntry->DiskSize - (LastStartingOffset + LastPartitionLength);
-#endif
-
- /* FIXME: Round-down to cylinder size */
+ /* Round-down to cylinder size */
LastUnusedPartitionLength =
ROUND_DOWN (DiskEntry->DiskSize - (LastStartingOffset + LastPartitionLength),
DiskEntry->CylinderSize);
}
}
+NTSTATUS
+STDCALL
+DiskQueryRoutine(PWSTR ValueName,
+ ULONG ValueType,
+ PVOID ValueData,
+ ULONG ValueLength,
+ PVOID Context,
+ PVOID EntryContext)
+{
+ PLIST_ENTRY ListHead = (PLIST_ENTRY)Context;
+ PULONG GlobalDiskCount = (PULONG)EntryContext;
+ PBIOSDISKENTRY BiosDiskEntry;
+ UNICODE_STRING NameU;
+
+ if (ValueType == REG_SZ &&
+ ValueLength == 20 * sizeof(WCHAR))
+ {
+ BiosDiskEntry = RtlAllocateHeap(ProcessHeap, 0, sizeof(BIOSDISKENTRY));
+ if (BiosDiskEntry == NULL)
+ {
+ return STATUS_NO_MEMORY;
+ }
+ BiosDiskEntry->DiskNumber = (*GlobalDiskCount)++;
+
+ NameU.Buffer = (PWCHAR)ValueData;
+ NameU.Length = NameU.MaximumLength = 8 * sizeof(WCHAR);
+ RtlUnicodeStringToInteger(&NameU, 16, &BiosDiskEntry->Checksum);
+
+ NameU.Buffer = (PWCHAR)ValueData + 9;
+ RtlUnicodeStringToInteger(&NameU, 16, &BiosDiskEntry->Signature);
+
+ InsertTailList(ListHead, &BiosDiskEntry->ListEntry);
+ }
+
+ return STATUS_SUCCESS;
+}
+
+#define ROOT_NAME L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter"
+
+STATIC VOID
+EnumerateBiosDiskEntries(PPARTLIST PartList)
+{
+ RTL_QUERY_REGISTRY_TABLE QueryTable[2];
+ WCHAR Name[100];
+ ULONG AdapterCount;
+ ULONG ControllerCount;
+ ULONG DiskCount;
+ NTSTATUS Status;
+ ULONG GlobalDiskCount=0;
+
+
+ memset(QueryTable, 0, sizeof(QueryTable));
+ QueryTable[0].Name = L"Identifier";
+ QueryTable[0].QueryRoutine = DiskQueryRoutine;
+ QueryTable[0].EntryContext = (PVOID)&GlobalDiskCount;
+
+ AdapterCount = 0;
+ while (1)
+ {
+ swprintf(Name, L"%s\\%lu", ROOT_NAME, AdapterCount);
+ Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
+ Name,
+ &QueryTable[1],
+ NULL,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ break;
+ }
+
+ swprintf(Name, L"%s\\%lu\\DiskController", ROOT_NAME, AdapterCount);
+ Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
+ Name,
+ &QueryTable[1],
+ NULL,
+ NULL);
+ if (NT_SUCCESS(Status))
+ {
+ ControllerCount = 0;
+ while (1)
+ {
+ swprintf(Name, L"%s\\%lu\\DiskController\\%lu", ROOT_NAME, AdapterCount, ControllerCount);
+ Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
+ Name,
+ &QueryTable[1],
+ NULL,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ break;
+ }
+
+ swprintf(Name, L"%s\\%lu\\DiskController\\%lu\\DiskPeripheral", ROOT_NAME, AdapterCount, ControllerCount);
+ Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
+ Name,
+ &QueryTable[1],
+ NULL,
+ NULL);
+ if (NT_SUCCESS(Status))
+ {
+ DiskCount = 0;
+ while (1)
+ {
+ swprintf(Name, L"%s\\%lu\\DiskController\\%lu\\DiskPeripheral\\%lu", ROOT_NAME, AdapterCount, ControllerCount, DiskCount);
+ Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
+ Name,
+ QueryTable,
+ (PVOID)&PartList->BiosDiskListHead,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ break;
+ }
+ DiskCount++;
+ }
+ }
+ ControllerCount++;
+ }
+ }
+ AdapterCount++;
+ }
+}
static VOID
AddDiskToList (HANDLE FileHandle,
PDISKENTRY DiskEntry;
IO_STATUS_BLOCK Iosb;
NTSTATUS Status;
+ PPARTITION_SECTOR Mbr;
+ PULONG Buffer;
+ LARGE_INTEGER FileOffset;
+ WCHAR Identifier[20];
+ ULONG Checksum;
+ ULONG Signature;
+ ULONG i;
+ PLIST_ENTRY ListEntry;
+ PBIOSDISKENTRY BiosDiskEntry;
Status = NtDeviceIoControlFile (FileHandle,
NULL,
0,
&DiskGeometry,
sizeof(DISK_GEOMETRY));
- if (!NT_SUCCESS(Status))
+ if (!NT_SUCCESS (Status))
{
return;
}
return;
}
+ Mbr = RtlAllocateHeap(ProcessHeap,
+ 0,
+ DiskGeometry.BytesPerSector);
+
+ if (Mbr == NULL)
+ {
+ return;
+ }
+
+ FileOffset.QuadPart = 0;
+ Status = NtReadFile(FileHandle,
+ NULL,
+ NULL,
+ NULL,
+ &Iosb,
+ (PVOID)Mbr,
+ DiskGeometry.BytesPerSector,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ RtlFreeHeap(ProcessHeap,
+ 0,
+ Mbr);
+ DPRINT1("NtReadFile failed, status=%x\n", Status);
+ return;
+ }
+ Signature = Mbr->Signature;
+
+ /* Calculate the MBR checksum */
+ Checksum = 0;
+ Buffer = (PULONG)Mbr;
+ for (i = 0; i < 128; i++)
+ {
+ Checksum += Buffer[i];
+ }
+ Checksum = ~Checksum + 1;
+
+ RtlFreeHeap (ProcessHeap,
+ 0,
+ Mbr);
+
+ swprintf(Identifier, L"%08x-%08x-A", Checksum, Signature);
+ DPRINT("Identifier: %S\n", Identifier);
+
DiskEntry = (PDISKENTRY)RtlAllocateHeap (ProcessHeap,
0,
sizeof(DISKENTRY));
return;
}
+ DiskEntry->Checksum = Checksum;
+ DiskEntry->Signature = Signature;
+ DiskEntry->BiosFound = FALSE;
+
+ ListEntry = List->BiosDiskListHead.Flink;
+ while(ListEntry != &List->BiosDiskListHead)
+ {
+ BiosDiskEntry = CONTAINING_RECORD(ListEntry, BIOSDISKENTRY, ListEntry);
+ if (BiosDiskEntry->Signature == Signature &&
+ BiosDiskEntry->Checksum == Checksum)
+ {
+ if (!DiskEntry->BiosFound)
+ {
+ DiskEntry->BiosDiskNumber = BiosDiskEntry->DiskNumber;
+ DiskEntry->BiosFound = TRUE;
+ }
+ else
+ {
+ }
+ }
+ ListEntry = ListEntry->Flink;
+ }
+
+ if (!DiskEntry->BiosFound)
+ {
+ RtlFreeHeap(ProcessHeap, 0, DiskEntry);
+ return;
+ }
+
InitializeListHead (&DiskEntry->PartListHead);
DiskEntry->Cylinders = DiskGeometry.Cylinders.QuadPart;
DiskEntry->SectorsPerTrack = DiskGeometry.SectorsPerTrack;
DiskEntry->BytesPerSector = DiskGeometry.BytesPerSector;
- DPRINT("Cylinders %d\n", DiskEntry->Cylinders);
- DPRINT("TracksPerCylinder %d\n", DiskEntry->TracksPerCylinder);
- DPRINT("SectorsPerTrack %d\n", DiskEntry->SectorsPerTrack);
- DPRINT("BytesPerSector %d\n", DiskEntry->BytesPerSector);
+ DPRINT ("Cylinders %d\n", DiskEntry->Cylinders);
+ DPRINT ("TracksPerCylinder %d\n", DiskEntry->TracksPerCylinder);
+ DPRINT ("SectorsPerTrack %d\n", DiskEntry->SectorsPerTrack);
+ DPRINT ("BytesPerSector %d\n", DiskEntry->BytesPerSector);
DiskEntry->DiskSize =
DiskGeometry.Cylinders.QuadPart *
GetDriverName (DiskEntry);
- InsertTailList (&List->DiskListHead,
- &DiskEntry->ListEntry);
+ InsertAscendingList(&List->DiskListHead, DISKENTRY, ListEntry, DiskEntry, BiosDiskNumber);
LayoutBuffer = (DRIVE_LAYOUT_INFORMATION*)RtlAllocateHeap (ProcessHeap,
0,
8192);
if (NT_SUCCESS (Status))
{
+ if (LayoutBuffer->PartitionCount == 0)
+ {
+ DiskEntry->NewDisk = TRUE;
+ }
+
AddPartitionToList (DiskNumber,
DiskEntry,
LayoutBuffer);
PPARTLIST
-InitializePartitionList(VOID)
+CreatePartitionList (SHORT Left,
+ SHORT Top,
+ SHORT Right,
+ SHORT Bottom)
{
PPARTLIST List;
OBJECT_ATTRIBUTES ObjectAttributes;
SYSTEM_DEVICE_INFORMATION Sdi;
- DISK_GEOMETRY DiskGeometry;
IO_STATUS_BLOCK Iosb;
ULONG ReturnSize;
NTSTATUS Status;
if (List == NULL)
return NULL;
- List->Left = 0;
- List->Top = 0;
- List->Right = 0;
- List->Bottom = 0;
+ List->Left = Left;
+ List->Top = Top;
+ List->Right = Right;
+ List->Bottom = Bottom;
List->Line = 0;
List->CurrentPartition = NULL;
InitializeListHead (&List->DiskListHead);
+ InitializeListHead (&List->BiosDiskListHead);
- Status = NtQuerySystemInformation(SystemDeviceInformation,
- &Sdi,
- sizeof(SYSTEM_DEVICE_INFORMATION),
- &ReturnSize);
- if (!NT_SUCCESS(Status))
+ EnumerateBiosDiskEntries(List);
+
+ Status = NtQuerySystemInformation (SystemDeviceInformation,
+ &Sdi,
+ sizeof(SYSTEM_DEVICE_INFORMATION),
+ &ReturnSize);
+ if (!NT_SUCCESS (Status))
{
- RtlFreeHeap(ProcessHeap, 0, List);
- return(NULL);
+ RtlFreeHeap (ProcessHeap, 0, List);
+ return NULL;
}
for (DiskNumber = 0; DiskNumber < Sdi.NumberOfDisks; DiskNumber++)
{
- swprintf(Buffer,
- L"\\Device\\Harddisk%d\\Partition0",
- DiskNumber);
- RtlInitUnicodeString(&Name,
- Buffer);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- 0,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- 0x10001,
- &ObjectAttributes,
- &Iosb,
- 1,
- FILE_SYNCHRONOUS_IO_NONALERT);
+ swprintf (Buffer,
+ L"\\Device\\Harddisk%d\\Partition0",
+ DiskNumber);
+ RtlInitUnicodeString (&Name,
+ Buffer);
+
+ InitializeObjectAttributes (&ObjectAttributes,
+ &Name,
+ 0,
+ NULL,
+ NULL);
+
+ Status = NtOpenFile (&FileHandle,
+ FILE_GENERIC_READ,
+ &ObjectAttributes,
+ &Iosb,
+ FILE_SHARE_READ,
+ FILE_SYNCHRONOUS_IO_NONALERT);
if (NT_SUCCESS(Status))
{
AddDiskToList (FileHandle,
}
}
+ AssignDriverLetters (List);
+
List->TopDisk = 0;
List->TopPartition = 0;
}
-PPARTLIST
-CreatePartitionList(SHORT Left,
- SHORT Top,
- SHORT Right,
- SHORT Bottom)
-{
- PPARTLIST List;
-
- List = InitializePartitionList ();
- if (List == NULL)
- return NULL;
-
- List->Left = Left;
- List->Top = Top;
- List->Right = Right;
- List->Bottom = Bottom;
-
- DrawPartitionList (List);
-
- return List;
-}
-
-
VOID
-DestroyPartitionList(PPARTLIST List)
+DestroyPartitionList (PPARTLIST List)
{
PDISKENTRY DiskEntry;
+ PBIOSDISKENTRY BiosDiskEntry;
PPARTENTRY PartEntry;
PLIST_ENTRY Entry;
-#if 0
- COORD coPos;
- USHORT Width;
-
- /* clear occupied screen area */
- coPos.X = List->Left;
- Width = List->Right - List->Left + 1;
- for (coPos.Y = List->Top; coPos.Y <= List->Bottom; coPos.Y++)
- {
- FillConsoleOutputAttribute(0x17,
- Width,
- coPos,
- &i);
-
- FillConsoleOutputCharacter(' ',
- Width,
- coPos,
- &i);
- }
-#endif
/* Release disk and partition info */
while (!IsListEmpty (&List->DiskListHead))
RtlFreeHeap (ProcessHeap, 0, DiskEntry);
}
+ /* release the bios disk info */
+ while(!IsListEmpty(&List->BiosDiskListHead))
+ {
+ Entry = RemoveHeadList(&List->BiosDiskListHead);
+ BiosDiskEntry = CONTAINING_RECORD(Entry, BIOSDISKENTRY, ListEntry);
+
+ RtlFreeHeap(ProcessHeap, 0, BiosDiskEntry);
+ }
+
/* Release list head */
RtlFreeHeap (ProcessHeap, 0, List);
}
static VOID
-PrintEmptyLine(PPARTLIST List)
+PrintEmptyLine (PPARTLIST List)
{
COORD coPos;
ULONG Written;
USHORT Height;
Width = List->Right - List->Left - 1;
- Height = List->Bottom - List->Top - 1;
+ Height = List->Bottom - List->Top - 2;
- if (List->Line < 0 || List->Line > Height)
- return;
coPos.X = List->Left + 1;
coPos.Y = List->Top + 1 + List->Line;
- FillConsoleOutputAttribute(0x17,
- Width,
- coPos,
- &Written);
-
- FillConsoleOutputCharacter(' ',
- Width,
- coPos,
- &Written);
-
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ FillConsoleOutputAttribute (0x17,
+ Width,
+ coPos,
+ &Written);
+
+ FillConsoleOutputCharacter (' ',
+ Width,
+ coPos,
+ &Written);
+ }
List->Line++;
}
static VOID
-PrintPartitionData(PPARTLIST List,
- PDISKENTRY DiskEntry,
- PPARTENTRY PartEntry)
+PrintPartitionData (PPARTLIST List,
+ PDISKENTRY DiskEntry,
+ PPARTENTRY PartEntry)
{
CHAR LineBuffer[128];
COORD coPos;
PCHAR PartType;
Width = List->Right - List->Left - 1;
- Height = List->Bottom - List->Top - 1;
+ Height = List->Bottom - List->Top - 2;
- if (List->Line < 0 || List->Line > Height)
- return;
coPos.X = List->Left + 1;
coPos.Y = List->Top + 1 + List->Line;
Unit = "KB";
}
- sprintf(LineBuffer,
- " Unpartitioned space %6I64u %s",
- PartSize,
- Unit);
+ sprintf (LineBuffer,
+ " Unpartitioned space %6I64u %s",
+ PartSize,
+ Unit);
}
else
{
-
/* Determine partition type */
PartType = NULL;
if (PartEntry->New == TRUE)
}
#if 0
- if (PartEntry->PartInfo[0].PartitionLength.QuadPart >= 0x280000000ULL) /* 10 GB */
+ if (PartEntry->PartInfo[0].PartitionLength.QuadPart >= 0x280000000LL) /* 10 GB */
{
PartSize = (PartEntry->PartInfo[0].PartitionLength.QuadPart + (1 << 29)) >> 30;
Unit = "GB";
}
else
#endif
- if (PartEntry->PartInfo[0].PartitionLength.QuadPart >= 0xA00000ULL) /* 10 MB */
+ if (PartEntry->PartInfo[0].PartitionLength.QuadPart >= 0xA00000LL) /* 10 MB */
{
PartSize = (PartEntry->PartInfo[0].PartitionLength.QuadPart + (1 << 19)) >> 20;
Unit = "MB";
if (PartType == NULL)
{
sprintf (LineBuffer,
- "%c%c Type %-3lu %6I64u %s",
+ "%c%c Type %-3u %6I64u %s",
(PartEntry->DriveLetter == 0) ? '-' : PartEntry->DriveLetter,
(PartEntry->DriveLetter == 0) ? '-' : ':',
PartEntry->PartInfo[0].PartitionType,
Attribute = (List->CurrentDisk == DiskEntry &&
List->CurrentPartition == PartEntry) ? 0x71 : 0x17;
- FillConsoleOutputCharacter(' ',
- Width,
- coPos,
- &Written);
-
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ FillConsoleOutputCharacter (' ',
+ Width,
+ coPos,
+ &Written);
+ }
coPos.X += 4;
Width -= 8;
- FillConsoleOutputAttribute(Attribute,
- Width,
- coPos,
- &Written);
-
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ FillConsoleOutputAttribute (Attribute,
+ Width,
+ coPos,
+ &Written);
+ }
coPos.X++;
Width -= 2;
- WriteConsoleOutputCharacters(LineBuffer,
- min(strlen(LineBuffer), Width),
- coPos);
-
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ WriteConsoleOutputCharacters (LineBuffer,
+ min (strlen (LineBuffer), Width),
+ coPos);
+ }
List->Line++;
}
static VOID
-PrintDiskData(PPARTLIST List,
- PDISKENTRY DiskEntry)
+PrintDiskData (PPARTLIST List,
+ PDISKENTRY DiskEntry)
{
PPARTENTRY PartEntry;
PLIST_ENTRY Entry;
USHORT Height;
ULONGLONG DiskSize;
PCHAR Unit;
- SHORT PartIndex;
Width = List->Right - List->Left - 1;
- Height = List->Bottom - List->Top - 1;
+ Height = List->Bottom - List->Top - 2;
- if (List->Line < 0 || List->Line > Height)
- return;
coPos.X = List->Left + 1;
coPos.Y = List->Top + 1 + List->Line;
if (DiskEntry->DriverName.Length > 0)
{
- sprintf(LineBuffer,
- "%6I64u %s Harddisk %lu (Port=%hu, Bus=%hu, Id=%hu) on %wZ",
- DiskSize,
- Unit,
- DiskEntry->DiskNumber,
- DiskEntry->Port,
- DiskEntry->Bus,
- DiskEntry->Id,
- &DiskEntry->DriverName);
+ sprintf (LineBuffer,
+ "%6I64u %s Harddisk %lu (Port=%hu, Bus=%hu, Id=%hu) on %wZ",
+ DiskSize,
+ Unit,
+ DiskEntry->DiskNumber,
+ DiskEntry->Port,
+ DiskEntry->Bus,
+ DiskEntry->Id,
+ &DiskEntry->DriverName);
}
else
{
- sprintf(LineBuffer,
- "%6I64u %s Harddisk %lu (Port=%hu, Bus=%hu, Id=%hu)",
- DiskSize,
- Unit,
- DiskEntry->DiskNumber,
- DiskEntry->Port,
- DiskEntry->Bus,
- DiskEntry->Id);
+ sprintf (LineBuffer,
+ "%6I64u %s Harddisk %lu (Port=%hu, Bus=%hu, Id=%hu)",
+ DiskSize,
+ Unit,
+ DiskEntry->DiskNumber,
+ DiskEntry->Port,
+ DiskEntry->Bus,
+ DiskEntry->Id);
+ }
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ FillConsoleOutputAttribute (0x17,
+ Width,
+ coPos,
+ &Written);
+
+ FillConsoleOutputCharacter (' ',
+ Width,
+ coPos,
+ &Written);
}
-
- FillConsoleOutputAttribute(0x17,
- Width,
- coPos,
- &Written);
-
- FillConsoleOutputCharacter(' ',
- Width,
- coPos,
- &Written);
coPos.X++;
- WriteConsoleOutputCharacters(LineBuffer,
- min(strlen(LineBuffer), Width - 2),
- coPos);
-
+ if (List->Line >= 0 && List->Line <= Height)
+ {
+ WriteConsoleOutputCharacters (LineBuffer,
+ min (strlen (LineBuffer), Width - 2),
+ coPos);
+ }
List->Line++;
/* Print separator line */
- PrintEmptyLine(List);
+ PrintEmptyLine (List);
/* Print partition lines*/
Entry = DiskEntry->PartListHead.Flink;
}
/* Print separator line */
- PrintEmptyLine(List);
+ PrintEmptyLine (List);
}
VOID
-DrawPartitionList(PPARTLIST List)
+DrawPartitionList (PPARTLIST List)
{
- PLIST_ENTRY Entry;
+ PLIST_ENTRY Entry, Entry2;
PDISKENTRY DiskEntry;
- CHAR LineBuffer[128];
+ PPARTENTRY PartEntry = NULL;
COORD coPos;
ULONG Written;
SHORT i;
- SHORT DiskIndex;
-
- /* draw upper left corner */
- coPos.X = List->Left;
- coPos.Y = List->Top;
- FillConsoleOutputCharacter(0xDA, // '+',
- 1,
- coPos,
- &Written);
-
- /* draw upper edge */
- coPos.X = List->Left + 1;
- coPos.Y = List->Top;
- FillConsoleOutputCharacter(0xC4, // '-',
- List->Right - List->Left - 1,
- coPos,
- &Written);
-
- /* draw upper right corner */
- coPos.X = List->Right;
- coPos.Y = List->Top;
- FillConsoleOutputCharacter(0xBF, // '+',
- 1,
- coPos,
- &Written);
-
- /* draw left and right edge */
- for (i = List->Top + 1; i < List->Bottom; i++)
+ SHORT CurrentDiskLine;
+ SHORT CurrentPartLine;
+ SHORT LastLine;
+ BOOL CurrentPartLineFound = FALSE;
+ BOOL CurrentDiskLineFound = FALSE;
+
+ /* Calculate the line of the current disk and partition */
+ CurrentDiskLine = 0;
+ CurrentPartLine = 0;
+ LastLine = 0;
+ Entry = List->DiskListHead.Flink;
+ while (Entry != &List->DiskListHead)
{
- coPos.X = List->Left;
+ DiskEntry = CONTAINING_RECORD (Entry, DISKENTRY, ListEntry);
+ LastLine += 2;
+ if (CurrentPartLineFound == FALSE)
+ {
+ CurrentPartLine += 2;
+ }
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2, PARTENTRY, ListEntry);
+ if (PartEntry == List->CurrentPartition)
+ {
+ CurrentPartLineFound = TRUE;
+ }
+ Entry2 = Entry2->Flink;
+ if (CurrentPartLineFound == FALSE)
+ {
+ CurrentPartLine++;
+ }
+ LastLine++;
+ }
+ if (DiskEntry == List->CurrentDisk)
+ {
+ CurrentDiskLineFound = TRUE;
+ }
+ Entry = Entry->Flink;
+ if (Entry != &List->DiskListHead)
+ {
+ if (CurrentDiskLineFound == FALSE)
+ {
+ CurrentPartLine ++;
+ CurrentDiskLine = CurrentPartLine;
+ }
+ LastLine++;
+ }
+ else
+ {
+ LastLine--;
+ }
+ }
+
+ /* If it possible, make the disk name visible */
+ if (CurrentPartLine < List->Offset)
+ {
+ List->Offset = CurrentPartLine;
+ }
+ else if (CurrentPartLine - List->Offset > List->Bottom - List->Top - 2)
+ {
+ List->Offset = CurrentPartLine - (List->Bottom - List->Top - 2);
+ }
+ if (CurrentDiskLine < List->Offset && CurrentPartLine - CurrentDiskLine < List->Bottom - List->Top - 2)
+ {
+ List->Offset = CurrentDiskLine;
+ }
+
+
+ /* draw upper left corner */
+ coPos.X = List->Left;
+ coPos.Y = List->Top;
+ FillConsoleOutputCharacter (0xDA, // '+',
+ 1,
+ coPos,
+ &Written);
+
+ /* draw upper edge */
+ coPos.X = List->Left + 1;
+ coPos.Y = List->Top;
+ if (List->Offset == 0)
+ {
+ FillConsoleOutputCharacter (0xC4, // '-',
+ List->Right - List->Left - 1,
+ coPos,
+ &Written);
+ }
+ else
+ {
+ FillConsoleOutputCharacter (0xC4, // '-',
+ List->Right - List->Left - 5,
+ coPos,
+ &Written);
+ coPos.X = List->Right - 5;
+ WriteConsoleOutputCharacters ("(\x18)", // "(up)"
+ 3,
+ coPos);
+ coPos.X = List->Right - 2;
+ FillConsoleOutputCharacter (0xC4, // '-',
+ 2,
+ coPos,
+ &Written);
+ }
+
+ /* draw upper right corner */
+ coPos.X = List->Right;
+ coPos.Y = List->Top;
+ FillConsoleOutputCharacter (0xBF, // '+',
+ 1,
+ coPos,
+ &Written);
+
+ /* draw left and right edge */
+ for (i = List->Top + 1; i < List->Bottom; i++)
+ {
+ coPos.X = List->Left;
coPos.Y = i;
- FillConsoleOutputCharacter(0xB3, // '|',
- 1,
- coPos,
- &Written);
+ FillConsoleOutputCharacter (0xB3, // '|',
+ 1,
+ coPos,
+ &Written);
coPos.X = List->Right;
- FillConsoleOutputCharacter(0xB3, //'|',
- 1,
- coPos,
- &Written);
+ FillConsoleOutputCharacter (0xB3, //'|',
+ 1,
+ coPos,
+ &Written);
}
/* draw lower left corner */
coPos.X = List->Left;
coPos.Y = List->Bottom;
- FillConsoleOutputCharacter(0xC0, // '+',
- 1,
- coPos,
- &Written);
+ FillConsoleOutputCharacter (0xC0, // '+',
+ 1,
+ coPos,
+ &Written);
/* draw lower edge */
coPos.X = List->Left + 1;
coPos.Y = List->Bottom;
- FillConsoleOutputCharacter(0xC4, // '-',
- List->Right - List->Left - 1,
- coPos,
- &Written);
+ if (LastLine - List->Offset <= List->Bottom - List->Top - 2)
+ {
+ FillConsoleOutputCharacter (0xC4, // '-',
+ List->Right - List->Left - 1,
+ coPos,
+ &Written);
+ }
+ else
+ {
+ FillConsoleOutputCharacter (0xC4, // '-',
+ List->Right - List->Left - 5,
+ coPos,
+ &Written);
+ coPos.X = List->Right - 5;
+ WriteConsoleOutputCharacters ("(\x19)", // "(down)"
+ 3,
+ coPos);
+ coPos.X = List->Right - 2;
+ FillConsoleOutputCharacter (0xC4, // '-',
+ 2,
+ coPos,
+ &Written);
+ }
/* draw lower right corner */
coPos.X = List->Right;
coPos.Y = List->Bottom;
- FillConsoleOutputCharacter(0xD9, // '+',
- 1,
- coPos,
- &Written);
+ FillConsoleOutputCharacter (0xD9, // '+',
+ 1,
+ coPos,
+ &Written);
/* print list entries */
- List->Line = 0;
+ List->Line = - List->Offset;
Entry = List->DiskListHead.Flink;
while (Entry != &List->DiskListHead)
VOID
-ScrollDownPartitionList(PPARTLIST List)
+SelectPartition(PPARTLIST List, ULONG DiskNumber, ULONG PartitionNumber)
+{
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+ PLIST_ENTRY Entry1;
+ PLIST_ENTRY Entry2;
+ ULONG i;
+
+ /* Check for empty disks */
+ if (IsListEmpty (&List->DiskListHead))
+ return;
+
+ /* Check for first usable entry on next disk */
+ Entry1 = List->CurrentDisk->ListEntry.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1, DISKENTRY, ListEntry);
+
+ if (DiskEntry->DiskNumber == DiskNumber)
+ {
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2, PARTENTRY, ListEntry);
+
+ for (i = 0; i < 4; i++)
+ {
+ if (PartEntry->PartInfo[i].PartitionNumber == PartitionNumber)
+ {
+ List->CurrentDisk = DiskEntry;
+ List->CurrentPartition = PartEntry;
+ DrawPartitionList (List);
+ return;
+ }
+ }
+ Entry2 = Entry2->Flink;
+ }
+ return;
+ }
+ Entry1 = Entry1->Flink;
+ }
+}
+
+
+VOID
+ScrollDownPartitionList (PPARTLIST List)
{
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry;
VOID
-ScrollUpPartitionList(PPARTLIST List)
+ScrollUpPartitionList (PPARTLIST List)
{
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry;
PLIST_ENTRY Entry1;
PLIST_ENTRY Entry2;
- ULONG i;
/* Check for empty disks */
if (IsListEmpty (&List->DiskListHead))
}
-VOID
-GetActiveBootPartition(PPARTLIST List,
- PDISKENTRY *DiskEntry,
- PPARTENTRY *PartEntry)
+static PPARTENTRY
+GetPrevPartitionedEntry (PDISKENTRY DiskEntry,
+ PPARTENTRY CurrentEntry)
{
- PDISKENTRY LocalDiskEntry;
- PPARTENTRY LocalPartEntry;
+ PPARTENTRY PrevEntry;
PLIST_ENTRY Entry;
- ULONG i;
- *DiskEntry = NULL;
- *PartEntry = NULL;
+ if (CurrentEntry->ListEntry.Blink == &DiskEntry->PartListHead)
+ return NULL;
- /* Check for empty disk list */
- if (IsListEmpty (&List->DiskListHead))
- return;
+ Entry = CurrentEntry->ListEntry.Blink;
+ while (Entry != &DiskEntry->PartListHead)
+ {
+ PrevEntry = CONTAINING_RECORD (Entry,
+ PARTENTRY,
+ ListEntry);
+ if (PrevEntry->Unpartitioned == FALSE)
+ return PrevEntry;
- /* Get first disk entry from the disk list */
- Entry = List->DiskListHead.Flink;
- LocalDiskEntry = CONTAINING_RECORD (Entry, DISKENTRY, ListEntry);
+ Entry = Entry->Blink;
+ }
- /* Check for empty partition list */
- if (IsListEmpty (&LocalDiskEntry->PartListHead))
- return;
+ return NULL;
+}
- /* Search for active partition */
- Entry = LocalDiskEntry->PartListHead.Flink;
- while (Entry != &LocalDiskEntry->PartListHead)
- {
- LocalPartEntry = CONTAINING_RECORD (Entry, PARTENTRY, ListEntry);
- if (LocalPartEntry->PartInfo[0].BootIndicator)
- {
- *DiskEntry = LocalDiskEntry;
- *PartEntry = LocalPartEntry;
- return;
- }
+static PPARTENTRY
+GetNextPartitionedEntry (PDISKENTRY DiskEntry,
+ PPARTENTRY CurrentEntry)
+{
+ PPARTENTRY NextEntry;
+ PLIST_ENTRY Entry;
+
+ if (CurrentEntry->ListEntry.Flink == &DiskEntry->PartListHead)
+ return NULL;
+
+ Entry = CurrentEntry->ListEntry.Flink;
+ while (Entry != &DiskEntry->PartListHead)
+ {
+ NextEntry = CONTAINING_RECORD (Entry,
+ PARTENTRY,
+ ListEntry);
+ if (NextEntry->Unpartitioned == FALSE)
+ return NextEntry;
Entry = Entry->Flink;
}
+
+ return NULL;
+}
+
+
+static PPARTENTRY
+GetPrevUnpartitionedEntry (PDISKENTRY DiskEntry,
+ PPARTENTRY PartEntry)
+{
+ PPARTENTRY PrevPartEntry;
+
+ if (PartEntry->ListEntry.Blink != &DiskEntry->PartListHead)
+ {
+ PrevPartEntry = CONTAINING_RECORD (PartEntry->ListEntry.Blink,
+ PARTENTRY,
+ ListEntry);
+ if (PrevPartEntry->Unpartitioned == TRUE)
+ return PrevPartEntry;
+ }
+
+ return NULL;
+}
+
+
+static PPARTENTRY
+GetNextUnpartitionedEntry (PDISKENTRY DiskEntry,
+ PPARTENTRY PartEntry)
+{
+ PPARTENTRY NextPartEntry;
+
+ if (PartEntry->ListEntry.Flink != &DiskEntry->PartListHead)
+ {
+ NextPartEntry = CONTAINING_RECORD (PartEntry->ListEntry.Flink,
+ PARTENTRY,
+ ListEntry);
+ if (NextPartEntry->Unpartitioned == TRUE)
+ return NextPartEntry;
+ }
+
+ return NULL;
}
VOID
CreateNewPartition (PPARTLIST List,
- ULONGLONG PartitionSize)
+ ULONGLONG PartitionSize,
+ BOOLEAN AutoCreate)
{
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry;
DiskEntry = List->CurrentDisk;
PartEntry = List->CurrentPartition;
- if (PartitionSize == PartEntry->UnpartitionedLength)
+ if (AutoCreate == TRUE ||
+ PartitionSize == PartEntry->UnpartitionedLength)
{
/* Convert current entry to 'new (unformatted)' */
+ PartEntry->FormatState = Unformatted;
PartEntry->PartInfo[0].StartingOffset.QuadPart =
PartEntry->UnpartitionedOffset + DiskEntry->TrackSize;
PartEntry->PartInfo[0].PartitionLength.QuadPart =
PartEntry->PartInfo[0].PartitionType = PARTITION_ENTRY_UNUSED;
PartEntry->PartInfo[0].BootIndicator = FALSE; /* FIXME */
PartEntry->PartInfo[0].RewritePartition = TRUE;
+ PartEntry->PartInfo[1].RewritePartition = TRUE;
+ PartEntry->PartInfo[2].RewritePartition = TRUE;
+ PartEntry->PartInfo[3].RewritePartition = TRUE;
+
+ /* Get previous and next partition entries */
+ PrevPartEntry = GetPrevPartitionedEntry (DiskEntry,
+ PartEntry);
+ NextPartEntry = GetNextPartitionedEntry (DiskEntry,
+ PartEntry);
- /* Check for previous partition entry */
- if (PartEntry->ListEntry.Blink != &DiskEntry->PartListHead)
+ if (PrevPartEntry != NULL && NextPartEntry != NULL)
{
- PrevPartEntry = CONTAINING_RECORD (PartEntry->ListEntry.Blink,
- PARTENTRY,
- ListEntry);
+ /* Current entry is in the middle of the list */
+ /* Copy previous container partition data to current entry */
+ RtlCopyMemory (&PartEntry->PartInfo[1],
+ &PrevPartEntry->PartInfo[1],
+ sizeof(PARTITION_INFORMATION));
+ PartEntry->PartInfo[1].RewritePartition = TRUE;
- /* FIXME: Update extended partition entries */
+ /* Update previous container partition data */
+ PrevPartEntry->PartInfo[1].StartingOffset.QuadPart =
+ PartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
+
+ if (DiskEntry->PartListHead.Flink == &PrevPartEntry->ListEntry)
+ {
+ /* Special case - previous partition is first partition */
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ DiskEntry->DiskSize - PrevPartEntry->PartInfo[1].StartingOffset.QuadPart;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize;
+ }
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
+ }
+ else if (PrevPartEntry == NULL && NextPartEntry != NULL)
+ {
+ /* Current entry is the first entry */
+ return;
+ }
+ else if (PrevPartEntry != NULL && NextPartEntry == NULL)
+ {
+ /* Current entry is the last entry */
+
+ PrevPartEntry->PartInfo[1].StartingOffset.QuadPart =
+ PartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
+
+ if (DiskEntry->PartListHead.Flink == &PrevPartEntry->ListEntry)
+ {
+ /* Special case - previous partition is first partition */
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ DiskEntry->DiskSize - PrevPartEntry->PartInfo[1].StartingOffset.QuadPart;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize;
+ }
+
+ if ((PartEntry->PartInfo[1].StartingOffset.QuadPart +
+ PartEntry->PartInfo[1].PartitionLength.QuadPart) <
+ (1024LL * 255LL * 63LL * 512LL))
+ {
+ PrevPartEntry->PartInfo[1].PartitionType = PARTITION_EXTENDED;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionType = PARTITION_XINT13_EXTENDED;
+ }
+
+ PrevPartEntry->PartInfo[1].BootIndicator = FALSE;
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
}
+ PartEntry->AutoCreate = AutoCreate;
PartEntry->New = TRUE;
PartEntry->Unpartitioned = FALSE;
PartEntry->UnpartitionedOffset = 0ULL;
NewPartEntry->New = TRUE;
+ NewPartEntry->FormatState = Unformatted;
NewPartEntry->PartInfo[0].StartingOffset.QuadPart =
PartEntry->UnpartitionedOffset + DiskEntry->TrackSize;
NewPartEntry->PartInfo[0].PartitionLength.QuadPart =
NewPartEntry->PartInfo[0].PartitionType = PARTITION_ENTRY_UNUSED;
NewPartEntry->PartInfo[0].BootIndicator = FALSE; /* FIXME */
NewPartEntry->PartInfo[0].RewritePartition = TRUE;
+ NewPartEntry->PartInfo[1].RewritePartition = TRUE;
+ NewPartEntry->PartInfo[2].RewritePartition = TRUE;
+ NewPartEntry->PartInfo[3].RewritePartition = TRUE;
+
+ /* Get previous and next partition entries */
+ PrevPartEntry = GetPrevPartitionedEntry (DiskEntry,
+ NewPartEntry);
+ NextPartEntry = GetNextPartitionedEntry (DiskEntry,
+ NewPartEntry);
+
+ if (PrevPartEntry != NULL && NextPartEntry != NULL)
+ {
+ /* Current entry is in the middle of the list */
+
+ /* Copy previous container partition data to current entry */
+ RtlCopyMemory (&NewPartEntry->PartInfo[1],
+ &PrevPartEntry->PartInfo[1],
+ sizeof(PARTITION_INFORMATION));
+ NewPartEntry->PartInfo[1].RewritePartition = TRUE;
+
+ /* Update previous container partition data */
+
+ PrevPartEntry->PartInfo[1].StartingOffset.QuadPart =
+ NewPartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
+
+ if (DiskEntry->PartListHead.Flink == &PrevPartEntry->ListEntry)
+ {
+ /* Special case - previous partition is first partition */
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ DiskEntry->DiskSize - PrevPartEntry->PartInfo[1].StartingOffset.QuadPart;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ NewPartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize;
+ }
+
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
+ }
+ else if (PrevPartEntry == NULL && NextPartEntry != NULL)
+ {
+ /* Current entry is the first entry */
+ return;
+ }
+ else if (PrevPartEntry != NULL && NextPartEntry == NULL)
+ {
+ /* Current entry is the last entry */
+
+ PrevPartEntry->PartInfo[1].StartingOffset.QuadPart =
+ NewPartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
+
+ if (DiskEntry->PartListHead.Flink == &PrevPartEntry->ListEntry)
+ {
+ /* Special case - previous partition is first partition */
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ DiskEntry->DiskSize - PrevPartEntry->PartInfo[1].StartingOffset.QuadPart;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionLength.QuadPart =
+ NewPartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize;
+ }
+
+ if ((PartEntry->PartInfo[1].StartingOffset.QuadPart +
+ PartEntry->PartInfo[1].PartitionLength.QuadPart) <
+ (1024LL * 255LL * 63LL * 512LL))
+ {
+ PrevPartEntry->PartInfo[1].PartitionType = PARTITION_EXTENDED;
+ }
+ else
+ {
+ PrevPartEntry->PartInfo[1].PartitionType = PARTITION_XINT13_EXTENDED;
+ }
- /* FIXME: Update extended partition entries */
+ PrevPartEntry->PartInfo[1].BootIndicator = FALSE;
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
+ }
/* Update offset and size of the remaining unpartitioned disk space */
PartEntry->UnpartitionedOffset += PartitionSize;
DiskEntry->Modified = TRUE;
- /* FIXME: Update partition numbers and drive letters */
+ UpdatePartitionNumbers (DiskEntry);
+ AssignDriverLetters (List);
}
DiskEntry = List->CurrentDisk;
PartEntry = List->CurrentPartition;
- /* Get pointer to previous partition entry */
- PrevPartEntry = NULL;
- if (PartEntry->ListEntry.Blink != &DiskEntry->PartListHead)
+ /* Adjust container partition entries */
+
+ /* Get previous and next partition entries */
+ PrevPartEntry = GetPrevPartitionedEntry (DiskEntry,
+ PartEntry);
+ NextPartEntry = GetNextPartitionedEntry (DiskEntry,
+ PartEntry);
+
+ if (PrevPartEntry != NULL && NextPartEntry != NULL)
{
- PrevPartEntry = CONTAINING_RECORD (PartEntry->ListEntry.Blink,
- PARTENTRY,
- ListEntry);
- }
+ /* Current entry is in the middle of the list */
- /* Get pointer to previous partition entry */
- NextPartEntry = NULL;
- if (PartEntry->ListEntry.Flink != &DiskEntry->PartListHead)
+ /*
+ * The first extended partition can not be deleted
+ * as long as other extended partitions are present.
+ */
+ if (PrevPartEntry->ListEntry.Blink == &DiskEntry->PartListHead)
+ return;
+
+ /* Copy previous container partition data to current entry */
+ RtlCopyMemory (&PrevPartEntry->PartInfo[1],
+ &PartEntry->PartInfo[1],
+ sizeof(PARTITION_INFORMATION));
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
+ }
+ else if (PrevPartEntry == NULL && NextPartEntry != NULL)
{
- NextPartEntry = CONTAINING_RECORD (PartEntry->ListEntry.Flink,
- PARTENTRY,
- ListEntry);
+ /*
+ * A primary partition can not be deleted as long as
+ * extended partitions are present.
+ */
+ return;
}
+ else if (PrevPartEntry != NULL && NextPartEntry == NULL)
+ {
+ /* Current entry is the last entry */
+ RtlZeroMemory (&PrevPartEntry->PartInfo[1],
+ sizeof(PARTITION_INFORMATION));
+ PrevPartEntry->PartInfo[1].RewritePartition = TRUE;
+ }
+
- if ((PrevPartEntry != NULL && PrevPartEntry->Unpartitioned == TRUE) &&
- (NextPartEntry != NULL && NextPartEntry->Unpartitioned == TRUE))
+ /* Adjust unpartitioned disk space entries */
+
+ /* Get pointer to previous and next unpartitioned entries */
+ PrevPartEntry = GetPrevUnpartitionedEntry (DiskEntry,
+ PartEntry);
+
+ NextPartEntry = GetNextUnpartitionedEntry (DiskEntry,
+ PartEntry);
+
+ if (PrevPartEntry != NULL && NextPartEntry != NULL)
{
- /* Merge previous, current and next entry */
+ /* Merge previous, current and next unpartitioned entry */
/* Adjust the previous entries length */
- PrevPartEntry->UnpartitionedLength +=
+ PrevPartEntry->UnpartitionedLength +=
(PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize +
NextPartEntry->UnpartitionedLength);
- /* FIXME: Container entries ?? */
-
/* Remove the current entry */
RemoveEntryList (&PartEntry->ListEntry);
RtlFreeHeap (ProcessHeap,
/* Update current partition */
List->CurrentPartition = PrevPartEntry;
}
- else if (PrevPartEntry != NULL && PrevPartEntry->Unpartitioned == TRUE)
+ else if (PrevPartEntry != NULL && NextPartEntry == NULL)
{
- /* Merge current and previous entry */
+ /* Merge current and previous unpartitioned entry */
/* Adjust the previous entries length */
- PrevPartEntry->UnpartitionedLength +=
+ PrevPartEntry->UnpartitionedLength +=
(PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize);
- /* FIXME: Container entries ?? */
-
/* Remove the current entry */
RemoveEntryList (&PartEntry->ListEntry);
RtlFreeHeap (ProcessHeap,
/* Update current partition */
List->CurrentPartition = PrevPartEntry;
}
- else if (NextPartEntry != NULL && NextPartEntry->Unpartitioned == TRUE)
+ else if (PrevPartEntry == NULL && NextPartEntry != NULL)
{
- /* Merge current and next entry */
+ /* Merge current and next unpartitioned entry */
/* Adjust the next entries offset and length */
- NextPartEntry->UnpartitionedOffset =
+ NextPartEntry->UnpartitionedOffset =
PartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
- NextPartEntry->UnpartitionedLength +=
+ NextPartEntry->UnpartitionedLength +=
(PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize);
- /* FIXME: Container entries ?? */
-
/* Remove the current entry */
RemoveEntryList (&PartEntry->ListEntry);
RtlFreeHeap (ProcessHeap,
/* Nothing to merge but change current entry */
PartEntry->New = FALSE;
PartEntry->Unpartitioned = TRUE;
- PartEntry->UnpartitionedOffset =
+ PartEntry->UnpartitionedOffset =
PartEntry->PartInfo[0].StartingOffset.QuadPart - DiskEntry->TrackSize;
- PartEntry->UnpartitionedLength =
+ PartEntry->UnpartitionedLength =
PartEntry->PartInfo[0].PartitionLength.QuadPart + DiskEntry->TrackSize;
- /* FIXME: Container entries ?? */
-
/* Wipe the partition table */
RtlZeroMemory (&PartEntry->PartInfo,
sizeof(PartEntry->PartInfo));
DiskEntry->Modified = TRUE;
- /* FIXME: Update partition numbers and drive letters */
+ UpdatePartitionNumbers (DiskEntry);
+
+ AssignDriverLetters (List);
+}
+
+
+VOID
+CheckActiveBootPartition (PPARTLIST List)
+{
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+
+ /* Check for empty disk list */
+ if (IsListEmpty (&List->DiskListHead))
+ {
+ List->ActiveBootDisk = NULL;
+ List->ActiveBootPartition = NULL;
+ return;
+ }
+
+#if 0
+ if (List->ActiveBootDisk != NULL &&
+ List->ActiveBootPartition != NULL)
+ {
+ /* We already have an active boot partition */
+ return;
+ }
+#endif
+
+ DiskEntry = CONTAINING_RECORD (List->DiskListHead.Flink,
+ DISKENTRY,
+ ListEntry);
+
+ /* Check for empty partition list */
+ if (IsListEmpty (&DiskEntry->PartListHead))
+ {
+ List->ActiveBootDisk = NULL;
+ List->ActiveBootPartition = NULL;
+ return;
+ }
+
+ PartEntry = CONTAINING_RECORD (DiskEntry->PartListHead.Flink,
+ PARTENTRY,
+ ListEntry);
+
+ /* Set active boot partition */
+ if ((DiskEntry->NewDisk == TRUE) ||
+ (PartEntry->PartInfo[0].BootIndicator == FALSE &&
+ PartEntry->PartInfo[1].BootIndicator == FALSE &&
+ PartEntry->PartInfo[2].BootIndicator == FALSE &&
+ PartEntry->PartInfo[3].BootIndicator == FALSE))
+ {
+ PartEntry->PartInfo[0].BootIndicator = TRUE;
+ PartEntry->PartInfo[0].RewritePartition = TRUE;
+ DiskEntry->Modified = TRUE;
+ }
+ /* FIXME: Might be incorrect if partitions were created by Linux FDISK */
+ List->ActiveBootDisk = DiskEntry;
+ List->ActiveBootPartition = PartEntry;
}
+
+BOOLEAN
+CheckForLinuxFdiskPartitions (PPARTLIST List)
+{
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+ PLIST_ENTRY Entry1;
+ PLIST_ENTRY Entry2;
+ ULONG PartitionCount;
+ ULONG i;
+
+ Entry1 = List->DiskListHead.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1,
+ DISKENTRY,
+ ListEntry);
+
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2,
+ PARTENTRY,
+ ListEntry);
+
+ if (PartEntry->Unpartitioned == FALSE)
+ {
+ PartitionCount = 0;
+
+ for (i = 0; i < 4; i++)
+ {
+ if (!IsContainerPartition (PartEntry->PartInfo[i].PartitionType) &&
+ PartEntry->PartInfo[i].PartitionLength.QuadPart != 0ULL)
+ {
+ PartitionCount++;
+ }
+ }
+
+ if (PartitionCount > 1)
+ {
+ return TRUE;
+ }
+ }
+
+ Entry2 = Entry2->Flink;
+ }
+
+ Entry1 = Entry1->Flink;
+ }
+
+ return FALSE;
+}
+
+
+BOOLEAN
+WritePartitionsToDisk (PPARTLIST List)
+{
+ PDRIVE_LAYOUT_INFORMATION DriveLayout;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK Iosb;
+ WCHAR SrcPath[MAX_PATH];
+ WCHAR DstPath[MAX_PATH];
+ UNICODE_STRING Name;
+ HANDLE FileHandle;
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+ PLIST_ENTRY Entry1;
+ PLIST_ENTRY Entry2;
+ ULONG PartitionCount;
+ ULONG DriveLayoutSize;
+ ULONG Index;
+ NTSTATUS Status;
+
+ if (List == NULL)
+ {
+ return TRUE;
+ }
+
+ Entry1 = List->DiskListHead.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1,
+ DISKENTRY,
+ ListEntry);
+
+ if (DiskEntry->Modified == TRUE)
+ {
+ /* Count partitioned entries */
+ PartitionCount = 0;
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2,
+ PARTENTRY,
+ ListEntry);
+ if (PartEntry->Unpartitioned == FALSE)
+ {
+ PartitionCount += 4;
+ }
+
+ Entry2 = Entry2->Flink;
+ }
+
+ if (PartitionCount > 0)
+ {
+ DriveLayoutSize = sizeof (DRIVE_LAYOUT_INFORMATION) +
+ ((PartitionCount - 1) * sizeof (PARTITION_INFORMATION));
+ DriveLayout = (PDRIVE_LAYOUT_INFORMATION)RtlAllocateHeap (ProcessHeap,
+ 0,
+ DriveLayoutSize);
+ if (DriveLayout == NULL)
+ {
+ DPRINT1 ("RtlAllocateHeap() failed\n");
+ return FALSE;
+ }
+
+ RtlZeroMemory (DriveLayout,
+ DriveLayoutSize);
+
+ DriveLayout->PartitionCount = PartitionCount;
+ if (DiskEntry->Signature == 0)
+ {
+ LARGE_INTEGER SystemTime;
+ TIME_FIELDS TimeFields;
+ PUCHAR Buffer;
+
+ NtQuerySystemTime (&SystemTime);
+ RtlTimeToTimeFields (&SystemTime, &TimeFields);
+
+ Buffer = (PUCHAR)&DiskEntry->Signature;
+ Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF);
+ Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF);
+ Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF);
+ Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF);
+
+ /* FIXME:
+ * check for an existing signature
+ */
+
+ }
+
+ DriveLayout->Signature = DiskEntry->Signature;
+
+ Index = 0;
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD (Entry2,
+ PARTENTRY,
+ ListEntry);
+ if (PartEntry->Unpartitioned == FALSE)
+ {
+ RtlCopyMemory (&DriveLayout->PartitionEntry[Index],
+ &PartEntry->PartInfo[0],
+ 4 * sizeof (PARTITION_INFORMATION));
+ Index += 4;
+ }
+
+ Entry2 = Entry2->Flink;
+ }
+
+ swprintf (DstPath,
+ L"\\Device\\Harddisk%d\\Partition0",
+ DiskEntry->DiskNumber);
+ RtlInitUnicodeString (&Name,
+ DstPath);
+ InitializeObjectAttributes (&ObjectAttributes,
+ &Name,
+ 0,
+ NULL,
+ NULL);
+
+ Status = NtOpenFile (&FileHandle,
+ FILE_ALL_ACCESS,
+ &ObjectAttributes,
+ &Iosb,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT);
+ if (!NT_SUCCESS (Status))
+ {
+ DPRINT1 ("NtOpenFile() failed (Status %lx)\n", Status);
+ return FALSE;
+ }
+
+ Status = NtDeviceIoControlFile (FileHandle,
+ NULL,
+ NULL,
+ NULL,
+ &Iosb,
+ IOCTL_DISK_SET_DRIVE_LAYOUT,
+ DriveLayout,
+ DriveLayoutSize,
+ NULL,
+ 0);
+ if (!NT_SUCCESS (Status))
+ {
+ DPRINT1 ("NtDeviceIoControlFile() failed (Status %lx)\n", Status);
+ NtClose (FileHandle);
+ return FALSE;
+ }
+
+ RtlFreeHeap (ProcessHeap,
+ 0,
+ DriveLayout);
+
+ NtClose (FileHandle);
+
+ /* Install MBR code if the disk is new */
+ if (DiskEntry->NewDisk == TRUE)
+ {
+ wcscpy (SrcPath, SourceRootPath.Buffer);
+ wcscat (SrcPath, L"\\loader\\dosmbr.bin");
+
+ DPRINT1 ("Install MBR bootcode: %S ==> %S\n",
+ SrcPath, DstPath);
+
+ /* Install MBR bootcode */
+ Status = InstallMbrBootCodeToDisk (SrcPath,
+ DstPath);
+ if (!NT_SUCCESS (Status))
+ {
+ DPRINT1 ("InstallMbrBootCodeToDisk() failed (Status %lx)\n",
+ Status);
+ return FALSE;
+ }
+
+ DiskEntry->NewDisk = FALSE;
+ }
+ }
+ }
+
+ Entry1 = Entry1->Flink;
+ }
+
+ return TRUE;
+}
+
+BOOL SetMountedDeviceValues(PPARTLIST List)
+{
+ PLIST_ENTRY Entry1, Entry2;
+ PDISKENTRY DiskEntry;
+ PPARTENTRY PartEntry;
+
+ if (List == NULL)
+ {
+ return FALSE;
+ }
+
+ Entry1 = List->DiskListHead.Flink;
+ while (Entry1 != &List->DiskListHead)
+ {
+ DiskEntry = CONTAINING_RECORD (Entry1,
+ DISKENTRY,
+ ListEntry);
+
+ Entry2 = DiskEntry->PartListHead.Flink;
+ while (Entry2 != &DiskEntry->PartListHead)
+ {
+ PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
+ if (!PartEntry->Unpartitioned && PartEntry->DriveLetter)
+ {
+ if (!SetMountedDeviceValue(PartEntry->DriveLetter, DiskEntry->Signature, PartEntry->PartInfo[0].StartingOffset))
+ {
+ return FALSE;
+ }
+ }
+ Entry2 = Entry2->Flink;
+ }
+ Entry1 = Entry1->Flink;
+ }
+ return TRUE;
+}
+
+
+
/* EOF */