[TCPSVCS] Remove hardcoded C drive letter (#800)
[reactos.git] / base / setup / usetup / bootsup.c
index 183641c..37cd680 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS text-mode setup
- * FILE:            subsys/system/usetup/bootsup.c
+ * FILE:            base/setup/usetup/bootsup.c
  * PURPOSE:         Bootloader support functions
- * PROGRAMMER:      Eric Kohl
+ * PROGRAMMER:
  */
 
 #include "usetup.h"
@@ -78,6 +78,20 @@ typedef struct _FAT32_BOOTSECTOR
     USHORT             BootSectorMagic;                        // 0xAA55
 
 } FAT32_BOOTSECTOR, *PFAT32_BOOTSECTOR;
+
+typedef struct _BTRFS_BOOTSECTOR
+{
+    UCHAR JumpBoot[3];
+    UCHAR ChunkMapSize;
+    UCHAR BootDrive;
+    ULONGLONG PartitionStartLBA;
+    UCHAR Fill[1521]; // 1536 - 15
+    USHORT BootSectorMagic;
+} BTRFS_BOOTSECTOR, *PBTRFS_BOOTSECTOR;
+C_ASSERT(sizeof(BTRFS_BOOTSECTOR) == 3 * 512);
+
+// TODO: Add more bootsector structures!
+
 #include <poppack.h>
 
 extern PPARTLIST PartitionList;
@@ -93,8 +107,7 @@ CreateCommonFreeLoaderSections(
     PINICACHESECTION IniSection;
 
     /* Create "FREELOADER" section */
-    IniSection = IniCacheAppendSection(IniCache,
-                                       L"FREELOADER");
+    IniSection = IniCacheAppendSection(IniCache, L"FREELOADER");
 
 #if DBG
     if (IsUnattendedSetup)
@@ -288,125 +301,9 @@ CreateCommonFreeLoaderSections(
                       L"Seconds until highlighted choice will be started automatically:   ");
 }
 
-
-NTSTATUS
-CreateFreeLoaderIniForDos(
-    PWCHAR IniPath,
-    PWCHAR ArcPath)
-{
-    PINICACHE IniCache;
-    PINICACHESECTION IniSection;
-
-    IniCache = IniCacheCreate();
-
-    CreateCommonFreeLoaderSections(IniCache);
-
-    /* Create "Operating Systems" section */
-    IniSection = IniCacheAppendSection(IniCache, L"Operating Systems");
-
-    /* REACTOS=ReactOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"ReactOS",
-                      L"\"ReactOS\"");
-
-    /* ReactOS_Debug="ReactOS (Debug)" */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"ReactOS_Debug",
-                      L"\"ReactOS (Debug)\"");
-
-    /* DOS=Dos/Windows */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"DOS",
-                      L"\"DOS/Windows\"");
-
-    /* Create "ReactOS" section */
-    IniSection = IniCacheAppendSection(IniCache, L"ReactOS");
-
-    /* BootType=ReactOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootType",
-                      L"ReactOS");
-
-    /* SystemPath=<ArcPath> */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"SystemPath",
-                      ArcPath);
-
-    /* Create "ReactOS_Debug" section */
-    IniSection = IniCacheAppendSection(IniCache, L"ReactOS_Debug");
-
-    /* BootType=ReactOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootType",
-                      L"ReactOS");
-
-    /* SystemPath=<ArcPath> */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"SystemPath",
-                      ArcPath);
-
-    /* Options=/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"Options",
-                      L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS");
-
-    /* Create "DOS" section */
-    IniSection = IniCacheAppendSection(IniCache,
-                                       L"DOS");
-
-    /* BootType=BootSector */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootType",
-                      L"BootSector");
-
-    /* BootDrive=hd0 */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootDrive",
-                      L"hd0");
-
-    /* BootPartition=1 */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootPartition",
-                      L"1");
-
-    /* BootSector=BOOTSECT.DOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootSectorFile",
-                      L"BOOTSECT.DOS");
-
-    IniCacheSave(IniCache, IniPath);
-    IniCacheDestroy(IniCache);
-
-    return STATUS_SUCCESS;
-}
-
-
+static
 NTSTATUS
-CreateFreeLoaderEntry(
+CreateNTOSEntry(
     PINICACHE IniCache,
     PINICACHESECTION OSSection,
     PWCHAR Section,
@@ -441,7 +338,7 @@ CreateFreeLoaderEntry(
                       L"SystemPath",
                       ArcPath);
 
-    /* Options=*/
+    /* Options= */
     IniCacheInsertKey(IniSection,
                       NULL,
                       INSERT_LAST,
@@ -451,81 +348,172 @@ CreateFreeLoaderEntry(
     return STATUS_SUCCESS;
 }
 
-NTSTATUS
-CreateFreeLoaderIniForReactos(
-    PWCHAR IniPath,
+static
+VOID
+CreateFreeLoaderReactOSEntries(
+    PINICACHE IniCache,
     PWCHAR ArcPath)
 {
-    PINICACHE IniCache;
     PINICACHESECTION IniSection;
 
-    IniCache = IniCacheCreate();
-
-    CreateCommonFreeLoaderSections(IniCache);
-
     /* Create "Operating Systems" section */
     IniSection = IniCacheAppendSection(IniCache, L"Operating Systems");
 
     /* ReactOS */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS", L"\"ReactOS\"",
-                          L"Windows2003", ArcPath,
-                          L"");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS", L"\"ReactOS\"",
+                    L"Windows2003", ArcPath,
+                    L"");
 
     /* ReactOS_Debug */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_Debug", L"\"ReactOS (Debug)\"",
-                          L"Windows2003", ArcPath,
-                          L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS");
-
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_Debug", L"\"ReactOS (Debug)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS");
+#ifdef _WINKD_
+    /* ReactOS_VBoxDebug */
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_VBoxDebug", L"\"ReactOS (VBoxDebug)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=VBOX /SOS");
+#endif
 #if DBG
 #ifndef _WINKD_
     /* ReactOS_KdSerial */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_KdSerial", L"\"ReactOS (RosDbg)\"",
-                          L"Windows2003", ArcPath,
-                          L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /KDSERIAL");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_KdSerial", L"\"ReactOS (RosDbg)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /KDSERIAL");
 #endif
 
     /* ReactOS_Screen */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_Screen", L"\"ReactOS (Screen)\"",
-                          L"Windows2003", ArcPath,
-                          L"/DEBUG /DEBUGPORT=SCREEN /SOS");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_Screen", L"\"ReactOS (Screen)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=SCREEN /SOS");
 
     /* ReactOS_LogFile */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_LogFile", L"\"ReactOS (Log file)\"",
-                          L"Windows2003", ArcPath,
-                          L"/DEBUG /DEBUGPORT=FILE /SOS");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_LogFile", L"\"ReactOS (Log file)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=FILE /SOS");
 
     /* ReactOS_Ram */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_Ram", L"\"ReactOS (RAM Disk)\"",
-                          L"Windows2003", L"ramdisk(0)\\ReactOS",
-                          L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /RDIMAGEPATH=reactos.img /RDIMAGEOFFSET=32256");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_Ram", L"\"ReactOS (RAM Disk)\"",
+                    L"Windows2003", L"ramdisk(0)\\ReactOS",
+                    L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /RDPATH=reactos.img /RDIMAGEOFFSET=32256");
 
     /* ReactOS_EMS */
-    CreateFreeLoaderEntry(IniCache, IniSection,
-                          L"ReactOS_EMS", L"\"ReactOS (Emergency Management Services)\"",
-                          L"Windows2003", ArcPath,
-                          L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /redirect=com2 /redirectbaudrate=115200");
+    CreateNTOSEntry(IniCache, IniSection,
+                    L"ReactOS_EMS", L"\"ReactOS (Emergency Management Services)\"",
+                    L"Windows2003", ArcPath,
+                    L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /redirect=com2 /redirectbaudrate=115200");
 #endif
+}
+
+static
+NTSTATUS
+CreateFreeLoaderIniForReactOS(
+    PWCHAR IniPath,
+    PWCHAR ArcPath)
+{
+    PINICACHE IniCache;
+
+    /* Initialize the INI file */
+    IniCache = IniCacheCreate();
+
+    /* Create the common FreeLdr sections */
+    CreateCommonFreeLoaderSections(IniCache);
+
+    /* Add the ReactOS entries */
+    CreateFreeLoaderReactOSEntries(IniCache, ArcPath);
 
-    /* Save the ini file */
+    /* Save the INI file */
     IniCacheSave(IniCache, IniPath);
     IniCacheDestroy(IniCache);
 
     return STATUS_SUCCESS;
 }
 
+static
+NTSTATUS
+CreateFreeLoaderIniForReactOSAndBootSector(
+    IN PCWSTR IniPath,
+    IN PCWSTR ArcPath,
+    IN PCWSTR Section,
+    IN PCWSTR Description,
+    IN PCWSTR BootDrive,
+    IN PCWSTR BootPartition,
+    IN PCWSTR BootSector)
+{
+    PINICACHE IniCache;
+    PINICACHESECTION IniSection;
+
+    /* Initialize the INI file */
+    IniCache = IniCacheCreate();
+
+    /* Create the common FreeLdr sections */
+    CreateCommonFreeLoaderSections(IniCache);
+
+    /* Add the ReactOS entries */
+    CreateFreeLoaderReactOSEntries(IniCache, (PWCHAR)ArcPath);
+
+    /* Get "Operating Systems" section */
+    IniSection = IniCacheGetSection(IniCache, L"Operating Systems");
+
+    /* Insert entry into "Operating Systems" section */
+    IniCacheInsertKey(IniSection,
+                      NULL,
+                      INSERT_LAST,
+                      (PWCHAR)Section,
+                      (PWCHAR)Description);
+
+    /* Create new section */
+    IniSection = IniCacheAppendSection(IniCache, (PWCHAR)Section);
+
+    /* BootType=BootSector */
+    IniCacheInsertKey(IniSection,
+                      NULL,
+                      INSERT_LAST,
+                      L"BootType",
+                      L"BootSector");
+
+    /* BootDrive= */
+    IniCacheInsertKey(IniSection,
+                      NULL,
+                      INSERT_LAST,
+                      L"BootDrive",
+                      (PWCHAR)BootDrive);
+
+    /* BootPartition= */
+    IniCacheInsertKey(IniSection,
+                      NULL,
+                      INSERT_LAST,
+                      L"BootPartition",
+                      (PWCHAR)BootPartition);
+
+    /* BootSector= */
+    IniCacheInsertKey(IniSection,
+                      NULL,
+                      INSERT_LAST,
+                      L"BootSectorFile",
+                      (PWCHAR)BootSector);
+
+    /* Save the INI file */
+    IniCacheSave(IniCache, (PWCHAR)IniPath);
+    IniCacheDestroy(IniCache);
+
+    return STATUS_SUCCESS;
+}
 
+static
 NTSTATUS
 UpdateFreeLoaderIni(
     PWCHAR IniPath,
     PWCHAR ArcPath)
 {
-    UNICODE_STRING Name;
+    NTSTATUS Status;
     PINICACHE IniCache;
     PINICACHESECTION IniSection;
     PINICACHESECTION OsIniSection;
@@ -535,11 +523,8 @@ UpdateFreeLoaderIni(
     WCHAR SectionName2[200];
     PWCHAR KeyData;
     ULONG i,j;
-    NTSTATUS Status;
-
-    RtlInitUnicodeString(&Name, IniPath);
 
-    Status = IniCacheLoad(&IniCache, &Name, FALSE);
+    Status = IniCacheLoad(&IniCache, IniPath, FALSE);
     if (!NT_SUCCESS(Status))
         return Status;
 
@@ -576,18 +561,19 @@ UpdateFreeLoaderIni(
             wcscpy(SectionName2, KeyData);
         }
 
+        /* Search for an existing ReactOS entry */
         OsIniSection = IniCacheGetSection(IniCache, SectionName2);
         if (OsIniSection != NULL)
         {
             BOOLEAN UseExistingEntry = TRUE;
 
-            /* Check BootType */
+            /* Check for boot type "Windows2003" */
             Status = IniCacheGetKey(OsIniSection, L"BootType", &KeyData);
             if (NT_SUCCESS(Status))
             {
                 if ((KeyData == NULL) ||
-                        ( (_wcsicmp(KeyData, L"ReactOS") != 0) &&
-                          (_wcsicmp(KeyData, L"\"ReactOS\"") != 0) ))
+                    ( (_wcsicmp(KeyData, L"Windows2003") != 0) &&
+                      (_wcsicmp(KeyData, L"\"Windows2003\"") != 0) ))
                 {
                     /* This is not a ReactOS entry */
                     UseExistingEntry = FALSE;
@@ -600,17 +586,17 @@ UpdateFreeLoaderIni(
 
             if (UseExistingEntry)
             {
-                /* BootType is ReactOS. Now check SystemPath */
+                /* BootType is Windows2003. Now check SystemPath. */
                 Status = IniCacheGetKey(OsIniSection, L"SystemPath", &KeyData);
                 if (NT_SUCCESS(Status))
                 {
-                    swprintf(SystemPath, L"\"%S\"", ArcPath);
+                    swprintf(SystemPath, L"\"%s\"", ArcPath);
                     if ((KeyData == NULL) ||
-                            ((_wcsicmp(KeyData, ArcPath) != 0) &&
-                             (_wcsicmp(KeyData, SystemPath) != 0) ))
+                        (_wcsicmp(KeyData, ArcPath) != 0) &&
+                          (_wcsicmp(KeyData, SystemPath) != 0) ))
                     {
-                        /* This entry is a ReactOS entry, but the SystemRoot does not
-                           match the one we are looking for */
+                        /* This entry is a ReactOS entry, but the SystemRoot
+                           does not match the one we are looking for. */
                         UseExistingEntry = FALSE;
                     }
                 }
@@ -632,29 +618,11 @@ UpdateFreeLoaderIni(
         i++;
     }
 
-    /* <SectionName>=<OsName> */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      SectionName,
-                      OsName);
-
-    /* Create <SectionName> section */
-    IniSection = IniCacheAppendSection(IniCache, SectionName);
-
-    /* BootType=ReactOS */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"BootType",
-                      L"ReactOS");
-
-    /* SystemPath=<ArcPath> */
-    IniCacheInsertKey(IniSection,
-                      NULL,
-                      INSERT_LAST,
-                      L"SystemPath",
-                      ArcPath);
+    /* Create a new "ReactOS" entry */
+    CreateNTOSEntry(IniCache, IniSection,
+                    SectionName, OsName,
+                    L"Windows2003", ArcPath,
+                    L"");
 
     IniCacheSave(IniCache, IniPath);
     IniCacheDestroy(IniCache);
@@ -662,21 +630,95 @@ UpdateFreeLoaderIni(
     return STATUS_SUCCESS;
 }
 
+BOOLEAN
+IsThereAValidBootSector(PWSTR RootPath)
+{
+    /*
+     * We first demand that the bootsector has a valid signature at its end.
+     * We then check the first 3 bytes (as a ULONG) of the bootsector for a
+     * potential "valid" instruction (the BIOS starts execution of the bootsector
+     * at its beginning). Currently this criterium is that this ULONG must be
+     * non-zero. If both these tests pass, then the bootsector is valid; otherwise
+     * it is invalid and certainly needs to be overwritten.
+     */
+    BOOLEAN IsValid = FALSE;
+    NTSTATUS Status;
+    UNICODE_STRING Name;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+    HANDLE FileHandle;
+    LARGE_INTEGER FileOffset;
+    PUCHAR BootSector;
+    ULONG Instruction;
+
+    /* Allocate buffer for bootsector */
+    BootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+    if (BootSector == NULL)
+        return FALSE; // STATUS_INSUFFICIENT_RESOURCES;
+
+    /* Read current boot sector into buffer */
+    RtlInitUnicodeString(&Name, RootPath);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        GENERIC_READ | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        0,
+                        FILE_SYNCHRONOUS_IO_NONALERT);
+    if (!NT_SUCCESS(Status))
+        goto Quit;
+
+    RtlZeroMemory(BootSector, SECTORSIZE);
+
+    FileOffset.QuadPart = 0ULL;
+    Status = NtReadFile(FileHandle,
+                        NULL,
+                        NULL,
+                        NULL,
+                        &IoStatusBlock,
+                        BootSector,
+                        SECTORSIZE,
+                        &FileOffset,
+                        NULL);
+    NtClose(FileHandle);
+    if (!NT_SUCCESS(Status))
+        goto Quit;
+
+    /* Check the instruction; we use a ULONG to read three bytes */
+    Instruction = (*(PULONG)BootSector) & 0x00FFFFFF;
+    IsValid = (Instruction != 0x00000000);
+
+    /* Check the bootsector signature */
+    IsValid &= (*(PUSHORT)(BootSector + 0x1fe) == 0xaa55);
+
+Quit:
+    /* Free the boot sector */
+    RtlFreeHeap(ProcessHeap, 0, BootSector);
+    return IsValid; // Status;
+}
 
 NTSTATUS
-SaveCurrentBootSector(
+SaveBootSector(
     PWSTR RootPath,
-    PWSTR DstPath)
+    PWSTR DstPath,
+    ULONG Length)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PUCHAR BootSector;
 
     /* Allocate buffer for bootsector */
-    BootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+    BootSector = RtlAllocateHeap(ProcessHeap, 0, Length);
     if (BootSector == NULL)
         return STATUS_INSUFFICIENT_RESOURCES;
 
@@ -701,14 +743,15 @@ SaveCurrentBootSector(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
                         NULL,
                         &IoStatusBlock,
                         BootSector,
-                        SECTORSIZE,
-                        NULL,
+                        Length,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -749,36 +792,37 @@ SaveCurrentBootSector(
                          NULL,
                          &IoStatusBlock,
                          BootSector,
-                         SECTORSIZE,
+                         Length,
                          NULL,
                          NULL);
     NtClose(FileHandle);
 
-    /* Free the new boot sector */
+    /* Free the boot sector */
     RtlFreeHeap(ProcessHeap, 0, BootSector);
 
     return Status;
 }
 
-
+static
 NTSTATUS
 InstallFat16BootCodeToFile(
     PWSTR SrcPath,
     PWSTR DstPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PFAT_BOOTSECTOR OrigBootSector;
     PFAT_BOOTSECTOR NewBootSector;
 
     /* Allocate buffer for original bootsector */
     OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
     if (OrigBootSector == NULL)
-        return STATUS_INSUFFICIENT_RESOURCES ;
+        return STATUS_INSUFFICIENT_RESOURCES;
 
     /* Read current boot sector into buffer */
     RtlInitUnicodeString(&Name, RootPath);
@@ -801,6 +845,7 @@ InstallFat16BootCodeToFile(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
@@ -808,7 +853,7 @@ InstallFat16BootCodeToFile(
                         &IoStatusBlock,
                         OrigBootSector,
                         SECTORSIZE,
-                        NULL,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -847,6 +892,7 @@ InstallFat16BootCodeToFile(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
@@ -854,7 +900,7 @@ InstallFat16BootCodeToFile(
                         &IoStatusBlock,
                         NewBootSector,
                         SECTORSIZE,
-                        NULL,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -899,9 +945,6 @@ InstallFat16BootCodeToFile(
         return Status;
     }
 
-#if 0
-    FilePosition.QuadPart = 0;
-#endif
     Status = NtWriteFile(FileHandle,
                          NULL,
                          NULL,
@@ -919,21 +962,21 @@ InstallFat16BootCodeToFile(
     return Status;
 }
 
-
+static
 NTSTATUS
 InstallFat32BootCodeToFile(
     PWSTR SrcPath,
     PWSTR DstPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PFAT32_BOOTSECTOR OrigBootSector;
     PFAT32_BOOTSECTOR NewBootSector;
-    LARGE_INTEGER FileOffset;
 
     /* Allocate buffer for original bootsector */
     OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
@@ -1126,11 +1169,12 @@ InstallMbrBootCodeToDisk(
     PWSTR SrcPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PPARTITION_SECTOR OrigBootSector;
     PPARTITION_SECTOR NewBootSector;
 
@@ -1163,14 +1207,15 @@ InstallMbrBootCodeToDisk(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
                         NULL,
                         &IoStatusBlock,
                         OrigBootSector,
-                        SECTORSIZE,
-                        NULL,
+                        sizeof(PARTITION_SECTOR),
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -1228,10 +1273,13 @@ InstallMbrBootCodeToDisk(
         return Status;
     }
 
-    /* Copy partition table from old MBR to new */
-    RtlCopyMemory (&NewBootSector->Signature,
-                   &OrigBootSector->Signature,
-                   sizeof(PARTITION_SECTOR) - offsetof(PARTITION_SECTOR, Signature) /* Length of partition table */);
+    /*
+     * Copy the disk signature, the reserved fields and
+     * the partition table from the old MBR to the new one.
+     */
+    RtlCopyMemory(&NewBootSector->Signature,
+                  &OrigBootSector->Signature,
+                  sizeof(PARTITION_SECTOR) - offsetof(PARTITION_SECTOR, Signature) /* Length of partition table */);
 
     /* Free the original boot sector */
     RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1258,14 +1306,15 @@ InstallMbrBootCodeToDisk(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtWriteFile(FileHandle,
                          NULL,
                          NULL,
                          NULL,
                          &IoStatusBlock,
                          NewBootSector,
-                         SECTORSIZE,
-                         NULL,
+                         sizeof(PARTITION_SECTOR),
+                         &FileOffset,
                          NULL);
     NtClose(FileHandle);
 
@@ -1275,17 +1324,18 @@ InstallMbrBootCodeToDisk(
     return Status;
 }
 
-
+static
 NTSTATUS
 InstallFat12BootCodeToFloppy(
     PWSTR SrcPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PFAT_BOOTSECTOR OrigBootSector;
     PFAT_BOOTSECTOR NewBootSector;
 
@@ -1315,6 +1365,7 @@ InstallFat12BootCodeToFloppy(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
@@ -1322,7 +1373,7 @@ InstallFat12BootCodeToFloppy(
                         &IoStatusBlock,
                         OrigBootSector,
                         SECTORSIZE,
-                        NULL,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -1411,9 +1462,7 @@ InstallFat12BootCodeToFloppy(
         return Status;
     }
 
-#if 0
-    FilePosition.QuadPart = 0;
-#endif
+    FileOffset.QuadPart = 0ULL;
     Status = NtWriteFile(FileHandle,
                          NULL,
                          NULL,
@@ -1421,7 +1470,7 @@ InstallFat12BootCodeToFloppy(
                          &IoStatusBlock,
                          NewBootSector,
                          SECTORSIZE,
-                         NULL,
+                         &FileOffset,
                          NULL);
     NtClose(FileHandle);
 
@@ -1431,17 +1480,18 @@ InstallFat12BootCodeToFloppy(
     return Status;
 }
 
-
+static
 NTSTATUS
 InstallFat16BootCodeToDisk(
     PWSTR SrcPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PFAT_BOOTSECTOR OrigBootSector;
     PFAT_BOOTSECTOR NewBootSector;
 
@@ -1471,6 +1521,7 @@ InstallFat16BootCodeToDisk(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
@@ -1478,7 +1529,7 @@ InstallFat16BootCodeToDisk(
                         &IoStatusBlock,
                         OrigBootSector,
                         SECTORSIZE,
-                        NULL,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -1488,9 +1539,7 @@ InstallFat16BootCodeToDisk(
     }
 
     /* Allocate buffer for new bootsector */
-    NewBootSector = RtlAllocateHeap(ProcessHeap,
-                                    0,
-                                    SECTORSIZE);
+    NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
     if (NewBootSector == NULL)
     {
         RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1542,8 +1591,6 @@ InstallFat16BootCodeToDisk(
            FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
            FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
 
-    NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
-
     /* Free the original boot sector */
     RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
 
@@ -1569,9 +1616,7 @@ InstallFat16BootCodeToDisk(
         return Status;
     }
 
-#if 0
-    FilePosition.QuadPart = 0;
-#endif
+    FileOffset.QuadPart = 0ULL;
     Status = NtWriteFile(FileHandle,
                          NULL,
                          NULL,
@@ -1579,7 +1624,7 @@ InstallFat16BootCodeToDisk(
                          &IoStatusBlock,
                          NewBootSector,
                          SECTORSIZE,
-                         NULL,
+                         &FileOffset,
                          NULL);
     NtClose(FileHandle);
 
@@ -1589,20 +1634,20 @@ InstallFat16BootCodeToDisk(
     return Status;
 }
 
-
+static
 NTSTATUS
 InstallFat32BootCodeToDisk(
     PWSTR SrcPath,
     PWSTR RootPath)
 {
+    NTSTATUS Status;
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
     HANDLE FileHandle;
-    NTSTATUS Status;
+    LARGE_INTEGER FileOffset;
     PFAT32_BOOTSECTOR OrigBootSector;
     PFAT32_BOOTSECTOR NewBootSector;
-    LARGE_INTEGER FileOffset;
     USHORT BackupBootSector;
 
     /* Allocate buffer for original bootsector */
@@ -1631,6 +1676,7 @@ InstallFat32BootCodeToDisk(
         return Status;
     }
 
+    FileOffset.QuadPart = 0ULL;
     Status = NtReadFile(FileHandle,
                         NULL,
                         NULL,
@@ -1638,7 +1684,7 @@ InstallFat32BootCodeToDisk(
                         &IoStatusBlock,
                         OrigBootSector,
                         SECTORSIZE,
-                        NULL,
+                        &FileOffset,
                         NULL);
     NtClose(FileHandle);
     if (!NT_SUCCESS(Status))
@@ -1701,8 +1747,6 @@ InstallFat32BootCodeToDisk(
            FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
            FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
 
-    NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
-
     /* Get the location of the backup boot sector */
     BackupBootSector = OrigBootSector->BackupBootSector;
 
@@ -1795,6 +1839,240 @@ InstallFat32BootCodeToDisk(
     return Status;
 }
 
+static
+NTSTATUS
+InstallBtrfsBootCodeToDisk(
+    PWSTR SrcPath,
+    PWSTR RootPath)
+{
+    NTSTATUS Status;
+    UNICODE_STRING Name;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+    HANDLE FileHandle;
+    LARGE_INTEGER FileOffset;
+//  PEXT2_BOOTSECTOR OrigBootSector;
+    PBTRFS_BOOTSECTOR NewBootSector;
+    // USHORT BackupBootSector;
+    PARTITION_INFORMATION_EX PartInfo;
+
+#if 0
+    /* Allocate buffer for original bootsector */
+    OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+    if (OrigBootSector == NULL)
+        return STATUS_INSUFFICIENT_RESOURCES;
+
+    /* Read current boot sector into buffer */
+    RtlInitUnicodeString(&Name, RootPath);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        GENERIC_READ | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        0,
+                        FILE_SYNCHRONOUS_IO_NONALERT);
+    if (!NT_SUCCESS(Status))
+    {
+        RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+        return Status;
+    }
+
+    FileOffset.QuadPart = 0ULL;
+    Status = NtReadFile(FileHandle,
+                        NULL,
+                        NULL,
+                        NULL,
+                        &IoStatusBlock,
+                        OrigBootSector,
+                        SECTORSIZE,
+                        &FileOffset,
+                        NULL);
+    NtClose(FileHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+        return Status;
+    }
+#endif
+
+    /* Allocate buffer for new bootsector */
+    NewBootSector = RtlAllocateHeap(ProcessHeap, 0, sizeof(BTRFS_BOOTSECTOR));
+    if (NewBootSector == NULL)
+    {
+        // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    /* Read new bootsector from SrcPath */
+    RtlInitUnicodeString(&Name, SrcPath);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        GENERIC_READ | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        0,
+                        FILE_SYNCHRONOUS_IO_NONALERT);
+    if (!NT_SUCCESS(Status))
+    {
+        // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+        RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+        return Status;
+    }
+
+    Status = NtReadFile(FileHandle,
+                        NULL,
+                        NULL,
+                        NULL,
+                        &IoStatusBlock,
+                        NewBootSector,
+                        sizeof(BTRFS_BOOTSECTOR),
+                        NULL,
+                        NULL);
+    NtClose(FileHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+        RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+        return Status;
+    }
+
+#if 0
+    /* Adjust bootsector (copy a part of the FAT32 BPB) */
+    memcpy(&NewBootSector->OemName,
+           &OrigBootSector->OemName,
+           FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
+           FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
+
+    NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
+
+    /* Get the location of the backup boot sector */
+    BackupBootSector = OrigBootSector->BackupBootSector;
+
+    /* Free the original boot sector */
+    // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+#endif
+
+    /* Write new bootsector to RootPath */
+    RtlInitUnicodeString(&Name, RootPath);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               0,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        GENERIC_WRITE | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        0,
+                        FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
+        RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+        return Status;
+    }
+
+    /* Obtaining partition info and writing it to bootsector */
+    Status = NtDeviceIoControlFile(FileHandle,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   &IoStatusBlock,
+                                   IOCTL_DISK_GET_PARTITION_INFO_EX,
+                                   NULL,
+                                   0,
+                                   &PartInfo,
+                                   sizeof(PartInfo));
+
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("IOCTL_DISK_GET_PARTITION_INFO_EX failed (Status %lx)\n", Status);
+        NtClose(FileHandle);
+        RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+        return Status;
+    }
+
+    NewBootSector->PartitionStartLBA = PartInfo.StartingOffset.QuadPart / SECTORSIZE;
+
+    /* Write sector 0 */
+    FileOffset.QuadPart = 0ULL;
+    Status = NtWriteFile(FileHandle,
+                         NULL,
+                         NULL,
+                         NULL,
+                         &IoStatusBlock,
+                         NewBootSector,
+                         sizeof(BTRFS_BOOTSECTOR),
+                         &FileOffset,
+                         NULL);
+#if 0
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+        NtClose(FileHandle);
+        RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+        return Status;
+    }
+
+    /* Write backup boot sector */
+    if ((BackupBootSector != 0x0000) && (BackupBootSector != 0xFFFF))
+    {
+        FileOffset.QuadPart = (ULONGLONG)((ULONG)BackupBootSector * SECTORSIZE);
+        Status = NtWriteFile(FileHandle,
+                             NULL,
+                             NULL,
+                             NULL,
+                             &IoStatusBlock,
+                             NewBootSector,
+                             SECTORSIZE,
+                             &FileOffset,
+                             NULL);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+            NtClose(FileHandle);
+            RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+            return Status;
+        }
+    }
+
+    /* Write sector 14 */
+    FileOffset.QuadPart = 14 * SECTORSIZE;
+    Status = NtWriteFile(FileHandle,
+                         NULL,
+                         NULL,
+                         NULL,
+                         &IoStatusBlock,
+                         ((PUCHAR)NewBootSector + SECTORSIZE),
+                         SECTORSIZE,
+                         &FileOffset,
+                         NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+    }
+#endif
+    NtClose(FileHandle);
+
+    /* Free the new boot sector */
+    RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+
+    return Status;
+}
 
 static
 NTSTATUS
@@ -1802,12 +2080,12 @@ UnprotectBootIni(
     PWSTR FileName,
     PULONG Attributes)
 {
+    NTSTATUS Status;
     UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
     FILE_BASIC_INFORMATION FileInfo;
     HANDLE FileHandle;
-    NTSTATUS Status;
 
     RtlInitUnicodeString(&Name, FileName);
 
@@ -1869,19 +2147,18 @@ UnprotectBootIni(
     return Status;
 }
 
-
 static
 NTSTATUS
 ProtectBootIni(
     PWSTR FileName,
     ULONG Attributes)
 {
+    NTSTATUS Status;
     UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
     FILE_BASIC_INFORMATION FileInfo;
     HANDLE FileHandle;
-    NTSTATUS Status;
 
     RtlInitUnicodeString(&Name, FileName);
 
@@ -1931,23 +2208,20 @@ ProtectBootIni(
     return Status;
 }
 
-
+static
 NTSTATUS
 UpdateBootIni(
     PWSTR BootIniPath,
     PWSTR EntryName,
     PWSTR EntryValue)
 {
-    UNICODE_STRING Name;
+    NTSTATUS Status;
     PINICACHE Cache = NULL;
     PINICACHESECTION Section = NULL;
-    NTSTATUS Status;
     ULONG FileAttribute;
     PWCHAR OldValue = NULL;
 
-    RtlInitUnicodeString(&Name, BootIniPath);
-
-    Status = IniCacheLoad(&Cache, &Name, FALSE);
+    Status = IniCacheLoad(&Cache, BootIniPath, FALSE);
     if (!NT_SUCCESS(Status))
     {
         return Status;
@@ -1997,28 +2271,7 @@ UpdateBootIni(
     return Status;
 }
 
-
-BOOLEAN
-CheckInstallFatBootcodeToPartition(
-    PUNICODE_STRING SystemRootPath)
-{
-#ifdef __REACTOS__
-    if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") ||
-        DoesFileExist(SystemRootPath->Buffer, L"boot.ini"))
-    {
-        return TRUE;
-    }
-    else if (DoesFileExist(SystemRootPath->Buffer, L"io.sys") ||
-             DoesFileExist(SystemRootPath->Buffer, L"msdos.sys"))
-    {
-        return TRUE;
-    }
-#endif
-
-    return FALSE;
-}
-
-
+static
 NTSTATUS
 InstallFatBootcodeToPartition(
     PUNICODE_STRING SystemRootPath,
@@ -2026,63 +2279,79 @@ InstallFatBootcodeToPartition(
     PUNICODE_STRING DestinationArcPath,
     UCHAR PartitionType)
 {
-#ifdef __REACTOS__
+    NTSTATUS Status;
+    BOOLEAN DoesFreeLdrExist;
     WCHAR SrcPath[MAX_PATH];
     WCHAR DstPath[MAX_PATH];
-    NTSTATUS Status;
 
     /* FAT or FAT32 partition */
     DPRINT("System path: '%wZ'\n", SystemRootPath);
 
-    if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") == TRUE ||
-        DoesFileExist(SystemRootPath->Buffer, L"boot.ini") == TRUE)
+    /* Copy FreeLoader to the system partition, always overwriting the older version */
+    CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\freeldr.sys");
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.sys");
+
+    DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
+    Status = SetupCopyFile(SrcPath, DstPath);
+    if (!NT_SUCCESS(Status))
     {
-        /* Search root directory for 'ntldr' and 'boot.ini'. */
-        DPRINT("Found Microsoft Windows NT/2000/XP boot loader\n");
+        DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
+        return Status;
+    }
+
+    /* Prepare for possibly updating 'freeldr.ini' */
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.ini");
 
-        /* Copy FreeLoader to the boot partition */
-        wcscpy(SrcPath, SourceRootPath->Buffer);
-        wcscat(SrcPath, L"\\loader\\freeldr.sys");
-        wcscpy(DstPath, SystemRootPath->Buffer);
-        wcscat(DstPath, L"\\freeldr.sys");
+    DoesFreeLdrExist = DoesFileExist(NULL, DstPath);
+    if (DoesFreeLdrExist)
+    {
+        /* Update existing 'freeldr.ini' */
+        DPRINT1("Update existing 'freeldr.ini'\n");
 
-        DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
-        Status = SetupCopyFile(SrcPath, DstPath);
+        Status = UpdateFreeLoaderIni(DstPath, DestinationArcPath->Buffer);
         if (!NT_SUCCESS(Status))
         {
-            DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
+            DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
             return Status;
         }
+    }
+
+    /* Check for NT and other bootloaders */
+
+    // FIXME: Check for Vista+ bootloader!
+    /*** Status = FindNTOSBootLoader(PartitionHandle, NtLdr, &Version); ***/
+    /*** Status = FindNTOSBootLoader(PartitionHandle, BootMgr, &Version); ***/
+    if (DoesFileExist_2(SystemRootPath->Buffer, L"NTLDR") == TRUE ||
+        DoesFileExist_2(SystemRootPath->Buffer, L"BOOT.INI") == TRUE)
+    {
+        /* Search root directory for 'NTLDR' and 'BOOT.INI' */
+        DPRINT1("Found Microsoft Windows NT/2000/XP boot loader\n");
 
-        /* Create or update freeldr.ini */
-        if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
+        /* Create or update 'freeldr.ini' */
+        if (DoesFreeLdrExist == FALSE)
         {
             /* Create new 'freeldr.ini' */
             DPRINT1("Create new 'freeldr.ini'\n");
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
+            // CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.ini");
 
-            Status = CreateFreeLoaderIniForReactos(DstPath,
-                                                   DestinationArcPath->Buffer);
+            Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
             if (!NT_SUCCESS(Status))
             {
-                DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
+                DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
                 return Status;
             }
 
-            /* Install new bootcode */
+            /* Install new bootcode into a file */
+            CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"bootsect.ros");
+
             if (PartitionType == PARTITION_FAT32 ||
-                    PartitionType == PARTITION_FAT32_XINT13)
+                PartitionType == PARTITION_FAT32_XINT13)
             {
                 /* Install FAT32 bootcode */
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat32.bin");
-                wcscpy(DstPath, SystemRootPath->Buffer);
-                wcscat(DstPath, L"\\bootsect.ros");
+                CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat32.bin");
 
                 DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, DstPath);
-                Status = InstallFat32BootCodeToFile(SrcPath,
-                                                    DstPath,
+                Status = InstallFat32BootCodeToFile(SrcPath, DstPath,
                                                     SystemRootPath->Buffer);
                 if (!NT_SUCCESS(Status))
                 {
@@ -2093,14 +2362,10 @@ InstallFatBootcodeToPartition(
             else
             {
                 /* Install FAT16 bootcode */
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat.bin");
-                wcscpy(DstPath, SystemRootPath->Buffer);
-                wcscat(DstPath, L"\\bootsect.ros");
+                CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat.bin");
 
                 DPRINT1("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
-                Status = InstallFat16BootCodeToFile(SrcPath,
-                                                    DstPath,
+                Status = InstallFat16BootCodeToFile(SrcPath, DstPath,
                                                     SystemRootPath->Buffer);
                 if (!NT_SUCCESS(Status))
                 {
@@ -2109,26 +2374,12 @@ InstallFatBootcodeToPartition(
                 }
             }
         }
-        else
-        {
-            /* Update existing 'freeldr.ini' */
-            DPRINT1("Update existing 'freeldr.ini'\n");
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
-
-            Status = UpdateFreeLoaderIni(DstPath,
-                                         DestinationArcPath->Buffer);
-            if (!NT_SUCCESS(Status))
-            {
-                DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
-                return Status;
-            }
-        }
 
         /* Update 'boot.ini' */
-        wcscpy(DstPath, SystemRootPath->Buffer);
-        wcscat(DstPath, L"\\boot.ini");
+        CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"boot.ini");
 
+        /* Windows' NTLDR loads an external bootsector file when the specified drive
+           letter is C:, otherwise it will interpret it as a boot DOS path specifier. */
         DPRINT1("Update 'boot.ini': %S\n", DstPath);
         Status = UpdateBootIni(DstPath,
                                L"C:\\bootsect.ros",
@@ -2139,66 +2390,170 @@ InstallFatBootcodeToPartition(
             return Status;
         }
     }
-    else if (DoesFileExist(SystemRootPath->Buffer, L"io.sys") == TRUE ||
-             DoesFileExist(SystemRootPath->Buffer, L"msdos.sys") == TRUE)
+    else
     {
-        /* Search for root directory for 'io.sys' and 'msdos.sys'. */
-        DPRINT1("Found Microsoft DOS or Windows 9x boot loader\n");
+        /* Non-NT bootloaders: install our own bootloader */
 
-        /* Copy FreeLoader to the boot partition */
-        wcscpy(SrcPath, SourceRootPath->Buffer);
-        wcscat(SrcPath, L"\\loader\\freeldr.sys");
-        wcscpy(DstPath, SystemRootPath->Buffer);
-        wcscat(DstPath, L"\\freeldr.sys");
+        PCWSTR Section;
+        PCWSTR Description;
+        PCWSTR BootDrive;
+        PCWSTR BootPartition;
+        PCWSTR BootSector;
 
-        DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
-        Status = SetupCopyFile(SrcPath, DstPath);
-        if (!NT_SUCCESS(Status))
+        /* Search for COMPAQ MS-DOS 1.x (1.11, 1.12, based on MS-DOS 1.25) boot loader */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"IOSYS.COM") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"MSDOS.COM") == TRUE)
         {
-            DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
-            return Status;
+            DPRINT1("Found COMPAQ MS-DOS 1.x (1.11, 1.12) / MS-DOS 1.25 boot loader\n");
+
+            Section       = L"DOS";
+            Description   = L"\"COMPAQ MS-DOS 1.x / MS-DOS 1.25\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.DOS";
+        }
+        else
+        /* Search for Microsoft DOS or Windows 9x boot loader */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"IO.SYS") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"MSDOS.SYS") == TRUE)
+            // WINBOOT.SYS
+        {
+            DPRINT1("Found Microsoft DOS or Windows 9x boot loader\n");
+
+            Section       = L"DOS";
+            Description   = L"\"MS-DOS/Windows\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.DOS";
+        }
+        else
+        /* Search for IBM PC-DOS or DR-DOS 5.x boot loader */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"IBMIO.COM" ) == TRUE || // Some people refer to this file instead of IBMBIO.COM...
+            DoesFileExist_2(SystemRootPath->Buffer, L"IBMBIO.COM") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"IBMDOS.COM") == TRUE)
+        {
+            DPRINT1("Found IBM PC-DOS or DR-DOS 5.x or IBM OS/2 1.0\n");
+
+            Section       = L"DOS";
+            Description   = L"\"IBM PC-DOS or DR-DOS 5.x or IBM OS/2 1.0\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.DOS";
+        }
+        else
+        /* Search for DR-DOS 3.x boot loader */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"DRBIOS.SYS") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"DRBDOS.SYS") == TRUE)
+        {
+            DPRINT1("Found DR-DOS 3.x\n");
+
+            Section       = L"DOS";
+            Description   = L"\"DR-DOS 3.x\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.DOS";
+        }
+        else
+        /* Search for MS OS/2 1.x */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"OS2BOOT.COM") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"OS2BIO.COM" ) == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"OS2DOS.COM" ) == TRUE)
+        {
+            DPRINT1("Found MS OS/2 1.x\n");
+
+            Section       = L"DOS";
+            Description   = L"\"MS OS/2 1.x\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.OS2";
+        }
+        else
+        /* Search for MS or IBM OS/2 */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"OS2BOOT") == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"OS2LDR" ) == TRUE ||
+            DoesFileExist_2(SystemRootPath->Buffer, L"OS2KRNL") == TRUE)
+        {
+            DPRINT1("Found MS/IBM OS/2\n");
+
+            Section       = L"DOS";
+            Description   = L"\"MS/IBM OS/2\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.OS2";
+        }
+        else
+        /* Search for FreeDOS boot loader */
+        if (DoesFileExist_2(SystemRootPath->Buffer, L"kernel.sys") == TRUE)
+        {
+            DPRINT1("Found FreeDOS boot loader\n");
+
+            Section       = L"DOS";
+            Description   = L"\"FreeDOS\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.DOS";
+        }
+        else
+        {
+            /* No or unknown boot loader */
+            DPRINT1("No or unknown boot loader found\n");
+
+            Section       = L"Unknown";
+            Description   = L"\"Unknown Operating System\"";
+            BootDrive     = L"hd0";
+            BootPartition = L"1";
+            BootSector    = L"BOOTSECT.OLD";
         }
 
         /* Create or update 'freeldr.ini' */
-        if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
+        if (DoesFreeLdrExist == FALSE)
         {
             /* Create new 'freeldr.ini' */
             DPRINT1("Create new 'freeldr.ini'\n");
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
+            // CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.ini");
 
-            Status = CreateFreeLoaderIniForDos(DstPath,
-                                               DestinationArcPath->Buffer);
-            if (!NT_SUCCESS(Status))
+            if (IsThereAValidBootSector(SystemRootPath->Buffer))
             {
-                DPRINT1("CreateFreeLoaderIniForDos() failed (Status %lx)\n", Status);
-                return Status;
-            }
+                Status = CreateFreeLoaderIniForReactOSAndBootSector(
+                             DstPath, DestinationArcPath->Buffer,
+                             Section, Description,
+                             BootDrive, BootPartition, BootSector);
+                if (!NT_SUCCESS(Status))
+                {
+                    DPRINT1("CreateFreeLoaderIniForReactOSAndBootSector() failed (Status %lx)\n", Status);
+                    return Status;
+                }
 
-            /* Save current bootsector as 'BOOTSECT.DOS' */
-            wcscpy(SrcPath, SystemRootPath->Buffer);
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\bootsect.dos");
+                /* Save current bootsector */
+                CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, BootSector);
 
-            DPRINT1("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
-            Status = SaveCurrentBootSector(SrcPath,
-                                           DstPath);
-            if (!NT_SUCCESS(Status))
+                DPRINT1("Save bootsector: %S ==> %S\n", SystemRootPath->Buffer, DstPath);
+                Status = SaveBootSector(SystemRootPath->Buffer, DstPath, SECTORSIZE);
+                if (!NT_SUCCESS(Status))
+                {
+                    DPRINT1("SaveBootSector() failed (Status %lx)\n", Status);
+                    return Status;
+                }
+            }
+            else
             {
-                DPRINT1("SaveCurrentBootSector() failed (Status %lx)\n", Status);
-                return Status;
+                Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
+                if (!NT_SUCCESS(Status))
+                {
+                    DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
+                    return Status;
+                }
             }
 
-            /* Install new bootsector */
+            /* Install new bootsector on the disk */
             if (PartitionType == PARTITION_FAT32 ||
-                    PartitionType == PARTITION_FAT32_XINT13)
+                PartitionType == PARTITION_FAT32_XINT13)
             {
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat32.bin");
+                /* Install FAT32 bootcode */
+                CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat32.bin");
 
                 DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
-                Status = InstallFat32BootCodeToDisk(SrcPath,
-                                                    SystemRootPath->Buffer);
+                Status = InstallFat32BootCodeToDisk(SrcPath, SystemRootPath->Buffer);
                 if (!NT_SUCCESS(Status))
                 {
                     DPRINT1("InstallFat32BootCodeToDisk() failed (Status %lx)\n", Status);
@@ -2207,12 +2562,11 @@ InstallFatBootcodeToPartition(
             }
             else
             {
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat.bin");
+                /* Install FAT16 bootcode */
+                CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat.bin");
 
-                DPRINT1("Install FAT bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
-                Status = InstallFat16BootCodeToDisk(SrcPath,
-                                                    SystemRootPath->Buffer);
+                DPRINT1("Install FAT16 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
+                Status = InstallFat16BootCodeToDisk(SrcPath, SystemRootPath->Buffer);
                 if (!NT_SUCCESS(Status))
                 {
                     DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
@@ -2220,120 +2574,120 @@ InstallFatBootcodeToPartition(
                 }
             }
         }
-        else
-        {
-            /* Update existing 'freeldr.ini' */
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
-
-            Status = UpdateFreeLoaderIni(DstPath, DestinationArcPath->Buffer);
-            if (!NT_SUCCESS(Status))
-            {
-                DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
-                return Status;
-            }
-        }
     }
-    else
+
+    return STATUS_SUCCESS;
+}
+
+static
+NTSTATUS
+InstallBtrfsBootcodeToPartition(
+    PUNICODE_STRING SystemRootPath,
+    PUNICODE_STRING SourceRootPath,
+    PUNICODE_STRING DestinationArcPath,
+    UCHAR PartitionType)
+{
+    NTSTATUS Status;
+    BOOLEAN DoesFreeLdrExist;
+    WCHAR SrcPath[MAX_PATH];
+    WCHAR DstPath[MAX_PATH];
+
+    /* BTRFS partition */
+    DPRINT("System path: '%wZ'\n", SystemRootPath);
+
+    /* Copy FreeLoader to the system partition, always overwriting the older version */
+    CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\freeldr.sys");
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.sys");
+
+    DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
+    Status = SetupCopyFile(SrcPath, DstPath);
+    if (!NT_SUCCESS(Status))
     {
-        /* No or unknown boot loader */
-        DPRINT1("No or unknown boot loader found\n");
+        DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
+        return Status;
+    }
 
-        /* Copy FreeLoader to the boot partition */
-        wcscpy(SrcPath, SourceRootPath->Buffer);
-        wcscat(SrcPath, L"\\loader\\freeldr.sys");
-        wcscpy(DstPath, SystemRootPath->Buffer);
-        wcscat(DstPath, L"\\freeldr.sys");
+    /* Prepare for possibly copying 'freeldr.ini' */
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"freeldr.ini");
 
-        DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
-        Status = SetupCopyFile(SrcPath, DstPath);
+    DoesFreeLdrExist = DoesFileExist(NULL, DstPath);
+    if (DoesFreeLdrExist)
+    {
+        /* Update existing 'freeldr.ini' */
+        DPRINT1("Update existing 'freeldr.ini'\n");
+
+        Status = UpdateFreeLoaderIni(DstPath, DestinationArcPath->Buffer);
         if (!NT_SUCCESS(Status))
         {
-            DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
+            DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
             return Status;
         }
+    }
 
-        /* Create or update 'freeldr.ini' */
-        if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
+    /* Check for *nix bootloaders */
+
+    /* Create or update 'freeldr.ini' */
+    if (DoesFreeLdrExist == FALSE)
+    {
+        /* Create new 'freeldr.ini' */
+        DPRINT1("Create new 'freeldr.ini'\n");
+        CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"\\freeldr.ini");
+
+        /* Certainly SysLinux, GRUB, LILO... or an unknown boot loader */
+        DPRINT1("*nix or unknown boot loader found\n");
+
+        if (IsThereAValidBootSector(SystemRootPath->Buffer))
         {
-            /* Create new freeldr.ini */
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
+            PCWSTR BootSector = L"BOOTSECT.OLD";
 
-            DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
-            Status = CreateFreeLoaderIniForReactos(DstPath,
-                                                   DestinationArcPath->Buffer);
+            Status = CreateFreeLoaderIniForReactOSAndBootSector(
+                         DstPath, DestinationArcPath->Buffer,
+                         L"Linux", L"\"Linux\"",
+                         L"hd0", L"1", BootSector);
             if (!NT_SUCCESS(Status))
             {
-                DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
+                DPRINT1("CreateFreeLoaderIniForReactOSAndBootSector() failed (Status %lx)\n", Status);
                 return Status;
             }
 
-            /* Save current bootsector as 'BOOTSECT.OLD' */
-            wcscpy(SrcPath, SystemRootPath->Buffer);
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\bootsect.old");
+            /* Save current bootsector */
+            CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, BootSector);
 
-            DPRINT("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
-            Status = SaveCurrentBootSector(SrcPath,
-                                           DstPath);
+            DPRINT1("Save bootsector: %S ==> %S\n", SystemRootPath->Buffer, DstPath);
+            Status = SaveBootSector(SystemRootPath->Buffer, DstPath, sizeof(BTRFS_BOOTSECTOR));
             if (!NT_SUCCESS(Status))
             {
-                DPRINT1("SaveCurrentBootSector() failed (Status %lx)\n", Status);
+                DPRINT1("SaveBootSector() failed (Status %lx)\n", Status);
                 return Status;
             }
-
-            /* Install new bootsector */
-            if ((PartitionType == PARTITION_FAT32) ||
-                (PartitionType == PARTITION_FAT32_XINT13))
-            {
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat32.bin");
-
-                DPRINT("Install FAT32 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
-                Status = InstallFat32BootCodeToDisk(SrcPath,
-                                                    SystemRootPath->Buffer);
-                if (!NT_SUCCESS(Status))
-                {
-                    DPRINT1("InstallFat32BootCodeToDisk() failed (Status %lx)\n", Status);
-                    return Status;
-                }
-            }
-            else
+        }
+        else
+        {
+            Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
+            if (!NT_SUCCESS(Status))
             {
-                wcscpy(SrcPath, SourceRootPath->Buffer);
-                wcscat(SrcPath, L"\\loader\\fat.bin");
-
-                DPRINT("Install FAT bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
-                Status = InstallFat16BootCodeToDisk(SrcPath,
-                                                    SystemRootPath->Buffer);
-                if (!NT_SUCCESS(Status))
-                {
-                    DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
-                    return Status;
-                }
+                DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
+                return Status;
             }
         }
-        else
+
+        /* Install new bootsector on the disk */
+        // if (PartitionType == PARTITION_EXT2)
         {
-            /* Update existing 'freeldr.ini' */
-            wcscpy(DstPath, SystemRootPath->Buffer);
-            wcscat(DstPath, L"\\freeldr.ini");
+            /* Install BTRFS bootcode */
+            CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\btrfs.bin");
 
-            Status = UpdateFreeLoaderIni(DstPath,
-                                         DestinationArcPath->Buffer);
+            DPRINT1("Install BTRFS bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
+            Status = InstallBtrfsBootCodeToDisk(SrcPath, SystemRootPath->Buffer);
             if (!NT_SUCCESS(Status))
             {
-                DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
+                DPRINT1("InstallBtrfsBootCodeToDisk() failed (Status %lx)\n", Status);
                 return Status;
             }
         }
     }
 
     return STATUS_SUCCESS;
-#else
-    return STATUS_NOT_IMPLEMENTED;
-#endif
 }
 
 
@@ -2344,17 +2698,36 @@ InstallVBRToPartition(
     PUNICODE_STRING DestinationArcPath,
     UCHAR PartitionType)
 {
-    if ((PartitionType == PARTITION_FAT_12) ||
-        (PartitionType == PARTITION_FAT_16) ||
-        (PartitionType == PARTITION_HUGE) ||
-        (PartitionType == PARTITION_XINT13) ||
-        (PartitionType == PARTITION_FAT32) ||
-        (PartitionType == PARTITION_FAT32_XINT13))
-    {
-        return InstallFatBootcodeToPartition(SystemRootPath,
-                                             SourceRootPath,
-                                             DestinationArcPath,
-                                             PartitionType);
+    switch (PartitionType)
+    {
+        case PARTITION_FAT_12:
+        case PARTITION_FAT_16:
+        case PARTITION_HUGE:
+        case PARTITION_XINT13:
+        case PARTITION_FAT32:
+        case PARTITION_FAT32_XINT13:
+        {
+            return InstallFatBootcodeToPartition(SystemRootPath,
+                                                 SourceRootPath,
+                                                 DestinationArcPath,
+                                                 PartitionType);
+        }
+
+        case PARTITION_LINUX:
+        {
+            return InstallBtrfsBootcodeToPartition(SystemRootPath,
+                                                   SourceRootPath,
+                                                   DestinationArcPath,
+                                                   PartitionType);
+        }
+
+        case PARTITION_IFS:
+            DPRINT1("Partitions of type NTFS or HPFS are not supported yet!\n");
+            break;
+
+        default:
+            DPRINT1("PartitionType 0x%02X unknown!\n", PartitionType);
+            break;
     }
 
     return STATUS_UNSUCCESSFUL;
@@ -2366,19 +2739,25 @@ InstallFatBootcodeToFloppy(
     PUNICODE_STRING SourceRootPath,
     PUNICODE_STRING DestinationArcPath)
 {
-#ifdef __REACTOS__
-    UNICODE_STRING FloppyDevice = RTL_CONSTANT_STRING(L"\\Device\\Floppy0");
+    NTSTATUS Status;
+    PFILE_SYSTEM FatFS;
+    UNICODE_STRING FloppyDevice = RTL_CONSTANT_STRING(L"\\Device\\Floppy0\\");
     WCHAR SrcPath[MAX_PATH];
     WCHAR DstPath[MAX_PATH];
-    NTSTATUS Status;
 
     /* Format the floppy first */
-    Status = VfatFormat(&FloppyDevice,
-                        FMIFS_FLOPPY,
-                        NULL,
-                        TRUE,
-                        0,
-                        NULL);
+    FatFS = GetFileSystemByName(L"FAT");
+    if (!FatFS)
+    {
+        DPRINT1("FAT FS non existent on this system?!\n");
+        return STATUS_NOT_SUPPORTED;
+    }
+    Status = FatFS->FormatFunc(&FloppyDevice,
+                               FMIFS_FLOPPY,
+                               NULL,
+                               TRUE,
+                               0,
+                               NULL);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("VfatFormat() failed (Status %lx)\n", Status);
@@ -2386,10 +2765,8 @@ InstallFatBootcodeToFloppy(
     }
 
     /* Copy FreeLoader to the boot partition */
-    wcscpy(SrcPath, SourceRootPath->Buffer);
-    wcscat(SrcPath, L"\\loader\\freeldr.sys");
-
-    wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.sys");
+    CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\freeldr.sys");
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, FloppyDevice.Buffer, L"freeldr.sys");
 
     DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
     Status = SetupCopyFile(SrcPath, DstPath);
@@ -2400,34 +2777,29 @@ InstallFatBootcodeToFloppy(
     }
 
     /* Create new 'freeldr.ini' */
-    wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.ini");
+    CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, FloppyDevice.Buffer, L"freeldr.ini");
 
     DPRINT("Create new 'freeldr.ini'\n");
-    Status = CreateFreeLoaderIniForReactos(DstPath, DestinationArcPath->Buffer);
+    Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
     if (!NT_SUCCESS(Status))
     {
-        DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
+        DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
         return Status;
     }
 
-    /* Install FAT12/16 boosector */
-    wcscpy(SrcPath, SourceRootPath->Buffer);
-    wcscat(SrcPath, L"\\loader\\fat.bin");
-
-    wcscpy(DstPath, L"\\Device\\Floppy0");
+    /* Install FAT12 boosector */
+    CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat.bin");
+    RtlStringCchCopyW(DstPath, ARRAYSIZE(DstPath), FloppyDevice.Buffer);
 
     DPRINT("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
     Status = InstallFat12BootCodeToFloppy(SrcPath, DstPath);
     if (!NT_SUCCESS(Status))
     {
-        DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
+        DPRINT1("InstallFat12BootCodeToFloppy() failed (Status %lx)\n", Status);
         return Status;
     }
 
     return STATUS_SUCCESS;
-#else
-    return STATUS_NOT_IMPLEMENTED;
-#endif
 }
 
 /* EOF */