⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bootsup.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 4 页
字号:

  /* 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)
{
  UNICODE_STRING Name;
  OBJECT_ATTRIBUTES ObjectAttributes;
  IO_STATUS_BLOCK IoStatusBlock;
  FILE_BASIC_INFORMATION FileInfo;
  HANDLE FileHandle;
  NTSTATUS Status;

  RtlInitUnicodeString(&Name,
		       FileName);

  InitializeObjectAttributes(&ObjectAttributes,
			     &Name,
			     OBJ_CASE_INSENSITIVE,
			     NULL,
			     NULL);

  Status = NtOpenFile(&FileHandle,
		      GENERIC_READ|GENERIC_WRITE,
		      &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);
}


NTSTATUS
UpdateBootIni(PWSTR BootIniPath,
	      PWSTR EntryName,
	      PWSTR EntryValue)
{
  UNICODE_STRING Name;
  PINICACHE Cache = NULL;
  PINICACHESECTION Section = NULL;
  NTSTATUS Status;
  ULONG FileAttribute;
  PWCHAR OldValue = NULL;

  RtlInitUnicodeString(&Name,
		       BootIniPath);

  Status = IniCacheLoad(&Cache,
			&Name,
			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
CheckInstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath)
{
#ifdef __REACTOS__
  if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") ||
      DoesFileExist(SystemRootPath->Buffer, L"boot.ini"))
    {
      return TRUE;
    }
  else if (DoesFileExist(SystemRootPath->Buffer, L"io.sys") ||
	   DoesFileExist(SystemRootPath->Buffer, L"msdos.sys"))
    {
      return TRUE;
    }
#endif

  return FALSE;
}


NTSTATUS
InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,
			      PUNICODE_STRING SourceRootPath,
			      PUNICODE_STRING DestinationArcPath,
			      UCHAR PartitionType)
{
#ifdef __REACTOS__
	WCHAR SrcPath[MAX_PATH];
	WCHAR DstPath[MAX_PATH];
	NTSTATUS Status;

	/* FAT or FAT32 partition */
	DPRINT("System path: '%wZ'\n", SystemRootPath);

	if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") == TRUE ||
		DoesFileExist(SystemRootPath->Buffer, L"boot.ini") == TRUE)
	{
		/* Search root directory for 'ntldr' and 'boot.ini'. */
		DPRINT("Found Microsoft Windows NT/2000/XP boot loader\n");

		/* Copy FreeLoader to the boot partition */
		wcscpy(SrcPath, SourceRootPath->Buffer);
		wcscat(SrcPath, L"\\loader\\freeldr.sys");
		wcscpy(DstPath, SystemRootPath->Buffer);
		wcscat(DstPath, L"\\freeldr.sys");

		DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
		Status = SetupCopyFile(SrcPath, DstPath);
		if (!NT_SUCCESS(Status))
		{
			DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
			return Status;
		}

		/* Create or update freeldr.ini */
		if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
		{
			/* Create new 'freeldr.ini' */
			DPRINT1("Create new 'freeldr.ini'\n");
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			Status = CreateFreeLoaderIniForReactos(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
				return Status;
			}

			/* Install new bootcode */
			if (PartitionType == PARTITION_FAT32 ||
				PartitionType == PARTITION_FAT32_XINT13)
			{
				/* Install FAT32 bootcode */
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat32.bin");
				wcscpy(DstPath, SystemRootPath->Buffer);
				wcscat(DstPath, L"\\bootsect.ros");

				DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, DstPath);
				Status = InstallFat32BootCodeToFile(SrcPath,
					DstPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat32BootCodeToFile() failed (Status %lx)\n", Status);
					return Status;
				}
			}
			else
			{
				/* Install FAT16 bootcode */
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat.bin");
				wcscpy(DstPath, SystemRootPath->Buffer);
				wcscat(DstPath, L"\\bootsect.ros");

				DPRINT1("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
				Status = InstallFat16BootCodeToFile(SrcPath,
					DstPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat16BootCodeToFile() failed (Status %lx)\n", Status);
					return Status;
				}
			}
		}
		else
		{
			/* Update existing 'freeldr.ini' */
			DPRINT1("Update existing 'freeldr.ini'\n");
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			Status = UpdateFreeLoaderIni(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
				return Status;
			}
		}

		/* Update 'boot.ini' */
		wcscpy(DstPath, SystemRootPath->Buffer);
		wcscat(DstPath, L"\\boot.ini");

		DPRINT1("Update 'boot.ini': %S\n", DstPath);
		Status = UpdateBootIni(DstPath,
			L"C:\\bootsect.ros",
			L"\"ReactOS\"");
		if (!NT_SUCCESS(Status))
		{
			DPRINT1("UpdateBootIni() failed (Status %lx)\n", Status);
			return Status;
		}
	}
	else if (DoesFileExist(SystemRootPath->Buffer, L"io.sys") == TRUE ||
		DoesFileExist(SystemRootPath->Buffer, L"msdos.sys") == TRUE)
	{
		/* Search for root directory for 'io.sys' and 'msdos.sys'. */
		DPRINT1("Found Microsoft DOS or Windows 9x boot loader\n");

		/* Copy FreeLoader to the boot partition */
		wcscpy(SrcPath, SourceRootPath->Buffer);
		wcscat(SrcPath, L"\\loader\\freeldr.sys");
		wcscpy(DstPath, SystemRootPath->Buffer);
		wcscat(DstPath, L"\\freeldr.sys");

		DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
		Status = SetupCopyFile(SrcPath, DstPath);
		if (!NT_SUCCESS(Status))
		{
			DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
			return Status;
		}

		/* Create or update 'freeldr.ini' */
		if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
		{
			/* Create new 'freeldr.ini' */
			DPRINT1("Create new 'freeldr.ini'\n");
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			Status = CreateFreeLoaderIniForDos(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("CreateFreeLoaderIniForDos() failed (Status %lx)\n", Status);
				return Status;
			}

			/* Save current bootsector as 'BOOTSECT.DOS' */
			wcscpy(SrcPath, SystemRootPath->Buffer);
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\bootsect.dos");

			DPRINT1("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
			Status = SaveCurrentBootSector(SrcPath,
				DstPath);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("SaveCurrentBootSector() failed (Status %lx)\n", Status);
				return Status;
			}

			/* Install new bootsector */
			if (PartitionType == PARTITION_FAT32 ||
				PartitionType == PARTITION_FAT32_XINT13)
			{
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat32.bin");

				DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
				Status = InstallFat32BootCodeToDisk(SrcPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat32BootCodeToDisk() failed (Status %lx)\n", Status);
					return Status;
				}
			}
			else
			{
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat.bin");

				DPRINT1("Install FAT bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
				Status = InstallFat16BootCodeToDisk(SrcPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
					return Status;
				}
			}
		}
		else
		{
			/* Update existing 'freeldr.ini' */
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			Status = UpdateFreeLoaderIni(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
				return Status;
			}
		}
	}
	else
	{
		/* No or unknown boot loader */
		DPRINT1("No or unknown boot loader found\n");

		/* Copy FreeLoader to the boot partition */
		wcscpy(SrcPath, SourceRootPath->Buffer);
		wcscat(SrcPath, L"\\loader\\freeldr.sys");
		wcscpy(DstPath, SystemRootPath->Buffer);
		wcscat(DstPath, L"\\freeldr.sys");

		DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
		Status = SetupCopyFile(SrcPath, DstPath);
		if (!NT_SUCCESS(Status))
		{
			DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
			return Status;
		}

		/* Create or update 'freeldr.ini' */
		if (DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini") == FALSE)
		{
			/* Create new freeldr.ini */
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
			Status = CreateFreeLoaderIniForReactos(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
				return Status;
			}

			/* Save current bootsector as 'BOOTSECT.OLD' */
			wcscpy(SrcPath, SystemRootPath->Buffer);
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\bootsect.old");

			DPRINT("Save bootsector: %S ==> %S\n", SrcPath, DstPath);
			Status = SaveCurrentBootSector(SrcPath,
				DstPath);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("SaveCurrentBootSector() failed (Status %lx)\n", Status);
				return Status;
			}

			/* Install new bootsector */
			if (PartitionType == PARTITION_FAT32 ||
				PartitionType == PARTITION_FAT32_XINT13)
			{
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat32.bin");

				DPRINT("Install FAT32 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
				Status = InstallFat32BootCodeToDisk(SrcPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat32BootCodeToDisk() failed (Status %lx)\n", Status);
					return Status;
				}
			}
			else
			{
				wcscpy(SrcPath, SourceRootPath->Buffer);
				wcscat(SrcPath, L"\\loader\\fat.bin");

				DPRINT("Install FAT bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
				Status = InstallFat16BootCodeToDisk(SrcPath,
					SystemRootPath->Buffer);
				if (!NT_SUCCESS(Status))
				{
					DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
					return Status;
				}
			}
		}
		else
		{
			/* Update existing 'freeldr.ini' */
			wcscpy(DstPath, SystemRootPath->Buffer);
			wcscat(DstPath, L"\\freeldr.ini");

			Status = UpdateFreeLoaderIni(DstPath,
				DestinationArcPath->Buffer);
			if (!NT_SUCCESS(Status))
			{
				DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
				return Status;
			}
		}
	}

	return STATUS_SUCCESS;
#else
	return STATUS_NOT_IMPLEMENTED;
#endif
}


NTSTATUS
InstallFatBootcodeToFloppy(PUNICODE_STRING SourceRootPath,
			   PUNICODE_STRING DestinationArcPath)
{
#ifdef __REACTOS__
  WCHAR SrcPath[MAX_PATH];
  WCHAR DstPath[MAX_PATH];
  NTSTATUS Status;

  /* Copy FreeLoader to the boot partition */
  wcscpy(SrcPath, SourceRootPath->Buffer);
  wcscat(SrcPath, L"\\loader\\freeldr.sys");

  wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.sys");

  DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
  Status = SetupCopyFile(SrcPath, DstPath);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
      return Status;
    }

  /* Create new 'freeldr.ini' */
  wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.ini");

  DPRINT("Create new 'freeldr.ini'\n");
  Status = CreateFreeLoaderIniForReactos(DstPath,
					 DestinationArcPath->Buffer);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("CreateFreeLoaderIniForReactos() failed (Status %lx)\n", Status);
      return Status;
    }

  /* Install FAT12/16 boosector */
  wcscpy(SrcPath, SourceRootPath->Buffer);
  wcscat(SrcPath, L"\\loader\\fat.bin");

  wcscpy(DstPath, L"\\Device\\Floppy0");

  DPRINT("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
  Status = InstallFat16BootCodeToDisk(SrcPath,
				      DstPath);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
      return Status;
    }

  return STATUS_SUCCESS;
#else
  return STATUS_NOT_IMPLEMENTED;
#endif
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -