VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvMediaISO.cpp@ 44528

Last change on this file since 44528 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: DrvMediaISO.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * VBox storage devices: ISO image media driver
4 */
5
6/*
7 * Copyright (C) 2006-2012 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* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_DRV_ISO
22#include <VBox/vmm/pdmdrv.h>
23#include <iprt/assert.h>
24#include <iprt/file.h>
25#include <iprt/string.h>
26#include <iprt/uuid.h>
27
28#include "VBoxDD.h"
29
30
31/*******************************************************************************
32* Defined Constants And Macros *
33*******************************************************************************/
34/** Converts a pointer to MEDIAISO::IMedia to a PRDVMEDIAISO. */
35#define PDMIMEDIA_2_DRVMEDIAISO(pInterface) ( (PDRVMEDIAISO)((uintptr_t)pInterface - RT_OFFSETOF(DRVMEDIAISO, IMedia)) )
36
37
38/*******************************************************************************
39* Structures and Typedefs *
40*******************************************************************************/
41/**
42 * Block driver instance data.
43 *
44 * @implements PDMIMEDIA
45 */
46typedef struct DRVMEDIAISO
47{
48 /** The media interface. */
49 PDMIMEDIA IMedia;
50 /** Pointer to the driver instance. */
51 PPDMDRVINS pDrvIns;
52 /** Pointer to the filename. (Freed by MM) */
53 char *pszFilename;
54 /** File handle of the ISO file. */
55 RTFILE hFile;
56} DRVMEDIAISO, *PDRVMEDIAISO;
57
58
59
60/* -=-=-=-=- PDMIMEDIA -=-=-=-=- */
61
62/** @copydoc PDMIMEDIA::pfnGetSize */
63static DECLCALLBACK(uint64_t) drvMediaISOGetSize(PPDMIMEDIA pInterface)
64{
65 PDRVMEDIAISO pThis = PDMIMEDIA_2_DRVMEDIAISO(pInterface);
66 LogFlow(("drvMediaISOGetSize: '%s'\n", pThis->pszFilename));
67
68 uint64_t cbFile;
69 int rc = RTFileGetSize(pThis->hFile, &cbFile);
70 if (RT_SUCCESS(rc))
71 {
72 LogFlow(("drvMediaISOGetSize: returns %lld (%s)\n", cbFile, pThis->pszFilename));
73 return cbFile;
74 }
75
76 AssertMsgFailed(("Error querying ISO file size, rc=%Rrc. (%s)\n", rc, pThis->pszFilename));
77 return 0;
78}
79
80
81/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
82static DECLCALLBACK(int) drvMediaISOBiosGetPCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
83{
84 return VERR_NOT_IMPLEMENTED;
85}
86
87
88/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
89static DECLCALLBACK(int) drvMediaISOBiosSetPCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
90{
91 return VERR_NOT_IMPLEMENTED;
92}
93
94
95/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
96static DECLCALLBACK(int) drvMediaISOBiosGetLCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
97{
98 return VERR_NOT_IMPLEMENTED;
99}
100
101
102/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
103static DECLCALLBACK(int) drvMediaISOBiosSetLCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
104{
105 return VERR_NOT_IMPLEMENTED;
106}
107
108
109/**
110 * Read bits.
111 *
112 * @see PDMIMEDIA::pfnRead for details.
113 */
114static DECLCALLBACK(int) drvMediaISORead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
115{
116 PDRVMEDIAISO pThis = PDMIMEDIA_2_DRVMEDIAISO(pInterface);
117 LogFlow(("drvMediaISORead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n", off, pvBuf, cbRead, pThis->pszFilename));
118
119 Assert(pThis->hFile != NIL_RTFILE);
120 Assert(pvBuf);
121
122 /*
123 * Seek to the position and read.
124 */
125 int rc = RTFileReadAt(pThis->hFile, off, pvBuf, cbRead, NULL);
126 if (RT_SUCCESS(rc))
127 Log2(("drvMediaISORead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n"
128 "%16.*Rhxd\n",
129 off, pvBuf, cbRead, pThis->pszFilename,
130 cbRead, pvBuf));
131 else
132 AssertMsgFailed(("RTFileReadAt(%RTfile, %#llx, %p, %#x) -> %Rrc ('%s')\n",
133 pThis->hFile, off, pvBuf, cbRead, rc, pThis->pszFilename));
134 LogFlow(("drvMediaISORead: returns %Rrc\n", rc));
135 return rc;
136}
137
138
139/** @copydoc PDMIMEDIA::pfnWrite */
140static DECLCALLBACK(int) drvMediaISOWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
141{
142 AssertMsgFailed(("Attempt to write to an ISO file!\n"));
143 return VERR_NOT_IMPLEMENTED;
144}
145
146
147/** @copydoc PDMIMEDIA::pfnFlush */
148static DECLCALLBACK(int) drvMediaISOFlush(PPDMIMEDIA pInterface)
149{
150 /* No buffered data that still needs to be written. */
151 return VINF_SUCCESS;
152}
153
154
155/** @copydoc PDMIMEDIA::pfnGetUuid */
156static DECLCALLBACK(int) drvMediaISOGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
157{
158 LogFlow(("drvMediaISOGetUuid: returns VERR_NOT_IMPLEMENTED\n"));
159 return VERR_NOT_IMPLEMENTED;
160}
161
162
163/** @copydoc PDMIMEDIA::pfnIsReadOnly */
164static DECLCALLBACK(bool) drvMediaISOIsReadOnly(PPDMIMEDIA pInterface)
165{
166 return true;
167}
168
169/* -=-=-=-=- PDMIBASE -=-=-=-=- */
170
171/**
172 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
173 */
174static DECLCALLBACK(void *) drvMediaISOQueryInterface(PPDMIBASE pInterface, const char *pszIID)
175{
176 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
177 PDRVMEDIAISO pThis = PDMINS_2_DATA(pDrvIns, PDRVMEDIAISO);
178 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
179 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
180 return NULL;
181}
182
183/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
184
185/**
186 * Destruct a driver instance.
187 *
188 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
189 * resources can be freed correctly.
190 *
191 * @param pDrvIns The driver instance data.
192 */
193static DECLCALLBACK(void) drvMediaISODestruct(PPDMDRVINS pDrvIns)
194{
195 PDRVMEDIAISO pThis = PDMINS_2_DATA(pDrvIns, PDRVMEDIAISO);
196 LogFlow(("drvMediaISODestruct: '%s'\n", pThis->pszFilename));
197 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
198
199 RTFileClose(pThis->hFile);
200 pThis->hFile = NIL_RTFILE;
201
202 if (pThis->pszFilename)
203 {
204 MMR3HeapFree(pThis->pszFilename);
205 pThis->pszFilename = NULL;
206 }
207}
208
209
210/**
211 * Construct a ISO media driver instance.
212 *
213 * @copydoc FNPDMDRVCONSTRUCT
214 */
215static DECLCALLBACK(int) drvMediaISOConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
216{
217 PDRVMEDIAISO pThis = PDMINS_2_DATA(pDrvIns, PDRVMEDIAISO);
218 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
219
220 /*
221 * Init the static parts.
222 */
223 pThis->pDrvIns = pDrvIns;
224 pThis->hFile = NIL_RTFILE;
225 /* IBase */
226 pDrvIns->IBase.pfnQueryInterface = drvMediaISOQueryInterface;
227 /* IMedia */
228 pThis->IMedia.pfnRead = drvMediaISORead;
229 pThis->IMedia.pfnWrite = drvMediaISOWrite;
230 pThis->IMedia.pfnFlush = drvMediaISOFlush;
231 pThis->IMedia.pfnGetSize = drvMediaISOGetSize;
232 pThis->IMedia.pfnGetUuid = drvMediaISOGetUuid;
233 pThis->IMedia.pfnIsReadOnly = drvMediaISOIsReadOnly;
234 pThis->IMedia.pfnBiosGetPCHSGeometry = drvMediaISOBiosGetPCHSGeometry;
235 pThis->IMedia.pfnBiosSetPCHSGeometry = drvMediaISOBiosSetPCHSGeometry;
236 pThis->IMedia.pfnBiosGetLCHSGeometry = drvMediaISOBiosGetLCHSGeometry;
237 pThis->IMedia.pfnBiosSetLCHSGeometry = drvMediaISOBiosSetLCHSGeometry;
238
239 /*
240 * Read the configuration.
241 */
242 if (!CFGMR3AreValuesValid(pCfg, "Path\0"))
243 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
244
245 char *pszName;
246 int rc = CFGMR3QueryStringAlloc(pCfg, "Path", &pszName);
247 if (RT_FAILURE(rc))
248 return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Failed to query \"Path\" from the config"));
249
250 /*
251 * Open the image.
252 */
253 rc = RTFileOpen(&pThis->hFile, pszName, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
254 if (RT_SUCCESS(rc))
255 {
256 LogFlow(("drvMediaISOConstruct: ISO image '%s' opened successfully.\n", pszName));
257 pThis->pszFilename = pszName;
258 }
259 else
260 {
261 PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Failed to open ISO file \"%s\""), pszName);
262 MMR3HeapFree(pszName);
263 }
264
265 return rc;
266}
267
268
269/**
270 * ISO media driver registration record.
271 */
272const PDMDRVREG g_DrvMediaISO =
273{
274 /* u32Version */
275 PDM_DRVREG_VERSION,
276 /* szName */
277 "MediaISO",
278 /* szRCMod */
279 "",
280 /* szR0Mod */
281 "",
282 /* pszDescription */
283 "ISO media access driver.",
284 /* fFlags */
285 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
286 /* fClass. */
287 PDM_DRVREG_CLASS_MEDIA,
288 /* cMaxInstances */
289 ~0U,
290 /* cbInstance */
291 sizeof(DRVMEDIAISO),
292 /* pfnConstruct */
293 drvMediaISOConstruct,
294 /* pfnDestruct */
295 drvMediaISODestruct,
296 /* pfnRelocate */
297 NULL,
298 /* pfnIOCtl */
299 NULL,
300 /* pfnPowerOn */
301 NULL,
302 /* pfnReset */
303 NULL,
304 /* pfnSuspend */
305 NULL,
306 /* pfnResume */
307 NULL,
308 /* pfnAttach */
309 NULL,
310 /* pfnDetach */
311 NULL,
312 /* pfnPowerOff */
313 NULL,
314 /* pfnSoftReset */
315 NULL,
316 /* u32EndVersion */
317 PDM_DRVREG_VERSION
318};
319
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