ReactOS  0.4.14-dev-1007-g90d795b
install.c File Reference
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "install.h"
#include "volume.h"
#include "fat.h"
#include "fat32.h"
Include dependency graph for install.c:

Go to the source code of this file.

Functions

BOOL BackupBootSector (LPCTSTR lpszVolumeName)
 
BOOL InstallBootSector (LPCTSTR lpszVolumeType)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ BackupBootSector()

BOOL BackupBootSector ( LPCTSTR  lpszVolumeName)

Definition at line 61 of file install.c.

62 {
63  HANDLE hBackupFile;
64  TCHAR szFileName[MAX_PATH];
65  ULONG Count;
66  BYTE BootSectorBuffer[512];
67  DWORD dwNumberOfBytesWritten;
68  BOOL bRetVal;
69 
70  //
71  // Find the next unused filename and open it
72  //
73  for (Count=0; ; Count++)
74  {
75  //
76  // Generate the next filename
77  //
78  _stprintf(szFileName, _T("%s\\bootsect.%03ld"), lpszVolumeName, Count);
79 
80  //
81  // Try to create a new file, fail if exists
82  //
83  hBackupFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, /*FILE_ATTRIBUTE_SYSTEM*/0, NULL);
84 
85  //
86  // Check to see if it worked
87  //
88  if (hBackupFile != INVALID_HANDLE_VALUE)
89  {
90  break;
91  }
92 
93  //
94  // Nope, didn't work
95  // Check to see if it already existed
96  //
98  {
99  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
100  _tprintf(_T("Boot sector backup failed. Error code %ld.\n"), GetLastError());
101  return FALSE;
102  }
103  }
104 
105  //
106  // Try to read the boot sector
107  //
108  if (!ReadVolumeSector(0, BootSectorBuffer))
109  {
110  CloseHandle(hBackupFile);
111  return FALSE;
112  }
113 
114  //
115  // Try to write the boot sector data to the file
116  //
117  bRetVal = WriteFile(hBackupFile, BootSectorBuffer, 512, &dwNumberOfBytesWritten, NULL);
118  if (!bRetVal || (dwNumberOfBytesWritten != 512))
119  {
120  CloseHandle(hBackupFile);
121  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
122  _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());
123  return FALSE;
124  }
125 
126  _tprintf(_T("Boot sector backed up to file: %s\n"), szFileName);
127 
128  CloseHandle(hBackupFile);
129 
130  return TRUE;
131 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned char BYTE
Definition: mem.h:68
#define _stprintf
Definition: utility.h:124
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
unsigned int ULONG
Definition: retypes.h:1
#define CREATE_NEW
Definition: disk.h:69
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:73

Referenced by FatSwapFat32BootSector(), InstallBtrfsBootCodeToDisk(), InstallFat32BootCode(), and main().

◆ InstallBootSector()

BOOL InstallBootSector ( LPCTSTR  lpszVolumeType)

Definition at line 133 of file install.c.

134 {
135  BYTE BootSectorBuffer[512];
136 
137  //
138  // Read in the old boot sector
139  //
140  if (!ReadVolumeSector(0, BootSectorBuffer))
141  {
142  return FALSE;
143  }
144 
145  if (_tcsicmp(lpszVolumeType, _T("fat")) == 0)
146  {
147  //
148  // Update the BPB in the new boot sector
149  //
150  memcpy((fat_data+3), (BootSectorBuffer+3), 59 /*fat BPB length*/);
151 
152  //
153  // Write out new boot sector
154  //
155  if (!WriteVolumeSector(0, fat_data))
156  {
157  return FALSE;
158  }
159  }
160  else if (_tcsicmp(lpszVolumeType, _T("fat32")) == 0)
161  {
162  //
163  // Update the BPB in the new boot sector
164  //
165  memcpy((fat32_data+3), (BootSectorBuffer+3), 87 /*fat32 BPB length*/);
166 
167  //
168  // Write out new boot sector
169  //
170  if (!WriteVolumeSector(0, fat32_data))
171  {
172  return FALSE;
173  }
174 
175  //
176  // Write out new extra sector
177  //
178  if (!WriteVolumeSector(14, (fat32_data+512)))
179  {
180  return FALSE;
181  }
182  }
183  else
184  {
185  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
186  _tprintf(_T("File system type %s unknown.\n"), lpszVolumeType);
187  return FALSE;
188  }
189 
190  _tprintf(_T("%s boot sector installed.\n"), lpszVolumeType);
191 
192  return TRUE;
193 }
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
#define _tcsicmp
Definition: xmlstorage.h:205
#define _T(x)
Definition: vfdio.h:22
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
unsigned char BYTE
Definition: mem.h:68
BOOL WriteVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:105
BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:73

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)

Definition at line 37 of file install.c.

38 {
39  if (argc < 3)
40  {
41  _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n"));
42  return -1;
43  }
44 
45  if (!OpenVolume(argv[1]))
46  {
47  return -1;
48  }
49 
51 
53 
54  _tprintf(_T("You must now copy freeldr.sys & freeldr.ini to %s.\n"), argv[1]);
55 
56  CloseVolume();
57 
58  return 0;
59 }
static int argc
Definition: ServiceArgs.c:12
#define _tprintf
Definition: tchar.h:506
#define argv
Definition: mplay32.c:18
BOOL BackupBootSector(LPCTSTR lpszVolumeName)
Definition: install.c:61
HANDLE OpenVolume(const TCHAR *Volume, BOOLEAN AllowRemote, BOOLEAN NtfsOnly)
Definition: common.c:49
#define _T(x)
Definition: vfdio.h:22
void CloseVolume(void)
Definition: volume.c:68
BOOL InstallBootSector(LPCTSTR lpszVolumeType)
Definition: install.c:133