aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/fhandler_floppy.cc
blob: 2d17d99e640910b16efd6d56860c2f3c1701053d (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* fhandler_floppy.cc.  See fhandler.h for a description of the
   fhandler classes.

   Copyright 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <sys/termios.h>
#include <unistd.h>
#include <winioctl.h>
#include <asm/socket.h>
#include <cygwin/hdreg.h>
#include <cygwin/fs.h>
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "cygerrno.h"

/**********************************************************************/
/* fhandler_dev_floppy */

int
fhandler_dev_floppy::is_eom (int win_error)
{
  int ret = (win_error == ERROR_INVALID_PARAMETER);
  if (ret)
    debug_printf ("end of medium");
  return ret;
}

int
fhandler_dev_floppy::is_eof (int)
{
  int ret = 0;
  if (ret)
    debug_printf ("end of file");
  return ret;
}

fhandler_dev_floppy::fhandler_dev_floppy ()
  : fhandler_dev_raw ()
{
}

int
fhandler_dev_floppy::open (int flags, mode_t)
{
  /* The correct size of the buffer would be 512 bytes,
   * which is the atomic size, supported by WinNT.
   * Unfortunately, the performance is worse than
   * access to file system on same device!
   * Setting buffer size to a relatively big value
   * increases performance by means.
   * The new ioctl call with 'rdevio.h' header file
   * supports changing this value.
   *
   * Let's be smart: Let's take a multiplier of typical tar
   * and cpio buffer sizes by default!
  */
  devbufsiz = 61440L; /* 512L; */
  return fhandler_dev_raw::open (flags);
}

int
fhandler_dev_floppy::close (void)
{
  int ret;

  ret = writebuf ();
  if (ret)
    {
      fhandler_dev_raw::close ();
      return ret;
    }
  return fhandler_dev_raw::close ();
}

_off64_t
fhandler_dev_floppy::lseek (_off64_t offset, int whence)
{
  int ret;
  char buf[512];
  _off64_t drive_size = 0;
  _off64_t lloffset = offset;
  _off64_t current_position;
  _off64_t sector_aligned_offset;
  _off64_t bytes_left;
  DWORD low;
  LONG high = 0;

  DISK_GEOMETRY di;
  PARTITION_INFORMATION pi;
  DWORD bytes_read;

  if (!DeviceIoControl (get_handle (),
			  IOCTL_DISK_GET_DRIVE_GEOMETRY,
			  NULL, 0,
			  &di, sizeof (di),
			  &bytes_read, NULL))
    {
      __seterrno ();
      return -1;
    }
  debug_printf ("disk geometry: (%ld cyl)*(%ld trk)*(%ld sec)*(%ld bps)",
		 di.Cylinders.LowPart,
		 di.TracksPerCylinder,
		 di.SectorsPerTrack,
		 di.BytesPerSector);
  if (DeviceIoControl (get_handle (),
			 IOCTL_DISK_GET_PARTITION_INFO,
			 NULL, 0,
			 &pi, sizeof (pi),
			 &bytes_read, NULL))
    {
      debug_printf ("partition info: %ld (%ld)",
		      pi.StartingOffset.LowPart,
		      pi.PartitionLength.LowPart);
      drive_size = pi.PartitionLength.QuadPart;
    }
  else
    {
      drive_size = di.Cylinders.QuadPart * di.TracksPerCylinder *
		   di.SectorsPerTrack * di.BytesPerSector;
    }
  debug_printf ("drive size: %ld", drive_size);

  if (whence == SEEK_END && drive_size > 0)
    {
      lloffset = offset + drive_size;
      whence = SEEK_SET;
    }

  if (whence == SEEK_CUR)
    {
      low = SetFilePointer (get_handle (), 0, &high, FILE_CURRENT);
      if (low == INVALID_SET_FILE_POINTER && GetLastError ())
	{
	  __seterrno ();
	  return -1;
	}
      current_position = low + ((_off64_t) high << 32);
      if (is_writing ())
	current_position += devbufend - devbufstart;
      else
	current_position -= devbufend - devbufstart;

      lloffset += current_position;
      whence = SEEK_SET;
    }

  if (lloffset < 0 ||
      drive_size > 0 && lloffset > drive_size)
    {
      set_errno (EINVAL);
      return -1;
    }

  /* FIXME: sector can possibly be not 512 bytes long */
  sector_aligned_offset = (lloffset / 512) * 512;
  bytes_left = lloffset - sector_aligned_offset;

  if (whence == SEEK_SET)
    {
      /* Invalidate buffer. */
      ret = writebuf ();
      if (ret)
	return ret;
      devbufstart = devbufend = 0;

      low = sector_aligned_offset & UINT32_MAX;
      high = sector_aligned_offset >> 32;
      if (SetFilePointer (get_handle (), low, &high, FILE_BEGIN)
	  == INVALID_SET_FILE_POINTER && GetLastError ())
	{
	  __seterrno ();
	  return -1;
	}

      size_t len = bytes_left;
      raw_read (buf, len);
      return sector_aligned_offset + bytes_left;
    }

  set_errno (EINVAL);
  return -1;
}

int
fhandler_dev_floppy::ioctl (unsigned int cmd, void *buf)
{
  DISK_GEOMETRY di;
  PARTITION_INFORMATION pi;
  DWORD bytes_read;
  _off64_t drive_size = 0;
  _off64_t start = 0;
  switch (cmd)
    {
    case HDIO_GETGEO:
      {
	debug_printf ("HDIO_GETGEO");
	if (!DeviceIoControl (get_handle (),
			      IOCTL_DISK_GET_DRIVE_GEOMETRY,
			      NULL, 0,
			      &di, sizeof (di),
			      &bytes_read, NULL))
	  {
	    __seterrno ();
	    return -1;
	  }
	debug_printf ("disk geometry: (%ld cyl)*(%ld trk)*(%ld sec)*(%ld bps)",
		      di.Cylinders.LowPart,
		      di.TracksPerCylinder,
		      di.SectorsPerTrack,
		      di.BytesPerSector);
	if (DeviceIoControl (get_handle (),
			     IOCTL_DISK_GET_PARTITION_INFO,
			     NULL, 0,
			     &pi, sizeof (pi),
			     &bytes_read, NULL))
	  {
	    debug_printf ("partition info: %ld (%ld)",
			  pi.StartingOffset.LowPart,
			  pi.PartitionLength.LowPart);
	    start = pi.StartingOffset.QuadPart >> 9ULL;
	  }
	struct hd_geometry *geo = (struct hd_geometry *) buf;
	geo->heads = di.TracksPerCylinder;
	geo->sectors = di.SectorsPerTrack;
	geo->cylinders = di.Cylinders.LowPart;
	geo->start = start;
	return 0;
      }
    case BLKGETSIZE:
    case BLKGETSIZE64:
      {
	debug_printf ("BLKGETSIZE");
	if (!DeviceIoControl (get_handle (),
			      IOCTL_DISK_GET_DRIVE_GEOMETRY,
			      NULL, 0,
			      &di, sizeof (di),
			      &bytes_read, NULL))
	  {
	    __seterrno ();
	    return -1;
	  }
	debug_printf ("disk geometry: (%ld cyl)*(%ld trk)*(%ld sec)*(%ld bps)",
		      di.Cylinders.LowPart,
		      di.TracksPerCylinder,
		      di.SectorsPerTrack,
		      di.BytesPerSector);
	if (DeviceIoControl (get_handle (),
			     IOCTL_DISK_GET_PARTITION_INFO,
			     NULL, 0,
			     &pi, sizeof (pi),
			     &bytes_read, NULL))
	  {
	    debug_printf ("partition info: %ld (%ld)",
			  pi.StartingOffset.LowPart,
			  pi.PartitionLength.LowPart);
	    drive_size = pi.PartitionLength.QuadPart;
	  }
	else
	  {
	    drive_size = di.Cylinders.QuadPart * di.TracksPerCylinder *
			 di.SectorsPerTrack * di.BytesPerSector;
	  }
	if (cmd == BLKGETSIZE)
	  *(long *)buf = drive_size >> 9UL;
	else
	  *(_off64_t *)buf = drive_size;
	return 0;
      }
    case BLKRRPART:
      {
	debug_printf ("BLKRRPART");
	if (!DeviceIoControl (get_handle (),
			      IOCTL_DISK_UPDATE_DRIVE_SIZE,
			      NULL, 0,
			      &di, sizeof (di),
			      &bytes_read, NULL))
	  {
	    __seterrno ();
	    return -1;
	  }
	return 0;
      }
    case BLKSSZGET:
      {
	debug_printf ("BLKSSZGET");
	if (!DeviceIoControl (get_handle (),
			      IOCTL_DISK_GET_DRIVE_GEOMETRY,
			      NULL, 0,
			      &di, sizeof (di),
			      &bytes_read, NULL))
	  {
	    __seterrno ();
	    return -1;
	  }
	debug_printf ("disk geometry: (%ld cyl)*(%ld trk)*(%ld sec)*(%ld bps)",
		      di.Cylinders.LowPart,
		      di.TracksPerCylinder,
		      di.SectorsPerTrack,
		      di.BytesPerSector);
	*(int *)buf = di.BytesPerSector;
	return 0;
      }
    default:
      return fhandler_dev_raw::ioctl (cmd, buf);
    }
}