Author: hbelusca
Date: Wed May 31 02:03:10 2017
New Revision: 74715
URL:
http://svn.reactos.org/svn/reactos?rev=74715&view=rev
Log:
[USETUP]: Try to condense the different InstallFatXYBootSectorToDisk & ToFile
functions into a InstallFatXYBootSector() function that is called by both the ToDisk and
ToFile flavours.
Also, trim any potential trailing path separator from the NT disk partition path before
opening the partition object itself for read/write access.
This commit needs a review, especially concerning the different flags in the NtOpenFile
and NtCreateFile calls.
Modified:
branches/setup_improvements/base/setup/usetup/bootsup.c
Modified: branches/setup_improvements/base/setup/usetup/bootsup.c
URL:
http://svn.reactos.org/svn/reactos/branches/setup_improvements/base/setup/u…
==============================================================================
--- branches/setup_improvements/base/setup/usetup/bootsup.c [iso-8859-1] (original)
+++ branches/setup_improvements/base/setup/usetup/bootsup.c [iso-8859-1] Wed May 31
02:03:10 2017
@@ -3,7 +3,8 @@
* PROJECT: ReactOS text-mode setup
* FILE: base/setup/usetup/bootsup.c
* PURPOSE: Bootloader support functions
- * PROGRAMMER: Eric Kohl
+ * PROGRAMMERS: Eric Kohl
+ * Hermes Belusca-Maito (hermes.belusca(a)sfr.fr)
*/
#include "usetup.h"
@@ -11,6 +12,20 @@
#define NDEBUG
#include <debug.h>
+/*
+ * BIG FIXME!!
+ * ===========
+ *
+ * All that stuff *MUST* go into the fsutil.c module.
+ * Indeed, all that relates to filesystem formatting details and as such
+ * *MUST* be abstracted out from this module (bootsup.c).
+ * However, bootsup.c can still deal with MBR code (actually it'll have
+ * at some point to share or give it to partlist.c, because when we'll
+ * support GPT disks, things will change a bit).
+ * And, bootsup.c can still manage initializing / adding boot entries
+ * into NTLDR and FREELDR, and installing the latter, and saving the old
+ * MBR / boot sectors in files.
+ */
#define SECTORSIZE 512
#include <pshpack1.h>
@@ -90,10 +105,21 @@
#include <poppack.h>
-extern PPARTLIST PartitionList;
+/* End of BIG FIXME!! */
+
/* FUNCTIONS ****************************************************************/
+static VOID
+TrimTrailingPathSeparators_UStr(
+ IN OUT PUNICODE_STRING UnicodeString)
+{
+ while (UnicodeString->Length >= sizeof(WCHAR) &&
+ UnicodeString->Buffer[UnicodeString->Length / sizeof(WCHAR) - 1] ==
OBJ_NAME_PATH_SEPARATOR)
+ {
+ UnicodeString->Length -= sizeof(WCHAR);
+ }
+}
static
VOID
@@ -825,7 +851,8 @@
BOOLEAN
-IsThereAValidBootSector(PWSTR RootPath)
+IsThereAValidBootSector(
+ IN PCWSTR RootPath)
{
/*
* We first demand that the bootsector has a valid signature at its end.
@@ -835,7 +862,76 @@
* 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 RootPartition;
+ 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;
+ RtlZeroMemory(BootSector, SECTORSIZE);
+
+ /* Open the root partition - Remove any trailing backslash if needed */
+ RtlInitUnicodeString(&RootPartition, RootPath);
+ TrimTrailingPathSeparators_UStr(&RootPartition);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &RootPartition,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+
+ Status = NtOpenFile(&FileHandle,
+ GENERIC_READ | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT);
+ if (!NT_SUCCESS(Status))
+ goto Quit;
+
+ /* Read current boot sector into buffer */
+ 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
+SaveBootSector(
+ IN PCWSTR RootPath,
+ IN PCWSTR DstPath,
+ IN ULONG Length)
+{
NTSTATUS Status;
UNICODE_STRING Name;
OBJECT_ATTRIBUTES ObjectAttributes;
@@ -843,15 +939,15 @@
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
PUCHAR BootSector;
- ULONG Instruction;
/* Allocate buffer for bootsector */
- BootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ BootSector = RtlAllocateHeap(ProcessHeap, 0, Length);
if (BootSector == NULL)
- return FALSE; // STATUS_INSUFFICIENT_RESOURCES;
-
- /* Read current boot sector into buffer */
+ return STATUS_INSUFFICIENT_RESOURCES;
+
+ /* Open the root partition - Remove any trailing backslash if needed */
RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -866,77 +962,12 @@
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
-SaveBootSector(
- PWSTR RootPath,
- PWSTR DstPath,
- ULONG Length)
-{
- NTSTATUS Status;
- UNICODE_STRING Name;
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- HANDLE FileHandle;
- LARGE_INTEGER FileOffset;
- PUCHAR BootSector;
-
- /* Allocate buffer for bootsector */
- BootSector = RtlAllocateHeap(ProcessHeap, 0, Length);
- if (BootSector == NULL)
- return STATUS_INSUFFICIENT_RESOURCES;
+ {
+ RtlFreeHeap(ProcessHeap, 0, BootSector);
+ return Status;
+ }
/* 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, BootSector);
- return Status;
- }
-
FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
@@ -997,11 +1028,10 @@
return Status;
}
-
NTSTATUS
InstallMbrBootCodeToDisk(
- PWSTR SrcPath,
- PWSTR RootPath)
+ IN PCWSTR SrcPath,
+ IN PCWSTR RootPath)
{
NTSTATUS Status;
UNICODE_STRING Name;
@@ -1013,15 +1043,13 @@
PPARTITION_SECTOR NewBootSector;
/* Allocate buffer for original bootsector */
- OrigBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
- 0,
- sizeof(PARTITION_SECTOR));
+ OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, sizeof(PARTITION_SECTOR));
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
- /* Read current boot sector into buffer */
- RtlInitUnicodeString(&Name,
- RootPath);
+ /* Open the root partition - Remove any trailing backslash if needed */
+ RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -1041,6 +1069,7 @@
return Status;
}
+ /* Read current boot sector into buffer */
FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
@@ -1059,9 +1088,7 @@
}
/* Allocate buffer for new bootsector */
- NewBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
- 0,
- sizeof(PARTITION_SECTOR));
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, sizeof(PARTITION_SECTOR));
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1113,7 +1140,8 @@
*/
RtlCopyMemory(&NewBootSector->Signature,
&OrigBootSector->Signature,
- sizeof(PARTITION_SECTOR) - offsetof(PARTITION_SECTOR, Signature) /*
Length of partition table */);
+ sizeof(PARTITION_SECTOR) - offsetof(PARTITION_SECTOR, Signature)
+ /* Length of partition table */);
/* Free the original boot sector */
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1158,11 +1186,12 @@
return Status;
}
+
static
NTSTATUS
InstallFat12BootCodeToFloppy(
- PWSTR SrcPath,
- PWSTR RootPath)
+ IN PCWSTR SrcPath,
+ IN PCWSTR RootPath)
{
NTSTATUS Status;
UNICODE_STRING Name;
@@ -1178,8 +1207,9 @@
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
- /* Read current boot sector into buffer */
+ /* Open the root partition - Remove any trailing backslash if needed */
RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -1199,6 +1229,7 @@
return Status;
}
+ /* Read current boot sector into buffer */
FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
@@ -1316,10 +1347,10 @@
static
NTSTATUS
-InstallFat16BootCodeToFile(
- PWSTR SrcPath,
- PWSTR DstPath,
- PWSTR RootPath)
+InstallFat16BootCode(
+ IN PCWSTR SrcPath, // FAT16 bootsector source file (on the installation
medium)
+ IN HANDLE DstPath, // Where to save the bootsector built from the source +
partition information
+ IN HANDLE RootPartition) // Partition holding the (old) FAT16 information
{
NTSTATUS Status;
UNICODE_STRING Name;
@@ -1330,20 +1361,43 @@
PFAT_BOOTSECTOR OrigBootSector;
PFAT_BOOTSECTOR NewBootSector;
- /* Allocate buffer for original bootsector */
+ /* Allocate a buffer for the original bootsector */
OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
- /* Read current boot sector into buffer */
- RtlInitUnicodeString(&Name, RootPath);
-
+ /* Read the current partition boot sector into the buffer */
+ FileOffset.QuadPart = 0ULL;
+ Status = NtReadFile(RootPartition,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ OrigBootSector,
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ return Status;
+ }
+
+ /* Allocate a buffer for the new bootsector */
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ if (NewBootSector == NULL)
+ {
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ /* Read the new bootsector from SrcPath */
+ RtlInitUnicodeString(&Name, SrcPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
-
Status = NtOpenFile(&FileHandle,
GENERIC_READ | SYNCHRONIZE,
&ObjectAttributes,
@@ -1353,6 +1407,7 @@
if (!NT_SUCCESS(Status))
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
return Status;
}
@@ -1362,7 +1417,7 @@
NULL,
NULL,
&IoStatusBlock,
- OrigBootSector,
+ NewBootSector,
SECTORSIZE,
&FileOffset,
NULL);
@@ -1370,75 +1425,79 @@
if (!NT_SUCCESS(Status))
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- return Status;
- }
-
- /* Allocate buffer for new bootsector */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
- if (NewBootSector == NULL)
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- return STATUS_INSUFFICIENT_RESOURCES;
- }
-
- /* Read new bootsector from SrcPath */
- RtlInitUnicodeString(&Name, SrcPath);
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+ }
+
+ /* Adjust the bootsector (copy a part of the FAT16 BPB) */
+ memcpy(&NewBootSector->OemName,
+ &OrigBootSector->OemName,
+ FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
+ FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
+
+ /* Free the original boot sector */
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+
+ /* Write the new bootsector to DstPath */
+ FileOffset.QuadPart = 0ULL;
+ Status = NtWriteFile(DstPath,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ NewBootSector,
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+
+ /* Free the new boot sector */
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+
+ return Status;
+}
+
+static
+NTSTATUS
+InstallFat16BootCodeToFile(
+ IN PCWSTR SrcPath,
+ IN PCWSTR DstPath,
+ IN PCWSTR RootPath)
+{
+ NTSTATUS Status;
+ UNICODE_STRING Name;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ HANDLE PartitionHandle, FileHandle;
+
+ /*
+ * Open the root partition from which the boot sector
+ * parameters will be obtained.
+ * Remove any trailing backslash if needed.
+ */
+ RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
-
- Status = NtOpenFile(&FileHandle,
+ Status = NtOpenFile(&PartitionHandle,
GENERIC_READ | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
0,
- FILE_SYNCHRONOUS_IO_NONALERT);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- FileOffset.QuadPart = 0ULL;
- Status = NtReadFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- SECTORSIZE,
- &FileOffset,
- NULL);
- NtClose(FileHandle);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- /* Adjust bootsector (copy a part of the FAT BPB) */
- memcpy(&NewBootSector->OemName,
- &OrigBootSector->OemName,
- FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
- FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
-
- /* Free the original boot sector */
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
-
- /* Write new bootsector to DstPath */
+ FILE_SYNCHRONOUS_IO_NONALERT /* | FILE_SEQUENTIAL_ONLY */);
+ if (!NT_SUCCESS(Status))
+ return Status;
+
+ /* Open or create the file where the new bootsector will be saved */
RtlInitUnicodeString(&Name, DstPath);
-
InitializeObjectAttributes(&ObjectAttributes,
&Name,
- 0,
+ 0, // OBJ_CASE_INSENSITIVE,
NULL,
NULL);
-
Status = NtCreateFile(&FileHandle,
GENERIC_WRITE | SYNCHRONIZE,
&ObjectAttributes,
@@ -1452,24 +1511,17 @@
0);
if (!NT_SUCCESS(Status))
{
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- FileOffset.QuadPart = 0ULL;
- Status = NtWriteFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- SECTORSIZE,
- NULL,
- NULL);
+ DPRINT1("NtCreateFile() failed (Status %lx)\n", Status);
+ NtClose(PartitionHandle);
+ return Status;
+ }
+
+ /* Install the FAT16 boot sector */
+ Status = InstallFat16BootCode(SrcPath, FileHandle, PartitionHandle);
+
+ /* Close the file and the partition */
NtClose(FileHandle);
-
- /* Free the new boot sector */
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ NtClose(PartitionHandle);
return Status;
}
@@ -1477,365 +1529,53 @@
static
NTSTATUS
InstallFat16BootCodeToDisk(
- PWSTR SrcPath,
- PWSTR RootPath)
+ IN PCWSTR SrcPath,
+ IN PCWSTR RootPath)
{
NTSTATUS Status;
UNICODE_STRING Name;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
- HANDLE FileHandle;
- 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;
-
- /* Read current boot sector into buffer */
+ HANDLE PartitionHandle;
+
+ /*
+ * Open the root partition from which the boot sector parameters will be
+ * obtained; this is also where we will write the updated boot sector.
+ * Remove any trailing backslash if needed.
+ */
RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
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;
- }
-
- /* Allocate buffer for new bootsector */
- NewBootSector = 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,
- 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,
- SECTORSIZE,
- NULL,
- NULL);
- NtClose(FileHandle);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- /* Adjust bootsector (copy a part of the FAT16 BPB) */
- memcpy(&NewBootSector->OemName,
- &OrigBootSector->OemName,
- FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
- FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
-
- /* Free the original boot sector */
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
-
- /* Write new bootsector to RootPath */
- RtlInitUnicodeString(&Name, RootPath);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- 0,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- GENERIC_WRITE | SYNCHRONIZE,
+ Status = NtOpenFile(&PartitionHandle,
+ GENERIC_READ | 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;
- }
-
- FileOffset.QuadPart = 0ULL;
- Status = NtWriteFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- SECTORSIZE,
- &FileOffset,
- NULL);
- NtClose(FileHandle);
-
- /* Free the new boot sector */
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+
+ /* Install the FAT16 boot sector */
+ Status = InstallFat16BootCode(SrcPath, PartitionHandle, PartitionHandle);
+
+ /* Close the partition */
+ NtClose(PartitionHandle);
return Status;
}
+
static
NTSTATUS
-InstallFat32BootCodeToFile(
- PWSTR SrcPath,
- PWSTR DstPath,
- PWSTR RootPath)
-{
- NTSTATUS Status;
- UNICODE_STRING Name;
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- HANDLE FileHandle;
- LARGE_INTEGER FileOffset;
- PFAT32_BOOTSECTOR OrigBootSector;
- PFAT32_BOOTSECTOR NewBootSector;
-
- /* 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,
- NULL,
- NULL);
- NtClose(FileHandle);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- return Status;
- }
-
- /* Allocate buffer for new bootsector (2 sectors) */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * 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,
- 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,
- 2 * SECTORSIZE,
- NULL,
- NULL);
- NtClose(FileHandle);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- /* Adjust bootsector (copy a part of the FAT32 BPB) */
- memcpy(&NewBootSector->OemName,
- &OrigBootSector->OemName,
- FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
- FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
-
- /* Disable the backup boot sector */
- NewBootSector->BackupBootSector = 0;
-
- /* Free the original boot sector */
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
-
- /* Write the first sector of the new bootcode to DstPath */
- RtlInitUnicodeString(&Name, DstPath);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- 0,
- NULL,
- NULL);
-
- Status = NtCreateFile(&FileHandle,
- GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- NULL,
- FILE_ATTRIBUTE_NORMAL,
- 0,
- FILE_SUPERSEDE,
- FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
- NULL,
- 0);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- FileOffset.QuadPart = 0ULL;
- Status = NtWriteFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- SECTORSIZE,
- NULL,
- NULL);
- NtClose(FileHandle);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- /* Write the second sector of the new bootcode to boot disk sector 14 */
- RtlInitUnicodeString(&Name, RootPath);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- 0,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
- if (!NT_SUCCESS(Status))
- {
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
- return Status;
- }
-
- FileOffset.QuadPart = (ULONGLONG)(14 * SECTORSIZE);
- Status = NtWriteFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- ((PUCHAR)NewBootSector + SECTORSIZE),
- SECTORSIZE,
- &FileOffset,
- NULL);
- if (!NT_SUCCESS(Status))
- {
- }
- NtClose(FileHandle);
-
- /* Free the new boot sector */
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
-
- return Status;
-}
-
-static
-NTSTATUS
-InstallFat32BootCodeToDisk(
- PWSTR SrcPath,
- PWSTR RootPath)
+InstallFat32BootCode(
+ IN PCWSTR SrcPath, // FAT32 bootsector source file (on the installation
medium)
+ IN HANDLE DstPath, // Where to save the bootsector built from the source +
partition information
+ IN HANDLE RootPartition) // Partition holding the (old) FAT32 information
{
NTSTATUS Status;
UNICODE_STRING Name;
@@ -1847,20 +1587,43 @@
PFAT32_BOOTSECTOR NewBootSector;
USHORT BackupBootSector;
- /* Allocate buffer for original bootsector */
+ /* Allocate a buffer for the original bootsector */
OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
- /* Read current boot sector into buffer */
- RtlInitUnicodeString(&Name, RootPath);
-
+ /* Read the current boot sector into the buffer */
+ FileOffset.QuadPart = 0ULL;
+ Status = NtReadFile(RootPartition,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ OrigBootSector,
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ return Status;
+ }
+
+ /* Allocate a buffer for the new bootsector (2 sectors) */
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
+ if (NewBootSector == NULL)
+ {
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ /* Read the new bootsector from SrcPath */
+ RtlInitUnicodeString(&Name, SrcPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
-
Status = NtOpenFile(&FileHandle,
GENERIC_READ | SYNCHRONIZE,
&ObjectAttributes,
@@ -1870,9 +1633,281 @@
if (!NT_SUCCESS(Status))
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
- return Status;
- }
-
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+ }
+
+ FileOffset.QuadPart = 0ULL;
+ Status = NtReadFile(FileHandle,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ NewBootSector,
+ 2 * SECTORSIZE,
+ &FileOffset,
+ NULL);
+ NtClose(FileHandle);
+ if (!NT_SUCCESS(Status))
+ {
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+ }
+
+ /* Adjust the bootsector (copy a part of the FAT32 BPB) */
+ memcpy(&NewBootSector->OemName,
+ &OrigBootSector->OemName,
+ FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
+ FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
+
+ /*
+ * We know we copy the boot code to a file only when DstPath != RootPartition,
+ * otherwise the boot code is copied to the specified root partition.
+ */
+ if (DstPath != RootPartition)
+ {
+ /* Copy to a file: Disable the backup boot sector */
+ NewBootSector->BackupBootSector = 0;
+ }
+ else
+ {
+ /* Copy to a disk: Get the location of the backup boot sector */
+ BackupBootSector = OrigBootSector->BackupBootSector;
+ }
+
+ /* Free the original boot sector */
+ RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+
+ /* Write the first sector of the new bootcode to DstPath sector 0 */
+ FileOffset.QuadPart = 0ULL;
+ Status = NtWriteFile(DstPath,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ NewBootSector,
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+ }
+
+ if (DstPath == RootPartition)
+ {
+ /* Copy to a disk: Write the backup boot sector */
+ if ((BackupBootSector != 0x0000) && (BackupBootSector != 0xFFFF))
+ {
+ FileOffset.QuadPart = (ULONGLONG)((ULONG)BackupBootSector * SECTORSIZE);
+ Status = NtWriteFile(DstPath,
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ NewBootSector,
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+ return Status;
+ }
+ }
+ }
+
+ /* Write the second sector of the new bootcode to boot disk sector 14 */
+ // FileOffset.QuadPart = (ULONGLONG)(14 * SECTORSIZE);
+ FileOffset.QuadPart = 14 * SECTORSIZE;
+ Status = NtWriteFile(DstPath, // or really RootPartition ???
+ NULL,
+ NULL,
+ NULL,
+ &IoStatusBlock,
+ ((PUCHAR)NewBootSector + SECTORSIZE),
+ SECTORSIZE,
+ &FileOffset,
+ NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
+ }
+
+ /* Free the new boot sector */
+ RtlFreeHeap(ProcessHeap, 0, NewBootSector);
+
+ return Status;
+}
+
+static
+NTSTATUS
+InstallFat32BootCodeToFile(
+ IN PCWSTR SrcPath,
+ IN PCWSTR DstPath,
+ IN PCWSTR RootPath)
+{
+ NTSTATUS Status;
+ UNICODE_STRING Name;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ HANDLE PartitionHandle, FileHandle;
+
+ /*
+ * Open the root partition from which the boot sector parameters
+ * will be obtained.
+ * FIXME? It might be possible that we need to also open it for writing
+ * access in case we really need to still write the second portion of
+ * the boot sector ????
+ *
+ * Remove any trailing backslash if needed.
+ */
+ RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+ Status = NtOpenFile(&PartitionHandle,
+ GENERIC_READ | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT /* | FILE_SEQUENTIAL_ONLY */);
+ if (!NT_SUCCESS(Status))
+ return Status;
+
+ /* Open or create the file where (the first sector of ????) the new bootsector will
be saved */
+ RtlInitUnicodeString(&Name, DstPath);
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ 0, // OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+ Status = NtCreateFile(&FileHandle,
+ GENERIC_WRITE | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ NULL,
+ FILE_ATTRIBUTE_NORMAL,
+ 0,
+ FILE_SUPERSEDE, // FILE_OVERWRITE_IF, <- is used for FAT16
+ FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
+ NULL,
+ 0);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtCreateFile() failed (Status %lx)\n", Status);
+ NtClose(PartitionHandle);
+ return Status;
+ }
+
+ /* Install the FAT32 boot sector */
+ Status = InstallFat32BootCode(SrcPath, FileHandle, PartitionHandle);
+
+ /* Close the file and the partition */
+ NtClose(FileHandle);
+ NtClose(PartitionHandle);
+
+ return Status;
+}
+
+static
+NTSTATUS
+InstallFat32BootCodeToDisk(
+ IN PCWSTR SrcPath,
+ IN PCWSTR RootPath)
+{
+ NTSTATUS Status;
+ UNICODE_STRING Name;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ HANDLE PartitionHandle;
+
+ /*
+ * Open the root partition from which the boot sector parameters will be
+ * obtained; this is also where we will write the updated boot sector.
+ * Remove any trailing backslash if needed.
+ */
+ RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+ Status = NtOpenFile(&PartitionHandle,
+ GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT /* | FILE_SEQUENTIAL_ONLY */);
+ if (!NT_SUCCESS(Status))
+ return Status;
+
+ /* Install the FAT32 boot sector */
+ Status = InstallFat32BootCode(SrcPath, PartitionHandle, PartitionHandle);
+
+ /* Close the partition */
+ NtClose(PartitionHandle);
+
+ return Status;
+}
+
+
+// FIXME: This function does not really work
+static
+NTSTATUS
+InstallExt2BootCodeToDisk(
+ PWSTR SrcPath,
+ PWSTR RootPath)
+{
+ NTSTATUS Status;
+ UNICODE_STRING Name;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ HANDLE FileHandle;
+ LARGE_INTEGER FileOffset;
+// PEXT2_BOOTSECTOR OrigBootSector;
+ PEXT2_BOOTSECTOR NewBootSector;
+ // USHORT BackupBootSector;
+
+#if 0
+ /* Allocate buffer for original bootsector */
+ OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ if (OrigBootSector == NULL)
+ return STATUS_INSUFFICIENT_RESOURCES;
+
+ /* Open the root partition - Remove any trailing backslash if needed */
+ RtlInitUnicodeString(&Name, RootPath);
+ TrimTrailingPathSeparators_UStr(&Name);
+
+ 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;
+ }
+
+ /* Read current boot sector into buffer */
FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
@@ -1889,13 +1924,13 @@
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
return Status;
}
-
-
- /* Allocate buffer for new bootsector (2 sectors) */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
+#endif
+
+ /* Allocate buffer for new bootsector */
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, sizeof(EXT2_BOOTSECTOR));
if (NewBootSector == NULL)
{
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
return STATUS_INSUFFICIENT_RESOURCES;
}
@@ -1916,7 +1951,7 @@
FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(Status))
{
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
RtlFreeHeap(ProcessHeap, 0, NewBootSector);
return Status;
}
@@ -1927,17 +1962,18 @@
NULL,
&IoStatusBlock,
NewBootSector,
- 2 * SECTORSIZE,
+ sizeof(EXT2_BOOTSECTOR),
NULL,
NULL);
NtClose(FileHandle);
if (!NT_SUCCESS(Status))
{
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+ // 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,
@@ -1948,9 +1984,10 @@
BackupBootSector = OrigBootSector->BackupBootSector;
/* Free the original boot sector */
- RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
-
- /* Write the first sector of the new bootcode to DstPath */
+ // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
+#endif
+
+ /* Write new bootsector to RootPath */
RtlInitUnicodeString(&Name, RootPath);
InitializeObjectAttributes(&ObjectAttributes,
@@ -1980,9 +2017,10 @@
NULL,
&IoStatusBlock,
NewBootSector,
- SECTORSIZE,
+ sizeof(EXT2_BOOTSECTOR),
&FileOffset,
NULL);
+#if 0
if (!NT_SUCCESS(Status))
{
DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
@@ -2028,218 +2066,6 @@
{
DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
}
- NtClose(FileHandle);
-
- /* Free the new boot sector */
- RtlFreeHeap(ProcessHeap, 0, NewBootSector);
-
- return Status;
-}
-
-// FIXME: This function does not really work
-static
-NTSTATUS
-InstallExt2BootCodeToDisk(
- PWSTR SrcPath,
- PWSTR RootPath)
-{
- NTSTATUS Status;
- UNICODE_STRING Name;
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- HANDLE FileHandle;
- LARGE_INTEGER FileOffset;
-// PEXT2_BOOTSECTOR OrigBootSector;
- PEXT2_BOOTSECTOR NewBootSector;
- // USHORT BackupBootSector;
-
-#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(EXT2_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(EXT2_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;
- }
-
- /* Write sector 0 */
- FileOffset.QuadPart = 0ULL;
- Status = NtWriteFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- sizeof(EXT2_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);