如何在运行之间存储 kext 参数?

问题描述 投票:0回答:1

一个关于kext开发的问题。我的 IOKit kext 有一些可以在运行时更改的参数。找不到任何线索,我如何存储这些参数以便在下一次 kext 运行时检索和使用它们?比如,在文件中或者 nvram(首选)?或者我还不知道的另一个持久性存储?

小提示:我的 kext 有 OSBundleRequired=Root,所以我想当内核加载它时 root fs 是可用的。但我也认为 NVRAM 更可取,因为它更容易为最终用户管理。

macos iokit kernel-extension
1个回答
0
投票

好吧,发布后立即在这里找到了答案。对不起。 我应该删除我的问题吗?

八位字节的 MVE(即 8 位字节,uint8_t,当然需要 euid=0):

main.c:

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

int main(int argc, char * argv[])
{   const char * serviceName = "AppleEFINVRAM";
    const char * keyName = "MytestKey";

    io_service_t ioService = IOServiceGetMatchingService(kIOMainPortDefault,
                             IOServiceMatching(serviceName));

    if (!ioService)
    {   printf("Service \"%s\" not found\n", serviceName);
        return (1);
    }
    CFStringRef ioKey = CFStringCreateWithCString(kCFAllocatorDefault,
                        keyName,
                        CFStringGetSystemEncoding());

    CFNumberRef ioNumValue;
    uint8_t value = 0;
    CFTypeRef ioValue = IORegistryEntryCreateCFProperty(ioService, ioKey, kCFAllocatorDefault, 0);
    if (ioValue && CFGetTypeID(ioValue) == CFNumberGetTypeID())
    {   ioNumValue = ioValue;
        CFNumberGetValue(ioNumValue, kCFNumberSInt8Type, &value);
        printf("Got key, value=%d\n", (int)value);
    }
    if (ioValue) CFRelease(ioValue);

    value++;

    printf("Value will be set to %d\n", (int)value);
    ioNumValue = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &value);

    kern_return_t res = IORegistryEntrySetCFProperty(ioService, ioKey, ioNumValue);
    if (res != KERN_SUCCESS)
    {   printf("Cannot set value, error 0x%X\n", res);
    }
    CFRelease(ioNumValue);

    IOObjectRelease(ioService);

    return (0);
}

制作文件:

nvramTest: main.c
        ${CC} $^ -framework CoreFoundation -framework IOKit -o $@
© www.soinside.com 2019 - 2024. All rights reserved.