[USETUP]
[reactos.git] / reactos / base / setup / usetup / partlist.c
index e91f9fd..54e2d28 100644 (file)
@@ -18,7 +18,7 @@
  */
 /* COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS text-mode setup
- * FILE:            subsys/system/usetup/partlist.c
+ * FILE:            base/setup/usetup/partlist.c
  * PURPOSE:         Partition list functions
  * PROGRAMMER:      Eric Kohl
  *                  Casper S. Hornstrup (chorns@users.sourceforge.net)
 
 //#define DUMP_PARTITION_TABLE
 
+/* HELPERS FOR PARTITION TYPES **********************************************/
+
+typedef struct _PARTITION_TYPE
+{
+    UCHAR Type;
+    PCHAR Description;
+} PARTITION_TYPE, *PPARTITION_TYPE;
+
+/*
+ * This partition type list was ripped off the kernelDisk.c module from:
+ *
+ * Visopsys Operating System
+ * Copyright (C) 1998-2015 J. Andrew McLaughlin
+ *
+ * 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 the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ *
+ * See also https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
+ * and http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
+ * for a complete list.
+ */
+
+/* This is a table for keeping known partition type codes and descriptions */
+static PARTITION_TYPE PartitionTypes[] =
+{
+    { 0x00, "(Empty)" },
+    { 0x01, "FAT12" },
+    { 0x02, "XENIX root" },
+    { 0x03, "XENIX /usr" },
+    { 0x04, "FAT16 (small)" },
+    { 0x05, "Extended" },
+    { 0x06, "FAT16" },
+    { 0x07, "NTFS/HPFS/exFAT" },
+    { 0x08, "OS/2 or AIX boot" },
+    { 0x09, "AIX data" },
+    { 0x0A, "OS/2 Boot Manager" },
+    { 0x0B, "FAT32" },
+    { 0x0C, "FAT32 (LBA)" },
+    { 0x0E, "FAT16 (LBA)" },
+    { 0x0F, "Extended (LBA)" },
+    { 0x11, "Hidden FAT12" },
+    { 0x12, "FAT diagnostic" },
+    { 0x14, "Hidden FAT16 (small)" },
+    { 0x16, "Hidden FAT16" },
+    { 0x17, "Hidden HPFS or NTFS" },
+    { 0x1B, "Hidden FAT32" },
+    { 0x1C, "Hidden FAT32 (LBA)" },
+    { 0x1E, "Hidden FAT16 (LBA)" },
+    { 0x35, "JFS" },
+    { 0x39, "Plan 9" },
+    { 0x3C, "PartitionMagic" },
+    { 0x3D, "Hidden Netware" },
+    { 0x41, "PowerPC PReP" },
+    { 0x42, "Win2K dynamic extended" },
+    { 0x43, "Old Linux" },
+    { 0x44, "GoBack" },
+    { 0x4D, "QNX4.x" },
+    { 0x4D, "QNX4.x 2nd" },
+    { 0x4D, "QNX4.x 3rd" },
+    { 0x50, "Ontrack R/O" },
+    { 0x51, "Ontrack R/W or Novell" },
+    { 0x52, "CP/M" },
+    { 0x63, "GNU HURD or UNIX SysV" },
+    { 0x64, "Netware 2" },
+    { 0x65, "Netware 3/4" },
+    { 0x66, "Netware SMS" },
+    { 0x67, "Novell" },
+    { 0x68, "Novell" },
+    { 0x69, "Netware 5+" },
+    { 0x7E, "Veritas VxVM public" },
+    { 0x7F, "Veritas VxVM private" },
+    { 0x80, "Minix" },
+    { 0x81, "Linux or Minix" },
+    { 0x82, "Linux swap or Solaris" },
+    { 0x83, "Linux" },
+    { 0x84, "Hibernation" },
+    { 0x85, "Linux extended" },
+    { 0x86, "HPFS or NTFS mirrored" },
+    { 0x87, "HPFS or NTFS mirrored" },
+    { 0x8E, "Linux LVM" },
+    { 0x93, "Hidden Linux" },
+    { 0x9F, "BSD/OS" },
+    { 0xA0, "Laptop hibernation" },
+    { 0xA1, "Laptop hibernation" },
+    { 0xA5, "BSD, NetBSD, FreeBSD" },
+    { 0xA6, "OpenBSD" },
+    { 0xA7, "NeXTSTEP" },
+    { 0xA8, "OS-X UFS" },
+    { 0xA9, "NetBSD" },
+    { 0xAB, "OS-X boot" },
+    { 0xAF, "OS-X HFS" },
+    { 0xB6, "NT corrupt mirror" },
+    { 0xB7, "BSDI" },
+    { 0xB8, "BSDI swap" },
+    { 0xBE, "Solaris 8 boot" },
+    { 0xBF, "Solaris x86" },
+    { 0xC0, "NTFT" },
+    { 0xC1, "DR-DOS FAT12" },
+    { 0xC2, "Hidden Linux" },
+    { 0xC3, "Hidden Linux swap" },
+    { 0xC4, "DR-DOS FAT16 (small)" },
+    { 0xC5, "DR-DOS Extended" },
+    { 0xC6, "DR-DOS FAT16" },
+    { 0xC7, "HPFS mirrored" },
+    { 0xCB, "DR-DOS FAT32" },
+    { 0xCC, "DR-DOS FAT32 (LBA)" },
+    { 0xCE, "DR-DOS FAT16 (LBA)" },
+    { 0xD0, "MDOS" },
+    { 0xD1, "MDOS FAT12" },
+    { 0xD4, "MDOS FAT16 (small)" },
+    { 0xD5, "MDOS Extended" },
+    { 0xD6, "MDOS FAT16" },
+    { 0xD8, "CP/M-86" },
+    { 0xDF, "BootIt EMBRM(FAT16/32)" },
+    { 0xEB, "BeOS BFS" },
+    { 0xEE, "EFI GPT protective" },
+    { 0xEF, "EFI filesystem" },
+    { 0xF0, "Linux/PA-RISC boot" },
+    { 0xF2, "DOS 3.3+ second" },
+    { 0xFA, "Bochs" },
+    { 0xFB, "VmWare" },
+    { 0xFC, "VmWare swap" },
+    { 0xFD, "Linux RAID" },
+    { 0xFE, "NT hidden" },
+};
+
+VOID
+GetPartTypeStringFromPartitionType(
+    UCHAR partitionType,
+    PCHAR strPartType,
+    DWORD cchPartType)
+{
+    /* Determine partition type */
+
+    if (IsContainerPartition(partitionType))
+    {
+        StringCchCopy(strPartType, cchPartType, MUIGetString(STRING_EXTENDED_PARTITION));
+    }
+    else if (partitionType == PARTITION_ENTRY_UNUSED)
+    {
+        StringCchCopy(strPartType, cchPartType, MUIGetString(STRING_FORMATUNUSED));
+    }
+    else
+    {
+        UINT i;
+
+        /* Do the table lookup */
+        for (i = 0; i < ARRAYSIZE(PartitionTypes); i++)
+        {
+            if (partitionType == PartitionTypes[i].Type)
+            {
+                StringCchCopy(strPartType, cchPartType, PartitionTypes[i].Description);
+                return;
+            }
+        }
+
+        /* We are here because the partition type is unknown */
+        StringCchCopy(strPartType, cchPartType, MUIGetString(STRING_FORMATUNKNOWN));
+    }
+}
+
 /* FUNCTIONS ****************************************************************/
 
 #ifdef DUMP_PARTITION_TABLE
@@ -311,7 +484,7 @@ SystemConfigurationDataQueryRoutine(
     ULONG i;
 
     if (ValueType != REG_FULL_RESOURCE_DESCRIPTOR ||
-        ValueLength < sizeof (CM_FULL_RESOURCE_DESCRIPTOR))
+        ValueLength < sizeof(CM_FULL_RESOURCE_DESCRIPTOR))
         return STATUS_UNSUCCESSFUL;
 
     FullResourceDescriptor = (PCM_FULL_RESOURCE_DESCRIPTOR)ValueData;
@@ -329,7 +502,8 @@ SystemConfigurationDataQueryRoutine(
             FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize % sizeof(CM_INT13_DRIVE_PARAMETER) != 0)
             continue;
 
-        *Int13Drives = (CM_INT13_DRIVE_PARAMETER*) RtlAllocateHeap(ProcessHeap, 0, FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize);
+        *Int13Drives = (CM_INT13_DRIVE_PARAMETER*)RtlAllocateHeap(ProcessHeap, 0,
+                       FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize);
         if (*Int13Drives == NULL)
             return STATUS_NO_MEMORY;
 
@@ -425,7 +599,7 @@ EnumerateBiosDiskEntries(
                     DiskCount = 0;
                     while (1)
                     {
-                        BiosDiskEntry = (BIOSDISKENTRY*) RtlAllocateHeap(ProcessHeap, HEAP_ZERO_MEMORY, sizeof(BIOSDISKENTRY));
+                        BiosDiskEntry = (BIOSDISKENTRY*)RtlAllocateHeap(ProcessHeap, HEAP_ZERO_MEMORY, sizeof(BIOSDISKENTRY));
                         if (BiosDiskEntry == NULL)
                         {
                             break;
@@ -1008,9 +1182,7 @@ AddDiskToList(
                         NULL);
     if (!NT_SUCCESS(Status))
     {
-        RtlFreeHeap(ProcessHeap,
-                    0,
-                    Mbr);
+        RtlFreeHeap(ProcessHeap, 0, Mbr);
         DPRINT1("NtReadFile failed, status=%x\n", Status);
         return;
     }
@@ -1047,12 +1219,10 @@ AddDiskToList(
         DiskEntry->NoMbr = FALSE;
 
     /* Free Mbr sector buffer */
-    RtlFreeHeap(ProcessHeap,
-                0,
-                Mbr);
+    RtlFreeHeap(ProcessHeap, 0, Mbr);
 
     ListEntry = List->BiosDiskListHead.Flink;
-    while(ListEntry != &List->BiosDiskListHead)
+    while (ListEntry != &List->BiosDiskListHead)
     {
         BiosDiskEntry = CONTAINING_RECORD(ListEntry, BIOSDISKENTRY, ListEntry);
         /* FIXME:
@@ -1189,12 +1359,12 @@ AddDiskToList(
         }
         else
         {
-            DPRINT1("No matching aligment found! Partiton 1 starts at %I64u\n", DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart);
+            DPRINT1("No matching aligment found! Partition 1 starts at %I64u\n", DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart);
         }
     }
     else
     {
-        DPRINT1("No valid partiton table found! Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry->BytesPerSector);
+        DPRINT1("No valid partition table found! Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry->BytesPerSector);
     }
 
 
@@ -1210,18 +1380,12 @@ AddDiskToList(
     {
         for (i = 0; i < 4; i++)
         {
-            AddPartitionToDisk(DiskNumber,
-                               DiskEntry,
-                               i,
-                               FALSE);
+            AddPartitionToDisk(DiskNumber, DiskEntry, i, FALSE);
         }
 
         for (i = 4; i < DiskEntry->LayoutBuffer->PartitionCount; i += 4)
         {
-            AddPartitionToDisk(DiskNumber,
-                               DiskEntry,
-                               i,
-                               TRUE);
+            AddPartitionToDisk(DiskNumber, DiskEntry, i, TRUE);
         }
     }
 
@@ -1261,14 +1425,11 @@ CreatePartitionList(
     List->Line = 0;
     List->Offset = 0;
 
-    List->TopDisk = (ULONG)-1;
-    List->TopPartition = (ULONG)-1;
-
     List->CurrentDisk = NULL;
     List->CurrentPartition = NULL;
 
-    List->BootDisk = NULL;
-    List->BootPartition = NULL;
+    List->SystemDisk = NULL;
+    List->SystemPartition = NULL;
 
     List->TempDisk = NULL;
     List->TempPartition = NULL;
@@ -1311,9 +1472,7 @@ CreatePartitionList(
                             FILE_SYNCHRONOUS_IO_NONALERT);
         if (NT_SUCCESS(Status))
         {
-            AddDiskToList(FileHandle,
-                          DiskNumber,
-                          List);
+            AddDiskToList(FileHandle, DiskNumber, List);
 
             NtClose(FileHandle);
         }
@@ -1323,9 +1482,6 @@ CreatePartitionList(
 
     AssignDriveLetters(List);
 
-    List->TopDisk = 0;
-    List->TopPartition = 0;
-
     /* Search for first usable disk and partition */
     if (IsListEmpty(&List->DiskListHead))
     {
@@ -1399,8 +1555,8 @@ DestroyPartitionList(
         RtlFreeHeap(ProcessHeap, 0, DiskEntry);
     }
 
-    /* release the bios disk info */
-    while(!IsListEmpty(&List->BiosDiskListHead))
+    /* Release the bios disk info */
+    while (!IsListEmpty(&List->BiosDiskListHead))
     {
         Entry = RemoveHeadList(&List->BiosDiskListHead);
         BiosDiskEntry = CONTAINING_RECORD(Entry, BIOSDISKENTRY, ListEntry);
@@ -1463,7 +1619,9 @@ PrintPartitionData(
     LARGE_INTEGER PartSize;
     PCHAR Unit;
     UCHAR Attribute;
+    CHAR PartTypeString[32];
     PCHAR PartType;
+    PartType = PartTypeString;
 
     Width = List->Right - List->Left - 1;
     Height = List->Bottom - List->Top - 2;
@@ -1503,38 +1661,17 @@ PrintPartitionData(
     else
     {
         /* Determine partition type */
-        PartType = NULL;
+        PartTypeString[0] = '\0';
         if (PartEntry->New == TRUE)
         {
             PartType = MUIGetString(STRING_UNFORMATTED);
         }
         else if (PartEntry->IsPartitioned == TRUE)
         {
-            if ((PartEntry->PartitionType == PARTITION_FAT_12) ||
-                (PartEntry->PartitionType == PARTITION_FAT_16) ||
-                (PartEntry->PartitionType == PARTITION_HUGE) ||
-                (PartEntry->PartitionType == PARTITION_XINT13))
-            {
-                PartType = "FAT";
-            }
-            else if ((PartEntry->PartitionType == PARTITION_FAT32) ||
-                     (PartEntry->PartitionType == PARTITION_FAT32_XINT13))
-            {
-                PartType = "FAT32";
-            }
-            else if (PartEntry->PartitionType == PARTITION_EXT2)
-            {
-                PartType = "EXT2";
-            }
-            else if (PartEntry->PartitionType == PARTITION_IFS)
-            {
-                PartType = "NTFS"; /* FIXME: Not quite correct! */
-            }
-            else if ((PartEntry->PartitionType == PARTITION_EXTENDED) ||
-                     (PartEntry->PartitionType == PARTITION_XINT13_EXTENDED))
-            {
-                PartType = MUIGetString(STRING_EXTENDED_PARTITION);
-            }
+           GetPartTypeStringFromPartitionType(PartEntry->PartitionType,
+                                              PartTypeString,
+                                              30);
+           PartType = PartTypeString;
         }
 
         PartSize.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
@@ -1557,7 +1694,7 @@ PrintPartitionData(
             Unit = MUIGetString(STRING_KB);
         }
 
-        if (PartType == NULL)
+        if (strcmp(PartType, MUIGetString(STRING_FORMATUNKNOWN)) == 0)
         {
             sprintf(LineBuffer,
                     MUIGetString(STRING_HDDINFOUNK5),
@@ -1713,7 +1850,7 @@ PrintDiskData(
     /* Print separator line */
     PrintEmptyLine(List);
 
-    /* Print partition lines*/
+    /* Print partition lines */
     PrimaryEntry = DiskEntry->PrimaryPartListHead.Flink;
     while (PrimaryEntry != &DiskEntry->PrimaryPartListHead)
     {
@@ -2888,7 +3025,7 @@ DeleteCurrentPartition(
     DiskEntry = List->CurrentDisk;
     PartEntry = List->CurrentPartition;
 
-    /* Delete all logical partiton entries if an extended partition will be deleted */
+    /* Delete all logical partition entries if an extended partition will be deleted */
     if (DiskEntry->ExtendedPartition == PartEntry)
     {
         while (!IsListEmpty(&DiskEntry->LogicalPartListHead))
@@ -2905,11 +3042,8 @@ DeleteCurrentPartition(
     /* Adjust unpartitioned disk space entries */
 
     /* Get pointer to previous and next unpartitioned entries */
-    PrevPartEntry = GetPrevUnpartitionedEntry(DiskEntry,
-                                              PartEntry);
-
-    NextPartEntry = GetNextUnpartitionedEntry(DiskEntry,
-                                              PartEntry);
+    PrevPartEntry = GetPrevUnpartitionedEntry(DiskEntry, PartEntry);
+    NextPartEntry = GetNextUnpartitionedEntry(DiskEntry, PartEntry);
 
     if (PrevPartEntry != NULL && NextPartEntry != NULL)
     {
@@ -2976,7 +3110,7 @@ DeleteCurrentPartition(
 
 
 VOID
-CheckActiveBootPartition(
+CheckActiveSystemPartition(
     PPARTLIST List)
 {
     PDISKENTRY DiskEntry;
@@ -2984,18 +3118,18 @@ CheckActiveBootPartition(
     PLIST_ENTRY ListEntry;
 
     /* Check for empty disk list */
-    if (IsListEmpty (&List->DiskListHead))
+    if (IsListEmpty(&List->DiskListHead))
     {
-        List->BootDisk = NULL;
-        List->BootPartition = NULL;
+        List->SystemDisk = NULL;
+        List->SystemPartition = NULL;
         return;
     }
 
 #if 0
-    if (List->BootDisk != NULL &&
-        List->BootPartition != NULL)
+    if (List->SystemDisk != NULL &&
+        List->SystemPartition != NULL)
     {
-        /* We already have an active boot partition */
+        /* We already have an active system partition */
         return;
     }
 #endif
@@ -3004,18 +3138,23 @@ CheckActiveBootPartition(
     DiskEntry = List->CurrentDisk;
 
     /* Check for empty partition list */
-    if (IsListEmpty (&DiskEntry->PrimaryPartListHead))
+    if (IsListEmpty(&DiskEntry->PrimaryPartListHead))
     {
-        List->BootDisk = NULL;
-        List->BootPartition = NULL;
+        List->SystemDisk = NULL;
+        List->SystemPartition = NULL;
         return;
     }
 
+    /*
+     * Check the first partition of the disk in case it is fresh new,
+     * and if so, use it as the system partition.
+     */
+
     PartEntry = CONTAINING_RECORD(DiskEntry->PrimaryPartListHead.Flink,
                                   PARTENTRY,
                                   ListEntry);
 
-    /* Set active boot partition */
+    /* Set active system partition */
     if ((DiskEntry->NewDisk == TRUE) ||
         (PartEntry->BootIndicator == FALSE))
     {
@@ -3025,15 +3164,15 @@ CheckActiveBootPartition(
         DiskEntry->Dirty = TRUE;
 
         /* FIXME: Might be incorrect if partitions were created by Linux FDISK */
-        List->BootDisk = DiskEntry;
-        List->BootPartition = PartEntry;
+        List->SystemDisk = DiskEntry;
+        List->SystemPartition = PartEntry;
 
         return;
     }
 
     /* Disk is not new, scan all partitions to find a bootable one */
-    List->BootDisk = NULL;
-    List->BootPartition = NULL;
+    List->SystemDisk = NULL;
+    List->SystemPartition = NULL;
 
     ListEntry = DiskEntry->PrimaryPartListHead.Flink;
     while (ListEntry != &DiskEntry->PrimaryPartListHead)
@@ -3049,8 +3188,8 @@ CheckActiveBootPartition(
                 PartEntry->BootIndicator)
             {
                 /* Yes, we found it */
-                List->BootDisk = DiskEntry;
-                List->BootPartition = PartEntry;
+                List->SystemDisk = DiskEntry;
+                List->SystemPartition = PartEntry;
 
                 DPRINT("Found bootable partition disk %d, drive letter %c\n",
                        DiskEntry->DiskNumber, PartEntry->DriveLetter);