1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox storage devices:
|
---|
4 | * Host floppy block driver
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_HOST_FLOPPY
|
---|
28 | #ifdef RT_OS_LINUX
|
---|
29 | # include <sys/ioctl.h>
|
---|
30 | # include <linux/fd.h>
|
---|
31 | # include <sys/fcntl.h>
|
---|
32 | # include <errno.h>
|
---|
33 |
|
---|
34 | # elif defined(RT_OS_WINDOWS)
|
---|
35 | # include <windows.h>
|
---|
36 | # include <dbt.h>
|
---|
37 |
|
---|
38 | #elif defined(RT_OS_L4)
|
---|
39 |
|
---|
40 | #else /* !RT_OS_WINDOWS nor RT_OS_LINUX nor RT_OS_L4 */
|
---|
41 | # error "Unsupported Platform."
|
---|
42 | #endif /* !RT_OS_WINDOWS nor RT_OS_LINUX nor RT_OS_L4 */
|
---|
43 |
|
---|
44 | #include <VBox/pdmdrv.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/file.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/thread.h>
|
---|
49 | #include <iprt/semaphore.h>
|
---|
50 | #include <iprt/uuid.h>
|
---|
51 | #include <iprt/asm.h>
|
---|
52 | #include <iprt/critsect.h>
|
---|
53 |
|
---|
54 | #include "Builtins.h"
|
---|
55 | #include "DrvHostBase.h"
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Floppy driver instance data.
|
---|
60 | */
|
---|
61 | typedef struct DRVHOSTFLOPPY
|
---|
62 | {
|
---|
63 | DRVHOSTBASE Base;
|
---|
64 | /** Previous poll status. */
|
---|
65 | bool fPrevDiskIn;
|
---|
66 |
|
---|
67 | } DRVHOSTFLOPPY, *PDRVHOSTFLOPPY;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | #ifdef RT_OS_LINUX
|
---|
72 | /**
|
---|
73 | * Get media size and do change processing.
|
---|
74 | *
|
---|
75 | * @param pThis The instance data.
|
---|
76 | */
|
---|
77 | static DECLCALLBACK(int) drvHostFloppyGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
|
---|
78 | {
|
---|
79 | int rc = ioctl(pThis->FileDevice, FDFLUSH);
|
---|
80 | if (rc)
|
---|
81 | {
|
---|
82 | rc = RTErrConvertFromErrno (errno);
|
---|
83 | Log(("DrvHostFloppy: FDFLUSH ioctl(%s) failed, errno=%d rc=%Rrc\n", pThis->pszDevice, errno, rc));
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | floppy_drive_struct DrvStat;
|
---|
88 | rc = ioctl(pThis->FileDevice, FDGETDRVSTAT, &DrvStat);
|
---|
89 | if (rc)
|
---|
90 | {
|
---|
91 | rc = RTErrConvertFromErrno(errno);
|
---|
92 | Log(("DrvHostFloppy: FDGETDRVSTAT ioctl(%s) failed, errno=%d rc=%Rrc\n", pThis->pszDevice, errno, rc));
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 | pThis->fReadOnly = !(DrvStat.flags & FD_DISK_WRITABLE);
|
---|
96 |
|
---|
97 | return RTFileSeek(pThis->FileDevice, 0, RTFILE_SEEK_END, pcb);
|
---|
98 | }
|
---|
99 | #endif /* RT_OS_LINUX */
|
---|
100 |
|
---|
101 |
|
---|
102 | #ifdef RT_OS_LINUX
|
---|
103 | /**
|
---|
104 | * This thread will periodically poll the Floppy for media presence.
|
---|
105 | *
|
---|
106 | * @returns Ignored.
|
---|
107 | * @param ThreadSelf Handle of this thread. Ignored.
|
---|
108 | * @param pvUser Pointer to the driver instance structure.
|
---|
109 | */
|
---|
110 | static DECLCALLBACK(int) drvHostFloppyPoll(PDRVHOSTBASE pThis)
|
---|
111 | {
|
---|
112 | PDRVHOSTFLOPPY pThisFloppy = (PDRVHOSTFLOPPY)pThis;
|
---|
113 | floppy_drive_struct DrvStat;
|
---|
114 | int rc = ioctl(pThis->FileDevice, FDPOLLDRVSTAT, &DrvStat);
|
---|
115 | if (rc)
|
---|
116 | return RTErrConvertFromErrno(errno);
|
---|
117 |
|
---|
118 | RTCritSectEnter(&pThis->CritSect);
|
---|
119 | bool fDiskIn = !(DrvStat.flags & (FD_VERIFY | FD_DISK_NEWCHANGE));
|
---|
120 | if ( fDiskIn
|
---|
121 | && !pThisFloppy->fPrevDiskIn)
|
---|
122 | {
|
---|
123 | if (pThis->fMediaPresent)
|
---|
124 | DRVHostBaseMediaNotPresent(pThis);
|
---|
125 | rc = DRVHostBaseMediaPresent(pThis);
|
---|
126 | if (RT_FAILURE(rc))
|
---|
127 | {
|
---|
128 | pThisFloppy->fPrevDiskIn = fDiskIn;
|
---|
129 | RTCritSectLeave(&pThis->CritSect);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if ( !fDiskIn
|
---|
135 | && pThisFloppy->fPrevDiskIn
|
---|
136 | && pThis->fMediaPresent)
|
---|
137 | DRVHostBaseMediaNotPresent(pThis);
|
---|
138 | pThisFloppy->fPrevDiskIn = fDiskIn;
|
---|
139 |
|
---|
140 | RTCritSectLeave(&pThis->CritSect);
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 | #endif /* RT_OS_LINUX */
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
148 | */
|
---|
149 | static DECLCALLBACK(int) drvHostFloppyConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
150 | {
|
---|
151 | PDRVHOSTFLOPPY pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTFLOPPY);
|
---|
152 | LogFlow(("drvHostFloppyConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Validate configuration.
|
---|
156 | */
|
---|
157 | if (!CFGMR3AreValuesValid(pCfgHandle, "Path\0ReadOnly\0Interval\0Locked\0BIOSVisible\0"))
|
---|
158 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Init instance data.
|
---|
162 | */
|
---|
163 | int rc = DRVHostBaseInitData(pDrvIns, pCfgHandle, PDMBLOCKTYPE_FLOPPY_1_44);
|
---|
164 | if (RT_SUCCESS(rc))
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * Override stuff.
|
---|
168 | */
|
---|
169 | #ifdef RT_OS_LINUX
|
---|
170 | pThis->Base.pfnPoll = drvHostFloppyPoll;
|
---|
171 | pThis->Base.pfnGetMediaSize = drvHostFloppyGetMediaSize;
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * 2nd init part.
|
---|
176 | */
|
---|
177 | rc = DRVHostBaseInitFinish(&pThis->Base);
|
---|
178 | }
|
---|
179 | if (RT_FAILURE(rc))
|
---|
180 | {
|
---|
181 | if (!pThis->Base.fAttachFailError)
|
---|
182 | {
|
---|
183 | /* Suppressing the attach failure error must not affect the normal
|
---|
184 | * DRVHostBaseDestruct, so reset this flag below before leaving. */
|
---|
185 | pThis->Base.fKeepInstance = true;
|
---|
186 | rc = VINF_SUCCESS;
|
---|
187 | }
|
---|
188 | DRVHostBaseDestruct(pDrvIns);
|
---|
189 | pThis->Base.fKeepInstance = false;
|
---|
190 | }
|
---|
191 |
|
---|
192 | LogFlow(("drvHostFloppyConstruct: returns %Rrc\n", rc));
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Block driver registration record.
|
---|
199 | */
|
---|
200 | const PDMDRVREG g_DrvHostFloppy =
|
---|
201 | {
|
---|
202 | /* u32Version */
|
---|
203 | PDM_DRVREG_VERSION,
|
---|
204 | /* szDriverName */
|
---|
205 | "HostFloppy",
|
---|
206 | /* pszDescription */
|
---|
207 | "Host Floppy Block Driver.",
|
---|
208 | /* fFlags */
|
---|
209 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
210 | /* fClass. */
|
---|
211 | PDM_DRVREG_CLASS_BLOCK,
|
---|
212 | /* cMaxInstances */
|
---|
213 | ~0,
|
---|
214 | /* cbInstance */
|
---|
215 | sizeof(DRVHOSTFLOPPY),
|
---|
216 | /* pfnConstruct */
|
---|
217 | drvHostFloppyConstruct,
|
---|
218 | /* pfnDestruct */
|
---|
219 | DRVHostBaseDestruct,
|
---|
220 | /* pfnIOCtl */
|
---|
221 | NULL,
|
---|
222 | /* pfnPowerOn */
|
---|
223 | NULL,
|
---|
224 | /* pfnReset */
|
---|
225 | NULL,
|
---|
226 | /* pfnSuspend */
|
---|
227 | NULL,
|
---|
228 | /* pfnResume */
|
---|
229 | NULL,
|
---|
230 | /* pfnDetach */
|
---|
231 | NULL
|
---|
232 | };
|
---|
233 |
|
---|