ReactOS 0.4.16-dev-109-gf4cb10f
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
28BOOL 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
68void CloseVolume(void)
69{
71}
72
73BOOL 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
105BOOL 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}
HANDLE OpenVolume(const TCHAR *Volume, BOOLEAN AllowRemote, BOOLEAN NtfsOnly)
Definition: common.c:49
BOOL ReadVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:73
void CloseVolume(void)
Definition: volume.c:68
BOOL WriteVolumeSector(ULONG SectorNumber, PVOID SectorBuffer)
Definition: volume.c:105
static HANDLE hDiskVolume
Definition: volume.c:26
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define FILE_BEGIN
Definition: compat.h:761
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _tcscat
Definition: tchar.h:622
#define _tcscpy
Definition: tchar.h:623
#define _tprintf
Definition: tchar.h:506
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
uint32_t ULONG
Definition: typedefs.h:59
#define _T(x)
Definition: vfdio.h:22
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CreateFile
Definition: winbase.h:3749
char TCHAR
Definition: xmlstorage.h:189
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define _tcslen
Definition: xmlstorage.h:198