VirtualBox

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

Last change on this file since 58466 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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