分类: Android平台
2013-01-05 14:45:44
在開發kerneldriver時,總是會遇到討人厭的vermagic檢查,只要目前在run的kernel版本跟driver編譯時用的kernel版本不一致,就沒辦法insmod。
bash-3.2# insmod sdio.ko
sdio: version magic '2.6.28-271-gec75a15 preempt mod_unloadmodversions 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 moduleloading ││
│ │ [*] Moduleunloading │ │
│ │ [*] Forcedmoduleunloading ││
│ │ [ ] Module versioningsupport ││
│ │ [ ] Source checksum for allmodules
二、 使用modinfo時,可以看到目前這driver的vermagicfilename: 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 preemptmod_unload ARMv7
parm: g_sdio_debug_level:debug level (int)
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 tothe version strin
解釋;
CONFIG_LOCALVERSION_AUTO: │
│ │
│ This will try to automatically determine ifthe current tree isa │
│ release tree by looking for git tags thatbelong to thecurrent │
│ top of treerevision. │
│ │
│ A string of the format -gxxxxxxxx will beadded to thelocalversion │
│ if a git-based tree isfound. The string generated by this willbe │
│ appended after any matching localversion*files, and after thevalue │
│ set inCONFIG_LOCALVERSION. │
│ │
│ (The actual string used here is the firsteight charactersproduced │
│ by running thecommand: │
│
│ which is done within the script"scripts/setlocalversion".) │
│ │
│ Symbol: LOCALVERSION_AUTO[=y] │
│ Prompt: Automatically append versioninformation to the version string │
│ Defined atinit/Kconfig:84 │
│ Location: │
│ ingT