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