1 | /* $Id: DrvRawFile.cpp 26173 2010-02-02 21:11:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox stream drivers - Raw file output.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DEFAULT
|
---|
27 | #include <VBox/pdmdrv.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/file.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #include <iprt/semaphore.h>
|
---|
32 | #include <iprt/stream.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/uuid.h>
|
---|
35 |
|
---|
36 | #include "Builtins.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *******************************************************************************/
|
---|
42 | /** Converts a pointer to DRVRAWFILE::IMedia to a PDRVRAWFILE. */
|
---|
43 | #define PDMISTREAM_2_DRVRAWFILE(pInterface) ( (PDRVRAWFILE)((uintptr_t)pInterface - RT_OFFSETOF(DRVRAWFILE, IStream)) )
|
---|
44 |
|
---|
45 | /** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
|
---|
46 | #define PDMIBASE_2_DRVINS(pInterface) ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *******************************************************************************/
|
---|
52 | /**
|
---|
53 | * Raw file output driver instance data.
|
---|
54 | *
|
---|
55 | * @implements PDMISTREAM
|
---|
56 | */
|
---|
57 | typedef struct DRVRAWFILE
|
---|
58 | {
|
---|
59 | /** The stream interface. */
|
---|
60 | PDMISTREAM IStream;
|
---|
61 | /** Pointer to the driver instance. */
|
---|
62 | PPDMDRVINS pDrvIns;
|
---|
63 | /** Pointer to the file name. (Freed by MM) */
|
---|
64 | char *pszLocation;
|
---|
65 | /** Flag whether VirtualBox represents the server or client side. */
|
---|
66 | RTFILE OutputFile;
|
---|
67 | } DRVRAWFILE, *PDRVRAWFILE;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | /* -=-=-=-=- PDMISTREAM -=-=-=-=- */
|
---|
72 |
|
---|
73 | /** @copydoc PDMISTREAM::pfnWrite */
|
---|
74 | static DECLCALLBACK(int) drvRawFileWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *pcbWrite)
|
---|
75 | {
|
---|
76 | int rc = VINF_SUCCESS;
|
---|
77 | PDRVRAWFILE pThis = PDMISTREAM_2_DRVRAWFILE(pInterface);
|
---|
78 | LogFlow(("%s: pvBuf=%p *pcbWrite=%#x (%s)\n", __FUNCTION__, pvBuf, *pcbWrite, pThis->pszLocation));
|
---|
79 |
|
---|
80 | Assert(pvBuf);
|
---|
81 | if (pThis->OutputFile != NIL_RTFILE)
|
---|
82 | {
|
---|
83 | size_t cbWritten;
|
---|
84 | rc = RTFileWrite(pThis->OutputFile, pvBuf, *pcbWrite, &cbWritten);
|
---|
85 | #if 0
|
---|
86 | /* don't flush here, takes too long and we will loose characters */
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | RTFileFlush(pThis->OutputFile);
|
---|
89 | #endif
|
---|
90 | *pcbWrite = cbWritten;
|
---|
91 | }
|
---|
92 |
|
---|
93 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
101 | */
|
---|
102 | static DECLCALLBACK(void *) drvRawFileQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
103 | {
|
---|
104 | PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
|
---|
105 | PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
|
---|
106 |
|
---|
107 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
108 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMISTREAM, &pThis->IStream);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Power off a raw output stream driver instance.
|
---|
117 | *
|
---|
118 | * This does most of the destruction work, to avoid ordering dependencies.
|
---|
119 | *
|
---|
120 | * @param pDrvIns The driver instance data.
|
---|
121 | */
|
---|
122 | static DECLCALLBACK(void) drvRawFilePowerOff(PPDMDRVINS pDrvIns)
|
---|
123 | {
|
---|
124 | PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
|
---|
125 | LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
|
---|
126 |
|
---|
127 | if (pThis->OutputFile != NIL_RTFILE)
|
---|
128 | {
|
---|
129 | RTFileClose(pThis->OutputFile);
|
---|
130 | pThis->OutputFile = NIL_RTFILE;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Destruct a raw output stream driver instance.
|
---|
137 | *
|
---|
138 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
139 | * any non-VM resources can be freed correctly.
|
---|
140 | *
|
---|
141 | * @param pDrvIns The driver instance data.
|
---|
142 | */
|
---|
143 | static DECLCALLBACK(void) drvRawFileDestruct(PPDMDRVINS pDrvIns)
|
---|
144 | {
|
---|
145 | PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
|
---|
146 | LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
|
---|
147 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
148 |
|
---|
149 | if (pThis->pszLocation)
|
---|
150 | MMR3HeapFree(pThis->pszLocation);
|
---|
151 |
|
---|
152 | if (pThis->OutputFile != NIL_RTFILE)
|
---|
153 | {
|
---|
154 | RTFileClose(pThis->OutputFile);
|
---|
155 | pThis->OutputFile = NIL_RTFILE;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Construct a raw output stream driver instance.
|
---|
162 | *
|
---|
163 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
164 | */
|
---|
165 | static DECLCALLBACK(int) drvRawFileConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
166 | {
|
---|
167 | PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
|
---|
168 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Init the static parts.
|
---|
172 | */
|
---|
173 | pThis->pDrvIns = pDrvIns;
|
---|
174 | pThis->pszLocation = NULL;
|
---|
175 | pThis->OutputFile = NIL_RTFILE;
|
---|
176 | /* IBase */
|
---|
177 | pDrvIns->IBase.pfnQueryInterface = drvRawFileQueryInterface;
|
---|
178 | /* IStream */
|
---|
179 | pThis->IStream.pfnWrite = drvRawFileWrite;
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Read the configuration.
|
---|
183 | */
|
---|
184 | if (!CFGMR3AreValuesValid(pCfg, "Location\0"))
|
---|
185 | AssertFailedReturn(VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
|
---|
186 |
|
---|
187 | int rc = CFGMR3QueryStringAlloc(pCfg, "Location", &pThis->pszLocation);
|
---|
188 | if (RT_FAILURE(rc))
|
---|
189 | AssertMsgFailedReturn(("Configuration error: query \"Location\" resulted in %Rrc.\n", rc), rc);
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Open the raw file.
|
---|
193 | */
|
---|
194 | rc = RTFileOpen(&pThis->OutputFile, pThis->pszLocation, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
|
---|
195 | if (RT_FAILURE(rc))
|
---|
196 | {
|
---|
197 | LogRel(("RawFile%d: CreateFile failed rc=%Rrc\n", pDrvIns->iInstance));
|
---|
198 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("RawFile#%d failed to create the raw output file %s"), pDrvIns->iInstance, pThis->pszLocation);
|
---|
199 | }
|
---|
200 |
|
---|
201 | LogFlow(("drvRawFileConstruct: location %s\n", pThis->pszLocation));
|
---|
202 | LogRel(("RawFile#%u: location %s\n", pDrvIns->iInstance, pThis->pszLocation));
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Raw file driver registration record.
|
---|
209 | */
|
---|
210 | const PDMDRVREG g_DrvRawFile =
|
---|
211 | {
|
---|
212 | /* u32Version */
|
---|
213 | PDM_DRVREG_VERSION,
|
---|
214 | /* szName */
|
---|
215 | "RawFile",
|
---|
216 | /* szRCMod */
|
---|
217 | "",
|
---|
218 | /* szR0Mod */
|
---|
219 | "",
|
---|
220 | /* pszDescription */
|
---|
221 | "RawFile stream driver.",
|
---|
222 | /* fFlags */
|
---|
223 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
224 | /* fClass. */
|
---|
225 | PDM_DRVREG_CLASS_STREAM,
|
---|
226 | /* cMaxInstances */
|
---|
227 | ~0,
|
---|
228 | /* cbInstance */
|
---|
229 | sizeof(DRVRAWFILE),
|
---|
230 | /* pfnConstruct */
|
---|
231 | drvRawFileConstruct,
|
---|
232 | /* pfnDestruct */
|
---|
233 | drvRawFileDestruct,
|
---|
234 | /* pfnRelocate */
|
---|
235 | NULL,
|
---|
236 | /* pfnIOCtl */
|
---|
237 | NULL,
|
---|
238 | /* pfnPowerOn */
|
---|
239 | NULL,
|
---|
240 | /* pfnReset */
|
---|
241 | NULL,
|
---|
242 | /* pfnSuspend */
|
---|
243 | NULL,
|
---|
244 | /* pfnResume */
|
---|
245 | NULL,
|
---|
246 | /* pfnAttach */
|
---|
247 | NULL,
|
---|
248 | /* pfnDetach */
|
---|
249 | NULL,
|
---|
250 | /* pfnPowerOff */
|
---|
251 | drvRawFilePowerOff,
|
---|
252 | /* pfnSoftReset */
|
---|
253 | NULL,
|
---|
254 | /* u32EndVersion */
|
---|
255 | PDM_DRVREG_VERSION
|
---|
256 | };
|
---|
257 |
|
---|