VirtualBox

Changeset 91311 in vbox


Ignore:
Timestamp:
Sep 20, 2021 11:06:33 AM (3 years ago)
Author:
vboxsync
Message:

Devices/Security/DrvTpmEmuTpms: Make use of the PDM VFS interface iv available for loading/storing the TPM state (untested), bugref:10075

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Security/DrvTpmEmuTpms.cpp

    r91137 r91311  
    6868    /** Pointer to the driver instance. */
    6969    PPDMDRVINS          pDrvIns;
     70    /** The VFS interface of the driver below for NVRAM/TPM state loading and storing. */
     71    PPDMIVFSCONNECTOR   pDrvVfs;
    7072
    7173    /** The TPM version we are emulating. */
     
    262264
    263265
     266/**
     267 * Tries to load the NVRAM from the VFS driver below.
     268 *
     269 * @returns VBox status code.
     270 * @param   pThis               The emulator driver instance data.
     271 */
     272static int drvTpmEmuTpmsNvramLoadFromVfs(PDRVTPMEMU pThis)
     273{
     274    uint64_t cbState = 0;
     275    int rc = pThis->pDrvVfs->pfnQuerySize(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_PERMANENT_ALL_NAME, &cbState);
     276    if (RT_SUCCESS(rc))
     277    {
     278        pThis->pvNvPermall = RTMemAllocZ(cbState);
     279        if (pThis->pvNvPermall)
     280        {
     281            pThis->cbNvPermall = (size_t)cbState;
     282            rc = pThis->pDrvVfs->pfnReadAll(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_PERMANENT_ALL_NAME,
     283                                            pThis->pvNvPermall, pThis->cbNvPermall);
     284            if (RT_SUCCESS(rc))
     285            {
     286                /* Load the volatile state if existing. */
     287                rc = pThis->pDrvVfs->pfnQuerySize(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_VOLATILESTATE_NAME, &cbState);
     288                if (RT_SUCCESS(rc))
     289                {
     290                    pThis->pvNvVolatile = RTMemAllocZ(cbState);
     291                    if (pThis->pvNvVolatile)
     292                    {
     293                        pThis->cbNvVolatile = (size_t)cbState;
     294                        rc = pThis->pDrvVfs->pfnReadAll(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_VOLATILESTATE_NAME,
     295                                                        pThis->pvNvVolatile, pThis->cbNvVolatile);
     296                    }
     297                }
     298                else if (rc == VERR_NOT_FOUND)
     299                    rc = VINF_SUCCESS; /* This is fine if there is no volatile state. */
     300            }
     301        }
     302        else
     303            rc = VERR_NO_MEMORY;
     304    }
     305    else if (rc == VERR_NOT_FOUND)
     306        rc = VINF_SUCCESS; /* This is fine for the first start of a new VM. */
     307
     308    return rc;
     309}
     310
     311
     312/**
     313 * Stores the NVRAM content using the VFS driver below.
     314 *
     315 * @returns VBox status code.
     316 * @param   pThis               The emulator driver instance data.
     317 */
     318static int drvTpmEmuTpmsNvramStoreToVfs(PDRVTPMEMU pThis)
     319{
     320    AssertPtr(pThis->pvNvPermall);
     321    int rc = pThis->pDrvVfs->pfnWriteAll(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_PERMANENT_ALL_NAME,
     322                                         pThis->pvNvPermall, pThis->cbNvPermall);
     323    if (   RT_SUCCESS(rc)
     324        && pThis->pvNvVolatile)
     325        rc = pThis->pDrvVfs->pfnWriteAll(pThis->pDrvVfs, pThis->pDrvIns->pReg->szName, TPM_VOLATILESTATE_NAME,
     326                                         pThis->pvNvVolatile, pThis->cbNvVolatile);
     327
     328    return rc;
     329}
     330
     331
     332/* -=-=-=-=- PDMITPMCONNECTOR interface callabcks. -=-=-=-=- */
     333
     334
    264335/** @interface_method_impl{PDMITPMCONNECTOR,pfnGetVersion} */
    265336static DECLCALLBACK(TPMVERSION) drvTpmEmuTpmsGetVersion(PPDMITPMCONNECTOR pInterface)
     
    690761    if (!pThis->fSsmCalled)
    691762    {
    692         int rc = drvTpmEmuTpmsNvramStore(pThis);
     763        int rc;
     764        if (pThis->pDrvVfs)
     765            rc = drvTpmEmuTpmsNvramStoreToVfs(pThis);
     766        else
     767            rc = drvTpmEmuTpmsNvramStore(pThis);
    693768        AssertRC(rc);
    694769    }
     
    754829    TPMLIB_SetDebugLevel(~0);
    755830
    756     int rc = CFGMR3QueryStringAlloc(pCfg, "NvramPath", &pThis->pszNvramPath);
    757     if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND)
     831    /*
     832     * Try attach the VFS driver below and query it's VFS interface.
     833     */
     834    PPDMIBASE pBase = NULL;
     835    int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
     836    if (RT_FAILURE(rc) && rc != VERR_PDM_NO_ATTACHED_DRIVER)
    758837        return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
    759                                    N_("Configuration error: querying \"NvramPath\" resulted in %Rrc"), rc);
    760 
    761     rc = drvTpmEmuTpmsNvramLoad(pThis);
    762     if (RT_FAILURE(rc))
    763         return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
    764                                    N_("Failed to load TPM NVRAM data with %Rrc"), rc);
     838                                   N_("Failed to attach driver below us! %Rrc"), rc);
     839    if (pBase)
     840    {
     841        pThis->pDrvVfs = PDMIBASE_QUERY_INTERFACE(pBase, PDMIVFSCONNECTOR);
     842        if (!pThis->pDrvVfs)
     843            return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
     844                                    N_("No VFS interface below"));
     845
     846        rc = drvTpmEmuTpmsNvramLoadFromVfs(pThis);
     847        if (RT_FAILURE(rc))
     848            return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
     849                                       N_("Failed to load TPM NVRAM data with %Rrc"), rc);
     850    }
     851    else
     852    {
     853        rc = CFGMR3QueryStringAlloc(pCfg, "NvramPath", &pThis->pszNvramPath);
     854        if (RT_FAILURE(rc))
     855            return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
     856                                       N_("Configuration error: querying \"NvramPath\" resulted in %Rrc"), rc);
     857
     858        rc = drvTpmEmuTpmsNvramLoad(pThis);
     859        if (RT_FAILURE(rc))
     860            return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
     861                                       N_("Failed to load TPM NVRAM data with %Rrc"), rc);
     862    }
    765863
    766864    TPMLIB_TPMVersion enmVersion = TPMLIB_TPM_VERSION_2;
Note: See TracChangeset for help on using the changeset viewer.

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