2 * FreeLoader - install.c
4 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
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.
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.
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.
28 * These includes are required to define
29 * the fat_data and fat32_data arrays.
34 BOOL
BackupBootSector(LPCTSTR lpszVolumeName
);
35 BOOL
InstallBootSector(LPCTSTR lpszVolumeType
);
37 int main(int argc
, char *argv
[])
41 _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n"));
45 if (!OpenVolume(argv
[1]))
50 BackupBootSector(argv
[1]);
52 InstallBootSector(argv
[2]);
54 _tprintf(_T("You must now copy freeldr.sys & freeldr.ini to %s.\n"), argv
[1]);
61 BOOL
BackupBootSector(LPCTSTR lpszVolumeName
)
64 TCHAR szFileName
[MAX_PATH
];
66 BYTE BootSectorBuffer
[512];
67 DWORD dwNumberOfBytesWritten
;
71 // Find the next unused filename and open it
73 for (Count
=0; ; Count
++)
76 // Generate the next filename
78 _stprintf(szFileName
, _T("%s\\bootsect.%03ld"), lpszVolumeName
, Count
);
81 // Try to create a new file, fail if exists
83 hBackupFile
= CreateFile(szFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, /*FILE_ATTRIBUTE_SYSTEM*/0, NULL
);
86 // Check to see if it worked
88 if (hBackupFile
!= INVALID_HANDLE_VALUE
)
95 // Check to see if it already existed
97 if (!(GetLastError() != ERROR_ALREADY_EXISTS
))
99 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
100 _tprintf(_T("Boot sector backup failed. Error code %ld.\n"), GetLastError());
106 // Try to read the boot sector
108 if (!ReadVolumeSector(0, BootSectorBuffer
))
110 CloseHandle(hBackupFile
);
115 // Try to write the boot sector data to the file
117 bRetVal
= WriteFile(hBackupFile
, BootSectorBuffer
, 512, &dwNumberOfBytesWritten
, NULL
);
118 if (!bRetVal
|| (dwNumberOfBytesWritten
!= 512))
120 CloseHandle(hBackupFile
);
121 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
122 _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());
126 _tprintf(_T("Boot sector backed up to file: %s\n"), szFileName
);
128 CloseHandle(hBackupFile
);
133 BOOL
InstallBootSector(LPCTSTR lpszVolumeType
)
135 BYTE BootSectorBuffer
[512];
138 // Read in the old boot sector
140 if (!ReadVolumeSector(0, BootSectorBuffer
))
145 if (_tcsicmp(lpszVolumeType
, _T("fat")) == 0)
148 // Update the BPB in the new boot sector
150 memcpy((fat_data
+3), (BootSectorBuffer
+3), 59 /*fat BPB length*/);
153 // Write out new boot sector
155 if (!WriteVolumeSector(0, fat_data
))
160 else if (_tcsicmp(lpszVolumeType
, _T("fat32")) == 0)
163 // Update the BPB in the new boot sector
165 memcpy((fat32_data
+3), (BootSectorBuffer
+3), 87 /*fat32 BPB length*/);
168 // Write out new boot sector
170 if (!WriteVolumeSector(0, fat32_data
))
176 // Write out new extra sector
178 if (!WriteVolumeSector(14, (fat32_data
+512)))
185 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
186 _tprintf(_T("File system type %s unknown.\n"), lpszVolumeType
);
190 _tprintf(_T("%s boot sector installed.\n"), lpszVolumeType
);