-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathplatform_win32.c
167 lines (132 loc) · 4.39 KB
/
platform_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright (C) 2010 by Manish Regmi (regmi dot manish at gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/
#ifdef WIN32
#include <windows.h>
#include <winioctl.h>
#include <fcntl.h>
#include "platform.h"
#define DEVICE "\\\\.\\PhysicalDrive0"
int ext2explore_log(const char *str, ...);
FileHandle open_disk(const char *path, int *sect_size)
{
HANDLE handle;
DISK_GEOMETRY dsk;
BOOL bResult;
DWORD junk;
handle = CreateFileA(path, GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0, 0 );
if(handle == INVALID_HANDLE_VALUE)
{
ext2explore_log("Error Opening %s. Error Code %X\n", path, GetLastError());
return handle;
}
bResult = DeviceIoControl(handle, // device we are querying
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer, so pass zero
&dsk, sizeof(dsk), // output buffer
&junk, // discard count of bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O
if((!bResult) || (dsk.BytesPerSector < SECTOR_SIZE))
*sect_size = SECTOR_SIZE;
else
*sect_size = dsk.BytesPerSector;
return handle;
}
int get_ndisks()
{
HANDLE hDevice; // handle to the drive to be examined
int ndisks = 0;
char path[] = {"\\\\.\\PhysicalDrive0"};
do {
//TRACE("NDISKS %s", path);
hDevice = CreateFileA(path, // drive to open
GENERIC_READ,
FILE_SHARE_READ, // share mode
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // don't copy any file's attributes
if(hDevice == INVALID_HANDLE_VALUE)
{
ext2explore_log("Error Opening %s. Error Code %X\n", path, GetLastError());
break;
}
CloseHandle(hDevice);
ndisks++;
path[strlen(path) - 1] = (char)('0' + ndisks);
}while(hDevice != INVALID_HANDLE_VALUE);
return ndisks;
}
void close_disk(FileHandle handle)
{
CloseHandle(handle);
}
int read_disk(FileHandle hnd, void *ptr, lloff_t sector, int nsects, int sectorsize)
{
lloff_t offset;
DWORD rd, len;
DWORD low;
LONG high;
BOOL ret;
offset = sector * sectorsize;
low = (DWORD)(offset & 0xFFFFFFFF);
high = (DWORD)((offset >> 32)& 0xFFFFFFFF);
low = SetFilePointer(hnd, low, &high, FILE_BEGIN);
len = nsects * sectorsize;
ret = ReadFile(hnd, ptr, len, &rd, NULL);
if(!ret)
return -1;
return rd;
}
int write_disk(FileHandle hnd, void *ptr, lloff_t sector, int nsects, int sectorsize)
{
lloff_t offset;
DWORD rd, len;
DWORD low;
LONG high;
BOOL ret;
offset = sector * sectorsize;
low = (DWORD)(offset & 0xFFFFFFFF);
high = (DWORD)((offset >> 32)& 0xFFFFFFFF);
low = SetFilePointer(hnd, low, &high, FILE_BEGIN);
len = nsects * sectorsize;
ret = ReadFile(hnd, ptr, len, &rd, NULL);
if(!ret)
return -1;
return rd;
}
int get_nthdevice(char *path, int ndisks)
{
static int dev = 0;
if(!path)
return -1;
strcpy(path, DEVICE);
path[17] = dev + '0';
dev++;
if(dev >= ndisks)
{
dev = 0;
return -1;
}
return 0;
}
#endif