https://git.reactos.org/?p=reactos.git;a=commitdiff;h=f13a379153e3b42a49ee2…
commit f13a379153e3b42a49ee22c4d21102db5329d064
Author: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
AuthorDate: Wed May 31 01:47:39 2017 +0000
Commit: Hermès Bélusca-Maïto <hermes.belusca-maito(a)reactos.org>
CommitDate: Sun Oct 14 21:14:31 2018 +0200
[USETUP] Move some functions around, but no functional code changes otherwise.
svn path=/branches/setup_improvements/; revision=74714
---
base/setup/usetup/bootsup.c | 736 ++++++++++++++++++++++----------------------
1 file changed, 370 insertions(+), 366 deletions(-)
diff --git a/base/setup/usetup/bootsup.c b/base/setup/usetup/bootsup.c
index 37cd680029..0a3e94d54b 100644
--- a/base/setup/usetup/bootsup.c
+++ b/base/setup/usetup/bootsup.c
@@ -630,6 +630,204 @@ UpdateFreeLoaderIni(
return STATUS_SUCCESS;
}
+static
+NTSTATUS
+UnprotectBootIni(
+ PWSTR FileName,
+ PULONG Attributes)
+{
+ NTSTATUS Status;
+ UNICODE_STRING Name;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ FILE_BASIC_INFORMATION FileInfo;
+ HANDLE FileHandle;
+
+ RtlInitUnicodeString(&Name, FileName);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+
+ Status = NtOpenFile(&FileHandle,
+ GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT);
+ if (Status == STATUS_NO_SUCH_FILE)
+ {
+ DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
+ *Attributes = 0;
+ return STATUS_SUCCESS;
+ }
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
+ return Status;
+ }
+
+ Status = NtQueryInformationFile(FileHandle,
+ &IoStatusBlock,
+ &FileInfo,
+ sizeof(FILE_BASIC_INFORMATION),
+ FileBasicInformation);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
+ NtClose(FileHandle);
+ return Status;
+ }
+
+ *Attributes = FileInfo.FileAttributes;
+
+ /* Delete attributes SYSTEM, HIDDEN and READONLY */
+ FileInfo.FileAttributes = FileInfo.FileAttributes &
+ ~(FILE_ATTRIBUTE_SYSTEM |
+ FILE_ATTRIBUTE_HIDDEN |
+ FILE_ATTRIBUTE_READONLY);
+
+ Status = NtSetInformationFile(FileHandle,
+ &IoStatusBlock,
+ &FileInfo,
+ sizeof(FILE_BASIC_INFORMATION),
+ FileBasicInformation);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
+ }
+
+ NtClose(FileHandle);
+ 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;
+
+ RtlInitUnicodeString(&Name, FileName);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ NULL,
+ NULL);
+
+ Status = NtOpenFile(&FileHandle,
+ GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ 0,
+ FILE_SYNCHRONOUS_IO_NONALERT);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
+ return Status;
+ }
+
+ Status = NtQueryInformationFile(FileHandle,
+ &IoStatusBlock,
+ &FileInfo,
+ sizeof(FILE_BASIC_INFORMATION),
+ FileBasicInformation);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
+ NtClose(FileHandle);
+ return Status;
+ }
+
+ FileInfo.FileAttributes = FileInfo.FileAttributes | Attributes;
+
+ Status = NtSetInformationFile(FileHandle,
+ &IoStatusBlock,
+ &FileInfo,
+ sizeof(FILE_BASIC_INFORMATION),
+ FileBasicInformation);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
+ }
+
+ NtClose(FileHandle);
+ return Status;
+}
+
+static
+NTSTATUS
+UpdateBootIni(
+ PWSTR BootIniPath,
+ PWSTR EntryName,
+ PWSTR EntryValue)
+{
+ NTSTATUS Status;
+ PINICACHE Cache = NULL;
+ PINICACHESECTION Section = NULL;
+ ULONG FileAttribute;
+ PWCHAR OldValue = NULL;
+
+ Status = IniCacheLoad(&Cache, BootIniPath, FALSE);
+ if (!NT_SUCCESS(Status))
+ {
+ return Status;
+ }
+
+ Section = IniCacheGetSection(Cache,
+ L"operating systems");
+ if (Section == NULL)
+ {
+ IniCacheDestroy(Cache);
+ return STATUS_UNSUCCESSFUL;
+ }
+
+ /* Check - maybe record already exists */
+ Status = IniCacheGetKey(Section, EntryName, &OldValue);
+
+ /* If either key was not found, or contains something else - add new one */
+ if (!NT_SUCCESS(Status) || wcscmp(OldValue, EntryValue))
+ {
+ IniCacheInsertKey(Section,
+ NULL,
+ INSERT_LAST,
+ EntryName,
+ EntryValue);
+ }
+
+ Status = UnprotectBootIni(BootIniPath,
+ &FileAttribute);
+ if (!NT_SUCCESS(Status))
+ {
+ IniCacheDestroy(Cache);
+ return Status;
+ }
+
+ Status = IniCacheSave(Cache, BootIniPath);
+ if (!NT_SUCCESS(Status))
+ {
+ IniCacheDestroy(Cache);
+ return Status;
+ }
+
+ FileAttribute |= (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_READONLY);
+ Status = ProtectBootIni(BootIniPath, FileAttribute);
+
+ IniCacheDestroy(Cache);
+
+ return Status;
+}
+
+
BOOLEAN
IsThereAValidBootSector(PWSTR RootPath)
{
@@ -803,11 +1001,10 @@ SaveBootSector(
return Status;
}
-static
+
NTSTATUS
-InstallFat16BootCodeToFile(
+InstallMbrBootCodeToDisk(
PWSTR SrcPath,
- PWSTR DstPath,
PWSTR RootPath)
{
NTSTATUS Status;
@@ -816,16 +1013,19 @@ InstallFat16BootCodeToFile(
IO_STATUS_BLOCK IoStatusBlock;
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
- PFAT_BOOTSECTOR OrigBootSector;
- PFAT_BOOTSECTOR NewBootSector;
+ PPARTITION_SECTOR OrigBootSector;
+ PPARTITION_SECTOR NewBootSector;
/* Allocate buffer for original bootsector */
- OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ OrigBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
+ 0,
+ sizeof(PARTITION_SECTOR));
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
/* Read current boot sector into buffer */
- RtlInitUnicodeString(&Name, RootPath);
+ RtlInitUnicodeString(&Name,
+ RootPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -852,7 +1052,7 @@ InstallFat16BootCodeToFile(
NULL,
&IoStatusBlock,
OrigBootSector,
- SECTORSIZE,
+ sizeof(PARTITION_SECTOR),
&FileOffset,
NULL);
NtClose(FileHandle);
@@ -863,7 +1063,9 @@ InstallFat16BootCodeToFile(
}
/* Allocate buffer for new bootsector */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ NewBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
+ 0,
+ sizeof(PARTITION_SECTOR));
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -892,15 +1094,14 @@ InstallFat16BootCodeToFile(
return Status;
}
- FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
NewBootSector,
- SECTORSIZE,
- &FileOffset,
+ sizeof(PARTITION_SECTOR),
+ NULL,
NULL);
NtClose(FileHandle);
if (!NT_SUCCESS(Status))
@@ -910,17 +1111,19 @@ InstallFat16BootCodeToFile(
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));
+ /*
+ * 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);
- /* Write new bootsector to DstPath */
- RtlInitUnicodeString(&Name, DstPath);
+ /* Write new bootsector to RootPath */
+ RtlInitUnicodeString(&Name, RootPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -928,31 +1131,28 @@ InstallFat16BootCodeToFile(
NULL,
NULL);
- Status = NtCreateFile(&FileHandle,
- GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- NULL,
- FILE_ATTRIBUTE_NORMAL,
- 0,
- FILE_OVERWRITE_IF,
- FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
- NULL,
- 0);
+ 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;
}
+ FileOffset.QuadPart = 0ULL;
Status = NtWriteFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
NewBootSector,
- SECTORSIZE,
- NULL,
+ sizeof(PARTITION_SECTOR),
+ &FileOffset,
NULL);
NtClose(FileHandle);
@@ -964,9 +1164,8 @@ InstallFat16BootCodeToFile(
static
NTSTATUS
-InstallFat32BootCodeToFile(
+InstallFat12BootCodeToFloppy(
PWSTR SrcPath,
- PWSTR DstPath,
PWSTR RootPath)
{
NTSTATUS Status;
@@ -975,8 +1174,8 @@ InstallFat32BootCodeToFile(
IO_STATUS_BLOCK IoStatusBlock;
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
- PFAT32_BOOTSECTOR OrigBootSector;
- PFAT32_BOOTSECTOR NewBootSector;
+ PFAT_BOOTSECTOR OrigBootSector;
+ PFAT_BOOTSECTOR NewBootSector;
/* Allocate buffer for original bootsector */
OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
@@ -1004,6 +1203,7 @@ InstallFat32BootCodeToFile(
return Status;
}
+ FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
NULL,
@@ -1011,7 +1211,7 @@ InstallFat32BootCodeToFile(
&IoStatusBlock,
OrigBootSector,
SECTORSIZE,
- NULL,
+ &FileOffset,
NULL);
NtClose(FileHandle);
if (!NT_SUCCESS(Status))
@@ -1020,8 +1220,10 @@ InstallFat32BootCodeToFile(
return Status;
}
- /* Allocate buffer for new bootsector (2 sectors) */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
+ /* Allocate buffer for new bootsector */
+ NewBootSector = RtlAllocateHeap(ProcessHeap,
+ 0,
+ SECTORSIZE);
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1056,7 +1258,7 @@ InstallFat32BootCodeToFile(
NULL,
&IoStatusBlock,
NewBootSector,
- 2 * SECTORSIZE,
+ SECTORSIZE,
NULL,
NULL);
NtClose(FileHandle);
@@ -1067,61 +1269,16 @@ InstallFat32BootCodeToFile(
return Status;
}
- /* Adjust bootsector (copy a part of the FAT32 BPB) */
+ /* Adjust bootsector (copy a part of the FAT16 BPB) */
memcpy(&NewBootSector->OemName,
&OrigBootSector->OemName,
- FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
- FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
-
- /* Disable the backup boot sector */
- NewBootSector->BackupBootSector = 0;
+ FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
+ FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
/* 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;
- }
-
- 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 */
+ /* Write new bootsector to RootPath */
RtlInitUnicodeString(&Name, RootPath);
InitializeObjectAttributes(&ObjectAttributes,
@@ -1135,26 +1292,24 @@ InstallFat32BootCodeToFile(
&ObjectAttributes,
&IoStatusBlock,
0,
- FILE_SYNCHRONOUS_IO_NONALERT);
+ 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 = (ULONGLONG)(14 * SECTORSIZE);
+ FileOffset.QuadPart = 0ULL;
Status = NtWriteFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
- ((PUCHAR)NewBootSector + SECTORSIZE),
+ NewBootSector,
SECTORSIZE,
&FileOffset,
NULL);
- if (!NT_SUCCESS(Status))
- {
- }
NtClose(FileHandle);
/* Free the new boot sector */
@@ -1163,10 +1318,11 @@ InstallFat32BootCodeToFile(
return Status;
}
-
+static
NTSTATUS
-InstallMbrBootCodeToDisk(
+InstallFat16BootCodeToFile(
PWSTR SrcPath,
+ PWSTR DstPath,
PWSTR RootPath)
{
NTSTATUS Status;
@@ -1175,19 +1331,16 @@ InstallMbrBootCodeToDisk(
IO_STATUS_BLOCK IoStatusBlock;
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
- PPARTITION_SECTOR OrigBootSector;
- PPARTITION_SECTOR NewBootSector;
+ PFAT_BOOTSECTOR OrigBootSector;
+ PFAT_BOOTSECTOR NewBootSector;
/* Allocate buffer for original bootsector */
- OrigBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
- 0,
- sizeof(PARTITION_SECTOR));
+ OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
if (OrigBootSector == NULL)
return STATUS_INSUFFICIENT_RESOURCES;
/* Read current boot sector into buffer */
- RtlInitUnicodeString(&Name,
- RootPath);
+ RtlInitUnicodeString(&Name, RootPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -1214,7 +1367,7 @@ InstallMbrBootCodeToDisk(
NULL,
&IoStatusBlock,
OrigBootSector,
- sizeof(PARTITION_SECTOR),
+ SECTORSIZE,
&FileOffset,
NULL);
NtClose(FileHandle);
@@ -1225,9 +1378,7 @@ InstallMbrBootCodeToDisk(
}
/* Allocate buffer for new bootsector */
- NewBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
- 0,
- sizeof(PARTITION_SECTOR));
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1256,14 +1407,15 @@ InstallMbrBootCodeToDisk(
return Status;
}
+ FileOffset.QuadPart = 0ULL;
Status = NtReadFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
NewBootSector,
- sizeof(PARTITION_SECTOR),
- NULL,
+ SECTORSIZE,
+ &FileOffset,
NULL);
NtClose(FileHandle);
if (!NT_SUCCESS(Status))
@@ -1273,19 +1425,17 @@ InstallMbrBootCodeToDisk(
return Status;
}
- /*
- * 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 */);
+ /* 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 RootPath */
- RtlInitUnicodeString(&Name, RootPath);
+ /* Write new bootsector to DstPath */
+ RtlInitUnicodeString(&Name, DstPath);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
@@ -1293,15 +1443,19 @@ InstallMbrBootCodeToDisk(
NULL,
NULL);
- Status = NtOpenFile(&FileHandle,
- GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
+ Status = NtCreateFile(&FileHandle,
+ GENERIC_WRITE | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ NULL,
+ FILE_ATTRIBUTE_NORMAL,
+ 0,
+ FILE_OVERWRITE_IF,
+ FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
+ NULL,
+ 0);
if (!NT_SUCCESS(Status))
{
- DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
RtlFreeHeap(ProcessHeap, 0, NewBootSector);
return Status;
}
@@ -1313,8 +1467,8 @@ InstallMbrBootCodeToDisk(
NULL,
&IoStatusBlock,
NewBootSector,
- sizeof(PARTITION_SECTOR),
- &FileOffset,
+ SECTORSIZE,
+ NULL,
NULL);
NtClose(FileHandle);
@@ -1326,7 +1480,7 @@ InstallMbrBootCodeToDisk(
static
NTSTATUS
-InstallFat12BootCodeToFloppy(
+InstallFat16BootCodeToDisk(
PWSTR SrcPath,
PWSTR RootPath)
{
@@ -1383,9 +1537,7 @@ InstallFat12BootCodeToFloppy(
}
/* Allocate buffer for new bootsector */
- NewBootSector = RtlAllocateHeap(ProcessHeap,
- 0,
- SECTORSIZE);
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1482,8 +1634,9 @@ InstallFat12BootCodeToFloppy(
static
NTSTATUS
-InstallFat16BootCodeToDisk(
+InstallFat32BootCodeToFile(
PWSTR SrcPath,
+ PWSTR DstPath,
PWSTR RootPath)
{
NTSTATUS Status;
@@ -1492,8 +1645,8 @@ InstallFat16BootCodeToDisk(
IO_STATUS_BLOCK IoStatusBlock;
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
- PFAT_BOOTSECTOR OrigBootSector;
- PFAT_BOOTSECTOR NewBootSector;
+ PFAT32_BOOTSECTOR OrigBootSector;
+ PFAT32_BOOTSECTOR NewBootSector;
/* Allocate buffer for original bootsector */
OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
@@ -1529,7 +1682,7 @@ InstallFat16BootCodeToDisk(
&IoStatusBlock,
OrigBootSector,
SECTORSIZE,
- &FileOffset,
+ NULL,
NULL);
NtClose(FileHandle);
if (!NT_SUCCESS(Status))
@@ -1538,8 +1691,8 @@ InstallFat16BootCodeToDisk(
return Status;
}
- /* Allocate buffer for new bootsector */
- NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
+ /* Allocate buffer for new bootsector (2 sectors) */
+ NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
if (NewBootSector == NULL)
{
RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
@@ -1555,46 +1708,92 @@ InstallFat16BootCodeToDisk(
NULL,
NULL);
- Status = NtOpenFile(&FileHandle,
- GENERIC_READ | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
+ 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, OrigBootSector);
RtlFreeHeap(ProcessHeap, 0, NewBootSector);
return Status;
}
- Status = NtReadFile(FileHandle,
- NULL,
- NULL,
- NULL,
- &IoStatusBlock,
- NewBootSector,
- SECTORSIZE,
- NULL,
- NULL);
+ FileOffset.QuadPart = 0ULL;
+ Status = NtWriteFile(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 */
+ /* Write the second sector of the new bootcode to boot disk sector 14 */
RtlInitUnicodeString(&Name, RootPath);
InitializeObjectAttributes(&ObjectAttributes,
@@ -1608,24 +1807,26 @@ InstallFat16BootCodeToDisk(
&ObjectAttributes,
&IoStatusBlock,
0,
- FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
+ FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(Status))
{
- DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
RtlFreeHeap(ProcessHeap, 0, NewBootSector);
return Status;
}
- FileOffset.QuadPart = 0ULL;
+ FileOffset.QuadPart = (ULONGLONG)(14 * SECTORSIZE);
Status = NtWriteFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
- NewBootSector,
+ ((PUCHAR)NewBootSector + SECTORSIZE),
SECTORSIZE,
&FileOffset,
NULL);
+ if (!NT_SUCCESS(Status))
+ {
+ }
NtClose(FileHandle);
/* Free the new boot sector */
@@ -2074,203 +2275,6 @@ InstallBtrfsBootCodeToDisk(
return Status;
}
-static
-NTSTATUS
-UnprotectBootIni(
- PWSTR FileName,
- PULONG Attributes)
-{
- NTSTATUS Status;
- UNICODE_STRING Name;
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- FILE_BASIC_INFORMATION FileInfo;
- HANDLE FileHandle;
-
- RtlInitUnicodeString(&Name, FileName);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- OBJ_CASE_INSENSITIVE,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
- if (Status == STATUS_NO_SUCH_FILE)
- {
- DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
- *Attributes = 0;
- return STATUS_SUCCESS;
- }
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
- return Status;
- }
-
- Status = NtQueryInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInfo,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
- NtClose(FileHandle);
- return Status;
- }
-
- *Attributes = FileInfo.FileAttributes;
-
- /* Delete attributes SYSTEM, HIDDEN and READONLY */
- FileInfo.FileAttributes = FileInfo.FileAttributes &
- ~(FILE_ATTRIBUTE_SYSTEM |
- FILE_ATTRIBUTE_HIDDEN |
- FILE_ATTRIBUTE_READONLY);
-
- Status = NtSetInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInfo,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
- }
-
- NtClose(FileHandle);
- 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;
-
- RtlInitUnicodeString(&Name, FileName);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- OBJ_CASE_INSENSITIVE,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
- return Status;
- }
-
- Status = NtQueryInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInfo,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
- NtClose(FileHandle);
- return Status;
- }
-
- FileInfo.FileAttributes = FileInfo.FileAttributes | Attributes;
-
- Status = NtSetInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInfo,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- if (!NT_SUCCESS(Status))
- {
- DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
- }
-
- NtClose(FileHandle);
- return Status;
-}
-
-static
-NTSTATUS
-UpdateBootIni(
- PWSTR BootIniPath,
- PWSTR EntryName,
- PWSTR EntryValue)
-{
- NTSTATUS Status;
- PINICACHE Cache = NULL;
- PINICACHESECTION Section = NULL;
- ULONG FileAttribute;
- PWCHAR OldValue = NULL;
-
- Status = IniCacheLoad(&Cache, BootIniPath, FALSE);
- if (!NT_SUCCESS(Status))
- {
- return Status;
- }
-
- Section = IniCacheGetSection(Cache,
- L"operating systems");
- if (Section == NULL)
- {
- IniCacheDestroy(Cache);
- return STATUS_UNSUCCESSFUL;
- }
-
- /* Check - maybe record already exists */
- Status = IniCacheGetKey(Section, EntryName, &OldValue);
-
- /* If either key was not found, or contains something else - add new one */
- if (!NT_SUCCESS(Status) || wcscmp(OldValue, EntryValue))
- {
- IniCacheInsertKey(Section,
- NULL,
- INSERT_LAST,
- EntryName,
- EntryValue);
- }
-
- Status = UnprotectBootIni(BootIniPath,
- &FileAttribute);
- if (!NT_SUCCESS(Status))
- {
- IniCacheDestroy(Cache);
- return Status;
- }
-
- Status = IniCacheSave(Cache, BootIniPath);
- if (!NT_SUCCESS(Status))
- {
- IniCacheDestroy(Cache);
- return Status;
- }
-
- FileAttribute |= (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_READONLY);
- Status = ProtectBootIni(BootIniPath, FileAttribute);
-
- IniCacheDestroy(Cache);
-
- return Status;
-}
-
static
NTSTATUS
InstallFatBootcodeToPartition(