VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvRawImage.cpp@ 42996

Last change on this file since 42996 was 40282, checked in by vboxsync, 13 years ago

*: gcc-4.7: ~0 => ~0U in initializers (warning: narrowing conversion of -1' from int' to `unsigned int' inside { } is ill-formed in C++11 [-Wnarrowing])

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: DrvRawImage.cpp 40282 2012-02-28 21:02:40Z vboxsync $ */
2/** @file
3 * VBox storage devices: Raw image driver
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_RAW_IMAGE
23#include <VBox/vmm/pdmdrv.h>
24#include <iprt/assert.h>
25#include <iprt/file.h>
26#include <iprt/string.h>
27#include <iprt/uuid.h>
28
29#include "VBoxDD.h"
30
31
32/*******************************************************************************
33* Structures and Typedefs *
34*******************************************************************************/
35/**
36 * Block driver instance data.
37 *
38 * @implements PDMIMEDIA
39 */
40typedef struct DRVRAWIMAGE
41{
42 /** The media interface. */
43 PDMIMEDIA IMedia;
44 /** Pointer to the driver instance. */
45 PPDMDRVINS pDrvIns;
46 /** Pointer to the filename. (Freed by MM) */
47 char *pszFilename;
48 /** File handle of the raw image file. */
49 RTFILE hFile;
50 /** True if the image is operating in readonly mode. */
51 bool fReadOnly;
52} DRVRAWIMAGE, *PDRVRAWIMAGE;
53
54
55
56/* -=-=-=-=- PDMIMEDIA -=-=-=-=- */
57
58/** @copydoc PDMIMEDIA::pfnGetSize */
59static DECLCALLBACK(uint64_t) drvRawImageGetSize(PPDMIMEDIA pInterface)
60{
61 PDRVRAWIMAGE pThis = RT_FROM_MEMBER(pInterface, DRVRAWIMAGE, IMedia);
62 LogFlow(("drvRawImageGetSize: '%s'\n", pThis->pszFilename));
63
64 uint64_t cbFile;
65 int rc = RTFileGetSize(pThis->hFile, &cbFile);
66 if (RT_SUCCESS(rc))
67 {
68 LogFlow(("drvRawImageGetSize: returns %lld (%s)\n", cbFile, pThis->pszFilename));
69 return cbFile;
70 }
71
72 AssertMsgFailed(("Error querying Raw image file size, rc=%Rrc. (%s)\n", rc, pThis->pszFilename));
73 return 0;
74}
75
76
77/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
78static DECLCALLBACK(int) drvRawImageBiosGetPCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
79{
80 return VERR_NOT_IMPLEMENTED;
81}
82
83
84/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
85static DECLCALLBACK(int) drvRawImageBiosSetPCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
86{
87 return VERR_NOT_IMPLEMENTED;
88}
89
90
91/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
92static DECLCALLBACK(int) drvRawImageBiosGetLCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
93{
94 return VERR_NOT_IMPLEMENTED;
95}
96
97
98/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
99static DECLCALLBACK(int) drvRawImageBiosSetLCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
100{
101 return VERR_NOT_IMPLEMENTED;
102}
103
104
105/**
106 * Read bits.
107 *
108 * @see PDMIMEDIA::pfnRead for details.
109 */
110static DECLCALLBACK(int) drvRawImageRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
111{
112 PDRVRAWIMAGE pThis = RT_FROM_MEMBER(pInterface, DRVRAWIMAGE, IMedia);
113 LogFlow(("drvRawImageRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n", off, pvBuf, cbRead, pThis->pszFilename));
114
115 Assert(pThis->hFile != NIL_RTFILE);
116 Assert(pvBuf);
117
118 /*
119 * Seek to the position and read.
120 */
121 int rc = RTFileSeek(pThis->hFile, off, RTFILE_SEEK_BEGIN, NULL);
122 if (RT_SUCCESS(rc))
123 {
124 rc = RTFileRead(pThis->hFile, pvBuf, cbRead, NULL);
125 if (RT_SUCCESS(rc))
126 {
127 Log2(("drvRawImageRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n"
128 "%16.*Rhxd\n",
129 off, pvBuf, cbRead, pThis->pszFilename,
130 cbRead, pvBuf));
131 }
132 else
133 AssertMsgFailed(("RTFileRead(%RTfile, %p, %#x) -> %Rrc (off=%#llx '%s')\n",
134 pThis->hFile, pvBuf, cbRead, rc, off, pThis->pszFilename));
135 }
136 else
137 AssertMsgFailed(("RTFileSeek(%RTfile,%#llx,) -> %Rrc\n", pThis->hFile, off, rc));
138 LogFlow(("drvRawImageRead: returns %Rrc\n", rc));
139 return rc;
140}
141
142
143/** @copydoc PDMIMEDIA::pfnWrite */
144static DECLCALLBACK(int) drvRawImageWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
145{
146 PDRVRAWIMAGE pThis = RT_FROM_MEMBER(pInterface, DRVRAWIMAGE, IMedia);
147 LogFlow(("drvRawImageWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n", off, pvBuf, cbWrite, pThis->pszFilename));
148
149 Assert(pThis->hFile != NIL_RTFILE);
150 Assert(pvBuf);
151
152 /*
153 * Seek to the position and write.
154 */
155 int rc = RTFileSeek(pThis->hFile, off, RTFILE_SEEK_BEGIN, NULL);
156 if (RT_SUCCESS(rc))
157 {
158 rc = RTFileWrite(pThis->hFile, pvBuf, cbWrite, NULL);
159 if (RT_SUCCESS(rc))
160 {
161 Log2(("drvRawImageWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n"
162 "%16.*Rhxd\n",
163 off, pvBuf, cbWrite, pThis->pszFilename,
164 cbWrite, pvBuf));
165 }
166 else
167 AssertMsgFailed(("RTFileWrite(%RTfile, %p, %#x) -> %Rrc (off=%#llx '%s')\n",
168 pThis->hFile, pvBuf, cbWrite, rc, off, pThis->pszFilename));
169 }
170 else
171 AssertMsgFailed(("RTFileSeek(%RTfile,%#llx,) -> %Rrc\n", pThis->hFile, off, rc));
172 LogFlow(("drvRawImageWrite: returns %Rrc\n", rc));
173 return rc;
174}
175
176
177/** @copydoc PDMIMEDIA::pfnFlush */
178static DECLCALLBACK(int) drvRawImageFlush(PPDMIMEDIA pInterface)
179{
180 PDRVRAWIMAGE pThis = RT_FROM_MEMBER(pInterface, DRVRAWIMAGE, IMedia);
181 LogFlow(("drvRawImageFlush: (%s)\n", pThis->pszFilename));
182
183 Assert(pThis->hFile != NIL_RTFILE);
184 int rc = RTFileFlush(pThis->hFile);
185 LogFlow(("drvRawImageFlush: returns %Rrc\n", rc));
186 return rc;
187}
188
189
190/** @copydoc PDMIMEDIA::pfnGetUuid */
191static DECLCALLBACK(int) drvRawImageGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
192{
193 LogFlow(("drvRawImageGetUuid: returns VERR_NOT_IMPLEMENTED\n"));
194 return VERR_NOT_IMPLEMENTED;
195}
196
197
198/** @copydoc PDMIMEDIA::pfnIsReadOnly */
199static DECLCALLBACK(bool) drvRawImageIsReadOnly(PPDMIMEDIA pInterface)
200{
201 PDRVRAWIMAGE pThis = RT_FROM_MEMBER(pInterface, DRVRAWIMAGE, IMedia);
202 return pThis->fReadOnly;
203}
204
205
206/* -=-=-=-=- PDMIBASE -=-=-=-=- */
207
208/**
209 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
210 */
211static DECLCALLBACK(void *) drvRawImageQueryInterface(PPDMIBASE pInterface, const char *pszIID)
212{
213 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
214 PDRVRAWIMAGE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWIMAGE);
215
216 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
217 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
218 return NULL;
219}
220
221/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
222
223/**
224 * Destruct a driver instance.
225 *
226 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
227 * resources can be freed correctly.
228 *
229 * @param pDrvIns The driver instance data.
230 */
231static DECLCALLBACK(void) drvRawImageDestruct(PPDMDRVINS pDrvIns)
232{
233 PDRVRAWIMAGE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWIMAGE);
234 LogFlow(("drvRawImageDestruct: '%s'\n", pThis->pszFilename));
235 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
236
237 RTFileClose(pThis->hFile);
238 pThis->hFile = NIL_RTFILE;
239
240 if (pThis->pszFilename)
241 {
242 MMR3HeapFree(pThis->pszFilename);
243 pThis->pszFilename = NULL;
244 }
245}
246
247
248/**
249 * Construct a raw image driver instance.
250 *
251 * @copydoc FNPDMDRVCONSTRUCT
252 */
253static DECLCALLBACK(int) drvRawImageConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
254{
255 PDRVRAWIMAGE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWIMAGE);
256 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
257
258 /*
259 * Init the static parts.
260 */
261 pThis->pDrvIns = pDrvIns;
262 pThis->hFile = NIL_RTFILE;
263 /* IBase */
264 pDrvIns->IBase.pfnQueryInterface = drvRawImageQueryInterface;
265 /* IMedia */
266 pThis->IMedia.pfnRead = drvRawImageRead;
267 pThis->IMedia.pfnWrite = drvRawImageWrite;
268 pThis->IMedia.pfnFlush = drvRawImageFlush;
269 pThis->IMedia.pfnGetSize = drvRawImageGetSize;
270 pThis->IMedia.pfnGetUuid = drvRawImageGetUuid;
271 pThis->IMedia.pfnIsReadOnly = drvRawImageIsReadOnly;
272 pThis->IMedia.pfnBiosGetPCHSGeometry = drvRawImageBiosGetPCHSGeometry;
273 pThis->IMedia.pfnBiosSetPCHSGeometry = drvRawImageBiosSetPCHSGeometry;
274 pThis->IMedia.pfnBiosGetLCHSGeometry = drvRawImageBiosGetLCHSGeometry;
275 pThis->IMedia.pfnBiosSetLCHSGeometry = drvRawImageBiosSetLCHSGeometry;
276
277 /*
278 * Read the configuration.
279 */
280 if (!CFGMR3AreValuesValid(pCfg, "Path\0"))
281 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
282
283 char *pszName;
284 int rc = CFGMR3QueryStringAlloc(pCfg, "Path", &pszName);
285 if (RT_FAILURE(rc))
286 {
287 AssertMsgFailed(("Configuration error: query for \"Path\" string return %Rrc.\n", rc));
288 return rc;
289 }
290
291 /*
292 * Open the image.
293 */
294 rc = RTFileOpen(&pThis->hFile, pszName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
295 if (RT_SUCCESS(rc))
296 {
297 LogFlow(("drvRawImageConstruct: Raw image '%s' opened successfully.\n", pszName));
298 pThis->pszFilename = pszName;
299 pThis->fReadOnly = false;
300 }
301 else
302 {
303 rc = RTFileOpen(&pThis->hFile, pszName, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
304 if (RT_SUCCESS(rc))
305 {
306 LogFlow(("drvRawImageConstruct: Raw image '%s' opened successfully.\n", pszName));
307 pThis->pszFilename = pszName;
308 pThis->fReadOnly = true;
309 }
310 else
311 {
312 AssertMsgFailed(("Could not open Raw image file %s, rc=%Rrc\n", pszName, rc));
313 MMR3HeapFree(pszName);
314 }
315 }
316
317 return rc;
318}
319
320
321/**
322 * Raw image driver registration record.
323 */
324const PDMDRVREG g_DrvRawImage =
325{
326 /* u32Version */
327 PDM_DRVREG_VERSION,
328 /* szName */
329 "RawImage",
330 /* szRCMod */
331 "",
332 /* szR0Mod */
333 "",
334 /* pszDescription */
335 "Raw image access driver.",
336 /* fFlags */
337 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
338 /* fClass. */
339 PDM_DRVREG_CLASS_MEDIA,
340 /* cMaxInstances */
341 ~0U,
342 /* cbInstance */
343 sizeof(DRVRAWIMAGE),
344 /* pfnConstruct */
345 drvRawImageConstruct,
346 /* pfnDestruct */
347 drvRawImageDestruct,
348 /* pfnRelocate */
349 NULL,
350 /* pfnIOCtl */
351 NULL,
352 /* pfnPowerOn */
353 NULL,
354 /* pfnReset */
355 NULL,
356 /* pfnSuspend */
357 NULL,
358 /* pfnResume */
359 NULL,
360 /* pfnAttach */
361 NULL,
362 /* pfnDetach */
363 NULL,
364 /* pfnPowerOff */
365 NULL,
366 /* pfnSoftReset */
367 NULL,
368 /* u32EndVersion */
369 PDM_DRVREG_VERSION
370};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette