ReactOS  0.4.14-dev-1252-g782ff23
volume.c
Go to the documentation of this file.
1 /*
2  * FreeLoader - volume.c
3  *
4  * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include <windows.h>
22 #include <stdio.h>
23 #include <tchar.h>
24 #include "volume.h"
25 
27 
28 BOOL OpenVolume(LPCTSTR lpszVolumeName)
29 {
30  TCHAR RealVolumeName[MAX_PATH];
31 
32  //
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)
37  //
38  if ((_tcslen(lpszVolumeName) == 2) && (lpszVolumeName[1] == _T(':')))
39  {
40  _tcscpy(RealVolumeName, _T("\\\\.\\"));
41  _tcscat(RealVolumeName, lpszVolumeName);
42  }
43  else
44  {
45  _tcscpy(RealVolumeName, lpszVolumeName);
46  }
47 
48  _tprintf(_T("Opening volume \'%s\'\n"), lpszVolumeName);
49 
50  hDiskVolume = CreateFile(RealVolumeName,
53  NULL,
55  0,
56  NULL);
57 
59  {
60  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
61  _tprintf(_T("Failed. Error code %ld.\n"), GetLastError());
62  return FALSE;
63  }
64 
65  return TRUE;
66 }
67 
68 void CloseVolume(void)
69 {
71 }
72 
73 BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
74 {
75  DWORD dwNumberOfBytesRead;
76  DWORD dwFilePosition;
77  BOOL bRetVal;
78 
79  //
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.
85  //
86  dwFilePosition = SetFilePointer(hDiskVolume, (SectorNumber * 512), NULL, FILE_BEGIN);
87  if (dwFilePosition != (SectorNumber * 512))
88  {
89  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
90  _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError());
91  return FALSE;
92  }
93 
94  bRetVal = ReadFile(hDiskVolume, SectorBuffer, 512, &dwNumberOfBytesRead, NULL);
95  if (!bRetVal || (dwNumberOfBytesRead != 512))
96  {
97  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
98  _tprintf(_T("ReadFile() failed. Error code %ld.\n"), GetLastError());
99  return FALSE;
100  }
101 
102  return TRUE;
103 }
104 
105 BOOL WriteVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
106 {
107  DWORD dwNumberOfBytesWritten;
108  DWORD dwFilePosition;
109  BOOL bRetVal;
110 
111  //
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.
117  //
118  dwFilePosition = SetFilePointer(hDiskVolume, (SectorNumber * 512), NULL, FILE_BEGIN);
119  if (dwFilePosition != (SectorNumber * 512))
120  {
121  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
122  _tprintf(_T("SetFilePointer() failed. Error code %ld.\n"), GetLastError());
123  return FALSE;
124  }
125 
126  bRetVal = WriteFile(hDiskVolume, SectorBuffer, 512, &dwNumberOfBytesWritten, NULL);
127  if (!bRetVal || (dwNumberOfBytesWritten != 512))
128  {
129  _tprintf(_T("%s:%d: "), __FILE__, __LINE__);
130  _tprintf(_T("WriteFile() failed. Error code %ld.\n"), GetLastError());
131  return FALSE;
132  }
133 
134  return TRUE;
135 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
static HANDLE hDiskVolume
Definition: volume.c:26
#define FILE_SHARE_READ
Definition: compat.h:125
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define MAX_PATH
Definition: compat.h:26
BOOL OpenVolume(LPCTSTR lpszVolumeName)
Definition: volume.c:28
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GENERIC_READ
Definition: compat.h:124
BOOL WriteVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:105
#define FILE_BEGIN
Definition: winbase.h:112
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
void CloseVolume(void)
Definition: volume.c:68
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
unsigned int ULONG
Definition: retypes.h:1
BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:73
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123