ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

install.c
Go to the documentation of this file.
00001 /*
00002  *  FreeLoader - install.c
00003  *
00004  *  Copyright (C) 2001  Brian Palmer  <brianp@sginet.com>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #include <windows.h>
00022 #include <tchar.h>
00023 #include <stdio.h>
00024 #include "install.h"
00025 #include "volume.h"
00026 #include "../bootsect/fat.h"
00027 #include "../bootsect/fat32.h"
00028 
00029 BOOL    BackupBootSector(LPCTSTR lpszVolumeName);
00030 BOOL    InstallBootSector(LPCTSTR lpszVolumeType);
00031 
00032 int main(int argc, char *argv[])
00033 {
00034 
00035     if (argc < 3)
00036     {
00037         _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n"));
00038         return -1;
00039     }
00040 
00041     if (!OpenVolume(argv[1]))
00042     {
00043         return -1;
00044     }
00045 
00046     BackupBootSector(argv[1]);
00047 
00048     InstallBootSector(argv[2]);
00049 
00050     _tprintf(_T("You must now copy freeldr.sys & freeldr.ini to %s.\n"), argv[1]);
00051 
00052     CloseVolume();
00053 
00054     return 0;
00055 }
00056 
00057 BOOL BackupBootSector(LPCTSTR lpszVolumeName)
00058 {
00059     HANDLE  hBackupFile;
00060     TCHAR   szFileName[MAX_PATH];
00061     ULONG   Count;
00062     BYTE    BootSectorBuffer[512];
00063     DWORD   dwNumberOfBytesWritten;
00064     BOOL    bRetVal;
00065 
00066     //
00067     // Find the next unused filename and open it
00068     //
00069     for (Count=0; ; Count++)
00070     {
00071         //
00072         // Generate the next filename
00073         //
00074         _stprintf(szFileName, _T("%s\\bootsect.%03ld"), lpszVolumeName, Count);
00075 
00076         //
00077         // Try to create a new file, fail if exists
00078         //
00079         hBackupFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, /*FILE_ATTRIBUTE_SYSTEM*/0, NULL);
00080 
00081         //
00082         // Check to see if it worked
00083         //
00084         if (hBackupFile != INVALID_HANDLE_VALUE)
00085         {
00086             break;
00087         }
00088 
00089         //
00090         // Nope, didn't work
00091         // Check to see if it already existed
00092         //
00093         if (!(GetLastError() != ERROR_ALREADY_EXISTS))
00094         {
00095             _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
00096             _tprintf(_T("Boot sector backup failed. Error code %ld.\n"), GetLastError());
00097             return FALSE;
00098         }
00099     }
00100 
00101     //
00102     // Try to read the boot sector
00103     //
00104     if (!ReadVolumeSector(0, BootSectorBuffer))
00105     {
00106         CloseHandle(hBackupFile);
00107         return FALSE;
00108     }
00109 
00110     //
00111     // Try to write the boot sector data to the file
00112     //
00113     bRetVal = WriteFile(hBackupFile, BootSectorBuffer, 512, &dwNumberOfBytesWritten, NULL);
00114     if (!bRetVal || (dwNumberOfBytesWritten != 512))
00115     {
00116         CloseHandle(hBackupFile);
00117         _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
00118         _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());
00119         return FALSE;
00120     }
00121 
00122     _tprintf(_T("Boot sector backed up to file: %s\n"), szFileName);
00123 
00124     CloseHandle(hBackupFile);
00125 
00126     return TRUE;
00127 }
00128 
00129 BOOL InstallBootSector(LPCTSTR lpszVolumeType)
00130 {
00131     BYTE    BootSectorBuffer[512];
00132 
00133     //
00134     // Read in the old boot sector
00135     //
00136     if (!ReadVolumeSector(0, BootSectorBuffer))
00137     {
00138         return FALSE;
00139     }
00140 
00141     if (_tcsicmp(lpszVolumeType, _T("fat")) == 0)
00142     {
00143         //
00144         // Update the BPB in the new boot sector
00145         //
00146         memcpy((fat_data+3), (BootSectorBuffer+3), 59 /*fat BPB length*/);
00147 
00148         //
00149         // Write out new boot sector
00150         //
00151         if (!WriteVolumeSector(0, fat_data))
00152         {
00153             return FALSE;
00154         }
00155     }
00156     else if (_tcsicmp(lpszVolumeType, _T("fat32")) == 0)
00157     {
00158         //
00159         // Update the BPB in the new boot sector
00160         //
00161         memcpy((fat32_data+3), (BootSectorBuffer+3), 87 /*fat32 BPB length*/);
00162 
00163         //
00164         // Write out new boot sector
00165         //
00166         if (!WriteVolumeSector(0, fat32_data))
00167         {
00168             return FALSE;
00169         }
00170 
00171         //
00172         // Write out new extra sector
00173         //
00174         if (!WriteVolumeSector(14, (fat32_data+512)))
00175         {
00176             return FALSE;
00177         }
00178     }
00179     else
00180     {
00181         _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
00182         _tprintf(_T("File system type %s unknown.\n"), lpszVolumeType);
00183         return FALSE;
00184     }
00185 
00186     _tprintf(_T("%s boot sector installed.\n"), lpszVolumeType);
00187 
00188     return TRUE;
00189 }

Generated on Sun May 27 2012 04:19:18 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.