VirtualBox

Ignore:
Timestamp:
Apr 29, 2015 7:45:00 AM (10 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
99854
Message:

DevSmc: retrieve the SMC key from the host OS if possible

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/DevSmc.cpp

    r55500 r55502  
    3535# include <iprt/asm-amd64-x86.h>
    3636# include <iprt/once.h>
     37#endif
     38#if defined(RT_OS_DARWIN) && defined(IN_RING3)
     39# include "IOKit/IOKitLib.h"
    3740#endif
    3841
     
    501504
    502505#endif /* IN_RING0 */
     506
     507#if defined(IN_RING3) && defined(RT_OS_DARWIN)
     508
     509/**
     510 * Preferred method to retrieve the SMC key.
     511 *
     512 * @param   pabKey  where to store the key.
     513 * @param   cbKey   size of the buffer.
     514 */
     515static int getSmcKeyOs(char *pabKey, uint32_t cbKey)
     516{
     517    /*
     518     * Method as described in Amit Singh's article:
     519     *   http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
     520     */
     521    typedef struct
     522    {
     523        uint32_t   key;
     524        uint8_t    pad0[22];
     525        uint32_t   datasize;
     526        uint8_t    pad1[10];
     527        uint8_t    cmd;
     528        uint32_t   pad2;
     529        uint8_t    data[32];
     530    } AppleSMCBuffer;
     531
     532    AssertReturn(cbKey >= 65, VERR_INTERNAL_ERROR);
     533
     534    io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
     535                                                       IOServiceMatching("AppleSMC"));
     536    if (!service)
     537        return VERR_NOT_FOUND;
     538
     539    io_connect_t    port = (io_connect_t)0;
     540    kern_return_t   kr   = IOServiceOpen(service, mach_task_self(), 0, &port);
     541    IOObjectRelease(service);
     542
     543    if (kr != kIOReturnSuccess)
     544        return RTErrConvertFromDarwin(kr);
     545
     546    AppleSMCBuffer  inputStruct    = { 0, {0}, 32, {0}, 5, };
     547    AppleSMCBuffer  outputStruct;
     548    size_t          cbOutputStruct = sizeof(outputStruct);
     549
     550    for (int i = 0; i < 2; i++)
     551    {
     552        inputStruct.key = (uint32_t)(i == 0 ? 'OSK0' : 'OSK1');
     553        kr = IOConnectCallStructMethod((mach_port_t)port,
     554                                       (uint32_t)2,
     555                                       (const void *)&inputStruct,
     556                                       sizeof(inputStruct),
     557                                       (void *)&outputStruct,
     558                                       &cbOutputStruct);
     559        if (kr != kIOReturnSuccess)
     560        {
     561            IOServiceClose(port);
     562            return RTErrConvertFromDarwin(kr);
     563        }
     564
     565        for (int j = 0; j < 32; j++)
     566            pabKey[j + i*32] = outputStruct.data[j];
     567    }
     568
     569    IOServiceClose(port);
     570
     571    pabKey[64] = 0;
     572
     573    return VINF_SUCCESS;
     574}
     575
     576#endif /* IN_RING3 && RT_OS_DARWIN */
    503577
    504578#ifdef IN_RING3 /* For now. */
     
    13461420    if (fGetKeyFromRealSMC)
    13471421    {
    1348         rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*u64Arg*/);
     1422#ifdef RT_OS_DARWIN
     1423        rc = getSmcKeyOs(pThis->szOsk0And1, sizeof(pThis->szOsk0And1));
     1424        if (RT_FAILURE(rc))
     1425        {
     1426            LogRel(("SMC: Retrieving the SMC key from OS failed, trying to read it from hardware\n"));
     1427#endif
     1428            rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*u64Arg*/);
     1429#ifdef RT_OS_DARWIN
     1430        }
     1431        else
     1432            LogRel(("SMC: Successfully retrieved the SMC key from the OS\n"));
     1433#endif
    13491434        if (RT_FAILURE(rc))
    13501435            return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
Note: See TracChangeset for help on using the changeset viewer.

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