Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenvolume.c
Go to the documentation of this file.
00001 /* 00002 * FreeLoader - volume.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 <stdio.h> 00023 #include <tchar.h> 00024 #include "volume.h" 00025 00026 static HANDLE hDiskVolume = NULL; 00027 00028 BOOL OpenVolume(LPCTSTR lpszVolumeName) 00029 { 00030 TCHAR RealVolumeName[MAX_PATH]; 00031 00032 // 00033 // If they passed in a drive letter (e.g. "A:") 00034 // then try to open the physical device volume, 00035 // otherwise we will assume it is a disk image 00036 // file they are writing to. (not fully supported yet) 00037 // 00038 if ((_tcslen(lpszVolumeName) == 2) && (lpszVolumeName[1] == _T(':'))) 00039 { 00040 _tcscpy(RealVolumeName, _T("\\\\.\\")); 00041 _tcscat(RealVolumeName, lpszVolumeName); 00042 } 00043 else 00044 { 00045 _tcscpy(RealVolumeName, lpszVolumeName); 00046 } 00047 00048 _tprintf(_T("Opening volume \'%s\'\n"), lpszVolumeName); 00049 00050 hDiskVolume = CreateFile(RealVolumeName, 00051 GENERIC_READ | GENERIC_WRITE, 00052 FILE_SHARE_READ | FILE_SHARE_WRITE, 00053 NULL, 00054 OPEN_EXISTING, 00055 0, 00056 NULL); 00057 00058 if (hDiskVolume == INVALID_HANDLE_VALUE) 00059 { 00060 _tprintf(_T("%s:%d: "), __FILE__, __LINE__); 00061 _tprintf(_T("Failed. Error code %ld.\n"), GetLastError()); 00062 return FALSE; 00063 } 00064 00065 return TRUE; 00066 } 00067 00068 void CloseVolume(void) 00069 { 00070 CloseHandle(hDiskVolume); 00071 } 00072 00073 BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer) 00074 { 00075 DWORD dwNumberOfBytesRead; 00076 DWORD dwFilePosition; 00077 BOOL bRetVal; 00078 00079 // 00080 // FIXME: this doesn't seem to handle the situation 00081 // properly when SectorNumber is bigger than the 00082 // amount of sectors on the disk. Seems to me that 00083 // the call to SetFilePointer() should just give an 00084 // out of bounds error or something but it doesn't. 00085 // 00086 dwFilePosition = SetFilePointer(hDiskVolume, (SectorNumber * 512), NULL, FILE_BEGIN); 00087 if (dwFilePosition != (SectorNumber * 512)) 00088 { 00089 _tprintf(_T("%s:%d: "), __FILE__, __LINE__); 00090 _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError()); 00091 return FALSE; 00092 } 00093 00094 bRetVal = ReadFile(hDiskVolume, SectorBuffer, 512, &dwNumberOfBytesRead, NULL); 00095 if (!bRetVal || (dwNumberOfBytesRead != 512)) 00096 { 00097 _tprintf(_T("%s:%d: "), __FILE__, __LINE__); 00098 _tprintf(_T("ReadFile() failed. Error code %ld.\n"), GetLastError()); 00099 return FALSE; 00100 } 00101 00102 return TRUE; 00103 } 00104 00105 BOOL WriteVolumeSector(ULONG SectorNumber, PVOID SectorBuffer) 00106 { 00107 DWORD dwNumberOfBytesWritten; 00108 DWORD dwFilePosition; 00109 BOOL bRetVal; 00110 00111 // 00112 // FIXME: this doesn't seem to handle the situation 00113 // properly when SectorNumber is bigger than the 00114 // amount of sectors on the disk. Seems to me that 00115 // the call to SetFilePointer() should just give an 00116 // out of bounds error or something but it doesn't. 00117 // 00118 dwFilePosition = SetFilePointer(hDiskVolume, (SectorNumber * 512), NULL, FILE_BEGIN); 00119 if (dwFilePosition != (SectorNumber * 512)) 00120 { 00121 _tprintf(_T("%s:%d: "), __FILE__, __LINE__); 00122 _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError()); 00123 return FALSE; 00124 } 00125 00126 bRetVal = WriteFile(hDiskVolume, SectorBuffer, 512, &dwNumberOfBytesWritten, NULL); 00127 if (!bRetVal || (dwNumberOfBytesWritten != 512)) 00128 { 00129 _tprintf(_T("%s:%d: "), __FILE__, __LINE__); 00130 _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError()); 00131 return FALSE; 00132 } 00133 00134 return TRUE; 00135 } Generated on Sun May 27 2012 04:19:20 for ReactOS by
1.7.6.1
|