ReactOS 0.4.17-dev-1133-gc25e4fd
bootsect.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NTFS FS library
4 * FILE: lib/fslib/ntfslib/bootsect.c
5 * PURPOSE: NTFS lib
6 * PROGRAMMERS: Pierre Schweitzer, Klachkov Valery
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ntfslib.h>
12
13#define NDEBUG
14#include <debug.h>
15
16
17/* FUNCTIONS *****************************************************************/
18
19static
20VOID
22{
23 BootSector->Jump[0] = 0xEB; // jmp
24 BootSector->Jump[1] = 0x52; // 82
25 BootSector->Jump[2] = 0x90; // nop
26}
27
28static
29VOID
31{
32 BootSector->OEMID.QuadPart = OEM_ID;
33}
34
35static
36VOID
38{
39 // See: https://en.wikipedia.org/wiki/BIOS_parameter_block
40
41 BiosParametersBlock->BytesPerSector = (USHORT)BYTES_PER_SECTOR;
42 BiosParametersBlock->SectorsPerCluster = (BYTE)SECTORS_PER_CLUSTER;
43
44 BiosParametersBlock->MediaId = IS_HARD_DRIVE ? 0xF8 : 0xF0;
45
46 BiosParametersBlock->SectorsPerTrack = (USHORT)SECTORS_PER_TRACK;
47 BiosParametersBlock->Heads = DISK_HEADS;
48 BiosParametersBlock->HiddenSectorsCount = HIDDEN_SECTORS;
49}
50
51static
52VOID
54{
55 // See: https://en.wikipedia.org/wiki/BIOS_parameter_block
56
57 ExBiosParametersBlock->Header = EBPB_HEADER;
58 ExBiosParametersBlock->SectorCount = TOTAL_SECTORS;
59
60 ExBiosParametersBlock->MftLocation = LAYOUT.MftLcn;
61 ExBiosParametersBlock->MftMirrLocation = LAYOUT.MftMirrLcn;
62
63 ExBiosParametersBlock->ClustersPerMftRecord = LAYOUT.ClustersPerMftRecord;
64 ExBiosParametersBlock->ClustersPerIndexRecord = LAYOUT.ClustersPerIndexRecord;
65
66 ExBiosParametersBlock->SerialNumber = LAYOUT.SerialNumber;
67}
68
69//
70// Writes the backup boot sector to the last sector of the volume, which is the
71// sector immediately following the addressable area (LAYOUT.TotalSectors).
72//
73static
76{
80 PBYTE Sector;
82
83 Sector = RtlAllocateHeap(RtlGetProcessHeap(), 0, Bps);
84 if (!Sector)
86
87 RtlZeroMemory(Sector, Bps);
88 RtlCopyMemory(Sector, BootSector, (Bps < sizeof(BOOT_SECTOR)) ? Bps : sizeof(BOOT_SECTOR));
89
90 Offset.QuadPart = (LONGLONG)TOTAL_SECTORS * Bps;
91
93 NULL,
94 NULL,
95 NULL,
97 Sector,
98 Bps,
99 &Offset,
100 NULL);
101 if (!NT_SUCCESS(Status))
102 {
103 DPRINT1("Backup boot sector write failed (Status %lx)\n", Status);
104 }
105
106 FREE(Sector);
107
108 return Status;
109}
110
113{
117 PBYTE BootRegion;
120
121 // The whole $Boot region (8192 bytes rounded up to clusters). Only the
122 // first sector carries the BPB; the rest is zeroed bootstrap area.
123 RegionSize = LAYOUT.BootClusters * BYTES_PER_CLUSTER;
124
125 BootRegion = RtlAllocateHeap(RtlGetProcessHeap(), 0, RegionSize);
126 if (!BootRegion)
127 {
129 }
130
131 RtlZeroMemory(BootRegion, RegionSize);
132
133 BootSector = (PBOOT_SECTOR)BootRegion;
134
137
140
141 BootSector->EndSector = BOOT_SECTOR_END;
142
143 // Write the $Boot region at LCN 0.
144 Offset.QuadPart = 0LL;
146 NULL,
147 NULL,
148 NULL,
150 BootRegion,
152 &Offset,
153 NULL);
154 if (!NT_SUCCESS(Status))
155 {
156 DPRINT1("BootSector write failed. NtWriteFile() failed (Status %lx)\n", Status);
157 FREE(BootRegion);
158 return Status;
159 }
160
161 // Write the backup boot sector at the very end of the volume.
163
164 FREE(BootRegion);
165
166 return Status;
167}
#define FREE(ptr, size)
Definition: auth_des.c:64
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define BYTES_PER_SECTOR
Definition: volume.c:37
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
static VOID FillExBiosParametersBlock(OUT PEXTENDED_BIOS_PARAMETERS_BLOCK ExBiosParametersBlock)
Definition: bootsect.c:53
static VOID FillBiosParametersBlock(OUT PBIOS_PARAMETERS_BLOCK BiosParametersBlock)
Definition: bootsect.c:37
static NTSTATUS WriteBackupBootSector(IN PBOOT_SECTOR BootSector)
Definition: bootsect.c:75
static VOID FillJumpInstruction(OUT PBOOT_SECTOR BootSector)
Definition: bootsect.c:21
static VOID FillOemId(OUT PBOOT_SECTOR BootSector)
Definition: bootsect.c:30
NTSTATUS WriteBootSector(VOID)
Definition: bootsect.c:112
_In_ PIO_STATUS_BLOCK IoStatusBlock
Definition: dispmprt.h:608
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
struct _BOOT_SECTOR * PBOOT_SECTOR
Status
Definition: gdiplustypes.h:24
#define TOTAL_SECTORS
Definition: ntfslib.h:74
#define BYTES_PER_CLUSTER
Definition: ntfslib.h:72
#define OEM_ID
Definition: ntfslib.h:86
#define DISK_HANDLE
Definition: ntfslib.h:61
#define EBPB_HEADER
Definition: ntfslib.h:87
#define DISK_HEADS
Definition: ntfslib.h:81
#define IS_HARD_DRIVE
Definition: ntfslib.h:76
#define SECTORS_PER_TRACK
Definition: ntfslib.h:68
#define SECTORS_PER_CLUSTER
Definition: ntfslib.h:71
#define BOOT_SECTOR_END
Definition: ntfslib.h:88
#define HIDDEN_SECTORS
Definition: ntfslib.h:262
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:176
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
BYTE * PBYTE
Definition: pedump.c:66
unsigned short USHORT
Definition: pedump.c:61
#define LL
Definition: tui.h:167
int64_t LONGLONG
Definition: typedefs.h:68
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct _BootSector BootSector
Definition: vfat.h:108
unsigned char BYTE
Definition: xxhash.c:193