基于nuvoton970学习 uboo2013 之 配置过程

一、介绍

nuvoton 970, 使用的配置为include/configs/nuc970_evb.h。start.S在arch/arm/cpu/arm926ejs/start.S。因为配置文件中使用了CONFIG_NAND_U_BOOT选项,所以会编译出两个目标。

1:u-boot-spl.bin。2:u-boot.bin。

u-boot-spl.bin引导u-boot.bin。

u-boot.bin引导linux。

每一款芯片都会有一段内部固化的程序,叫做BootRom,这段程序是我们不可修改的。芯片一上电Power On,便会启动BootRom。有的芯片内部BootRom负责把第二阶段的Bootloader加载到SRAM中,有的会初始化外部RAM,直接把bootloader加载到外部RAM中运行。而nuc970内的BootRom则是把外部bootloader加载到片内SRAM中运行,一般片内SRAM都会比较小,不能够加载运行一个完整的u-boot,所以u-boot就有了spl机制,spl即 Secondary Program Loader, BootRom为第一阶段, spl就是第二阶段了。u-boot-spl负责初始化外部RAM,并把u-boot主体加载到外部RAM中运行,u-boot主体再去引导kernel。

二、编译和源码分析

我打算按 1、配置阶段为:配置命令–>配置的详细过程

2、u-boot-spl.bin的编译过程

3、u-boot.bin的编译过程

三个阶段开始分析解读:

1、配置命令和配置的详细过程分析

配置命令:

make nuc970_config

在Makefile中能看到:

nuc970_nand_config		\
nuc970_config:			unconfig
		@mkdir	-p	$(obj)include	$(obj)board/nuvoton/nuc970evb
		@mkdir	-p	$(obj)nand_spl/board/nuvoton/nuc970evb
		@echo "#define CONFIG_NAND_U_BOOT" > $(obj)include/config.h
		@echo "CONFIG_NAND_U_BOOT = y" >> $(obj)include/config.mk
		@echo "RAM_TEXT = 0xE00000" >> $(obj)board/nuvoton/nuc970evb/config.tmp
		@$(MKCONFIG) nuc970_evb arm arm926ejs nuc970evb nuvoton nuc970

首先从这里看出,nuvoton 的配置并没有按照 2013版本的配置去写,这个在后面mkconfig中也有体现,标准被配的写法文章添加。

mkdir -p 的含义:创建指定路径,如果路径存在也不会报错,如果路径的父路径不存在也会创建父路径。所以上面两个创建路径的目的就是保证指定的路径是存在的。接下来三个echo 语句,是往指定文件里面写入内容,重点是最后一句:

@$(MKCONFIG) nuc970_evb arm arm926ejs nuc970evb nuvoton nuc970

查找Makefile 可知$(MKCONFIG)的值为:

MKCONFIG	:=	$(SRCTREE)/mkconfig

即当前路径下的 mkconfig 文件,所以配置命令可写成:

mkconfig nuc970_evb arm arm926ejs nuc970evb nuvoton nuc970。

hp:~$ file mkconfig
mkconfig: POSIX shell script, ASCII text executable

查看文件属性,为shell脚本,贴出mkconfig内容:

#!/bin/sh -e

APPEND=no       # Default: Create new config file
BOARD_NAME=""   # Name to print in make output
TARGETS=""

arch=""
cpu=""
board=""
vendor=""
soc=""
options=""
# 上面定义一些变量赋值为空
##############################################################

##############################################################
# $# 是传入的参数的个数, $1 是传入的第一个参数
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}
elif [ "${MAKEFLAGS+set}${MAKELEVEL+set}" = "setset" ] ; then
        # only warn when using a config target in the Makefile
        cat <<-EOF

        warning: Please migrate to boards.cfg.  Failure to do so will
                 mean removal of your board in the next release.

        EOF
        sleep 5
fi
# 上面if的两种情况,对应配置的两种情况,一种是上面nvoton使用的,就是elif的情况,
# warnning提示下个版本的uboot的配置就不支持这种配置方式了


#######################################################
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
# 判断第一个参数,nuvoton传入的第一个参数 nuc970_evb 
# 所以上面的while 走 *)出来。
#

#
# 检查参数的个数要 大于等于7 小于等于7
[ $# -lt 4 ] && exit 1
[ $# -gt 7 ] && exit 1

# Strip all options and/or _config suffixes
CONFIG_NAME=" ${1%_config}"
# $1 = nuc970_evb 所以 CONFIG_NAME="nuc970_evb_config"
# BOARD_NAME="nuc970_evb_config"
[ "${BOARD_NAME}" ] || BOARD_NAME="${1%_config}"

# $2=arm  $3=arm926ejs  $4=nuc970evb  
# 所以 arch=arm ,  cpu=arm926ejs board=nuc970evb  
arch="$2"
cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $1}'`
spl_cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $2}'`
if [ "$4" = "-" ] ; then
        board=${BOARD_NAME}
else
        board="$4"
fi

# $5=nuvoton   $6=nuc970
# 所以 vendor=nuvoton   soc=nuv970
#
[ $# -gt 4 ] && [ "$5" != "-" ] && vendor="$5"
[ $# -gt 5 ] && [ "$6" != "-" ] && soc="$6"

# 传入参数为6 所以下方{}语句不执行
[ $# -gt 6 ] && [ "$7" != "-" ] && {
        # 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
        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
}

# -a 这里是条件与的意思 如果 $(ARCH)不为空 并且 $(ARCH) 不等于 $(arch)的话会执行报错语句
#
if [ "${ARCH}" -a "${ARCH}" != "${arch}" ]; then
        echo "Failed: \$ARCH=${ARCH}, should be '${arch}' for ${BOARD_NAME}" 1>&2
        exit 1
fi
# $options为空,所以走 else
if [ "$options" ] ; then
        echo "Configuring for ${BOARD_NAME} - Board: ${CONFIG_NAME}, Options: ${options}"
else
        echo "Configuring for ${BOARD_NAME} board..."
fi

#
# Create link to architecture specific headers
# 编译目标路径和源码路径不一致走第一分支,一样的话走 else
# 这里要 include 下创建一些路径和软连接
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
# 删除include 下的 asm, 建立一个软连接
#	include/asm  -->   arch/arm/include/asm 
fi
# 删除include 下的 asm/arch
rm -f asm/arch

# 根据soc的值,soc=nuv970, 
# 把asm/arch链接到对应的路径, include/asm/arch ---> include/asm/arch-nuv970
if [ -z "${soc}" ] ; then
        ln -s ${LNPREFIX}arch-${cpu} asm/arch
else
        ln -s ${LNPREFIX}arch-${soc} asm/arch
fi

# 
# 创建一个链接 include/asm/proc ---->   include/asm/proc-armv
if [ "${arch}" = "arm" ] ; then
        rm -f asm/proc
        ln -s ${LNPREFIX}proc-armv asm/proc
fi

#
# 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
# 

# Assign board directory to BOARDIR variable
if [ -z "${vendor}" ] ; then
    BOARDDIR=${board}
else
    BOARDDIR=${vendor}/${board}
fi

#
# 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

##############################################################################
#
# 创建 include/config.h 配置文件,并写入内容
echo "/* Automatically generated - do not edit */" >>config.h

for i in ${TARGETS} ; do
        i="`echo ${i} | sed '/=/ {s/=/  /;q; } ; { s/$/ 1/; }'`"
        echo "#define CONFIG_${i}" >>config.h ;
done

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

cat << EOF >> config.h
#define CONFIG_BOARDDIR board/$BOARDDIR
#include <config_cmd_defaults.h>
#include <config_defaults.h>
#include <configs/${CONFIG_NAME}.h>
#include <asm/config.h>
#include <config_fallbacks.h>
#include <config_uncmd_spl.h>
EOF

exit 0

总结配置过程就是,把对应的头文件路径通过建立软链接联系起来,创建include/config.h和include/config.mk文件给make使用。

tips:

Makefile中有一段配置命令如下:

unconfig:
		@rm -f $(obj)include/config.h	$(obj)include/config.mk	\
			   $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp	\
			   $(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep

%_config:		unconfig
		@$(MKCONFIG) -A $(@:_config=)

# $(@:_config=) 就是获取参数的前半部分,就是去掉_config
#

配置命令呢,boards.cfg中查找。

例如:

socfpga_cyclone5      arm        armv7       socfpga             altera         socfpga

执行配置就是:make socfpga_cyclone5_config

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
        }
#
# 根据配置命令去 boards.cfg 文件中查找对应的配置,
# line= " socfpga_cyclone5   arm  armv7   socfpga  altera   socfpga"
#
        set ${line}
#	设置参数: $1 $2 $3 .......
#	
#
        # add default board name if needed
        [ $# = 3 ] && set ${line} ${1}
  else
  ....
  fi

个人感觉u-boot 2013版本更像是中间过渡版本,后面版本添加个人配置更灵活。