[VFATLIB] Fix FAT partitions formatting in a non clean fashion.
authorPierre Schweitzer <pierre@reactos.org>
Tue, 20 Feb 2018 23:16:36 +0000 (00:16 +0100)
committerPierre Schweitzer <pierre@reactos.org>
Tue, 20 Feb 2018 23:26:13 +0000 (00:26 +0100)
(So the fun begins)
In spite of what VFATLIB headers pretend, there's not magic in FAT boot sector.
The 3 first bytes are just the jump instruction (to the boot code). No jump, no boot.
Also, some (many?) FAT implementations rely on the jump code to help detecting that
a FAT volume is really a FAT volume. Like MS FastFAT. Or our own FAT recognizer in FS_REC.
The story is that, up to that commit, we zeroed the 3 first bytes; leading to broken
FAT volumes.
This got hidden in most cases by the fact that during setup, when we install boot
loader, we erase parts of the boot sector, including the jump instruction, making the
volume valid again. But that wouldn't fix secondary volumes where the boot loader isn't
installed.
And, also, imagine a scenario where you want to install ReactOS on a newly formatted volume
with MS FastFAT instead of our own implementation... That would simply not work to
the fact that the driver wouldn't recognize the fresh formatted volume!

(So the non fashion begins)
Fix this by putting a not that valid jump into the boot sector when formatting our
partitions. That way, our volume is always regarding a FAT view point. But, instead of
putting values that mean (nearly) nothing. We should also put a dummy bootloader
displaying the user and error message, as done by dosfstools.

(So the hope begins)
This opens the way for trying to install ReactOS with MS FastFAT (doesn't work yet).

CORE-11819
CORE-14362

sdk/lib/fslib/vfatlib/fat12.c
sdk/lib/fslib/vfatlib/fat16.c
sdk/lib/fslib/vfatlib/fat32.c
sdk/lib/fslib/vfatlib/vfatlib.h

index d3cffdd..bf96f8d 100644 (file)
@@ -38,9 +38,9 @@ Fat12WriteBootSector(IN HANDLE FileHandle,
     RtlZeroMemory(NewBootSector, BootSector->BytesPerSector);
 
     /* Copy FAT16 BPB to new bootsector */
-    memcpy(&NewBootSector->OEMName[0],
-           &BootSector->OEMName[0],
-           FIELD_OFFSET(FAT16_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT16_BOOT_SECTOR, OEMName));
+    memcpy(&NewBootSector->Jump[0],
+           &BootSector->Jump[0],
+           FIELD_OFFSET(FAT16_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT16_BOOT_SECTOR, Jump));
            /* FAT16 BPB length (up to (not including) Res2) */
 
     /* Write the boot sector signature */
@@ -276,6 +276,10 @@ Fat12Format(IN HANDLE FileHandle,
 
     RtlZeroMemory(&BootSector, sizeof(FAT16_BOOT_SECTOR));
     memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8);
+    /* FIXME: Add dummy bootloader for real */
+    BootSector.Jump[0] = 0xeb;
+    BootSector.Jump[1] = 0x3c;
+    BootSector.Jump[2] = 0x90;
     BootSector.BytesPerSector = DiskGeometry->BytesPerSector;
     BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector;
     BootSector.ReservedSectors = 1;
index d0beaa3..1438532 100644 (file)
@@ -38,9 +38,9 @@ Fat16WriteBootSector(IN HANDLE FileHandle,
     RtlZeroMemory(NewBootSector, BootSector->BytesPerSector);
 
     /* Copy FAT16 BPB to new bootsector */
-    memcpy(&NewBootSector->OEMName[0],
-           &BootSector->OEMName[0],
-           FIELD_OFFSET(FAT16_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT16_BOOT_SECTOR, OEMName));
+    memcpy(&NewBootSector->Jump[0],
+           &BootSector->Jump[0],
+           FIELD_OFFSET(FAT16_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT16_BOOT_SECTOR, Jump));
            /* FAT16 BPB length (up to (not including) Res2) */
 
     /* Write the boot sector signature */
@@ -283,6 +283,10 @@ Fat16Format(IN HANDLE FileHandle,
 
     RtlZeroMemory(&BootSector, sizeof(FAT16_BOOT_SECTOR));
     memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8);
+    /* FIXME: Add dummy bootloader for real */
+    BootSector.Jump[0] = 0xeb;
+    BootSector.Jump[1] = 0x3c;
+    BootSector.Jump[2] = 0x90;
     BootSector.BytesPerSector = DiskGeometry->BytesPerSector;
     BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector;
     BootSector.ReservedSectors = 1;
index 030f120..1987af4 100644 (file)
@@ -38,9 +38,9 @@ Fat32WriteBootSector(IN HANDLE FileHandle,
     RtlZeroMemory(NewBootSector, BootSector->BytesPerSector);
 
     /* Copy FAT32 BPB to new bootsector */
-    memcpy(&NewBootSector->OEMName[0],
-           &BootSector->OEMName[0],
-           FIELD_OFFSET(FAT32_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT32_BOOT_SECTOR, OEMName));
+    memcpy(&NewBootSector->Jump[0],
+           &BootSector->Jump[0],
+           FIELD_OFFSET(FAT32_BOOT_SECTOR, Res2) - FIELD_OFFSET(FAT32_BOOT_SECTOR, Jump));
            /* FAT32 BPB length (up to (not including) Res2) */
 
     /* Write the boot sector signature */
@@ -428,6 +428,10 @@ Fat32Format(IN HANDLE FileHandle,
 
     RtlZeroMemory(&BootSector, sizeof(FAT32_BOOT_SECTOR));
     memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8);
+    /* FIXME: Add dummy bootloader for real */
+    BootSector.Jump[0] = 0xeb;
+    BootSector.Jump[1] = 0x58;
+    BootSector.Jump[2] = 0x90;
     BootSector.BytesPerSector = DiskGeometry->BytesPerSector;
     BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector;
     BootSector.ReservedSectors = 32;
index 40b1de3..2078851 100644 (file)
@@ -27,9 +27,7 @@
 #include <pshpack1.h>
 typedef struct _FAT16_BOOT_SECTOR
 {
-    unsigned char  magic0;                      // 0
-    unsigned char  res0;                        // 1
-    unsigned char  magic1;                      // 2
+    unsigned char  Jump[3];                     // 0
     unsigned char  OEMName[8];                  // 3
     unsigned short BytesPerSector;              // 11
     unsigned char  SectorsPerCluster;           // 13
@@ -55,9 +53,7 @@ typedef struct _FAT16_BOOT_SECTOR
 
 typedef struct _FAT32_BOOT_SECTOR
 {
-    unsigned char  magic0;                      // 0
-    unsigned char  res0;                        // 1
-    unsigned char  magic1;                      // 2
+    unsigned char  Jump[3];                     // 0
     unsigned char  OEMName[8];                  // 3
     unsigned short BytesPerSector;              // 11
     unsigned char  SectorsPerCluster;           // 13