Sync with trunk r63647.
[reactos.git] / boot / freeldr / install / install.c
1 /*
2 * FreeLoader - install.c
3 *
4 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <windows.h>
22 #include <tchar.h>
23 #include <stdio.h>
24 #include "install.h"
25 #include "volume.h"
26 #include "../bootsect/fat.h"
27 #include "../bootsect/fat32.h"
28
29 BOOL BackupBootSector(LPCTSTR lpszVolumeName);
30 BOOL InstallBootSector(LPCTSTR lpszVolumeType);
31
32 int main(int argc, char *argv[])
33 {
34
35 if (argc < 3)
36 {
37 _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n"));
38 return -1;
39 }
40
41 if (!OpenVolume(argv[1]))
42 {
43 return -1;
44 }
45
46 BackupBootSector(argv[1]);
47
48 InstallBootSector(argv[2]);
49
50 _tprintf(_T("You must now copy freeldr.sys & freeldr.ini to %s.\n"), argv[1]);
51
52 CloseVolume();
53
54 return 0;
55 }
56
57 BOOL BackupBootSector(LPCTSTR lpszVolumeName)
58 {
59 HANDLE hBackupFile;
60 TCHAR szFileName[MAX_PATH];
61 ULONG Count;
62 BYTE BootSectorBuffer[512];
63 DWORD dwNumberOfBytesWritten;
64 BOOL bRetVal;
65
66 //
67 // Find the next unused filename and open it
68 //
69 for (Count=0; ; Count++)
70 {
71 //
72 // Generate the next filename
73 //
74 _stprintf(szFileName, _T("%s\\bootsect.%03ld"), lpszVolumeName, Count);
75
76 //
77 // Try to create a new file, fail if exists
78 //
79 hBackupFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, /*FILE_ATTRIBUTE_SYSTEM*/0, NULL);
80
81 //
82 // Check to see if it worked
83 //
84 if (hBackupFile != INVALID_HANDLE_VALUE)
85 {
86 break;
87 }
88
89 //
90 // Nope, didn't work
91 // Check to see if it already existed
92 //
93 if (!(GetLastError() != ERROR_ALREADY_EXISTS))
94 {
95 _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
96 _tprintf(_T("Boot sector backup failed. Error code %ld.\n"), GetLastError());
97 return FALSE;
98 }
99 }
100
101 //
102 // Try to read the boot sector
103 //
104 if (!ReadVolumeSector(0, BootSectorBuffer))
105 {
106 CloseHandle(hBackupFile);
107 return FALSE;
108 }
109
110 //
111 // Try to write the boot sector data to the file
112 //
113 bRetVal = WriteFile(hBackupFile, BootSectorBuffer, 512, &dwNumberOfBytesWritten, NULL);
114 if (!bRetVal || (dwNumberOfBytesWritten != 512))
115 {
116 CloseHandle(hBackupFile);
117 _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
118 _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());
119 return FALSE;
120 }
121
122 _tprintf(_T("Boot sector backed up to file: %s\n"), szFileName);
123
124 CloseHandle(hBackupFile);
125
126 return TRUE;
127 }
128
129 BOOL InstallBootSector(LPCTSTR lpszVolumeType)
130 {
131 BYTE BootSectorBuffer[512];
132
133 //
134 // Read in the old boot sector
135 //
136 if (!ReadVolumeSector(0, BootSectorBuffer))
137 {
138 return FALSE;
139 }
140
141 if (_tcsicmp(lpszVolumeType, _T("fat")) == 0)
142 {
143 //
144 // Update the BPB in the new boot sector
145 //
146 memcpy((fat_data+3), (BootSectorBuffer+3), 59 /*fat BPB length*/);
147
148 //
149 // Write out new boot sector
150 //
151 if (!WriteVolumeSector(0, fat_data))
152 {
153 return FALSE;
154 }
155 }
156 else if (_tcsicmp(lpszVolumeType, _T("fat32")) == 0)
157 {
158 //
159 // Update the BPB in the new boot sector
160 //
161 memcpy((fat32_data+3), (BootSectorBuffer+3), 87 /*fat32 BPB length*/);
162
163 //
164 // Write out new boot sector
165 //
166 if (!WriteVolumeSector(0, fat32_data))
167 {
168 return FALSE;
169 }
170
171 //
172 // Write out new extra sector
173 //
174 if (!WriteVolumeSector(14, (fat32_data+512)))
175 {
176 return FALSE;
177 }
178 }
179 else
180 {
181 _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
182 _tprintf(_T("File system type %s unknown.\n"), lpszVolumeType);
183 return FALSE;
184 }
185
186 _tprintf(_T("%s boot sector installed.\n"), lpszVolumeType);
187
188 return TRUE;
189 }