=分析uboot的配置过程(mkconfig脚本)=
uboot怎么配置?我们在终端上执行make NAME_config时的运行过程解析!
%_config:: unconfig
@$(MKCONFIG) -A $(@:_config=)
我们执行make *_config时会运行makefile的这两行程序,先分析下:
这个其实就是运行uboot根目录下的mkconfig文件,
并传入2个参数:
$1: -A
$2: $(@:_config=) :去掉目标*_config中的_config字符串也就是 *
然后开始运行mkconfig
APPEND=no # Default: Create new config file
BOARD_NAME="" # Name to print in make output
TARGETS=""
arch=""
cpu=""
board=""
vendor=""
soc=""
options=""
if [ \( $# -eq 2 \) -a \( "$1" = "-A" \) ] ; then
# Automatic mode
line=`egrep -i "^[[:space:]]*${2}[[:space:]]" boards.cfg` || {
echo "make: *** No rule to make target \`$2_config'. Stop." >&2
exit 1
}
set ${line}
# add default board name if needed
[ $# = 3 ] && set ${line} ${1}
fi
这里面涉及到shell脚本if的使用语法,单分支语句结构
if [ 条件表达式 ]; then
指令
fi
分析条件语句:
\( $# -eq 2 \) -a \( "$1" = "-A" \)
其中-a表示&& (语法:-o = or = ||, -a = and = && )
( $# -eq 2 )表示:输入的参数数目=2
( “$1” = “-A” ) 表示:输入的第一个参数=-A
这里条件满足,执行内部语句。
line=`egrep -i "^[[:space:]]*${2}[[:space:]]" boards.cfg` ||
{echo "make: *** No rule to make target \`$2_config'. Stop." >&2 exit 1}
两个语句执行||。
语句1:搜索文件获得模式。-i表示当进行比较时忽略字符的大小写。表示在boards.cfg里面搜索字符串"^ [[:space:]]*${2}[[:space:]]"即含有2#参数的字符串
例如:配置命令为
make halley2_xImage_nand_spl_boot_config
在boards.cfg里面line586
halley2_xImage_nand_spl_boot mips xburst halley2 ingenic x1000 halley2:SPL_SFC_NAND,SPL_OS_BOOT,MTD_SFCNAND
如果有该行代码则赋值给line,否则打印
"make: *** No rule to make target \`$2_config'. Stop."
同时退出。
继续!
set ${line}
表示把line中存储的变量作为本文件的输入参数,即执行该语句之前参数数量为2,执行之后为7!
while [ $# -gt 0 ] ; do
case "$1" in
--) shift ; break ;;
-a) shift ; APPEND=yes ;;
-n) shift ; BOARD_NAME="${1%_config}" ; shift ;;
-t) shift ; TARGETS="`echo $1 | sed 's:_: :g'` ${TARGETS}" ; shift ;;
*) break ;;
esac
done
关于shell指令的while循环语法:
while expression
do
command
done
条件是:参数个数$# -greater than 0 then…
关于shell指令的case语法:
case $xxx in
yyy)
do something
;;
esac
检查1#参数是否有这几个符号,这里1#参数是
halley2_xImage_nand_spl_boot
条件不满足,则不执行该语句。
[ $# -lt 4 ] && exit 1 //$# -less than 4 退出
[ $# -gt 7 ] && exit 1 //$# -great than 7 退出
# Strip all options and/or _config suffixes
CONFIG_NAME="${1%_config}"
[ "${BOARD_NAME}" ] || BOARD_NAME="${1%_config}"
如果定义了BOARD_NAME,则给它赋值。BOARD_NAME=halley2_xImage_nand_spl_boot_config
arch="$2" #赋值arch=mips
cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $1}'` #赋值cpu=xburst
spl_cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $2}'` #
awk为shell命令,具体参见收藏具体介绍。
if [ "$4" = "-" ] ; then
board=${BOARD_NAME}
else
board="$4"
fi
board="$4"=halley2
1# | halley2_xImage_nand_spl_boot |
---|---|
2# | mips |
3# | xburst |
4# | halley2 |
5# | ingenic |
6# | x1000 |
7# | halley2:SPL_SFC_NAND,SPL_OS_BOOT,MTD_SFCNAND |
[ $# -gt 4 ] && [ "$5" != "-" ] && vendor="$5"
[ $# -gt 5 ] && [ "$6" != "-" ] && soc="$6"
这里参数数量为7,两个条件均满足,所以vendor="$5" &&soc="$6"
[ $# -gt 6 ] && [ "$7" != "-" ] && {
tmp="${7%:*}"
if [ "$tmp" ] ; then
CONFIG_NAME="$tmp"
fi
# Check if we only have a colon...
if [ "${tmp}" != "$7" ] ; then
options=${7#*:}
TARGETS="`echo ${options} | sed 's:,: :g'` ${TARGETS}"
fi
}
以上条件满足,
#check if we have a board config name in the options field
# the options field mave have a board config name and a list
# of options, both separated by a colon (’:’); the options are
# separated by commas (’,’).
#
# Check for board name
#截取7#参数:
前面的部分和后面的部分,分别给CONFIG_NAME
和options
语法1:${VALUE%.*}或${VALUE%%.*}
删除VALUE字符串中以分隔符“.”匹配的右边字符,保留左边字符。tmp="${7%:*}"
这里tmp=halley2
CONFIG_NAME="$tmp"=halley2
语法2:sed是shell指令
if [ "${ARCH}" -a "${ARCH}" != "${arch}" ]; then
echo "Failed: \$ARCH=${ARCH}, should be '${arch}' for ${BOARD_NAME}" 1>&2
exit 1
fi
语法:-a表示and,条件与
这里条件不满足。
if [ "$options" ] ; then
echo "Configuring for ${BOARD_NAME} - Board: ${CONFIG_NAME}, Options: ${options}"
else
echo "Configuring for ${BOARD_NAME} board..."
fi
options
变量存在,打印以下内容Configuring for ${BOARD_NAME} - Board: ${CONFIG_NAME}, Options: ${options}
=Configuring for halley2_xImage_nand_spl_boot - Board: halley2, Options: SPL_SFC_NAND,SPL_OS_BOOT,MTD_SFCNAND
if [ "$SRCTREE" != "$OBJTREE" ] ; then
mkdir -p ${OBJTREE}/include
mkdir -p ${OBJTREE}/include2
cd ${OBJTREE}/include2
rm -f asm
ln -s ${SRCTREE}/arch/${arch}/include/asm asm
LNPREFIX=${SRCTREE}/arch/${arch}/include/asm/
cd ../include
mkdir -p asm
else
cd ./include
rm -f asm
ln -s ../arch/${arch}/include/asm asm
fi
条件不满足,进入else
分支,创建一个名称为asm的链接文件指向../arch/${arch}/include/asm
如果提示一下错误:
不可使用共享文件夹的方式进行
if [ -z "${soc}" ] ; then
ln -s ${LNPREFIX}arch-${cpu} asm/arch
else
ln -s ${LNPREFIX}arch-${soc} asm/arch
fi
语法1:shell脚本中的if 参数-a至-z
[-z string] “string”的长度为零则为真
这里条件不满足,执行else
创建链接文件asm/arch指向${LNPREFIX}arch-${soc}
即arch-x1000
#
# Create include file for Make
#
( echo "ARCH = ${arch}"
if [ ! -z "$spl_cpu" ] ; then
echo 'ifeq ($(CONFIG_SPL_BUILD),y)'
echo "CPU = ${spl_cpu}"
echo "else"
echo "CPU = ${cpu}"
echo "endif"
else
echo "CPU = ${cpu}"
fi
echo "BOARD = ${board}"
[ "${vendor}" ] && echo "VENDOR = ${vendor}"
[ "${soc}" ] && echo "SOC = ${soc}"
exit 0 ) > config.mk
在include/建立config.mk文件,
1.echo "ARCH = ${arch}" ----------------------->ARCH = mips
2.
if [ ! -z "$spl_cpu" ] ; then
echo 'ifeq ($(CONFIG_SPL_BUILD),y)'
echo "CPU = ${spl_cpu}"
echo "else"
echo "CPU = ${cpu}"
echo "endif"
else
echo "CPU = ${cpu}"
fi
-------------------->CPU = xburst
3.echo "BOARD = ${board}"--------------------->BOARD = halley2
4.echo "VENDOR = ${vendor}"--------------->VENDOR = ingenic
5.echo "SOC = ${soc}"--------------------->SOC = x1000
#
# 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
创建文件./include/config.h,打印第一句到文件
for i in ${TARGETS} ; do
i="`echo ${i} | sed '/=/ {s/=/ /;q; } ; { s/$/ 1/; }'`"
echo "#define CONFIG_${i}" >>config.h ;
done
???需要查阅语法
对应文本为:
#define CONFIG_SPL_SFC_NAND 1
#define CONFIG_SPL_OS_BOOT 1
#define CONFIG_MTD_SFCNAND 1
打印下列语句到文件
echo "#define CONFIG_SYS_ARCH \"${arch}\"" >> config.h
echo "#define CONFIG_SYS_CPU \"${cpu}\"" >> config.h
echo "#define CONFIG_SYS_BOARD \"${board}\"" >> config.h
[ "${vendor}" ] && echo "#define CONFIG_SYS_VENDOR \"${vendor}\"" >> config.h
[ "${soc}" ] && echo "#define CONFIG_SYS_SOC \"${soc}\"" >> config.h
===
#define CONFIG_SYS_ARCH "mips"
#define CONFIG_SYS_CPU "xburst"
#define CONFIG_SYS_BOARD "halley2"
#define CONFIG_SYS_VENDOR "ingenic"
#define CONFIG_SYS_SOC "x1000"
cat << EOF >> config.h
#define CONFIG_BOARDDIR board/$BOARDDIR
#include
#include
#include
#include
#include
#include
EOF
=============
#define CONFIG_BOARDDIR board/ingenic/halley2
#include
#include
#include
#include
#include
#include