From 167018105926586f426af093e1c883a3a03ed7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sat, 17 Oct 2020 01:28:27 +0200 Subject: [PATCH] WIP - [SETUPLIB] Install a valid bootcode sector after formatting. -- Overlaps with my refactoring of bootcode handling. --- base/setup/lib/bootsup.c | 7 +- base/setup/lib/bootsup.h | 7 ++ base/setup/lib/fsutil.c | 138 +++++++++++++++++++++++++++++++++++-- base/setup/lib/fsutil.h | 4 ++ base/setup/usetup/usetup.c | 2 + 5 files changed, 150 insertions(+), 8 deletions(-) diff --git a/base/setup/lib/bootsup.c b/base/setup/lib/bootsup.c index 37f7bdb43abfa..bc51977fee295 100644 --- a/base/setup/lib/bootsup.c +++ b/base/setup/lib/bootsup.c @@ -548,7 +548,7 @@ SaveBootSector( } -static +// static NTSTATUS InstallBootCodeToDisk( IN PCWSTR SrcPath, @@ -1272,6 +1272,7 @@ InstallFatBootcodeToFloppy( // FormatPartition(...) Status = FormatFileSystem(FloppyDevice, L"FAT", + /**/ SourceRootPath->Buffer, /* HACK HACK! */ /**/ FMIFS_FLOPPY, NULL, TRUE, @@ -1308,7 +1309,8 @@ InstallFatBootcodeToFloppy( return Status; } - /* Install FAT12 boosector */ +#if 0 // Already done by FormatFileSystem() + /* Install FAT12 bootcode */ CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, SourceRootPath->Buffer, L"\\loader\\fat.bin"); CombinePaths(DstPath, ARRAYSIZE(DstPath), 1, FloppyDevice); @@ -1319,6 +1321,7 @@ InstallFatBootcodeToFloppy( DPRINT1("InstallBootCodeToDisk(FAT12) failed (Status %lx)\n", Status); return Status; } +#endif return STATUS_SUCCESS; } diff --git a/base/setup/lib/bootsup.h b/base/setup/lib/bootsup.h index 3dcb2b6fd030c..0e2f259bdd4e0 100644 --- a/base/setup/lib/bootsup.h +++ b/base/setup/lib/bootsup.h @@ -44,4 +44,11 @@ InstallFatBootcodeToFloppy( IN PUNICODE_STRING SourceRootPath, IN PUNICODE_STRING DestinationArcPath); +/** Temporary HACKs! **/ +NTSTATUS +InstallBootCodeToDisk( + IN PCWSTR SrcPath, + IN PCWSTR RootPath, + IN PFS_INSTALL_BOOTCODE InstallBootCode); + /* EOF */ diff --git a/base/setup/lib/fsutil.c b/base/setup/lib/fsutil.c index e81f2118d362b..fe716db9dc7cd 100644 --- a/base/setup/lib/fsutil.c +++ b/base/setup/lib/fsutil.c @@ -20,6 +20,8 @@ #include "fsrec.h" #include "bootcode.h" #include "fsutil.h" +#include "filesup.h" // For CombinePaths(). +#include "bootsup.h" // For InstallBootCodeToDisk(). #include #include @@ -231,6 +233,7 @@ ChkdskFileSystem_UStr( { // BOOLEAN Argument = FALSE; // Callback(DONE, 0, &Argument); + DPRINT1("Unknown file system '%S'\n", FileSystemName); return STATUS_NOT_SUPPORTED; } @@ -270,13 +273,16 @@ NTSTATUS FormatFileSystem_UStr( IN PUNICODE_STRING DriveRoot, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN PFMIFSCALLBACK Callback) { + NTSTATUS Status; PFILE_SYSTEM FileSystem; + WCHAR SrcPath[MAX_PATH]; FileSystem = GetFileSystemByName(FileSystemName); @@ -284,21 +290,137 @@ FormatFileSystem_UStr( { // BOOLEAN Argument = FALSE; // Callback(DONE, 0, &Argument); + DPRINT1("Unknown file system '%S'\n", FileSystemName); return STATUS_NOT_SUPPORTED; } - return FileSystem->FormatFunc(DriveRoot, - MediaFlag, - Label, - QuickFormat, - ClusterSize, - Callback); + /* Do the formatting */ + Status = FileSystem->FormatFunc(DriveRoot, + MediaFlag, + Label, + QuickFormat, + ClusterSize, + Callback); + + /* If no path to source root for finding any boot sector file, we are done */ + if (!SourceRootPathForBootSector) + return Status; + + /* Install a sane boot sector */ + + if (wcsicmp(FileSystemName, L"FAT") == 0 || + wcsicmp(FileSystemName, L"FAT32") == 0) + { + if (wcsicmp(FileSystemName, L"FAT32") == 0 /* || + PartitionType == PARTITION_FAT32 || + PartitionType == PARTITION_FAT32_XINT13 */) + { + /* Install FAT32 bootcode */ + + /** HACK: In the future bootsector handling, these will be + ** pre-loaded separately, at the same time the Format/ChkDsk + ** handlers are initialized. **/ + CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, + SourceRootPathForBootSector, + L"\\loader\\fat32.bin"); + + DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, DriveRoot->Buffer); + Status = InstallBootCodeToDisk(SrcPath, DriveRoot->Buffer, InstallFat32BootCode); + if (!NT_SUCCESS(Status)) + { + DPRINT1("InstallBootCodeToDisk(FAT32) failed (Status %lx)\n", Status); + // return Status; + } + } + else if (/*(PartitionType == PARTITION_FAT_12) &&*/ (MediaFlag == FMIFS_FLOPPY)) + { + /* Install FAT12 bootcode */ + + /** HACK: In the future bootsector handling, these will be + ** pre-loaded separately, at the same time the Format/ChkDsk + ** handlers are initialized. **/ + CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, + SourceRootPathForBootSector, + L"\\loader\\fat.bin"); + + DPRINT1("Install FAT12 bootcode: %S ==> %S\n", SrcPath, DriveRoot->Buffer); + Status = InstallBootCodeToDisk(SrcPath, DriveRoot->Buffer, InstallFat12BootCode); + if (!NT_SUCCESS(Status)) + { + DPRINT1("InstallBootCodeToDisk(FAT12) failed (Status %lx)\n", Status); + // return Status; + } + } + else + { + /* Install FAT16 bootcode */ + + /** HACK: In the future bootsector handling, these will be + ** pre-loaded separately, at the same time the Format/ChkDsk + ** handlers are initialized. **/ + CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, + SourceRootPathForBootSector, + L"\\loader\\fat.bin"); + + DPRINT1("Install FAT16 bootcode: %S ==> %S\n", SrcPath, DriveRoot->Buffer); + Status = InstallBootCodeToDisk(SrcPath, DriveRoot->Buffer, InstallFat16BootCode); + if (!NT_SUCCESS(Status)) + { + DPRINT1("InstallBootCodeToDisk(FAT16) failed (Status %lx)\n", Status); + // return Status; + } + } + } + /* + else if (wcsicmp(FileSystemName, L"NTFS") == 0) + { + return Status; + } + */ + else if (wcsicmp(FileSystemName, L"BTRFS") == 0) + { + /* Install BTRFS bootcode */ + + /** HACK: In the future bootsector handling, these will be + ** pre-loaded separately, at the same time the Format/ChkDsk + ** handlers are initialized. **/ + CombinePaths(SrcPath, ARRAYSIZE(SrcPath), 2, + SourceRootPathForBootSector, + L"\\loader\\btrfs.bin"); + + DPRINT1("Install BTRFS bootcode: %S ==> %S\n", SrcPath, DriveRoot->Buffer); + Status = InstallBootCodeToDisk(SrcPath, DriveRoot->Buffer, InstallBtrfsBootCode); + if (!NT_SUCCESS(Status)) + { + DPRINT1("InstallBootCodeToDisk(BTRFS) failed (Status %lx)\n", Status); + // return Status; + } + } + /* + else if (wcsicmp(FileSystemName, L"EXT2") == 0 || + wcsicmp(FileSystemName, L"EXT3") == 0 || + wcsicmp(FileSystemName, L"EXT4") == 0 || + wcsicmp(FileSystemName, L"FFS") == 0 || + wcsicmp(FileSystemName, L"REISERFS") == 0) + { + return Status; + } + */ + else + { + /* Unknown file system */ + DPRINT1("Unknown file system '%S'\n", FileSystemName); + // return STATUS_NOT_SUPPORTED; + } + + return Status; } NTSTATUS FormatFileSystem( IN PCWSTR DriveRoot, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PCWSTR Label, IN BOOLEAN QuickFormat, @@ -313,6 +435,7 @@ FormatFileSystem( return FormatFileSystem_UStr(&DriveRootU, FileSystemName, + /**/ SourceRootPathForBootSector, /**/ MediaFlag, &LabelU, QuickFormat, @@ -669,6 +792,7 @@ NTSTATUS // ERROR_NUMBER FormatPartition( IN PPARTENTRY PartEntry, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PCWSTR Label, IN BOOLEAN QuickFormat, @@ -744,6 +868,7 @@ FormatPartition( /* Format the partition */ Status = FormatFileSystem(PartitionRootPath, FileSystemName, + /**/ SourceRootPathForBootSector, /**/ MediaFlag, Label, QuickFormat, @@ -851,6 +976,7 @@ DoFormatting( /* Format the partition */ Status = FormatPartition(PartEntry, PartInfo.FileSystemName, + /**/ PartInfo.SourceRootPathForBootSector, /* HACK HACK! */ /**/ PartInfo.MediaFlag, PartInfo.Label, PartInfo.QuickFormat, diff --git a/base/setup/lib/fsutil.h b/base/setup/lib/fsutil.h index 5e68262e884ee..42b18809a856b 100644 --- a/base/setup/lib/fsutil.h +++ b/base/setup/lib/fsutil.h @@ -44,6 +44,7 @@ NTSTATUS FormatFileSystem_UStr( IN PUNICODE_STRING DriveRoot, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, @@ -54,6 +55,7 @@ NTSTATUS FormatFileSystem( IN PCWSTR DriveRoot, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PCWSTR Label, IN BOOLEAN QuickFormat, @@ -105,6 +107,7 @@ NTSTATUS // ERROR_NUMBER FormatPartition( IN PPARTENTRY PartEntry, IN PCWSTR FileSystemName, + /**/IN PCWSTR SourceRootPathForBootSector OPTIONAL, /* HACK HACK! */ IN FMIFS_MEDIA_FLAG MediaFlag, IN PCWSTR Label, IN BOOLEAN QuickFormat, @@ -168,6 +171,7 @@ typedef struct _FORMAT_PARTITION_INFO /* Input information given by the 'FSVOLNOTIFY_PREPAREFORMAT' step ****/ PCWSTR FileSystemName; + /**/PCWSTR SourceRootPathForBootSector; /* HACK HACK! */ FMIFS_MEDIA_FLAG MediaFlag; PCWSTR Label; BOOLEAN QuickFormat; diff --git a/base/setup/usetup/usetup.c b/base/setup/usetup/usetup.c index 1fd60496dc4ae..b902c3ff87f06 100644 --- a/base/setup/usetup/usetup.c +++ b/base/setup/usetup/usetup.c @@ -3480,6 +3480,8 @@ FsVolCallback( { /* Set up the necessary formatting information */ PrepareFormat(PartInfo, FileSystemList->Selected); + /** HACK HACK! **/ + /**/ PartInfo->SourceRootPathForBootSector = USetupData.SourceRootPath.Buffer; /**/ /* Display the formatting page */ Result = FormatPartitionPage(FsVolContext);