Instantly share code, notes, and snippets.

Embed
What would you like to do?

KVM OSX Guest SMC Read

  • 2015-02-20: OX 10.10.5 on Apple hardware can successfully build
  • NOTE: Credit and thanks to Amit Singh
  • Reference:

http://www.osxbook.com/book/bonus/chapter7/tpmdrmmyth/

Build

  • Create smc_read.c and paste in code:
vi smc_read.c
/*
 * smc_read.c: Written for Mac OS X 10.5. Compile as follows:
 *
 * gcc -Wall -o smc_read smc_read.c -framework IOKit
 */

#include <stdio.h>
#include <IOKit/IOKitLib.h>

typedef struct {
    uint32_t key;
    uint8_t  __d0[22];
    uint32_t datasize;
    uint8_t  __d1[10];
    uint8_t  cmd;
    uint32_t __d2;
    uint8_t  data[32];
} AppleSMCBuffer_t;

int
main(void)
{
    io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
                               IOServiceMatching("AppleSMC"));
    if (!service)
        return -1;

    io_connect_t port = (io_connect_t)0;
    kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &port);
    IOObjectRelease(service);
    if (kr != kIOReturnSuccess)
        return kr;

    AppleSMCBuffer_t inputStruct = { 'OSK0', {0}, 32, {0}, 5, }, outputStruct;
    size_t outputStructCnt = sizeof(outputStruct);

    kr = IOConnectCallStructMethod((mach_port_t)port, (uint32_t)2,
             (const void*)&inputStruct, sizeof(inputStruct),
             (void*)&outputStruct, &outputStructCnt);
    if (kr != kIOReturnSuccess)
        return kr;

    int i = 0;
    for (i = 0; i < 32; i++)
        printf("%c", outputStruct.data[i]);

    inputStruct.key = 'OSK1';
    kr = IOConnectCallStructMethod((mach_port_t)port, (uint32_t)2,
             (const void*)&inputStruct, sizeof(inputStruct),
             (void*)&outputStruct, &outputStructCnt);
    if (kr == kIOReturnSuccess)
        for (i = 0; i < 32; i++)
            printf("%c", outputStruct.data[i]);

    printf("\n");

    return IOServiceClose(port);
}
  • Compile:
gcc -Wall -o smc_read smc_read.c -framework IOKit
  • Run:
./smc_read
  • On 2015-12-09 output is:
ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc
  • Reference:

http://www.osxbook.com/book/bonus/chapter7/tpmdrmmyth/

https://bbs.archlinux.org/viewtopic.php?id=162768

Error Compiling

  • Error message:
2015-12-09 19:47:32.799 xcodebuild[9169:2013175] [MT] PluginLoading: Required plug-in compatibility UUID 7265231C-39B4-402C-89E1-16167C4CC990 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/OFPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
  • Confirm that Xcode command line tools are installed:
xcode-select --install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment