Skip to content

Commit

Permalink
WIP - [SETUPLIB] Install a valid bootcode sector after formatting. --…
Browse files Browse the repository at this point in the history
… Overlaps with my refactoring of bootcode handling.
  • Loading branch information
HBelusca committed Oct 13, 2023
1 parent 1966935 commit 1670181
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 8 deletions.
7 changes: 5 additions & 2 deletions base/setup/lib/bootsup.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ SaveBootSector(
}


static
// static
NTSTATUS
InstallBootCodeToDisk(
IN PCWSTR SrcPath,
Expand Down Expand Up @@ -1272,6 +1272,7 @@ InstallFatBootcodeToFloppy(
// FormatPartition(...)
Status = FormatFileSystem(FloppyDevice,
L"FAT",
/**/ SourceRootPath->Buffer, /* HACK HACK! */ /**/
FMIFS_FLOPPY,
NULL,
TRUE,
Expand Down Expand Up @@ -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);

Expand All @@ -1319,6 +1321,7 @@ InstallFatBootcodeToFloppy(
DPRINT1("InstallBootCodeToDisk(FAT12) failed (Status %lx)\n", Status);
return Status;
}
#endif

return STATUS_SUCCESS;
}
Expand Down
7 changes: 7 additions & 0 deletions base/setup/lib/bootsup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
138 changes: 132 additions & 6 deletions base/setup/lib/fsutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "fsrec.h"
#include "bootcode.h"
#include "fsutil.h"
#include "filesup.h" // For CombinePaths().
#include "bootsup.h" // For InstallBootCodeToDisk().

#include <fslib/vfatlib.h>
#include <fslib/btrfslib.h>
Expand Down Expand Up @@ -231,6 +233,7 @@ ChkdskFileSystem_UStr(
{
// BOOLEAN Argument = FALSE;
// Callback(DONE, 0, &Argument);
DPRINT1("Unknown file system '%S'\n", FileSystemName);
return STATUS_NOT_SUPPORTED;
}

Expand Down Expand Up @@ -270,35 +273,154 @@ 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);

if (!FileSystem || !FileSystem->FormatFunc)
{
// 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,
Expand All @@ -313,6 +435,7 @@ FormatFileSystem(

return FormatFileSystem_UStr(&DriveRootU,
FileSystemName,
/**/ SourceRootPathForBootSector, /**/
MediaFlag,
&LabelU,
QuickFormat,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -744,6 +868,7 @@ FormatPartition(
/* Format the partition */
Status = FormatFileSystem(PartitionRootPath,
FileSystemName,
/**/ SourceRootPathForBootSector, /**/
MediaFlag,
Label,
QuickFormat,
Expand Down Expand Up @@ -851,6 +976,7 @@ DoFormatting(
/* Format the partition */
Status = FormatPartition(PartEntry,
PartInfo.FileSystemName,
/**/ PartInfo.SourceRootPathForBootSector, /* HACK HACK! */ /**/
PartInfo.MediaFlag,
PartInfo.Label,
PartInfo.QuickFormat,
Expand Down
4 changes: 4 additions & 0 deletions base/setup/lib/fsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions base/setup/usetup/usetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1670181

Please sign in to comment.