参考文献:

http://www.ylmf.net/zhuanti/zt02/2010/1108/8747.html

http://www.linuxdiyf.com/viewarticle.php?id=107960

http://wenku.baidu.com/view/9b156d1f650e52ea55189852.html

http://wenku.baidu.com/view/0f36597f27284b73f2425024.html?from=related&hasrec=1

1 Kconfig和Makefile

毫不夸张地说,Kconfig和Makefile是我们浏览内核代码时最为依仗的两个文件。基本上,Linux 内核中每一个目录下边都会有一个Kconfig文件和一个Makefile文件。Kconfig和Makefile就好似一个城市的地图,地图引导我们去 认识一个城市,而Kconfig和Makefile则可以让我们了解一个内核目录下面的结构。在希望研究内核的某个子系统、某个驱动或其他某个部分时,都 有必要首先仔细阅读一下相关目录下的Kconfig和Makefile文件。

分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文档相关的内核配置菜单。在内核配置make menuconfig时,从Kconfig中读出菜单,用户选择后保存到.config的内核配置文档中。 在内核编译时,主Makefile调用这个.config,就知道了用户的选择。

假如想使这个驱动被编译,则要修改Makefile文件,因此,需要添加新的驱动时,需要修改的文件有两个:Kconfig,Makefile.

2 菜单组织结构

一般一个Kconfig文件表示的就是一个菜单,一个菜单由多个菜单项组成,其格式如下:

menu 菜单名菜单项或菜单链接1菜单项或菜单链接2...菜单项或菜单链接nendmenu
其中菜单项就是指菜单的子菜单,所谓菜单链接就是指链接到另一个Kconfig文件,如此一下,菜单就可以实现随意嵌套了.

例如:

# drivers/Kconfigmenu "Device Drivers"source "drivers/base/Kconfig"source "drivers/connector/Kconfig"source "drivers/mtd/Kconfig"source "drivers/of/Kconfig"source "drivers/parport/Kconfig"source "drivers/pnp/Kconfig"source "drivers/block/Kconfig"source "drivers/hello/Kconfig"config test  bool "提示字符串"  default y  ...endmenu 

2.1 菜单项

语法:

config <symbol><config options>
<symbol>为一符号,就好像代码中的局部变量x一样,可用于后边的表达式中.

例如:

config UEVENT_HELPER_PATH       string "path to uevent helper"       depends on HOTPLUG       default "/sbin/hotplug"       help  Path to uevent helper program forked by the kernel for  every uevent.
上面菜单项的属性string表示菜单的类型,每一个菜单项必须有一个类型.

注:每个config菜单项都会产生一个配置选项CONFIG_XXX, XXX即为<symbol>. 如上,则会产生一个配置项:CONFIG_UEVENT_HELPER_PATH,此配置项的值记录在内核根目录下的隐藏文件.config内, 例:~/WORKING_DIRECTORY/kernel/goldfish/.config文件内.

2.2 菜单链接

菜单链接的格式如下:

source "路径"
如:

source "drivers/pnp/Kconfig"

2.3 菜单属性

2.3.1 类型

类型可以是:bool、tristate、string、hex和int。

bool类型的只能选中或不选中,选中为y,不选中为n.

tristate类型的菜单项为值可为三种值,多了编译成内核模块的选项。其值可为y,n,m.

string类型表示需要用户输入一串字符串。

hex类型则需要用户输入一个16进制数。

int类型表示用户输入一个整型.

总结:

菜单类型属性就好比一个控件,bool相当于单选框,trstate相当于有三种状态的复选框,string相当于供用户输入字符串的文本编辑框,hex相当于供用户输入16进制数的文本编辑框,而int就相当于供用户输入整型数的文本编辑框。

类型关键字后边可跟随提示字符,也可以不跟随,取决于情况。如:

string "path to uevent helper"
bool "Prevent firmware from being built"

注:每一个菜单项必须有类型属性。

2.3.2 默认值

默认值属性default一般在类型属性后边,如:

config UEVENT_HELPER_PATHstring "path to uevent helper"default "/sbin/hotplug"
表示当前菜单项若用户没有选择或输入任何值时,所取的默认值.上述所示为当前的默认值为"/sbin/totplug".

2.3.3 依赖

依赖可以是"depends on"或"requires".

语法:

depends on/requires <expr>

<expre>为表达式,可为之前定义的菜单项名.

如:

depends on HOTPLUG
表示此菜单项显示与否取决于另外一个菜单项HOTPLUG ,只有当菜单项HOTPLUG这个菜单项有效显示,当前菜单项才会显示。

例如:

    config MODULES          bool "Enable loadable module support"           config MODVERSIONS          bool "Set version information on all module symbols"          depends on MODULES               comment "module support disabled"          depends on !MODULES 
菜单项MODVERSIONS的显示与否取决于菜单项MODULES。这种信赖关系常用在子菜单项中。

2.3.4 选择

语法:

choice选择项..endchoice

2.3.5 提示

语法:

comment "提示信息字符串"comment选项

comment只是用来给用户提示信息的,后跟字符串,此字符串也可以在终端中显示。

comment选项只可以是deponds on。

2.3.6 帮助

语法:

help/---help--- <字符串>
例如:
config EXTRA_FIRMWARE_DIRstring "Firmware blobs root directory"depends on EXTRA_FIRMWARE != ""default "firmware"help  This option controls the directory in which the kernel build system  looks for the firmware files listed in the EXTRA_FIRMWARE option.  The default is the firmware/ directory in the kernel source tree,  but by changing this option you can point it elsewhere, such as  the /lib/firmware/ directory or another separate directory  containing firmware files.
help相当于注释一样,在给编辑Kconfig文件的人看的,这样可以保持其可读性.

3 举例

Kconfig:

# drivers/Kconfigmenu "Device Drivers"source "drivers/base/Kconfig"source "drivers/connector/Kconfig"source "drivers/mtd/Kconfig"...endmenu
其对应的make menuconfig界面如下图所示:

Android内核驱动开发中的Kconfig文件结构分析(图文)_第1张图片

source "drivers/base/Kconfig"中的Kconfig内容如下:即对应着上图中的第一项"Generic Driver Option"的子菜单内容:

menu "Generic Driver Options"config UEVENT_HELPER_PATHstring "path to uevent helper"depends on HOTPLUGdefault "/sbin/hotplug"help  Path to uevent helper program forked by the kernel for  every uevent.config STANDALONEbool "Select only drivers that don't need compile-time external firmware" if EXPERIMENTALdefault yhelp  Select this option if you don't have magic firmware for drivers that  need it.  If unsure, say Y.config PREVENT_FIRMWARE_BUILDbool "Prevent firmware from being built"default yhelp  Say yes to avoid building firmware. Firmware is usually shipped  with the driver, and only when updating the firmware a rebuild  should be made.  If unsure say Y here.config FW_LOADERtristate "Userspace firmware loading support" if EMBEDDEDdepends on HOTPLUGdefault y---help---  This option is provided for the case where no in-kernel-tree modules  require userspace firmware loading support, but a module built outside  the kernel tree does.config FIRMWARE_IN_KERNELbool "Include in-kernel firmware blobs in kernel binary"depends on FW_LOADERdefault yhelp  The kernel source tree includes a number of firmware 'blobs'  which are used by various drivers. The recommended way to  use these is to run "make firmware_install" and to copy the  resulting binary files created in usr/lib/firmware directory  of the kernel tree to the /lib/firmware on your system so  that they can be loaded by userspace helpers on request.  Enabling this option will build each required firmware blob  into the kernel directly, where request_firmware() will find  them without having to call out to userspace. This may be  useful if your root file system requires a device which uses  such firmware, and do not wish to use an initrd.  This single option controls the inclusion of firmware for  every driver which uses request_firmware() and ships its  firmware in the kernel source tree, to avoid a proliferation  of 'Include firmware for xxx device' options.  Say 'N' and let firmware be loaded from userspace.config EXTRA_FIRMWAREstring "External firmware blobs to build into the kernel binary"depends on FW_LOADERhelp  This option allows firmware to be built into the kernel, for the  cases where the user either cannot or doesn't want to provide it from  userspace at runtime (for example, when the firmware in question is  required for accessing the boot device, and the user doesn't want to  use an initrd).  This option is a string, and takes the (space-separated) names of the  firmware files -- the same names which appear in MODULE_FIRMWARE()  and request_firmware() in the source. These files should exist under  the directory specified by the EXTRA_FIRMWARE_DIR option, which is  by default the firmware/ subdirectory of the kernel source tree.  So, for example, you might set CONFIG_EXTRA_FIRMWARE="usb8388.bin",  copy the usb8388.bin file into the firmware/ directory, and build the  kernel. Then any request_firmware("usb8388.bin") will be  satisfied internally without needing to call out to userspace.  WARNING: If you include additional firmware files into your binary  kernel image which are not available under the terms of the GPL,  then it may be a violation of the GPL to distribute the resulting  image -- since it combines both GPL and non-GPL work. You should  consult a lawyer of your own before distributing such an image.config EXTRA_FIRMWARE_DIRstring "Firmware blobs root directory"depends on EXTRA_FIRMWARE != ""default "firmware"help  This option controls the directory in which the kernel build system  looks for the firmware files listed in the EXTRA_FIRMWARE option.  The default is the firmware/ directory in the kernel source tree,  but by changing this option you can point it elsewhere, such as  the /lib/firmware/ directory or another separate directory  containing firmware files.config DEBUG_DRIVERbool "Driver Core verbose debug messages"depends on DEBUG_KERNELhelp  Say Y here if you want the Driver core to produce a bunch of  debug messages to the system log. Select this if you are having a  problem with the driver core and want to see more of what is  going on.  If you are unsure about this, say N here.config DEBUG_DEVRESbool "Managed device resources verbose debug messages"depends on DEBUG_KERNELhelp  This option enables kernel parameter devres.log. If set to  non-zero, devres debug messages are printed. Select this if  you are having a problem with devres or want to debug  resource management for a managed device. devres.log can be  switched on and off from sysfs node.  If you are unsure about this, Say N here.config SYS_HYPERVISORbooldefault nendmenu
显示效果如下图所示:

Android内核驱动开发中的Kconfig文件结构分析(图文)_第2张图片


各个目录下的Kconfig文件经过最终配置,最终会在内核根目录下生成一个.config文件,这是个隐藏文件,这个文件记录着各个选项的配置及值。供Makefile文件使用.

如:

.config:

## Automatically generated make config: don't edit# Linux kernel version: 2.6.29# Thu Dec 15 21:15:25 2011#CONFIG_ARM=yCONFIG_SYS_SUPPORTS_APM_EMULATION=y# CONFIG_GENERIC_GPIO is not setCONFIG_GENERIC_TIME=yCONFIG_GENERIC_CLOCKEVENTS=yCONFIG_MMU=y# CONFIG_NO_IOPORT is not setCONFIG_GENERIC_HARDIRQS=yCONFIG_STACKTRACE_SUPPORT=yCONFIG_HAVE_LATENCYTOP_SUPPORT=yCONFIG_LOCKDEP_SUPPORT=yCONFIG_TRACE_IRQFLAGS_SUPPORT=yCONFIG_HARDIRQS_SW_RESEND=yCONFIG_GENERIC_IRQ_PROBE=yCONFIG_RWSEM_GENERIC_SPINLOCK=y# CONFIG_ARCH_HAS_ILOG2_U32 is not set# CONFIG_ARCH_HAS_ILOG2_U64 is not setCONFIG_GENERIC_HWEIGHT=yCONFIG_GENERIC_CALIBRATE_DELAY=yCONFIG_GENERIC_HARDIRQS_NO__DO_IRQ=yCONFIG_VECTORS_BASE=0xffff0000CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config"

 
 每一个CONFIG_xxx记录着之前Kconfig文件内的菜单项的值. 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                                                                                            

更多相关文章

  1. Android震动和播放资源文件中的声音文件
  2. Android文件管理器开发对各类文件的打开以及处理
  3. xml文件
  4. android sdcard存储方案(基于fuse文件系统):之一
  5. android读取data/data/包名/file路径下的txt文件
  6. 文件多线程下载实现
  7. 基于Android TV端的文件选择器(UI比较丑,主要看逻辑)
  8. Android学习笔记(八)之Android 读写xml文件
  9. Android札记:防反编译、混淆文件p…

随机推荐

  1. Android中图片保存到本地,并及时更新到系
  2. Android(安卓)绘制图形时消除锯齿
  3. Android源码个个击破之PackageManager
  4. Android完全退出的方案
  5. MTK平台camera bsp学习之camera HW架构篇
  6. Android(安卓)电话 短信
  7. 一起学android之设置ListView数据显示的
  8. 关于android中自定义SurfaceView放在布局
  9. clickOnMenuItem应注意的
  10. (转)Android动画学习笔记-Android(安卓)A