[知其然不知其所以然-10] linux dmi table matching

来源:互联网 发布:朋友圈算法 编辑:程序博客网 时间:2024/06/03 23:01

我们经常需要给特定的平台写Quirk,也就是新加一段代码,这段代码只能给本平台使用。实现的原理就是,获取BIOS提供的板级信息,

代码里做判断,如果当前确实是指定平台上运行,那执行回调。talk is cheap, show me the code:


static struct dmi_system_id msr_save_dmi_table[] = {        {         .callback = msr_initialize_bdw,         .ident = "BROADWELL BDX_EP",         .matches = {                DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),                DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),                },        },        {}};static int pm_check_save_msr(void){        dmi_check_system(msr_save_dmi_table);        return 0;}

其中,dmi_check_system 函数,回遍历dmi_system_id数组的每个成员,

然后,对每个成员,都做如下判断,当所有的DMI_MATCH字段都匹配后,

执行callback(其实我写这篇文章就是为了突出“所有”这个词,写这么多像是骗稿费)。

代码很简单,直接提出来供参考:

int dmi_check_system(const struct dmi_system_id *list){        int count = 0;        const struct dmi_system_id *d;        for (d = list; !dmi_is_end_of_table(d); d++)                if (dmi_matches(d)) {                        count++;                        if (d->callback && d->callback(d))                                break;                }        return count;}

static bool dmi_matches(const struct dmi_system_id *dmi){        int i;        WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");        for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {                int s = dmi->matches[i].slot;                if (s == DMI_NONE)                        break;                if (dmi_ident[s]) {                        if (!dmi->matches[i].exact_match &&                            strstr(dmi_ident[s], dmi->matches[i].substr))                                continue;                        else if (dmi->matches[i].exact_match &&                                 !strcmp(dmi_ident[s], dmi->matches[i].substr))                                continue;                }                /* No match */                return false;        }        return true;}




0 0
原创粉丝点击