[SETUPLIB] Consolidate the FsUtil and PartList modules of the SetupLib.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Wed, 17 May 2017 23:31:52 +0000 (23:31 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 27 May 2018 18:18:51 +0000 (20:18 +0200)
- Add a PreparePartitionForFormatting routine that sets the partition ID depending on the chosen filesystem.
- The 'FORMATMACHINESTATE FormatState' machine-state and the 'TempPartition' members of the partition list structure is purely a USETUP convenience, so remove them from the PARTLIST structure and move them back into USETUP.
- Attempt to recognize the filesystem (set the 'FileSystem' member of PARTENTRY) of partitions we are adding into the PARTLIST list.
- Fix the return value of the SelectPartition function, which is by the way completely broken (it doesn't do what it is supposed to do; alternatively its naming is completely wrong...).

svn path=/branches/setup_improvements/; revision=74572
svn path=/branches/setup_improvements/; revision=74573

base/setup/lib/fsutil.c
base/setup/lib/fsutil.h
base/setup/lib/partlist.c
base/setup/lib/partlist.h
base/setup/usetup/partlist.h

index 468fed9..16da7f5 100644 (file)
 #include "fsutil.h"
 #include "partlist.h"
 
 #include "fsutil.h"
 #include "partlist.h"
 
-/** For FileSystems **/
 #include <fslib/vfatlib.h>
 #include <fslib/vfatlib.h>
-#include <fslib/ext2lib.h>
+// #include <fslib/ext2lib.h>
 // #include <fslib/ntfslib.h>
 
 #define NDEBUG
 #include <debug.h>
 
 
 // #include <fslib/ntfslib.h>
 
 #define NDEBUG
 #include <debug.h>
 
 
-
 FILE_SYSTEM RegisteredFileSystems[] =
 {
     { L"FAT"  , VfatFormat, VfatChkdsk },
 //  { L"FAT32", VfatFormat, VfatChkdsk },
 #if 0
 FILE_SYSTEM RegisteredFileSystems[] =
 {
     { L"FAT"  , VfatFormat, VfatChkdsk },
 //  { L"FAT32", VfatFormat, VfatChkdsk },
 #if 0
+    { L"FATX" , VfatxFormat, VfatxChkdsk },
+    { L"NTFS" , NtfsFormat, NtfsChkdsk },
+
     { L"EXT2" , Ext2Format, Ext2Chkdsk },
     { L"EXT2" , Ext2Format, Ext2Chkdsk },
-    { L"NTFS" , NtfsFormat, NtfsChkdsk }
+    { L"EXT3" , Ext2Format, Ext2Chkdsk },
+    { L"EXT4" , Ext2Format, Ext2Chkdsk },
+    { L"BTRFS", BtrfsFormatEx, BtrfsChkdskEx },
+    { L"FFS"  , FfsFormat , FfsChkdsk  },
+    { L"REISERFS", ReiserfsFormat, ReiserfsChkdsk },
 #endif
 };
 
 #endif
 };
 
@@ -198,7 +203,44 @@ GetFileSystem(
 
     CurrentFileSystem = NULL;
 
 
     CurrentFileSystem = NULL;
 
-#if 0 // FIXME: To be fully enabled when our storage stack & al. will work better!
+#if 0 // This is an example of old code...
+
+    if ((PartEntry->PartitionType == PARTITION_FAT_12) ||
+        (PartEntry->PartitionType == PARTITION_FAT_16) ||
+        (PartEntry->PartitionType == PARTITION_HUGE) ||
+        (PartEntry->PartitionType == PARTITION_XINT13) ||
+        (PartEntry->PartitionType == PARTITION_FAT32) ||
+        (PartEntry->PartitionType == PARTITION_FAT32_XINT13))
+    {
+        if (CheckFatFormat())
+            FileSystemName = L"FAT";
+        else
+            FileSystemName = NULL;
+    }
+    else if (PartEntry->PartitionType == PARTITION_EXT2)
+    {
+        if (CheckExt2Format())
+            FileSystemName = L"EXT2";
+        else
+            FileSystemName = NULL;
+    }
+    else if (PartEntry->PartitionType == PARTITION_IFS)
+    {
+        if (CheckNtfsFormat())
+            FileSystemName = L"NTFS";
+        else if (CheckHpfsFormat())
+            FileSystemName = L"HPFS";
+        else
+            FileSystemName = NULL;
+    }
+    else
+    {
+        FileSystemName = NULL;
+    }
+
+#endif
+
+#if 0 // FIXME: To be fully enabled when our storage stack & al. work better!
 
     /*
      * We don't have one...
 
     /*
      * We don't have one...
@@ -246,11 +288,13 @@ GetFileSystem(
     {
         // WARNING: See the warning above.
         FileSystemName = L"EXT2";
     {
         // WARNING: See the warning above.
         FileSystemName = L"EXT2";
+        // FIXME: We may have EXT3, 4 too...
     }
     else if (PartEntry->PartitionType == PARTITION_IFS)
     {
         // WARNING: See the warning above.
         FileSystemName = L"NTFS"; /* FIXME: Not quite correct! */
     }
     else if (PartEntry->PartitionType == PARTITION_IFS)
     {
         // WARNING: See the warning above.
         FileSystemName = L"NTFS"; /* FIXME: Not quite correct! */
+        // FIXME: We may have HPFS too...
     }
 
 #if 0
     }
 
 #if 0
@@ -268,9 +312,95 @@ Quit: // For code temporarily disabled above
             PartEntry->PartitionType, FileSystemName ? FileSystemName : L"None");
 
     if (FileSystemName != NULL)
             PartEntry->PartitionType, FileSystemName ? FileSystemName : L"None");
 
     if (FileSystemName != NULL)
-        CurrentFileSystem = GetFileSystemByName(/*FileSystemList,*/ FileSystemName);
+        CurrentFileSystem = GetFileSystemByName(FileSystemName);
 
     return CurrentFileSystem;
 }
 
 
     return CurrentFileSystem;
 }
 
+
+//
+// Formatting routines
+//
+
+BOOLEAN
+PreparePartitionForFormatting(
+    IN struct _PARTENTRY* PartEntry,
+    IN PFILE_SYSTEM FileSystem)
+{
+    if (!FileSystem)
+    {
+        DPRINT1("No file system specified?\n");
+        return FALSE;
+    }
+
+    if (wcscmp(FileSystem->FileSystemName, L"FAT") == 0)
+    {
+        if (PartEntry->SectorCount.QuadPart < 8192)
+        {
+            /* FAT12 CHS partition (disk is smaller than 4.1MB) */
+            SetPartitionType(PartEntry, PARTITION_FAT_12);
+        }
+        else if (PartEntry->StartSector.QuadPart < 1450560)
+        {
+            /* Partition starts below the 8.4GB boundary ==> CHS partition */
+
+            if (PartEntry->SectorCount.QuadPart < 65536)
+            {
+                /* FAT16 CHS partition (partition size < 32MB) */
+                SetPartitionType(PartEntry, PARTITION_FAT_16);
+            }
+            else if (PartEntry->SectorCount.QuadPart < 1048576)
+            {
+                /* FAT16 CHS partition (partition size < 512MB) */
+                SetPartitionType(PartEntry, PARTITION_HUGE);
+            }
+            else
+            {
+                /* FAT32 CHS partition (partition size >= 512MB) */
+                SetPartitionType(PartEntry, PARTITION_FAT32);
+            }
+        }
+        else
+        {
+            /* Partition starts above the 8.4GB boundary ==> LBA partition */
+
+            if (PartEntry->SectorCount.QuadPart < 1048576)
+            {
+                /* FAT16 LBA partition (partition size < 512MB) */
+                SetPartitionType(PartEntry, PARTITION_XINT13);
+            }
+            else
+            {
+                /* FAT32 LBA partition (partition size >= 512MB) */
+                SetPartitionType(PartEntry, PARTITION_FAT32_XINT13);
+            }
+        }
+    }
+#if 0
+    else if (wcscmp(FileSystem->FileSystemName, L"EXT2") == 0)
+    {
+        SetPartitionType(PartEntry, PARTITION_EXT2);
+    }
+    else if (wcscmp(FileSystem->FileSystemName, L"NTFS") == 0)
+    {
+        SetPartitionType(PartEntry, PARTITION_IFS);
+    }
+#endif
+    else
+    {
+        /* Unknown file system? */
+        DPRINT1("Unknown file system \"%S\"?\n", FileSystem->FileSystemName);
+        return FALSE;
+    }
+
+//
+// FIXME: Do this now, or after the partition was actually formatted??
+//
+    /* Set the new partition's file system proper */
+    PartEntry->FormatState = Formatted; // Well... This may be set after the real formatting takes place (in which case we should change the FormatState to another value)
+    PartEntry->FileSystem  = FileSystem;
+
+    return TRUE;
+}
+
 /* EOF */
 /* EOF */
index 9d6d4a0..6fbd864 100644 (file)
@@ -31,4 +31,10 @@ GetFileSystem(
     // IN PFILE_SYSTEM_LIST FileSystemList,
     IN struct _PARTENTRY* PartEntry);
 
     // IN PFILE_SYSTEM_LIST FileSystemList,
     IN struct _PARTENTRY* PartEntry);
 
+
+BOOLEAN
+PreparePartitionForFormatting(
+    IN struct _PARTENTRY* PartEntry,
+    IN PFILE_SYSTEM FileSystem);
+
 /* EOF */
 /* EOF */
index 40661b0..583544b 100644 (file)
@@ -487,6 +487,7 @@ AddPartitionToDisk(
     PPARTENTRY PartEntry;
 
     PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[PartitionIndex];
     PPARTENTRY PartEntry;
 
     PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[PartitionIndex];
+
     if (PartitionInfo->PartitionType == PARTITION_ENTRY_UNUSED ||
         ((LogicalPartition != FALSE) && IsContainerPartition(PartitionInfo->PartitionType)))
     {
     if (PartitionInfo->PartitionType == PARTITION_ENTRY_UNUSED ||
         ((LogicalPartition != FALSE) && IsContainerPartition(PartitionInfo->PartitionType)))
     {
@@ -497,9 +498,7 @@ AddPartitionToDisk(
                                 HEAP_ZERO_MEMORY,
                                 sizeof(PARTENTRY));
     if (PartEntry == NULL)
                                 HEAP_ZERO_MEMORY,
                                 sizeof(PARTENTRY));
     if (PartEntry == NULL)
-    {
         return;
         return;
-    }
 
     PartEntry->DiskEntry = DiskEntry;
 
 
     PartEntry->DiskEntry = DiskEntry;
 
@@ -523,12 +522,10 @@ AddPartitionToDisk(
         if (LogicalPartition == FALSE && DiskEntry->ExtendedPartition == NULL)
             DiskEntry->ExtendedPartition = PartEntry;
     }
         if (LogicalPartition == FALSE && DiskEntry->ExtendedPartition == NULL)
             DiskEntry->ExtendedPartition = PartEntry;
     }
-#if 0
     else if (IsRecognizedPartition(PartEntry->PartitionType))
     {
     else if (IsRecognizedPartition(PartEntry->PartitionType))
     {
-        // FIXME FIXME! We should completely rework how we get this 'FileSystemList' available...
-        PartEntry->FileSystem = GetFileSystem(/*FileSystemList,*/ PartEntry);
-        if (!PartEntry->FileSystem)
+        PartEntry->FileSystem = GetFileSystem(PartEntry);
+        if (PartEntry->FileSystem)
             PartEntry->FormatState = Preformatted;
         else
             PartEntry->FormatState = Unformatted;
             PartEntry->FormatState = Preformatted;
         else
             PartEntry->FormatState = Unformatted;
@@ -536,63 +533,7 @@ AddPartitionToDisk(
     }
     else
     {
     }
     else
     {
-        /* Unknown partition, so unknown partition format (may or may not be actually formatted) */
-        PartEntry->FormatState = UnknownFormat;
-    }
-#endif
-    else if ((PartEntry->PartitionType == PARTITION_FAT_12) ||
-             (PartEntry->PartitionType == PARTITION_FAT_16) ||
-             (PartEntry->PartitionType == PARTITION_HUGE) ||
-             (PartEntry->PartitionType == PARTITION_XINT13) ||
-             (PartEntry->PartitionType == PARTITION_FAT32) ||
-             (PartEntry->PartitionType == PARTITION_FAT32_XINT13))
-    {
-#if 0
-        if (CheckFatFormat())
-        {
-            PartEntry->FormatState = Preformatted;
-        }
-        else
-        {
-            PartEntry->FormatState = Unformatted;
-        }
-#endif
-        PartEntry->FormatState = Preformatted;
-    }
-    else if (PartEntry->PartitionType == PARTITION_EXT2)
-    {
-#if 0
-        if (CheckExt2Format())
-        {
-            PartEntry->FormatState = Preformatted;
-        }
-        else
-        {
-            PartEntry->FormatState = Unformatted;
-        }
-#endif
-        PartEntry->FormatState = Preformatted;
-    }
-    else if (PartEntry->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
-    {
+        /* Unknown partition, hence unknown partition format (may or may not be actually formatted) */
         PartEntry->FormatState = UnknownFormat;
     }
 
         PartEntry->FormatState = UnknownFormat;
     }
 
@@ -1257,9 +1198,6 @@ CreatePartitionList(VOID)
     List->SystemPartition = NULL;
     List->OriginalSystemPartition = NULL;
 
     List->SystemPartition = NULL;
     List->OriginalSystemPartition = NULL;
 
-    List->TempPartition = NULL;
-    List->FormatState = Start;
-
     InitializeListHead(&List->DiskListHead);
     InitializeListHead(&List->BiosDiskListHead);
 
     InitializeListHead(&List->DiskListHead);
     InitializeListHead(&List->BiosDiskListHead);
 
@@ -1392,7 +1330,10 @@ DestroyPartitionList(
     RtlFreeHeap(ProcessHeap, 0, List);
 }
 
     RtlFreeHeap(ProcessHeap, 0, List);
 }
 
-ULONG
+//
+// FIXME: This function is COMPLETELY BROKEN!!!!
+//
+BOOLEAN
 SelectPartition(
     IN PPARTLIST List,
     IN ULONG DiskNumber,
 SelectPartition(
     IN PPARTLIST List,
     IN ULONG DiskNumber,
@@ -2396,9 +2337,7 @@ DeleteCurrentPartition(
 
 VOID
 CheckActiveSystemPartition(
 
 VOID
 CheckActiveSystemPartition(
-    IN PPARTLIST List // ,
-    // IN PFILE_SYSTEM_LIST FileSystemList /* Needed for checking the FS of the candidate system partition */
-    )
+    IN PPARTLIST List)
 {
     PDISKENTRY DiskEntry;
     PPARTENTRY PartEntry;
 {
     PDISKENTRY DiskEntry;
     PPARTENTRY PartEntry;
@@ -2575,7 +2514,7 @@ CheckActiveSystemPartition(
      * NOTE also that for those architectures looking for a
      * partition boot indicator is insufficient.
      */
      * NOTE also that for those architectures looking for a
      * partition boot indicator is insufficient.
      */
-    FileSystem = GetFileSystem(/*FileSystemList,*/ List->OriginalSystemPartition);
+    FileSystem = GetFileSystem(List->OriginalSystemPartition);
     if (FileSystem == NULL)
     {
         DPRINT1("System partition %lu in disk %lu with no FS?!\n",
     if (FileSystem == NULL)
     {
         DPRINT1("System partition %lu in disk %lu with no FS?!\n",
@@ -2584,7 +2523,7 @@ CheckActiveSystemPartition(
         goto FindAndUseAlternativeSystemPartition;
     }
     // HACK: WARNING: We cannot write on this FS yet!
         goto FindAndUseAlternativeSystemPartition;
     }
     // HACK: WARNING: We cannot write on this FS yet!
-    // See fslist.c:GetFileSystem()
+    // See fsutil.c:GetFileSystem()
     if (List->OriginalSystemPartition->PartitionType == PARTITION_EXT2 ||
         List->OriginalSystemPartition->PartitionType == PARTITION_IFS)
     {
     if (List->OriginalSystemPartition->PartitionType == PARTITION_EXT2 ||
         List->OriginalSystemPartition->PartitionType == PARTITION_IFS)
     {
index 80a8823..7a6cec9 100644 (file)
@@ -17,19 +17,6 @@ typedef enum _FORMATSTATE
     Formatted
 } FORMATSTATE, *PFORMATSTATE;
 
     Formatted
 } FORMATSTATE, *PFORMATSTATE;
 
-typedef enum _FORMATMACHINESTATE
-{
-    Start,
-    FormatSystemPartition,
-    FormatInstallPartition,
-    FormatOtherPartition,
-    FormatDone,
-    CheckSystemPartition,
-    CheckInstallPartition,
-    CheckOtherPartition,
-    CheckDone
-} FORMATMACHINESTATE, *PFORMATMACHINESTATE;
-
 struct _FILE_SYSTEM;
 
 typedef struct _PARTENTRY
 struct _FILE_SYSTEM;
 
 typedef struct _PARTENTRY
@@ -163,9 +150,6 @@ typedef struct _PARTLIST
      */
     PPARTENTRY OriginalSystemPartition;
 
      */
     PPARTENTRY OriginalSystemPartition;
 
-    PPARTENTRY TempPartition;
-    FORMATMACHINESTATE FormatState;
-
     LIST_ENTRY DiskListHead;
     LIST_ENTRY BiosDiskListHead;
 
     LIST_ENTRY DiskListHead;
     LIST_ENTRY BiosDiskListHead;
 
@@ -234,7 +218,7 @@ VOID
 DestroyPartitionList(
     IN PPARTLIST List);
 
 DestroyPartitionList(
     IN PPARTLIST List);
 
-ULONG
+BOOLEAN
 SelectPartition(
     IN PPARTLIST List,
     IN ULONG DiskNumber,
 SelectPartition(
     IN PPARTLIST List,
     IN ULONG DiskNumber,
@@ -271,9 +255,7 @@ DeleteCurrentPartition(
 
 VOID
 CheckActiveSystemPartition(
 
 VOID
 CheckActiveSystemPartition(
-    IN PPARTLIST List // ,
-    // IN PFILE_SYSTEM_LIST FileSystemList /* Needed for checking the FS of the candidate system partition */
-    );
+    IN PPARTLIST List);
 
 BOOLEAN
 WritePartitionsToDisk(
 
 BOOLEAN
 WritePartitionsToDisk(
index 2ad6041..b8caf45 100644 (file)
@@ -27,7 +27,6 @@
 
 #include "../lib/partlist.h"
 
 
 #include "../lib/partlist.h"
 
-#if 0
 typedef enum _FORMATMACHINESTATE
 {
     Start,
 typedef enum _FORMATMACHINESTATE
 {
     Start,
@@ -40,7 +39,6 @@ typedef enum _FORMATMACHINESTATE
     CheckOtherPartition,
     CheckDone
 } FORMATMACHINESTATE, *PFORMATMACHINESTATE;
     CheckOtherPartition,
     CheckDone
 } FORMATMACHINESTATE, *PFORMATMACHINESTATE;
-#endif
 
 typedef struct _PARTLIST_UI
 {
 
 typedef struct _PARTLIST_UI
 {