zoukankan      html  css  js  c++  java
  • 内核模块编译时怎样绕过insmod时的版本检查

    1、Uboot:每个arm芯片或者海斯芯片都有各自的uboot。

    2、但他们的内核版本可以是一样的,主要是跟各自内核的进行的编译选项有关, 31的内核版本里加了版本检查选项“Kernel type->Symmetrical Multi-Processing”,而21的内核版本没有设置该选项。

     ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    在开发kernel driver时,总是会遇到讨人厌的vermagic检查,只要目前在run的kernel版本跟driver编译时用的kernel版本不一致,就没办法insmod。

    bash-3.2# insmod sdio.ko

    sdio: version magic '2.6.28-271-gec75a15 preempt mod_unload modversions ARMv7 '

    should be '2.6.28 preempt mod_unload ARMv7 '

    insmod: init_module 'sdio.ko' failed (Exec format error)

    这大大降低了开发速度,尤其是当你拿不到客户在用的kernel时,又要开发driver给他用,真的是很麻烦……

    那麼要怎麼利用恶心的方式绕过去呢???

    一、先把 Moudle version 检查关掉。

    user@host # ARCH=arm make menuconfig

    --- Enable loadable module support                                             │ │

    │ │         [ ]   Forced module loading                                      │ │

    │ │         [*]   Module unloading                                           │ │

    │ │         [*]     Forced module unloading                                  │ │

    │ │         [ ]   Module versioning support                                  │ │

    │ │         [ ]   Source checksum for all modules

    二、 使用modinfo时,可以看到目前这driver的vermagic

    filename: external_drivers/omap3530/Linux/sdio/sdio.ko
    author: Texas Instruments Inc
    alias: TIWLAN_SDIO
    license: GPL
    description: TI WLAN SDIO driver
    depends:
    vermagic: 2.6.28-271-gec75a15 preempt mod_unload ARMv7
    parm: g_sdio_debug_level:debug level (int)

    三、 修改 kernel 的 vermagic,再重新编译driver

    vermagic 的第一个值 2.6.28-noneed 是由这 include/linux/utsrelease.h里的 UTS_RELEASE 所定义。

    #define UTS_RELEASE "2.6.28-271-gec75a15"

    之后再由 include/linux/vermagic.h 里的 macro
    去组合出 VERMAGIC_STRING , 也就是 kernel 的vermagic。

    #include
    #include


    #ifdef CONFIG_SMP
    #define MODULE_VERMAGIC_SMP "SMP "
    #else
    #define MODULE_VERMAGIC_SMP ""
    #endif
    #ifdef CONFIG_PREEMPT
    #define MODULE_VERMAGIC_PREEMPT "preempt "
    #else
    #define MODULE_VERMAGIC_PREEMPT ""
    #endif完成编译后,你就可以得
    #ifdef CONFIG_MODULE_UNLOAD
    #define MODULE_VERMAGIC_MODULE_UNLOAD "mod_unload "
    #else
    #define MODULE_VERMAGIC_MODULE_UNLOAD ""
    #endif
    #ifndef CONFIG_MODVERSIONS
    #define MODULE_VERMAGIC_MODVERSIONS "modversions "
    #else
    #define MODULE_VERMAGIC_MODVERSIONS ""
    #endif
    #ifndef MODULE_ARCH_VERMAGIC
    #define MODULE_ARCH_VERMAGIC ""
    #endif

    #define VERMAGIC_STRING
    UTS_RELEASE " "
    MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT
    MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS
    MODULE_ARCH_VERMAGIC

    所以, 我们只要把 UTS_RELEASE 改成我们的数字即可,当然若是懒得去try组合后的字串,也可以直接将VERMAGIC_STRING改成你要的字串

    建议修改完 vermagic.h, utsrelease.h后,还是把kernel重编完再编kernel,比较保险。

    以下是修改后,用modinfo看的结果

    filename: external_drivers/omap3530/Linux/sdio/sdio.ko
    author: Texas Instruments Inc
    alias: TIWLAN_SDIO
    license: GPL
    description: TI WLAN SDIO driver
    depends:
    vermagic: 2.6.28 preempt mod_unload ARMv7
    parm: g_sdio_debug_level:debug level (int)

    ------------------------------------------------------------------------------------------

     另外若你是用git 做版本控制 , 那就会出现git的版本号在kernel 编号上
     所以要把他关掉
     
     
    General setup  --->
     [ ] Automatically append version information to the version strin

    解释;
    CONFIG_LOCALVERSION_AUTO:                                                   │ 
      │                                                                         │ 
      │ This will try to automatically determine if the current tree is a       │ 
      │ release tree by looking for git tags that belong to the current         │ 
      │ top of tree revision.                                                   │ 
      │                                                                         │ 
      │ A string of the format -gxxxxxxxx will be added to the localversion     │ 
      │ if a git-based tree is found.  The string generated by this will be     │ 
      │ appended after any matching localversion* files, and after the value    │ 
      │ set in CONFIG_LOCALVERSION.                                             │ 
      │                                                                         │ 
      │ (The actual string used here is the first eight characters produced     │ 
      │ by running the command:                                                 │ 
      │                                                                 
      │ which is done within the script "scripts/setlocalversion".)             │ 
      │                                                                         │ 
      │ Symbol: LOCALVERSION_AUTO [=y]                                          │ 
      │ Prompt: Automatically append version information to the version string  │ 
      │   Defined at init/Kconfig:84                                            │ 
      │   Location:                                                             │ 
      │ ingT

    修改 /arch/arm/include/asm/module.h
    /* Add __virt_to_phys patching state as well */
    /*#ifdef CONFIG_ARM_PATCH_PHYS_VIRT
    #ifdef CONFIG_ARM_PATCH_PHYS_VIRT_16BIT
    #define MODULE_ARCH_VERMAGIC_P2V "p2v16 "
    #else
    #define MODULE_ARCH_VERMAGIC_P2V "p2v8 "
    #endif
    #else*/
    #define MODULE_ARCH_VERMAGIC_P2V ""
    //#endif
  • 相关阅读:
    使用CustomValidate自定义验证控件
    C#中金额的大小写转换
    Andriod出错之Unable to build: the file dx.jar was not loaded from the SDK folder!
    VC 编写的打字练习
    机房工作笔记Ping只有单向通
    web服务协同学习笔记(1)
    Dll 学习3 将MDI子窗口封装在DLL中
    机房工作学习文件共享
    Andriod出错之Failed to find an AVD compatible with target 'Android 2.2'
    Andriod出错之wrapper was not properly loaded first
  • 原文地址:https://www.cnblogs.com/subo_peng/p/4723126.html
Copyright © 2011-2022 走看看