系统移植-(三)kernel内核源码的配置和编译过程

linux内核源码的配置和编译

  • 内核 -源码目录
  • 内核的配置
    • (一)配置交叉编译工具链
    • (二)README:解读内核配置过程
      • (1)配置内核源码支持当前的硬件平台
        • .config文件解读
      • (2)make menuconfig:配置内核
      • (3)编译内核生成:uImage
      • (4)测试uImage:kernel顶层目录/arch/arm/boot/uImage

内核 -源码目录

系统移植-(三)kernel内核源码的配置和编译过程_第1张图片
1.在ubuntu的家目录下创建kernel文件夹,将共享文件夹下的内核源码拷贝到kernel下
mkdir kernel
cd kernel
cp /mnt/hgfs/share/kernel-3.4.39-ok.tar.bz2 ./

2.对内核源码进行解压缩:
tar -vxf kernel-3.4.39-ok.tar.bz2

内核的配置

(1)make clean/disclean:清除编译产生的中间文件和检查makefile是否正确无误。
make mrproper :清除更彻底、干净
(2)ctags -R 或 make tags:在顶层kernel目录下创建tags索引文件

(一)配置交叉编译工具链

因为kernel要移植到ARM_目标板,因此需要配置交叉编译工具:
打开内核源码顶层目录下的Makefile,搜索:/CROSS_COMPILE

195 ARCH        ?= 
196 CROSS_COMPILE   ?= 
修改为:
195 ARCH        ?= arm
196 CROSS_COMPILE   ?= arm-none-linux-gnueabi-

注意:arm-none-linux-gnueabi-中,字符 ’ - ’ 后面不可有空格。

(二)README:解读内核配置过程

138 BUILD directory for the kernel:  //内核编译目录
139 
140    When compiling the kernel all output files will per default be
141    stored together with the kernel source code.
142    Using the option "make O=output/dir" allow you to specify an alternate
143    place for the output files (including .config).
144    Example:
145      kernel source code:    /usr/src/linux-3.N
146      build directory:       /home/name/build/kernel
147 
148    To configure and build the kernel use:	//配置和编译内核源码用以下命令:
149    cd /usr/src/linux-3.N
150    make O=/home/name/build/kernel menuconfig
151    make O=/home/name/build/kernel
152    sudo make O=/home/name/build/kernel modules_install install
153 
154    Please note: If the 'O=output/dir' option is used then it must be
155    used for all invocations of make.

157 CONFIGURING the kernel:   //内核的配置
158 
159    Do not skip this step even if you are only upgrading one minor
160    version.  New configuration options are added in each release, and
161    odd problems will turn up if the configuration files are not set up
162    as expected.  If you want to carry your existing configuration to a
163    new version with minimal work, use "make oldconfig", which will
164    only ask you for the answers to new questions.

//即使您只升级了一个次要的版本,也不要跳过这一步。每个版本都增加了新的配置选项,
如果配置文件没有按照预期设置好,就会出现奇怪的问题。
如果您想将现有的配置以最少的工作量转移到新版本中,
那么使用“make oldconfig”,它将只向您询问新问题的答案。

165 
166  - Alternate configuration commands are:	//配置命令的选择:
167     "make config"      Plain text interface.	//纯文本界面
168     "make menuconfig"  Text based color menus, radiolists & dialogs.   //彩色菜单对话框
179     "make defconfig"   Create a ./.config file by using the default
180                symbol values from either arch/$ARCH/defconfig
181                or arch/$ARCH/configs/${PLATFORM}_defconfig,
182                depending on the architecture.   //通过默认架构配置内核

183     "make ${PLATFORM}_defconfig"	//配置内核源码支持当前架构的硬件平台
184               Create a ./.config file by using the default
185               symbol values from
186               arch/$ARCH/configs/${PLATFORM}_defconfig.
187               Use "make help" to get a list of all available
188               platforms of your architecture.
//使用arch/$ arch/ configs/${PLATFORM}_defconfig中的默认符号值来创建一个当前目录下的.config文件。
使用“make help”可获取当前架构中所有可用平台的列表。

200    You can find more information on using the Linux kernel config tools
201    in Documentation/kbuild/kconfig.txt.   
你可以在Documentation/kbuild/kconfig.txt中找到更多关于使用Linux内核配置工具的信息。

(1)配置内核源码支持当前的硬件平台

1)方法一:make help

linux@ubuntu:~/kernel/kernel-3.4.39$ make help
make + 目标:
Cleaning targets:
  clean		  - Remove most generated files but keep the config and
                    enough build support to build external modules
  mrproper	  - Remove all generated files + config + various backup files
  distclean	  - mrproper + remove editor backup and patch files

Configuration targets:
  config	  - Update current config utilising a line-oriented program
  nconfig     - Update current config utilising a ncurses menu based program
 defconfig	  - New config with default from ARCH supplied defconfig
 menuconfig   - Text based color menus, radiolists & dialogs.   //彩色菜单对话框
 
Other generic targets://其他通用的目标
  all		  - Build all targets marked with [*]
* vmlinux	  - Build the bare kernel
* modules	  - Build all modules

Architecture specific targets (arm):
* zImage        - Compressed kernel image (arch/arm/boot/zImage)
  Image         - Uncompressed kernel image (arch/arm/boot/Image)
* xipImage      - XIP kernel image, if configured (arch/arm/boot/xipImage)
  uImage        - U-Boot wrapped zImage

fs6818_defconfig         - Build for fs6818

2)方法二:make ${PLATFORM}_defconfig

"make ${PLATFORM}_defconfig"
 arch/$ARCH/configs/${PLATFORM}_defconfig.

进入 arch/arm/configs/目录下,能找到支持自己平台板子的文件:{PLATFORM}_defconfig.
因为${PLATFORM}_defconfig文件必须根据源码目录下的Makefile配置才能生成.config文件。

在顶层目录下,执行命令:make ${PLATFORM}_defconfig,来让当前的内核支持自己的硬件平台。
eg:make fs6818_defconfig
执行结果如下:

linux@ubuntu:kernel-3.4.39$ make fs6818_defconfig
HOSTCC  scripts/basic/fixdep
HOSTCC  scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC  scripts/kconfig/zconf.tab.o
HOSTLD  scripts/kconfig/conf
# 
# configuration written to .config
#

执行完命令将板子的配置信息写入到内核源码顶层目录下的.config文件中。

.config文件解读

   1 #
   2 # Automatically generated file; DO NOT EDIT.
   3 # Linux/arm 3.4.39 Kernel Configuration
   4 #
   5 CONFIG_ARM=y
   6 CONFIG_HAVE_PWM=y
   7 CONFIG_SYS_SUPPORTS_APM_EMULATION=y
   8 CONFIG_GENERIC_GPIO=y               
   9 # CONFIG_ARCH_USES_GETTIMEOFFSET is not set
  10 CONFIG_GENERIC_CLOCKEVENTS=y
  11 CONFIG_GENERIC_CLOCKEVENTS_BROADCAST=y
  12 CONFIG_KTIME_SCALAR=y
  ...
 320 CONFIG_FS6818=y
~

CONFIG_FS6818=y:配置内核支持自己的板子FS6818

(2)make menuconfig:配置内核

make menuconfig:基于菜单选项方式对内核进行配置
注意:
(1)首次使用make menuconfig之前,需要先配置安装图形化界面的工具库:

	sudo apt-get install libncurses5-dev 

(2)出现下面错误:终端的字体太大,需要终端界面缩小一点

cripts/kconfig/mconf Kconfig
Your display is too small to run Menuconfig!
It must be at least 19 lines by 80 columns.
make[1]: *** [menuconfig] Error 1
make: *** [menuconfig] Error 2

make menuconfig执行过程:

linux@ubuntu:~/kernel/kernel-3.4.39$ make menuconfig
  HOSTCC  scripts/kconfig/conf.o
  HOSTCC  scripts/kconfig/lxdialog/checklist.o
  HOSTCC  scripts/kconfig/lxdialog/inputbox.o
  HOSTCC  scripts/kconfig/lxdialog/menubox.o
  HOSTCC  scripts/kconfig/lxdialog/textbox.o
  HOSTCC  scripts/kconfig/lxdialog/util.o
  HOSTCC  scripts/kconfig/lxdialog/yesno.o
  HOSTCC  scripts/kconfig/mconf.o
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

系统移植-(三)kernel内核源码的配置和编译过程_第2张图片
注:空格或者Y/N:选择三态/布尔类型选项

(3)编译内核生成:uImage

make uImage 
time make uImage -jx

注:
time:回显编译的时间
-jx:使用多线程的方式进行编译;x:可以是2、4、6、8

  • 编译过程中可能出现的错误
"mkimage" command not found - U-Boot images will not be built
make[1]: *** [arch/arm/boot/uImage] Error 1
make: *** [uImage] Error 2

错误的原因:找不到mkimage命令,根据提示分析mkimage存在于u-boot源码~/tools/目录下,
因此u-boot源码必须先进行编译之后才会有mkimage可执行程序。

  • 解决问题的方法:将u-boot源码的tools目录下的mkimage,拷贝到到ubuntu的/usr/bin目录下:
sudo cp ./tools/mkimage   /usr/bin
          u-boot目录      ubuntu目录

再次对内核源码进行编译即可通过。

(4)测试uImage:kernel顶层目录/arch/arm/boot/uImage

在ubuntu下,将kernel目录下的arch/arm/boot/uImage拷贝到~/tftpboot目录下,测试uImage是否可以正常启动,并且挂载根文件系统。

通过tftp的方式下载uImage到内存中,
通过nfs的方式挂载根文件系统。
注意:检查bootcmd和bootargs参数设置是否正确。

你可能感兴趣的:(系统移植-(三)kernel内核源码的配置和编译过程)