VirtualBox

source: vbox/trunk/src/VBox/Devices/Security/DrvTpmHost.cpp@ 90996

最後變更 在這個檔案從90996是 90996,由 vboxsync 提交於 4 年 前

Devices/Security: Only advertise the multiple localities supported capability if the driver below supports it, bugref:10075

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.1 KB
 
1/* $Id: DrvTpmHost.cpp 90996 2021-08-30 12:57:49Z vboxsync $ */
2/** @file
3 * TPM host access driver.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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_TCP /** @todo */
23#include <VBox/vmm/pdmdrv.h>
24#include <VBox/vmm/pdmtpmifs.h>
25#include <iprt/assert.h>
26#include <iprt/mem.h>
27#include <iprt/string.h>
28#include <iprt/semaphore.h>
29#include <iprt/uuid.h>
30#include <iprt/tpm.h>
31
32#include <iprt/formats/tpm.h>
33
34#include "VBoxDD.h"
35
36
37/*********************************************************************************************************************************
38* Defined Constants And Macros *
39*********************************************************************************************************************************/
40
41
42/*********************************************************************************************************************************
43* Structures and Typedefs *
44*********************************************************************************************************************************/
45
46/**
47 * TPM Host driver instance data.
48 *
49 * @implements PDMITPMCONNECTOR
50 */
51typedef struct DRVTPMHOST
52{
53 /** The stream interface. */
54 PDMITPMCONNECTOR ITpmConnector;
55 /** Pointer to the driver instance. */
56 PPDMDRVINS pDrvIns;
57
58 /** Handle to the host TPM. */
59 RTTPM hTpm;
60
61} DRVTPMHOST;
62/** Pointer to the TPM emulator instance data. */
63typedef DRVTPMHOST *PDRVTPMHOST;
64
65
66/*********************************************************************************************************************************
67* Internal Functions *
68*********************************************************************************************************************************/
69
70/** @interface_method_impl{PDMITPMCONNECTOR,pfnStartup} */
71static DECLCALLBACK(int) drvTpmHostStartup(PPDMITPMCONNECTOR pInterface, size_t cbCmdResp)
72{
73 RT_NOREF(pInterface, cbCmdResp);
74 return VINF_SUCCESS;
75}
76
77
78/** @interface_method_impl{PDMITPMCONNECTOR,pfnShutdown} */
79static DECLCALLBACK(int) drvTpmHostShutdown(PPDMITPMCONNECTOR pInterface)
80{
81 RT_NOREF(pInterface);
82 return VINF_SUCCESS;
83}
84
85
86/** @interface_method_impl{PDMITPMCONNECTOR,pfnReset} */
87static DECLCALLBACK(int) drvTpmHostReset(PPDMITPMCONNECTOR pInterface)
88{
89 RT_NOREF(pInterface);
90 return VINF_SUCCESS;
91}
92
93
94/** @interface_method_impl{PDMITPMCONNECTOR,pfnGetVersion} */
95static DECLCALLBACK(TPMVERSION) drvTpmHostGetVersion(PPDMITPMCONNECTOR pInterface)
96{
97 PDRVTPMHOST pThis = RT_FROM_MEMBER(pInterface, DRVTPMHOST, ITpmConnector);
98 RTTPMVERSION enmVersion = RTTpmGetVersion(pThis->hTpm);
99
100 switch (enmVersion)
101 {
102 case RTTPMVERSION_1_2:
103 return TPMVERSION_1_2;
104 case RTTPMVERSION_2_0:
105 return TPMVERSION_2_0;
106 case RTTPMVERSION_UNKNOWN:
107 default:
108 return TPMVERSION_UNKNOWN;
109 }
110
111 AssertFailed(); /* Shouldn't get here. */
112 return TPMVERSION_UNKNOWN;
113}
114
115
116/** @interface_method_impl{PDMITPMCONNECTOR,pfnGetLocalityMax} */
117static DECLCALLBACK(uint32_t) drvTpmHostGetLocalityMax(PPDMITPMCONNECTOR pInterface)
118{
119 PDRVTPMHOST pThis = RT_FROM_MEMBER(pInterface, DRVTPMHOST, ITpmConnector);
120 return RTTpmGetLocalityMax(pThis->hTpm);
121}
122
123
124/** @interface_method_impl{PDMITPMCONNECTOR,pfnGetEstablishedFlag} */
125static DECLCALLBACK(bool) drvTpmHostGetEstablishedFlag(PPDMITPMCONNECTOR pInterface)
126{
127 RT_NOREF(pInterface);
128 return false;
129}
130
131
132/** @interface_method_impl{PDMITPMCONNECTOR,pfnResetEstablishedFlag} */
133static DECLCALLBACK(int) drvTpmHostResetEstablishedFlag(PPDMITPMCONNECTOR pInterface, uint8_t bLoc)
134{
135 RT_NOREF(pInterface, bLoc);
136 return VINF_SUCCESS;
137}
138
139
140/** @interface_method_impl{PDMITPMCONNECTOR,pfnCmdExec} */
141static DECLCALLBACK(int) drvTpmHostCmdExec(PPDMITPMCONNECTOR pInterface, uint8_t bLoc, const void *pvCmd, size_t cbCmd, void *pvResp, size_t cbResp)
142{
143 RT_NOREF(bLoc);
144 PDRVTPMHOST pThis = RT_FROM_MEMBER(pInterface, DRVTPMHOST, ITpmConnector);
145
146 return RTTpmReqExec(pThis->hTpm, 0 /*bLoc*/, pvCmd, cbCmd, pvResp, cbResp, NULL /*pcbResp*/);
147}
148
149
150/** @interface_method_impl{PDMITPMCONNECTOR,pfnCmdCancel} */
151static DECLCALLBACK(int) drvTpmHostCmdCancel(PPDMITPMCONNECTOR pInterface)
152{
153 PDRVTPMHOST pThis = RT_FROM_MEMBER(pInterface, DRVTPMHOST, ITpmConnector);
154
155 return RTTpmReqCancel(pThis->hTpm);
156}
157
158
159/** @interface_method_impl{PDMIBASE,pfnQueryInterface} */
160static DECLCALLBACK(void *) drvTpmHostQueryInterface(PPDMIBASE pInterface, const char *pszIID)
161{
162 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
163 PDRVTPMHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVTPMHOST);
164 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
165 PDMIBASE_RETURN_INTERFACE(pszIID, PDMITPMCONNECTOR, &pThis->ITpmConnector);
166 return NULL;
167}
168
169
170/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
171
172/** @copydoc FNPDMDRVDESTRUCT */
173static DECLCALLBACK(void) drvTpmHostDestruct(PPDMDRVINS pDrvIns)
174{
175 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
176
177 PDRVTPMHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVTPMHOST);
178 LogFlow(("%s\n", __FUNCTION__));
179
180 if (pThis->hTpm != NIL_RTTPM)
181 {
182 int rc = RTTpmClose(pThis->hTpm);
183 AssertRC(rc);
184
185 pThis->hTpm = NIL_RTTPM;
186 }
187}
188
189
190/** @copydoc FNPDMDRVCONSTRUCT */
191static DECLCALLBACK(int) drvTpmHostConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
192{
193 RT_NOREF(fFlags);
194 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
195 PDRVTPMHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVTPMHOST);
196
197 /*
198 * Init the static parts.
199 */
200 pThis->pDrvIns = pDrvIns;
201 pThis->hTpm = NIL_RTTPM;
202
203 /* IBase */
204 pDrvIns->IBase.pfnQueryInterface = drvTpmHostQueryInterface;
205 /* ITpmConnector */
206 pThis->ITpmConnector.pfnStartup = drvTpmHostStartup;
207 pThis->ITpmConnector.pfnShutdown = drvTpmHostShutdown;
208 pThis->ITpmConnector.pfnReset = drvTpmHostReset;
209 pThis->ITpmConnector.pfnGetVersion = drvTpmHostGetVersion;
210 pThis->ITpmConnector.pfnGetLocalityMax = drvTpmHostGetLocalityMax;
211 pThis->ITpmConnector.pfnGetEstablishedFlag = drvTpmHostGetEstablishedFlag;
212 pThis->ITpmConnector.pfnResetEstablishedFlag = drvTpmHostResetEstablishedFlag;
213 pThis->ITpmConnector.pfnCmdExec = drvTpmHostCmdExec;
214 pThis->ITpmConnector.pfnCmdCancel = drvTpmHostCmdCancel;
215
216 /*
217 * Validate and read the configuration.
218 */
219 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "TpmId", "");
220
221 uint32_t idTpm = RTTPM_ID_DEFAULT;
222 int rc = CFGMR3QueryU32Def(pCfg, "TpmId", &idTpm, RTTPM_ID_DEFAULT);
223 if (RT_FAILURE(rc))
224 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
225 N_("Configuration error: querying \"TpmId\" resulted in %Rrc"), rc);
226
227 rc = RTTpmOpen(&pThis->hTpm, idTpm);
228 if (RT_FAILURE(rc))
229 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
230 N_("DrvTpmHost%d: Opening TPM with id %u failed with %Rrc"), idTpm, rc);
231
232 LogRel(("DrvTpmHost#%d: Connected to TPM %u.\n", pDrvIns->iInstance, idTpm));
233 return VINF_SUCCESS;
234}
235
236
237/**
238 * TPM host driver registration record.
239 */
240const PDMDRVREG g_DrvTpmHost =
241{
242 /* u32Version */
243 PDM_DRVREG_VERSION,
244 /* szName */
245 "TpmHost",
246 /* szRCMod */
247 "",
248 /* szR0Mod */
249 "",
250 /* pszDescription */
251 "TPM host driver.",
252 /* fFlags */
253 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
254 /* fClass. */
255 PDM_DRVREG_CLASS_STREAM,
256 /* cMaxInstances */
257 ~0U,
258 /* cbInstance */
259 sizeof(DRVTPMHOST),
260 /* pfnConstruct */
261 drvTpmHostConstruct,
262 /* pfnDestruct */
263 drvTpmHostDestruct,
264 /* pfnRelocate */
265 NULL,
266 /* pfnIOCtl */
267 NULL,
268 /* pfnPowerOn */
269 NULL,
270 /* pfnReset */
271 NULL,
272 /* pfnSuspend */
273 NULL,
274 /* pfnResume */
275 NULL,
276 /* pfnAttach */
277 NULL,
278 /* pfnDetach */
279 NULL,
280 /* pfnPowerOff */
281 NULL,
282 /* pfnSoftReset */
283 NULL,
284 /* u32EndVersion */
285 PDM_DRVREG_VERSION
286};
287
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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