File: w32disk.c

package info (click to toggle)
fosfat 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ansic: 3,052; sh: 837; makefile: 549
file content (231 lines) | stat: -rw-r--r-- 5,942 bytes parent folder | download
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * libw32disk: Win95 and WinNT low level disk access.
 * Copyright (C) 2007-2010 Mathieu Schroeter <mathieu@schroetersa.ch>
 *
 * This file is part of Fosfat.
 *
 * This library 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 3 of the License, or
 * (at your option) any later version.
 *
 * This library 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, see <http://www.gnu.org/licenses/>.
 *
 */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#include <winioctl.h>

#include "w32disk.h"

struct w32disk_s {
  HANDLE win95_vwin32;
  HANDLE winnt_disk;

  unsigned int drive_idx;
  size_t       sector_size;
};

#define ISHDL_VALID(h) ((h) != INVALID_HANDLE_VALUE)


static inline void
multiply_512 (DWORD input, DWORD *low_result, DWORD *high_result)
{
  *low_result  = input << 9;
  *high_result = input >> (32 - 9);
}

static HANDLE
win95_get_vwin32 (void)
{
  return CreateFile ("\\\\.\\vwin32", 0,
                     0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);
}

static HANDLE
winnt_get_disk (unsigned int drive_idx)
{
  char path_disk[] = "\\\\.\\A:";

  path_disk[4] = 'A' + drive_idx;
  return CreateFile (path_disk, GENERIC_READ,
                     FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                     OPEN_EXISTING, 0, NULL);
}

static int
win95_read_sectors (HANDLE vwin32_device, unsigned int drive_idx, void *buffer,
                    unsigned long int first_sector2read, size_t c_sectors2read)
{
  const DWORD vwin32_dioc_dos_driveinfo = 6;
  DWORD cb_outputbuffer;
  BOOL io_ctl;

  struct ctl_block_s {
    DWORD  sec_start;
    WORD   sec_nb;
    LPVOID pbuffer;
  } __attribute__ ((__packed__)) ctl_block;

  struct io_reg_s {
    DWORD ebx, edx, ecx, eax, edi, esi, flags;
  } regs = {
    0, 0, 0, 0, 0, 0, 0
  };

  if (vwin32_device == INVALID_HANDLE_VALUE)
    return 0;

  if (c_sectors2read > 65536L)
    return 0;

  ctl_block.sec_start = first_sector2read;
  ctl_block.sec_nb    = c_sectors2read;
  ctl_block.pbuffer   = buffer;

  /*
   * SI contains read/write mode flags
   *   SI = 0h for read and SI = 1h for write
   * CX must be equal to ffffh for int 21h's 7305h extention
   * DS:BX -> base addr of the control block structure
   * DL must contain the drive number (01h=A:, 02h=B: etc)
   */

  regs.eax = 0x7305;
  regs.ebx = (DWORD) &ctl_block;
  regs.ecx = (DWORD) -1;
  regs.edx = drive_idx + 1;

  io_ctl = DeviceIoControl (vwin32_device, vwin32_dioc_dos_driveinfo, &regs,
                            sizeof (regs), &regs, sizeof (regs),
                            &cb_outputbuffer, NULL);
  return io_ctl && !(regs.flags & 1);
}

static int
winnt_read_sectors (HANDLE disk_device, void *buffer,
                    unsigned long int first_sector2read,
                    size_t c_sectors2read)
{
  DWORD byte_idx_low, byte_idx_high;
  DWORD byte_cnt,     byte_cnt_high;
  DWORD cb_read;
  DWORD setfileptr;
  LONG  lbyte_idx_high;

  if (disk_device == INVALID_HANDLE_VALUE)
    return 0;

  multiply_512 (first_sector2read, &byte_idx_low, &byte_idx_high);
  multiply_512 (c_sectors2read,    &byte_cnt,     &byte_cnt_high);

  if (byte_cnt_high)
    return 0;

  lbyte_idx_high = byte_idx_high;

  SetLastError (ERROR_SUCCESS);

  setfileptr =
    SetFilePointer (disk_device, byte_idx_low, &lbyte_idx_high, FILE_BEGIN);

  if (setfileptr == (DWORD) -1 && GetLastError () != ERROR_SUCCESS)
    return 0;

  return ReadFile (disk_device, buffer, byte_cnt, &cb_read, NULL)
         && cb_read == byte_cnt;
}


unsigned int
w32disk_getdriveindex (w32disk_t *disk)
{
  return disk ? disk->drive_idx : 0;
}

w32disk_t *
w32disk_new (unsigned int drive_idx)
{
  w32disk_t *disk;

  disk = calloc (1, sizeof (w32disk_t));
  if (!disk)
    return NULL;

  disk->sector_size  = 512;
  disk->drive_idx    = drive_idx;
  disk->win95_vwin32 = win95_get_vwin32 ();

  if (!ISHDL_VALID (disk->win95_vwin32))
    disk->winnt_disk = winnt_get_disk (drive_idx);

  if (!ISHDL_VALID (disk->win95_vwin32) && ISHDL_VALID (disk->winnt_disk))
  {
    DISK_GEOMETRY geometry;
    DWORD cb_out;
    BOOL ioctl;

    ioctl = DeviceIoControl (disk->winnt_disk,
                             IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                             &geometry, sizeof (geometry), &cb_out, NULL);
    if (ioctl)
      disk->sector_size = geometry.BytesPerSector;
  }

  return disk;
}

void
w32disk_free (w32disk_t *disk)
{
  if (!disk)
    return;

  if (ISHDL_VALID (disk->win95_vwin32))
    CloseHandle (disk->win95_vwin32);
  if (ISHDL_VALID (disk->winnt_disk))
    CloseHandle (disk->winnt_disk);

  free (disk);
}

int
w32disk_valid (w32disk_t *disk)
{
  if (!disk)
    return 0;

  return ISHDL_VALID (disk->win95_vwin32) || ISHDL_VALID (disk->winnt_disk);
}

int
w32disk_readsectors (w32disk_t *disk, void *buffer,
                     unsigned long int sector_idx, size_t c_sectors)
{
  if (!disk)
    return 0;

  if (ISHDL_VALID (disk->win95_vwin32))
    return win95_read_sectors (disk->win95_vwin32,
                               disk->drive_idx, buffer, sector_idx, c_sectors);

  if (ISHDL_VALID (disk->winnt_disk))
    return winnt_read_sectors (disk->winnt_disk, buffer, sector_idx, c_sectors);

  return 0;
}

size_t
w32disk_sectorsize (w32disk_t *disk)
{
  return disk ? disk->sector_size : 0;
}