branch update for HEAD-2003050101
[reactos.git] / subsys / system / usetup / bootsup.c
index 77edba3..c9af78c 100644 (file)
@@ -828,8 +828,8 @@ CHECKPOINT1;
         87); /* FAT32 BPB length */
 
   /* Disable the backup boot sector */
-  NewBootSector[0x32] = 0xFF;
-  NewBootSector[0x33] = 0xFF;
+  NewBootSector[0x32] = 0x00;
+  NewBootSector[0x33] = 0x00;
 
   /* Free the original boot sector */
   RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -922,6 +922,165 @@ CHECKPOINT1;
 
 
 NTSTATUS
+InstallMBRBootCodeToDisk(PWSTR SrcPath,
+                          PWSTR RootPath)
+{
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  IO_STATUS_BLOCK IoStatusBlock;
+  UNICODE_STRING Name;
+  HANDLE FileHandle;
+  NTSTATUS Status;
+  PUCHAR OrigBootSector;
+  PUCHAR NewBootSector;
+
+  /* Allocate buffer for original bootsector */
+  OrigBootSector = (PUCHAR)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,
+                     FILE_READ_ACCESS,
+                     &ObjectAttributes,
+                     &IoStatusBlock,
+                     0,
+                     FILE_SYNCHRONOUS_IO_ALERT);
+  if (!NT_SUCCESS(Status))
+  {
+    RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+    return(Status);
+  }
+
+  Status = NtReadFile(FileHandle,
+                     NULL,
+                     NULL,
+                     NULL,
+                     &IoStatusBlock,
+                     OrigBootSector,
+                     SECTORSIZE,
+                     NULL,
+                     NULL);
+  NtClose(FileHandle);
+  if (!NT_SUCCESS(Status))
+  {
+    RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+    return(Status);
+  }
+
+
+  /* Allocate buffer for new bootsector */
+  NewBootSector = (PUCHAR)RtlAllocateHeap(ProcessHeap,
+                                         0,
+                                         SECTORSIZE);
+  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,
+                     FILE_READ_ACCESS,
+                     &ObjectAttributes,
+                     &IoStatusBlock,
+                     0,
+                     FILE_SYNCHRONOUS_IO_ALERT);
+  if (!NT_SUCCESS(Status))
+  {
+    RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+    RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+    return(Status);
+  }
+
+  Status = NtReadFile(FileHandle,
+                     NULL,
+                     NULL,
+                     NULL,
+                     &IoStatusBlock,
+                     NewBootSector,
+                     SECTORSIZE,
+                     NULL,
+                     NULL);
+  NtClose(FileHandle);
+  if (!NT_SUCCESS(Status))
+  {
+    RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+    RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+    return(Status);
+  }
+
+  /* Copy partition table from old MBR to new */
+  memcpy((NewBootSector + 446), (OrigBootSector + 446), 4*16 /* Length of partition table */);
+
+  /* Free the original boot sector */
+  RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+
+  /* Write new bootsector to RootPath */
+  RtlInitUnicodeString(&Name,
+                      RootPath);
+
+  InitializeObjectAttributes(&ObjectAttributes,
+                            &Name,
+                            0,
+                            NULL,
+                            NULL);
+
+  Status = NtCreateFile(&FileHandle,
+                       FILE_WRITE_ACCESS,
+                       &ObjectAttributes,
+                       &IoStatusBlock,
+                       NULL,
+                       FILE_ATTRIBUTE_NORMAL,
+                       0,
+                       FILE_OVERWRITE_IF,
+                       FILE_SYNCHRONOUS_IO_ALERT | FILE_SEQUENTIAL_ONLY,
+                       NULL,
+                       0);
+  if (!NT_SUCCESS(Status))
+  {
+    RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+    return(Status);
+  }
+
+  Status = NtWriteFile(FileHandle,
+                      NULL,
+                      NULL,
+                      NULL,
+                      &IoStatusBlock,
+                      NewBootSector,
+                      SECTORSIZE,
+                      NULL,
+                      NULL);
+  NtClose(FileHandle);
+
+  /* Free the new boot sector */
+  RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+
+  return(Status);
+}
+
+
+NTSTATUS
 InstallFat16BootCodeToDisk(PWSTR SrcPath,
                           PWSTR RootPath)
 {
@@ -1199,7 +1358,7 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
         87); /* FAT32 BPB length */
 
   /* Get the location of the backup boot sector */
-  BackupBootSector = (OrigBootSector[0x33] << 8) + OrigBootSector[0x33];
+  BackupBootSector = (OrigBootSector[0x33] << 8) + OrigBootSector[0x32];
 
   /* Free the original boot sector */
   RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1247,7 +1406,7 @@ InstallFat32BootCodeToDisk(PWSTR SrcPath,
   }
 
   /* Write backup boot sector */
-  if (BackupBootSector != 0xFFFF)
+  if ((BackupBootSector != 0x0000) && (BackupBootSector != 0xFFFF))
   {
     FileOffset.QuadPart = (ULONGLONG)((ULONG)BackupBootSector * SECTORSIZE);
     Status = NtWriteFile(FileHandle,