Get rid of the "old boot method" entries.
[reactos.git] / reactos / base / setup / usetup / bootsup.c
index 49ec7d7..f4f1227 100644 (file)
@@ -12,9 +12,9 @@
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 /*
  * COPYRIGHT:       See COPYING in the top level directory
 
 #define SECTORSIZE 512
 
+#include <pshpack1.h>
+typedef struct _FAT_BOOTSECTOR
+{
+       UCHAR           JumpBoot[3];                            // Jump instruction to boot code
+       CHAR            OemName[8];                                     // "MSWIN4.1" for MS formatted volumes
+       USHORT          BytesPerSector;                         // Bytes per sector
+       UCHAR           SectorsPerCluster;                      // Number of sectors in a cluster
+       USHORT          ReservedSectors;                        // Reserved sectors, usually 1 (the bootsector)
+       UCHAR           NumberOfFats;                           // Number of FAT tables
+       USHORT          RootDirEntries;                         // Number of root directory entries (fat12/16)
+       USHORT          TotalSectors;                           // Number of total sectors on the drive, 16-bit
+       UCHAR           MediaDescriptor;                        // Media descriptor byte
+       USHORT          SectorsPerFat;                          // Sectors per FAT table (fat12/16)
+       USHORT          SectorsPerTrack;                        // Number of sectors in a track
+       USHORT          NumberOfHeads;                          // Number of heads on the disk
+       ULONG           HiddenSectors;                          // Hidden sectors (sectors before the partition start like the partition table)
+       ULONG           TotalSectorsBig;                        // This field is the new 32-bit total count of sectors on the volume
+       UCHAR           DriveNumber;                            // Int 0x13 drive number (e.g. 0x80)
+       UCHAR           Reserved1;                                      // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
+       UCHAR           BootSignature;                          // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
+       ULONG           VolumeSerialNumber;                     // Volume serial number
+       CHAR            VolumeLabel[11];                        // Volume label. This field matches the 11-byte volume label recorded in the root directory
+       CHAR            FileSystemType[8];                      // One of the strings "FAT12   ", "FAT16   ", or "FAT     "
+
+       UCHAR           BootCodeAndData[448];           // The remainder of the boot sector
+
+       USHORT          BootSectorMagic;                        // 0xAA55
+
+} FAT_BOOTSECTOR, *PFAT_BOOTSECTOR;
+
+typedef struct _FAT32_BOOTSECTOR
+{
+       UCHAR           JumpBoot[3];                            // Jump instruction to boot code
+       CHAR            OemName[8];                                     // "MSWIN4.1" for MS formatted volumes
+       USHORT          BytesPerSector;                         // Bytes per sector
+       UCHAR           SectorsPerCluster;                      // Number of sectors in a cluster
+       USHORT          ReservedSectors;                        // Reserved sectors, usually 1 (the bootsector)
+       UCHAR           NumberOfFats;                           // Number of FAT tables
+       USHORT          RootDirEntries;                         // Number of root directory entries (fat12/16)
+       USHORT          TotalSectors;                           // Number of total sectors on the drive, 16-bit
+       UCHAR           MediaDescriptor;                        // Media descriptor byte
+       USHORT          SectorsPerFat;                          // Sectors per FAT table (fat12/16)
+       USHORT          SectorsPerTrack;                        // Number of sectors in a track
+       USHORT          NumberOfHeads;                          // Number of heads on the disk
+       ULONG           HiddenSectors;                          // Hidden sectors (sectors before the partition start like the partition table)
+       ULONG           TotalSectorsBig;                        // This field is the new 32-bit total count of sectors on the volume
+       ULONG           SectorsPerFatBig;                       // This field is the FAT32 32-bit count of sectors occupied by ONE FAT. BPB_FATSz16 must be 0
+       USHORT          ExtendedFlags;                          // Extended flags (fat32)
+       USHORT          FileSystemVersion;                      // File system version (fat32)
+       ULONG           RootDirStartCluster;            // Starting cluster of the root directory (fat32)
+       USHORT          FsInfo;                                         // Sector number of FSINFO structure in the reserved area of the FAT32 volume. Usually 1.
+       USHORT          BackupBootSector;                       // If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
+       UCHAR           Reserved[12];                           // Reserved for future expansion
+       UCHAR           DriveNumber;                            // Int 0x13 drive number (e.g. 0x80)
+       UCHAR           Reserved1;                                      // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
+       UCHAR           BootSignature;                          // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
+       ULONG           VolumeSerialNumber;                     // Volume serial number
+       CHAR            VolumeLabel[11];                        // Volume label. This field matches the 11-byte volume label recorded in the root directory
+       CHAR            FileSystemType[8];                      // Always set to the string "FAT32   "
+
+       UCHAR           BootCodeAndData[420];           // The remainder of the boot sector
+
+       USHORT          BootSectorMagic;                        // 0xAA55
+
+} FAT32_BOOTSECTOR, *PFAT32_BOOTSECTOR;
+#include <poppack.h>
+
+extern PPARTLIST PartitionList;
+
 /* FUNCTIONS ****************************************************************/
 
 
@@ -43,7 +112,7 @@ CreateCommonFreeLoaderSections(PINICACHE IniCache)
   IniSection = IniCacheAppendSection(IniCache,
                                     L"FREELOADER");
 
-#ifdef DBG
+#if DBG
   if (IsUnattendedSetup)
   {
     /* DefaultOS=ReactOS */
@@ -51,7 +120,7 @@ CreateCommonFreeLoaderSections(PINICACHE IniCache)
                    NULL,
                    INSERT_LAST,
                    L"DefaultOS",
-                   L"ReactOS_Debug");
+                   L"ReactOS_KdSerial");
   }
   else
 #endif
@@ -64,7 +133,7 @@ CreateCommonFreeLoaderSections(PINICACHE IniCache)
                    L"ReactOS");
   }
 
-#ifdef DBG
+#if DBG
   if (IsUnattendedSetup)
 #endif
   {
@@ -75,7 +144,7 @@ CreateCommonFreeLoaderSections(PINICACHE IniCache)
                    L"TimeOut",
                    L"0");
   }
-#ifdef DBG
+#if DBG
   else
   {
     /* Timeout=0 or 10 */
@@ -305,7 +374,7 @@ CreateFreeLoaderIniForDos(PWCHAR IniPath,
                    L"SystemPath",
                    ArcPath);
 
-  /* Options=/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS*/
+  /* Options=/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS */
   IniCacheInsertKey(IniSection,
                    NULL,
                    INSERT_LAST,
@@ -351,6 +420,52 @@ CreateFreeLoaderIniForDos(PWCHAR IniPath,
 }
 
 
+NTSTATUS
+CreateFreeLoaderEntry(
+    PINICACHE IniCache,
+    PINICACHESECTION OSSection,
+    PWCHAR Section,
+    PWCHAR Description,
+    PWCHAR BootType,
+    PWCHAR ArcPath,
+    PWCHAR Options)
+{
+    PINICACHESECTION IniSection;
+
+    /* Insert entry into "Operating Systems" section */
+    IniCacheInsertKey(OSSection,
+        NULL,
+        INSERT_LAST,
+        Section,
+        Description);
+
+    /* Create new section */
+    IniSection = IniCacheAppendSection(IniCache, Section);
+
+    /* BootType= */
+    IniCacheInsertKey(IniSection,
+        NULL,
+        INSERT_LAST,
+        L"BootType",
+        BootType);
+
+    /* SystemPath= */
+    IniCacheInsertKey(IniSection,
+        NULL,
+        INSERT_LAST,
+        L"SystemPath",
+        ArcPath);
+
+    /* Options=*/
+    IniCacheInsertKey(IniSection,
+        NULL,
+        INSERT_LAST,
+        L"Options",
+        Options);
+
+    return STATUS_SUCCESS;
+}
+
 NTSTATUS
 CreateFreeLoaderIniForReactos(PWCHAR IniPath,
                              PWCHAR ArcPath)
@@ -366,69 +481,37 @@ CreateFreeLoaderIniForReactos(PWCHAR IniPath,
   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)\"");
-
-  /* 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);
-
-  /* Options=/SOS*/
-  IniCacheInsertKey(IniSection,
-                   NULL,
-                   INSERT_LAST,
-                   L"Options",
-                   L"/SOS");
-
-  /* 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");
+    /* ReactOS */
+    CreateFreeLoaderEntry(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");
+
+#if DBG
+    /* ReactOS_KdSerial */
+    CreateFreeLoaderEntry(IniCache, IniSection,
+        L"ReactOS_KdSerial", L"\"ReactOS (RosDbg)\"",
+        L"Windows2003", ArcPath,
+        L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /KDSERIAL");
+
+    /* ReactOS_LogFile */
+    CreateFreeLoaderEntry(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"ReactOS", L"ramdisk(0)\\ReactOS",
+        L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /RDIMAGEPATH=reactos.img /RDIMAGEOFFSET=32256");
+#endif
 
   /* Save the ini file */
   IniCacheSave(IniCache, IniPath);
@@ -911,7 +994,6 @@ InstallFat32BootCodeToFile(PWSTR SrcPath,
   NtClose(FileHandle);
   if (!NT_SUCCESS(Status))
   {
-CHECKPOINT1;
     RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
     return(Status);
   }
@@ -1069,53 +1151,53 @@ NTSTATUS
 InstallMbrBootCodeToDisk (PWSTR SrcPath,
                          PWSTR RootPath)
 {
-  OBJECT_ATTRIBUTES ObjectAttributes;
-  IO_STATUS_BLOCK IoStatusBlock;
-  UNICODE_STRING Name;
-  HANDLE FileHandle;
-  NTSTATUS Status;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+    UNICODE_STRING Name;
+    HANDLE FileHandle;
+    NTSTATUS Status;
   PPARTITION_SECTOR OrigBootSector;
   PPARTITION_SECTOR NewBootSector;
 
-  /* Allocate buffer for original bootsector */
+    /* Allocate buffer for original bootsector */
   OrigBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
                                                      0,
                                                       sizeof(PARTITION_SECTOR));
   if (OrigBootSector == NULL)
     return(STATUS_INSUFFICIENT_RESOURCES);
 
-  /* Read current boot sector into buffer */
+    /* Read current boot sector into buffer */
   RtlInitUnicodeString(&Name,
                       RootPath);
 
-  InitializeObjectAttributes(&ObjectAttributes,
-                            &Name,
-                            OBJ_CASE_INSENSITIVE,
-                            NULL,
-                            NULL);
-
-  Status = NtOpenFile(&FileHandle,
-                     GENERIC_READ,
-                     &ObjectAttributes,
-                     &IoStatusBlock,
-                     0,
-                     FILE_SYNCHRONOUS_IO_NONALERT);
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        GENERIC_READ,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        0,
+                        FILE_SYNCHRONOUS_IO_NONALERT);
   if (!NT_SUCCESS(Status))
   {
     RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
     return(Status);
   }
 
-  Status = NtReadFile(FileHandle,
-                     NULL,
-                     NULL,
-                     NULL,
-                     &IoStatusBlock,
+    Status = NtReadFile(FileHandle,
+                        NULL,
+                        NULL,
+                        NULL,
+                        &IoStatusBlock,
                      OrigBootSector,
-                     SECTORSIZE,
-                     NULL,
-                     NULL);
-  NtClose(FileHandle);
+                        SECTORSIZE,
+                        NULL,
+                        NULL);
+    NtClose(FileHandle);
   if (!NT_SUCCESS(Status))
   {
     RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1231,11 +1313,12 @@ InstallFat16BootCodeToDisk(PWSTR SrcPath,
   UNICODE_STRING Name;
   HANDLE FileHandle;
   NTSTATUS Status;
-  PUCHAR OrigBootSector;
-  PUCHAR NewBootSector;
+  PFAT_BOOTSECTOR OrigBootSector;
+  PFAT_BOOTSECTOR NewBootSector;
+  PARTITION_INFORMATION *PartInfo;
 
   /* Allocate buffer for original bootsector */
-  OrigBootSector = (PUCHAR)RtlAllocateHeap(ProcessHeap,
+  OrigBootSector = RtlAllocateHeap(ProcessHeap,
                                           0,
                                           SECTORSIZE);
   if (OrigBootSector == NULL)
@@ -1281,7 +1364,7 @@ InstallFat16BootCodeToDisk(PWSTR SrcPath,
 
 
   /* Allocate buffer for new bootsector */
-  NewBootSector = (PUCHAR)RtlAllocateHeap(ProcessHeap,
+  NewBootSector = RtlAllocateHeap(ProcessHeap,
                                          0,
                                          SECTORSIZE);
   if (NewBootSector == NULL)
@@ -1331,9 +1414,12 @@ InstallFat16BootCodeToDisk(PWSTR SrcPath,
   }
 
   /* Adjust bootsector (copy a part of the FAT16 BPB) */
-  memcpy((NewBootSector + 3),
-        (OrigBootSector + 3),
-        59);  /* FAT16 BPB length*/
+  memcpy(&NewBootSector->BytesPerSector,
+         &OrigBootSector->BytesPerSector,
+         51); /* FAT16 BPB length */
+
+  PartInfo = &PartitionList->CurrentPartition->PartInfo[PartitionList->CurrentPartitionNumber];
+  NewBootSector->HiddenSectors = PartInfo->HiddenSectors;
 
   /* Free the original boot sector */
   RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1391,13 +1477,14 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
   UNICODE_STRING Name;
   HANDLE FileHandle;
   NTSTATUS Status;
-  PUCHAR OrigBootSector;
-  PUCHAR NewBootSector;
+  PFAT32_BOOTSECTOR OrigBootSector;
+  PFAT32_BOOTSECTOR NewBootSector;
   LARGE_INTEGER FileOffset;
   USHORT BackupBootSector;
+  PARTITION_INFORMATION *PartInfo;
 
   /* Allocate buffer for original bootsector */
-  OrigBootSector = (PUCHAR)RtlAllocateHeap(ProcessHeap,
+  OrigBootSector = RtlAllocateHeap(ProcessHeap,
                                           0,
                                           SECTORSIZE);
   if (OrigBootSector == NULL)
@@ -1443,7 +1530,7 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
 
 
   /* Allocate buffer for new bootsector (2 sectors) */
-  NewBootSector = (PUCHAR)RtlAllocateHeap(ProcessHeap,
+  NewBootSector = RtlAllocateHeap(ProcessHeap,
                                          0,
                                          2 * SECTORSIZE);
   if (NewBootSector == NULL)
@@ -1493,12 +1580,15 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
   }
 
   /* Adjust bootsector (copy a part of the FAT32 BPB) */
-  memcpy((NewBootSector + 3),
-        (OrigBootSector + 3),
-        87); /* FAT32 BPB length */
+  memcpy(&NewBootSector->BytesPerSector,
+         &OrigBootSector->BytesPerSector,
+         79); /* FAT32 BPB length */
+
+  PartInfo = &PartitionList->CurrentPartition->PartInfo[PartitionList->CurrentPartitionNumber];
+  NewBootSector->HiddenSectors = PartInfo->HiddenSectors;
 
   /* Get the location of the backup boot sector */
-  BackupBootSector = (OrigBootSector[0x33] << 8) + OrigBootSector[0x32];
+  BackupBootSector = OrigBootSector->BackupBootSector;
 
   /* Free the original boot sector */
   RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1574,7 +1664,7 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
                       NULL,
                       NULL,
                       &IoStatusBlock,
-                      (NewBootSector + SECTORSIZE),
+                      ((PUCHAR)NewBootSector + SECTORSIZE),
                       SECTORSIZE,
                       &FileOffset,
                       NULL);
@@ -1826,7 +1916,7 @@ InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
        NTSTATUS Status;
 
        /* FAT or FAT32 partition */
-       DPRINT1("System path: '%wZ'\n", SystemRootPath);
+       DPRINT("System path: '%wZ'\n", SystemRootPath);
 
        if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") == TRUE ||
                DoesFileExist(SystemRootPath->Buffer, L"boot.ini") == TRUE)
@@ -2040,7 +2130,7 @@ InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
                wcscpy(DstPath, SystemRootPath->Buffer);
                wcscat(DstPath, L"\\freeldr.sys");
 
-               DPRINT1("Copy: %S ==> %S\n", SrcPath, DstPath);
+               DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
                Status = SetupCopyFile(SrcPath, DstPath);
                if (!NT_SUCCESS(Status))
                {
@@ -2055,7 +2145,7 @@ InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
                        wcscpy(DstPath, SystemRootPath->Buffer);
                        wcscat(DstPath, L"\\freeldr.ini");
 
-                       DPRINT1("Copy: %S ==> %S\n", SrcPath, DstPath);
+                       DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
                        Status = CreateFreeLoaderIniForReactos(DstPath,
                                DestinationArcPath->Buffer);
                        if (!NT_SUCCESS(Status))
@@ -2069,7 +2159,7 @@ InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
                        wcscpy(DstPath, SystemRootPath->Buffer);
                        wcscat(DstPath, L"\\bootsect.old");
 
-                       DPRINT1("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
+                       DPRINT("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
                        Status = SaveCurrentBootSector(SrcPath,
                                DstPath);
                        if (!NT_SUCCESS(Status))
@@ -2131,6 +2221,28 @@ InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
 #endif
 }
 
+NTSTATUS
+InstallVBRToPartition(PUNICODE_STRING SystemRootPath,
+                      PUNICODE_STRING SourceRootPath,
+                      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);
+    }
+    
+    return STATUS_UNSUCCESSFUL;
+}
+
 
 NTSTATUS
 InstallFatBootcodeToFloppy(PUNICODE_STRING SourceRootPath,