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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "../bootsect/fat.h"
27 #include "../bootsect/fat32.h"
29 BOOL
BackupBootSector(LPCTSTR lpszVolumeName
);
30 BOOL
InstallBootSector(LPCTSTR lpszVolumeType
);
32 int main(int argc
, char *argv
[])
37 _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n"));
41 if (!OpenVolume(argv
[1]))
46 BackupBootSector(argv
[1]);
48 InstallBootSector(argv
[2]);
50 _tprintf(_T("You must now copy freeldr.sys & freeldr.ini to %s.\n"), argv
[1]);
57 BOOL
BackupBootSector(LPCTSTR lpszVolumeName
)
60 TCHAR szFileName
[MAX_PATH
];
62 BYTE BootSectorBuffer
[512];
63 DWORD dwNumberOfBytesWritten
;
67 // Find the next unused filename and open it
69 for (Count
=0; ; Count
++)
72 // Generate the next filename
74 _stprintf(szFileName
, _T("%s\\bootsect.%03d"), lpszVolumeName
, Count
);
77 // Try to create a new file, fail if exists
79 hBackupFile
= CreateFile(szFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, /*FILE_ATTRIBUTE_SYSTEM*/0, NULL
);
82 // Check to see if it worked
84 if (hBackupFile
!= INVALID_HANDLE_VALUE
)
91 // Check to see if it already existed
93 if (!(GetLastError() != ERROR_ALREADY_EXISTS
))
95 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
96 _tprintf(_T("Boot sector backup failed. Error code %d.\n"), GetLastError());
102 // Try to read the boot sector
104 if (!ReadVolumeSector(0, BootSectorBuffer
))
106 CloseHandle(hBackupFile
);
111 // Try to write the boot sector data to the file
113 bRetVal
= WriteFile(hBackupFile
, BootSectorBuffer
, 512, &dwNumberOfBytesWritten
, NULL
);
114 if (!bRetVal
|| (dwNumberOfBytesWritten
!= 512))
116 CloseHandle(hBackupFile
);
117 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
118 _tprintf(_T("WriteFile() failed. Error code %d.\n"), GetLastError());
122 _tprintf(_T("Boot sector backed up to file: %s\n"), szFileName
);
124 CloseHandle(hBackupFile
);
129 BOOL
InstallBootSector(LPCTSTR lpszVolumeType
)
131 BYTE BootSectorBuffer
[512];
134 // Read in the old boot sector
136 if (!ReadVolumeSector(0, BootSectorBuffer
))
141 if (_tcsicmp(lpszVolumeType
, _T("fat")) == 0)
144 // Update the BPB in the new boot sector
146 memcpy((fat_data
+3), (BootSectorBuffer
+3), 59 /*fat BPB length*/);
149 // Write out new boot sector
151 if (!WriteVolumeSector(0, fat_data
))
156 else if (_tcsicmp(lpszVolumeType
, _T("fat32")) == 0)
159 // Update the BPB in the new boot sector
161 memcpy((fat32_data
+3), (BootSectorBuffer
+3), 87 /*fat32 BPB length*/);
164 // Write out new boot sector
166 if (!WriteVolumeSector(0, fat32_data
))
172 // Write out new extra sector
174 if (!WriteVolumeSector(14, (fat_data
+512) ))
181 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
182 _tprintf(_T("File system type %s unknown.\n"), lpszVolumeType
);
186 _tprintf(_T("%s boot sector installed.\n"), lpszVolumeType
);