2 * FreeLoader - volume.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.
26 static HANDLE hDiskVolume
= NULL
;
28 BOOL
OpenVolume(LPCTSTR lpszVolumeName
)
30 TCHAR RealVolumeName
[MAX_PATH
];
33 // If they passed in a drive letter (e.g. "A:")
34 // then try to open the physical device volume,
35 // otherwise we will assume it is a disk image
36 // file they are writing to. (not fully supported yet)
38 if ((_tcslen(lpszVolumeName
) == 2) && (lpszVolumeName
[1] == _T(':')))
40 _tcscpy(RealVolumeName
, _T("\\\\.\\"));
41 _tcscat(RealVolumeName
, lpszVolumeName
);
45 _tcscpy(RealVolumeName
, lpszVolumeName
);
48 _tprintf(_T("Opening volume \'%s\'\n"), lpszVolumeName
);
50 hDiskVolume
= CreateFile(RealVolumeName
,
51 GENERIC_READ
| GENERIC_WRITE
,
52 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
58 if (hDiskVolume
== INVALID_HANDLE_VALUE
)
60 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
61 _tprintf(_T("Failed. Error code %ld.\n"), GetLastError());
68 void CloseVolume(void)
70 CloseHandle(hDiskVolume
);
73 BOOL
ReadVolumeSector(ULONG SectorNumber
, PVOID SectorBuffer
)
75 DWORD dwNumberOfBytesRead
;
80 // FIXME: this doesn't seem to handle the situation
81 // properly when SectorNumber is bigger than the
82 // amount of sectors on the disk. Seems to me that
83 // the call to SetFilePointer() should just give an
84 // out of bounds error or something but it doesn't.
86 dwFilePosition
= SetFilePointer(hDiskVolume
, (SectorNumber
* 512), NULL
, FILE_BEGIN
);
87 if (dwFilePosition
!= (SectorNumber
* 512))
89 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
90 _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError());
94 bRetVal
= ReadFile(hDiskVolume
, SectorBuffer
, 512, &dwNumberOfBytesRead
, NULL
);
95 if (!bRetVal
|| (dwNumberOfBytesRead
!= 512))
97 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
98 _tprintf(_T("ReadFile() failed. Error code %ld.\n"), GetLastError());
105 BOOL
WriteVolumeSector(ULONG SectorNumber
, PVOID SectorBuffer
)
107 DWORD dwNumberOfBytesWritten
;
108 DWORD dwFilePosition
;
112 // FIXME: this doesn't seem to handle the situation
113 // properly when SectorNumber is bigger than the
114 // amount of sectors on the disk. Seems to me that
115 // the call to SetFilePointer() should just give an
116 // out of bounds error or something but it doesn't.
118 dwFilePosition
= SetFilePointer(hDiskVolume
, (SectorNumber
* 512), NULL
, FILE_BEGIN
);
119 if (dwFilePosition
!= (SectorNumber
* 512))
121 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
122 _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError());
126 bRetVal
= WriteFile(hDiskVolume
, SectorBuffer
, 512, &dwNumberOfBytesWritten
, NULL
);
127 if (!bRetVal
|| (dwNumberOfBytesWritten
!= 512))
129 _tprintf(_T("%s:%d: "), __FILE__
, __LINE__
);
130 _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());