2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
29 #include <ext4_config.h>
\r
30 #include <ext4_blockdev.h>
\r
31 #include <ext4_errno.h>
\r
33 #include <stdbool.h>
\r
37 #include <windows.h>
\r
38 #include <winioctl.h>
\r
41 /**@brief Default filename.*/
\r
42 static const char *fname = "ext2";
\r
44 /**@brief IO block size.*/
\r
45 #define EXT4_IORAW_BSIZE 512
\r
47 /**@brief Image file descriptor.*/
\r
48 static HANDLE dev_file;
\r
51 /**********************BLOCKDEV INTERFACE**************************************/
\r
52 static int io_raw_open(struct ext4_blockdev *bdev);
\r
53 static int io_raw_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
\r
55 static int io_raw_bwrite(struct ext4_blockdev *bdev, const void *buf,
\r
56 uint64_t blk_id, uint32_t blk_cnt);
\r
57 static int io_raw_close(struct ext4_blockdev *bdev);
\r
62 /******************************************************************************/
\r
63 EXT4_BLOCKDEV_STATIC_INSTANCE(
\r
73 /******************************************************************************/
\r
74 static int io_raw_open(struct ext4_blockdev *bdev)
\r
79 BOOL bResult = FALSE;
\r
82 sprintf(path, "\\\\.\\%s", fname);
\r
84 dev_file = CreateFile (path,
\r
85 GENERIC_READ | GENERIC_WRITE,
\r
86 FILE_SHARE_WRITE | FILE_SHARE_READ,
\r
89 FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
\r
92 if (dev_file == INVALID_HANDLE_VALUE){
\r
96 bResult = DeviceIoControl(dev_file,
\r
97 IOCTL_DISK_GET_DRIVE_GEOMETRY,
\r
101 (LPOVERLAPPED) NULL);
\r
103 if(bResult == FALSE){
\r
104 CloseHandle(dev_file);
\r
109 disk_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
\r
110 (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
\r
112 _filedev.ph_bsize = pdg.BytesPerSector;
\r
113 _filedev.ph_bcnt = disk_size / pdg.BytesPerSector;
\r
119 /******************************************************************************/
\r
121 static int io_raw_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
\r
124 long hipart = blk_id >> (32-9);
\r
125 long lopart = blk_id << 9;
\r
129 lopart = SetFilePointer (dev_file, lopart, &hipart, FILE_BEGIN);
\r
131 if (lopart == -1 && NO_ERROR != (err = GetLastError ()))
\r
138 if (!ReadFile (dev_file, buf, blk_cnt * 512, &n, NULL))
\r
140 err = GetLastError ();
\r
146 /******************************************************************************/
\r
147 static int io_raw_bwrite(struct ext4_blockdev *bdev, const void *buf,
\r
148 uint64_t blk_id, uint32_t blk_cnt)
\r
150 long hipart = blk_id >> (32-9);
\r
151 long lopart = blk_id << 9;
\r
155 lopart = SetFilePointer (dev_file, lopart, &hipart, FILE_BEGIN);
\r
157 if (lopart == -1 && NO_ERROR != (err = GetLastError ()))
\r
164 if (!WriteFile (dev_file, buf, blk_cnt * 512, &n, NULL))
\r
166 err = GetLastError ();
\r
172 /******************************************************************************/
\r
173 static int io_raw_close(struct ext4_blockdev *bdev)
\r
175 CloseHandle(dev_file);
\r
179 /******************************************************************************/
\r
180 struct ext4_blockdev* ext4_io_raw_dev_get(void)
\r
184 /******************************************************************************/
\r
185 void ext4_io_raw_filename(const char *n)
\r
190 /******************************************************************************/
\r