A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

在主 Makefile 中使用 mkconfig 的地方如下所示:



  • x210_sd_config :        unconfig



  •         @$(MKCONFIG) $(@:_config=) arm s5pc11x x210 samsung s5pc110



  •         @echo "TEXT_BASE = 0xc3e00000" > $(obj)board/samsung/x210/config.mk


在第 2 行使用了 mkconfig ,在这里使用了 Makefile 中的替换引用规则:类似常看到的例子 obj=$(srcfile:%.c=%.o) ,将所有的 .c 文件替换成 .o (这里只是替换名字,不涉及其他)。这里的 $(@:_config=) 是一样的: @ 代表的是目标 x210_sd_config ,那么 $(@:_config=) 就是将 x210_sd_config 中的 _config 替换为空,得到 x210_sd 。

经过以上可知,第 2 行的转换结果就是:

mkconfig x210_sd arm s5pc11x x210 samsung s5pc110

解析:将 6 个参数传递给 mkconfig 。

$1: x210_sd          BOARD_NAME                开发板名

$2: arm                 ARCH                                架构

$3: s5pc11x          CPU                                   CPU

$4: x210               BOARD                              开发板

$5: samsung         VENDOR                           厂商

$6: s5pc110          SOC                                   SOC

2. 参数检查

代码:11 ~ 12行



  • APPEND=no        # Default: Create new config file



  • BOARD_NAME=""        # Name to print in make output


APPEND 表示是否要创建 include/config.h 文件,在最后有了解; BOARD_NAME 为空

代码:14 ~ 21行



  • while [ $# -gt 0 ] ; do



  •         case "$1" in



  •         --) shift ; break ;;



  •         -a) shift ; APPEND=yes ;;



  •         -n) shift ; BOARD_NAME="${1%%_config}" ; shift ;;



  •         *)  break ;;



  •         esac



  • done


一开始就进入 while 循环中执行,判断条件是 $# > 0,则执行循环,$# 为 6,然后根据  $1 的参数进行执行,$1=x210_sd,只匹配到 * ,则执行 break 语句,跳出 while 循环

代码:23行

[ "${BOARD_NAME}" ] || BOARD_NAME="$1"

如果 BOARD_NAME 为真,则后面的不执行,为假,则执行 BOARD_NAME="$1",即此时 BOARD_NAME= x210_sd

代码:25 ~ 26行



  • [ $# -lt 4 ] && exit 1



  • [ $# -gt 6 ] && exit 1


如果 $# 小于 4 ,则 exit 1 ( mkconfig 脚本返回 1 ),即退出。

如果 $# 大于 6 ,则也返回 1 。

所以:mkconfig 脚本传参只能是 4、5、6 ,如果大于 6 或者小于 4 都不行。

3. 创建符号链接

代码:30 ~ 118行



  • #



  • # Create link to architecture specific headers



  • #



  • if [ "$SRCTREE" != "$OBJTREE" ] ; then



  •         mkdir -p ${OBJTREE}/include



  •         mkdir -p ${OBJTREE}/include2



  •         cd ${OBJTREE}/include2



  •         rm -f asm



  •         ln -s ${SRCTREE}/include/asm-$2 asm



  •         LNPREFIX="../../include2/asm/"



  •         cd ../include



  •         rm -rf asm-$2



  •         rm -f asm



  •         mkdir asm-$2



  •         ln -s asm-$2 asm



  • else



  •         cd ./include



  •         rm -f asm



  •         ln -s asm-$2 asm



  • fi







  • rm -f asm-$2/arch







  • if [ -z "$6" -o "$6" = "NULL" ] ; then



  •         ln -s ${LNPREFIX}arch-$3 asm-$2/arch



  • else



  •         ln -s ${LNPREFIX}arch-$6 asm-$2/arch



  • fi







  • # create link for s3c24xx SoC



  • if [ "$3" = "s3c24xx" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • # create link for s3c64xx SoC



  • if [ "$3" = "s3c64xx" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • # create link for s5pc1xx SoC



  • if [ "$3" = "s5pc1xx" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • # create link for s5pc11x SoC



  • if [ "$3" = "s5pc11x" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • # create link for s5p64xx SoC



  • if [ "$3" = "s5p64xx" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • # create link for s5p644x SoC



  • if [ "$3" = "s5p644x" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi







  • if [ "$2" = "arm" ] ; then



  •         rm -f asm-$2/proc



  •         ln -s ${LNPREFIX}proc-armv asm-$2/proc



  • fi







  • # create link for s3c64xx-mp SoC



  • if [ "$3" = "s3c64xx-mp" ] ; then



  •         rm -f regs.h



  •         ln -s $6.h regs.h



  •         rm -f asm-$2/arch



  •         ln -s arch-$3 asm-$2/arch



  • fi


为什么要创建符号链接?这些符号链接文件的存在就是整个配置过程的核心,这些符号链接文件(文件夹)的主要作用是给头文件包含等过程提供指向性连接。根本目的是让 uboot 具有可移植性。

uboot 可移植性的实现原理:在 uboot 中有很多彼此平行的代码,各自属于各自不同的架构/ CPU /开发板,我们在具体到一个开发板的编译时用符号连接的方式提供一个具体的名字的文件夹供编译时使用。这样就可以在配置的过程中通过不同的配置使用不同的文件,就可以正确的包含正确的文件。

代码过程

(1)首先判断源码目录和输出目录是否是一致的。若一致,则执行 then 后的语句;否则,则执行 else 后的语句。在这里,不分析 then 后的语句,分析 else 后的语句。创建 include/asm 文件,并链接到 include/asm-arm 文件。

(2)删除 include/asm-arm/arch 文件

(3)执行第二个 if 语句,判断条件是若 $6 字符串的长度为 0 或者 $6 为 NULL,则执行 then 后的语句,创建include/asm-arm/arch 文件,并链接到 include/asm-arm/arch-s5pc11x;否则,将执行 else 后的语句,创建 include/asm-arm/arch 文件,并链接到 include/asm-arm/arch-s5pc110 。

(4)执行第三个 if 语句,在这里我们只看 "$3" = "s5pc11x" 的选项,其他的类似。判断条件是 $3 为 s5pc11x 的话,则执行——创建 include/regs.h 文件,并链接到 include/s5pc110.h 文件,删除 include/asm-arm/arch ,然后再创建 include/asm-arm/arch,并链接到 include/arch-s5pc11x 。 

  总结:一共创建了 4 个符号链接。这 4 个符号链接将来在写代码过程中,头文件包含时非常有用。譬如一个头文件包含可能是:#include <asm/xx.h> 。

4. 创建 include/config.mk 文件

代码:120 ~ 130行



  • #



  • # Create include file for Make



  • #



  • echo "ARCH   = $2" >  config.mk



  • echo "CPU    = $3" >> config.mk



  • echo "BOARD  = $4" >> config.mk







  • [ "$5" ] && [ "$5" != "NULL" ] && echo "VENDOR = $5" >> config.mk







  • [ "$6" ] && [ "$6" != "NULL" ] && echo "SOC    = $6" >> config.mk


创建 include/config.mk 文件:将 ARCH=arm、CPU=s5pc11x、BOARD=x210、VENDOR=samsung、SOC=s5pc110 写入到 include/config.mk 文件中。

创建的 include/config.mk 文件是为了让主 Makefile 在 133 行去包含的。

5. 创建 include/config.h 头文件

代码:131 ~ 最后



  • #



  • # Create board specific header file



  • #



  • if [ "$APPEND" = "yes" ]        # Append to existing config file



  • then



  •         echo >> config.h



  • else



  •         > config.h                # Create new config file



  • fi



  • echo "/* Automatically generated - do not edit */" >>config.h



  • echo "#include <configs/$1.h>" >>config.h







  • exit 0


若 APPEND 为 yes,则将新内容追加到 config.h 中,否则创建一个新的 config.h 文件,最后打印两行内容,追加到 config.h 中,执行成功后则退出。

include/config.h 文件里面的内容就一行 #include<configs/x210_sd.h>,这个头文件是我们移植 x210 开发板时,对开发板的宏定义配置文件,这个文件是移植 x210_sd 时最主要的文件。

x210_sd.h 文件会被用来生成一个 autoconfig.mk 文件,这个文件会被主 Makefile 引入,指导整个编译过程。这里面的这些宏定义会影响我们 uboot 中大部分的 .c 文件中一些条件编译的选择,从而最终实现可移植性。

6. 注意

uboot 的整个配置过程,很多文件之间是有关联的(有时候这个文件是在那个文件中创建出来的;有时候这个文件被那个文件包含进去;有时候这个文件是由那个文件的内容生成的决定的)

注意:uboot 中配置和编译过程,所有的文件或者全局变量都是字符串形式的(不是指的 C 语言字符串的概念,指的是都是字符组成的序列)。这意味着我们整个 uboot 的配置过程都是字符串匹配的,所以一定要细节,注意大小写,要注意不要输错字符,因为一旦错一个最后会出现一些莫名其妙的错误,很难排查,这个是 uboot 移植过程中新手来说最难的地方。


1 个回复

倒序浏览

很不错,受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马