1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox storage devices:
|
---|
4 | * Host floppy block driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #define LOG_GROUP LOG_GROUP_DRV_HOST_FLOPPY
|
---|
24 | #ifdef RT_OS_LINUX
|
---|
25 | # include <sys/ioctl.h>
|
---|
26 | # include <linux/fd.h>
|
---|
27 | # include <sys/fcntl.h>
|
---|
28 | # include <errno.h>
|
---|
29 |
|
---|
30 | # elif defined(RT_OS_WINDOWS)
|
---|
31 | # include <windows.h>
|
---|
32 | # include <dbt.h>
|
---|
33 |
|
---|
34 | #elif defined(RT_OS_L4)
|
---|
35 |
|
---|
36 | #else /* !RT_OS_WINDOWS nor RT_OS_LINUX nor RT_OS_L4 */
|
---|
37 | # error "Unsupported Platform."
|
---|
38 | #endif /* !RT_OS_WINDOWS nor RT_OS_LINUX nor RT_OS_L4 */
|
---|
39 |
|
---|
40 | #include <VBox/vmm/pdmdrv.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/file.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 | #include <iprt/semaphore.h>
|
---|
46 | #include <iprt/uuid.h>
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/critsect.h>
|
---|
49 |
|
---|
50 | #include "VBoxDD.h"
|
---|
51 | #include "DrvHostBase.h"
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Floppy driver instance data.
|
---|
56 | */
|
---|
57 | typedef struct DRVHOSTFLOPPY
|
---|
58 | {
|
---|
59 | DRVHOSTBASE Base;
|
---|
60 | /** Previous poll status. */
|
---|
61 | bool fPrevDiskIn;
|
---|
62 |
|
---|
63 | } DRVHOSTFLOPPY, *PDRVHOSTFLOPPY;
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | #ifdef RT_OS_WINDOWS
|
---|
68 | /**
|
---|
69 | * Get media size - needs a special IOCTL.
|
---|
70 | *
|
---|
71 | * @param pThis The instance data.
|
---|
72 | */
|
---|
73 | static DECLCALLBACK(int) drvHostFloppyGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
|
---|
74 | {
|
---|
75 | DISK_GEOMETRY geom;
|
---|
76 | DWORD cbBytesReturned;
|
---|
77 | int rc;
|
---|
78 | int cbSectors;
|
---|
79 |
|
---|
80 | memset(&geom, 0, sizeof(geom));
|
---|
81 | rc = DeviceIoControl((HANDLE)RTFileToNative(pThis->hFileDevice), IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
---|
82 | NULL, 0, &geom, sizeof(geom), &cbBytesReturned, NULL);
|
---|
83 | if (rc) {
|
---|
84 | cbSectors = geom.Cylinders.QuadPart * geom.TracksPerCylinder * geom.SectorsPerTrack;
|
---|
85 | *pcb = cbSectors * geom.BytesPerSector;
|
---|
86 | rc = VINF_SUCCESS;
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | DWORD dwLastError;
|
---|
91 |
|
---|
92 | dwLastError = GetLastError();
|
---|
93 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
94 | Log(("DrvHostFloppy: IOCTL_DISK_GET_DRIVE_GEOMETRY(%s) failed, LastError=%d rc=%Rrc\n",
|
---|
95 | pThis->pszDevice, dwLastError, rc));
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 | #endif /* RT_OS_WINDOWS */
|
---|
102 |
|
---|
103 | #ifdef RT_OS_LINUX
|
---|
104 | /**
|
---|
105 | * Get media size and do change processing.
|
---|
106 | *
|
---|
107 | * @param pThis The instance data.
|
---|
108 | */
|
---|
109 | static DECLCALLBACK(int) drvHostFloppyGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
|
---|
110 | {
|
---|
111 | int rc = ioctl(RTFileToNative(pThis->hFileDevice), FDFLUSH);
|
---|
112 | if (rc)
|
---|
113 | {
|
---|
114 | rc = RTErrConvertFromErrno (errno);
|
---|
115 | Log(("DrvHostFloppy: FDFLUSH ioctl(%s) failed, errno=%d rc=%Rrc\n", pThis->pszDevice, errno, rc));
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | floppy_drive_struct DrvStat;
|
---|
120 | rc = ioctl(RTFileToNative(pThis->hFileDevice), FDGETDRVSTAT, &DrvStat);
|
---|
121 | if (rc)
|
---|
122 | {
|
---|
123 | rc = RTErrConvertFromErrno(errno);
|
---|
124 | Log(("DrvHostFloppy: FDGETDRVSTAT ioctl(%s) failed, errno=%d rc=%Rrc\n", pThis->pszDevice, errno, rc));
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 | pThis->fReadOnly = !(DrvStat.flags & FD_DISK_WRITABLE);
|
---|
128 |
|
---|
129 | return RTFileSeek(pThis->hFileDevice, 0, RTFILE_SEEK_END, pcb);
|
---|
130 | }
|
---|
131 | #endif /* RT_OS_LINUX */
|
---|
132 |
|
---|
133 |
|
---|
134 | #ifdef RT_OS_LINUX
|
---|
135 | /**
|
---|
136 | * This thread will periodically poll the Floppy for media presence.
|
---|
137 | *
|
---|
138 | * @returns Ignored.
|
---|
139 | * @param ThreadSelf Handle of this thread. Ignored.
|
---|
140 | * @param pvUser Pointer to the driver instance structure.
|
---|
141 | */
|
---|
142 | static DECLCALLBACK(int) drvHostFloppyPoll(PDRVHOSTBASE pThis)
|
---|
143 | {
|
---|
144 | PDRVHOSTFLOPPY pThisFloppy = (PDRVHOSTFLOPPY)pThis;
|
---|
145 | floppy_drive_struct DrvStat;
|
---|
146 | int rc = ioctl(RTFileToNative(pThis->hFileDevice), FDPOLLDRVSTAT, &DrvStat);
|
---|
147 | if (rc)
|
---|
148 | return RTErrConvertFromErrno(errno);
|
---|
149 |
|
---|
150 | RTCritSectEnter(&pThis->CritSect);
|
---|
151 | bool fDiskIn = !(DrvStat.flags & (FD_VERIFY | FD_DISK_NEWCHANGE));
|
---|
152 | if ( fDiskIn
|
---|
153 | && !pThisFloppy->fPrevDiskIn)
|
---|
154 | {
|
---|
155 | if (pThis->fMediaPresent)
|
---|
156 | DRVHostBaseMediaNotPresent(pThis);
|
---|
157 | rc = DRVHostBaseMediaPresent(pThis);
|
---|
158 | if (RT_FAILURE(rc))
|
---|
159 | {
|
---|
160 | pThisFloppy->fPrevDiskIn = fDiskIn;
|
---|
161 | RTCritSectLeave(&pThis->CritSect);
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | if ( !fDiskIn
|
---|
167 | && pThisFloppy->fPrevDiskIn
|
---|
168 | && pThis->fMediaPresent)
|
---|
169 | DRVHostBaseMediaNotPresent(pThis);
|
---|
170 | pThisFloppy->fPrevDiskIn = fDiskIn;
|
---|
171 |
|
---|
172 | RTCritSectLeave(&pThis->CritSect);
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | }
|
---|
175 | #endif /* RT_OS_LINUX */
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
180 | */
|
---|
181 | static DECLCALLBACK(int) drvHostFloppyConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
182 | {
|
---|
183 | PDRVHOSTFLOPPY pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTFLOPPY);
|
---|
184 | LogFlow(("drvHostFloppyConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Validate configuration.
|
---|
188 | */
|
---|
189 | if (!CFGMR3AreValuesValid(pCfg, "Path\0ReadOnly\0Interval\0Locked\0BIOSVisible\0"))
|
---|
190 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Init instance data.
|
---|
194 | */
|
---|
195 | int rc = DRVHostBaseInitData(pDrvIns, pCfg, PDMBLOCKTYPE_FLOPPY_1_44);
|
---|
196 | if (RT_SUCCESS(rc))
|
---|
197 | {
|
---|
198 | /*
|
---|
199 | * Override stuff.
|
---|
200 | */
|
---|
201 | #ifdef RT_OS_WINDOWS
|
---|
202 | pThis->Base.pfnGetMediaSize = drvHostFloppyGetMediaSize;
|
---|
203 | #endif
|
---|
204 | #ifdef RT_OS_LINUX
|
---|
205 | pThis->Base.pfnPoll = drvHostFloppyPoll;
|
---|
206 | pThis->Base.pfnGetMediaSize = drvHostFloppyGetMediaSize;
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * 2nd init part.
|
---|
211 | */
|
---|
212 | rc = DRVHostBaseInitFinish(&pThis->Base);
|
---|
213 | }
|
---|
214 | if (RT_FAILURE(rc))
|
---|
215 | {
|
---|
216 | if (!pThis->Base.fAttachFailError)
|
---|
217 | {
|
---|
218 | /* Suppressing the attach failure error must not affect the normal
|
---|
219 | * DRVHostBaseDestruct, so reset this flag below before leaving. */
|
---|
220 | pThis->Base.fKeepInstance = true;
|
---|
221 | rc = VINF_SUCCESS;
|
---|
222 | }
|
---|
223 | DRVHostBaseDestruct(pDrvIns);
|
---|
224 | pThis->Base.fKeepInstance = false;
|
---|
225 | }
|
---|
226 |
|
---|
227 | LogFlow(("drvHostFloppyConstruct: returns %Rrc\n", rc));
|
---|
228 | return rc;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Block driver registration record.
|
---|
234 | */
|
---|
235 | const PDMDRVREG g_DrvHostFloppy =
|
---|
236 | {
|
---|
237 | /* u32Version */
|
---|
238 | PDM_DRVREG_VERSION,
|
---|
239 | /* szName */
|
---|
240 | "HostFloppy",
|
---|
241 | /* szRCMod */
|
---|
242 | "",
|
---|
243 | /* szR0Mod */
|
---|
244 | "",
|
---|
245 | /* pszDescription */
|
---|
246 | "Host Floppy Block Driver.",
|
---|
247 | /* fFlags */
|
---|
248 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
249 | /* fClass. */
|
---|
250 | PDM_DRVREG_CLASS_BLOCK,
|
---|
251 | /* cMaxInstances */
|
---|
252 | ~0U,
|
---|
253 | /* cbInstance */
|
---|
254 | sizeof(DRVHOSTFLOPPY),
|
---|
255 | /* pfnConstruct */
|
---|
256 | drvHostFloppyConstruct,
|
---|
257 | /* pfnDestruct */
|
---|
258 | DRVHostBaseDestruct,
|
---|
259 | /* pfnRelocate */
|
---|
260 | NULL,
|
---|
261 | /* pfnIOCtl */
|
---|
262 | NULL,
|
---|
263 | /* pfnPowerOn */
|
---|
264 | NULL,
|
---|
265 | /* pfnReset */
|
---|
266 | NULL,
|
---|
267 | /* pfnSuspend */
|
---|
268 | NULL,
|
---|
269 | /* pfnResume */
|
---|
270 | NULL,
|
---|
271 | /* pfnAttach */
|
---|
272 | NULL,
|
---|
273 | /* pfnDetach */
|
---|
274 | NULL,
|
---|
275 | /* pfnPowerOff */
|
---|
276 | NULL,
|
---|
277 | /* pfnSoftReset */
|
---|
278 | NULL,
|
---|
279 | /* u32EndVersion */
|
---|
280 | PDM_DRVREG_VERSION
|
---|
281 | };
|
---|
282 |
|
---|