参考:
https://www.cnblogs.com/wrjvszq/p/4217171.html
https://blog.csdn.net/hyyoxhk/article/details/81734733
目录
pc与嵌入式系统启动步骤
分析u-boot.bin编译过程
build_uboot.sh分析
Makefile分析
mkconfig文件分析
链接文件u-boot.lds分析
u-boot启动分析
start.S文件分析
board.c内start_armboot函数分析
main_loop函数分析:
run_command函数分析
添加一条新的指令(以hello为例):
PC | 嵌入式系统 |
---|---|
BIOS | bootloader u-boot使用的最多 |
引导操作系统Windows | linux内核 |
识别c、d盘 |
挂接“根文件系统” |
运行应用程序 qq、msn等 | 应用程序 |
BootLoader最终目的:启动内核
u-boot要实现的功能
./build_uboot.sh SCP_1GDDR 在itop4412中使用编译脚本“build_uboot.sh”编译 uboot,参数为SCP_1GDDR
./build_uboot.sh主要内容:
if [ "$1" = "SCP_1GDDR" ] || [ "$1" = "SCP_2GDDR" ] || [ "$1" = "SCP_1GDDR_Ubuntu" ] || [ "$1" = "SCP_2GDDR_Ubuntu" ]
then
sec_path="../CodeSign4SecureBoot_SCP/"
CoreBoard_type="SCP"
if [ "$1" = "SCP_1GDDR" ]
then
make itop_4412_android_config_scp_1GDDR
make -j$CPU_JOB_NUM
Makefile文件:搜索 编译参数SCP_2GDDR 可得到
itop_4412_android_config_scp_1GDDR: unconfig
@$(MKCONFIG) $(@:_config=) arm arm_cortexa9 smdkc210 samsung s5pc210 SCP_1GDDR
MKCONFIG := $(SRCTREE)/mkconfig 表示当前目录下的mkconfig 所以这句代码可以表示为: ($1:就表示第1个参数或者命令)
mkconfig itop_4412_android_config_scp_2GDDR arm arm_cortexa9 smdkc210 samsung s5pc210 SCP_2GDDR
$0 $1 $2 $3 $4 $5 $6 $7
该条语句具体内容请看下文mkconfig分析。
OBJS = cpu/$(CPU)/start.o
ifeq ($(CPU),i386)
OBJS += cpu/$(CPU)/start16.o
OBJS += cpu/$(CPU)/resetvec.o
endif
ifeq ($(CPU),ppc4xx)
OBJS += cpu/$(CPU)/resetvec.o
endif
ifeq ($(CPU),mpc85xx)
OBJS += cpu/$(CPU)/resetvec.o
endif
OBJS := $(addprefix $(obj),$(OBJS))
LIBS = lib_generic/libgeneric.a
LIBS += lib_generic/lzma/liblzma.a
LIBS += lib_generic/lzo/liblzo.a
………………
ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND)
………………
$(obj)u-boot.bin: $(obj)u-boot
$(obj)u-boot: depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds
$(GEN_UBOOT)
ifeq ($(CONFIG_KALLSYMS),y)
smap=`$(call SYSTEM_MAP,u-boot) | \
awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "\\\\000"}'` ; \
$(CC) $(CFLAGS) -DSYSTEM_MAP="\"$${smap}\"" \
-c common/system_map.c -o $(obj)common/system_map.o
$(GEN_UBOOT) $(obj)common/system_map.o
mkconfig简单摘要:
mkconfig itop_4412_android_config_scp_2GDDR arm arm_cortexa9 smdkc210 samsung s5pc210 SCP_2GDDR
$0 $1 $2 $3 $4 $5 $6 $7
if [ "$7" = "SCP_1GDDR" ] || [ "$7" = "SCP_2GDDR" ] || [ "$7" = "POP_1GDDR" ] || [ "$7" = "POP_2GDDR" ]
then
BOARD_NAME="itop_4412_android"
echo "CoreBoard OS is android or linux...... "
……
[ "${BOARD_NAME}" ] || BOARD_NAME="$1"
[ $# -lt 4 ] && exit 1
[ $# -gt 7 ] && exit 1
……
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
if [ "$2" = "arm" ] ; then
rm -f asm-$2/proc
ln -s ${LNPREFIX}proc-armv asm-$2/proc
fi
#
# 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
……
#
# 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
for i in ${TARGETS} ; do
echo "#define CONFIG_MK_${i} 1" >>config.h ;
done
#add by dg for all itop4412 type boards
[ "$7" ] && [ "$7" != "NULL" ] && echo "#define CONFIG_$7" >> config.h
if [ "$7" = "SCP_1GDDR" ] || [ "$7" = "SCP_2GDDR" ] || [ "$7" = "POP_1GDDR" ] || [ "$7" = "POP_2GDDR" ]
then
BOARD_NAME="itop_4412_android"
echo "CoreBoard OS is android or linux...... " 命名BOARD_NAME,并打印一句话。
[ "${BOARD_NAME}" ] || BOARD_NAME="$1" 如果没有BOARD_NAME,那么就定义为 参数$1
[ $# -lt 4 ] && exit 1
[ $# -gt 7 ] && exit 1 如果参数小于4个或者大于7个就退出 显然不会退出
if [ "$SRCTREE" != "$SRCTREE" ] ; then 在Makefile源码中"$SRCTREE" "$SRCTREE" 两者相等,所以不执行if内容
cd ./include
rm -f asm
ln -s asm-$2 asm 代入$2就是 In -s asm-arm asm 创建一个连接文件 asm指向asm-arm
驱动源码中加入头文件#include
rm -f asm-$2/arch 删除asm-arm/arch
ln -s ${LNPREFIX}arch-$6 asm-$2/arch In -s arch-s5pc210 asm-arm/arch 在asm-arm目录下建一个arch文件,是一个链接文件指向arch-s5pc210
rm -f asm-$2/proc 删除asm-arm/proc
ln -s ${LNPREFIX}proc-armv asm-$2/proc In -s proc-armv asm-arm/proc 创建链接文件asm-arm/proc,指向proc-armv
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
ARCH = arm
CPU = arm_cortexa9
BOARD = smdkc210
VENDOR = samsung
SOC = s5pc210
> config.h 新建config.h头文件
echo "/* Automatically generated - do not edit */" >>config.h
for i in ${TARGETS} ; do
echo "#define CONFIG_MK_${i} 1" >>config.h;
done
追加 /* Automatically generated - do not edit */ 和#define CONFIG_MK_${i} 1 到config.h
---------mkconfig文件所做工作:
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
. = 0x00000000; 0xc3e00000
. = ALIGN(4);
.text :
{
cpu/arm_cortexa9/start.o (.text)
cpu/arm_cortexa9/s5pc210/cpu_init.o (.text)
board/samsung/smdkc210/lowlevel_init.o (.text)
common/ace_sha1.o (.text)
*(.text)
}
. = ALIGN(4);
.rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) }
. = ALIGN(4);
.data : { *(.data) }
. = ALIGN(4);
.got : { *(.got) }
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
. = ALIGN(4);
__bss_start = .;
.bss : { *(.bss) }
_end = .;
}
cd /home/topeet/android4.0/iTop4412_uboot && /usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-ld -Bstatic -T u-boot.lds -Ttext 0xc3e00000 $UNDEF_SYM 所以地址0x00000000会加上0xc3e00000
第一个链接的是cpu/arm_cortexa9/start.o (.text),因此u-boot.bin的入口代码在cpu/arm_cortexa9/start.o,对应的代码在cpu/arm920t/start.S中。
57-65行
中断向量设置
58行跳转到reset到184行
194-197行
设置CPU在SVC工作模式,获取最高等级的权限
201-216行
cache初始化
关闭cache
220-226行
关闭mmu(内存肯定不能用)
240-243行
读取OM状态
#define POWER_BASE 0x10020000
假设是eMMC启动,bit3=bit5=1 0x28
执行271行 保存OM状态为0x28 BOOT_EMMC441
293行
初始化PLL(时钟)、复用口、内存
跳转到
board/samsung/smdkc210/lowlevel_init文件中
296行
手机冷启动强制PS_HOLD输出高电平
301-304行
要准备调用C语言,从汇编到C语言必须设置堆栈
317-322行
判断程序是在eMMC中还是在内存中
不相等,直接执行后面的
326-332行
点亮两个灯
336-338行
延时
359-380行
367-368行
emmc441_boot跳到418行
418-433行
426行 emmc441_uboot_copy 是在bl2中
把uboot从emmc中拷贝到内存中(mmu是关闭的)
428-433行
如果拷贝失败,则去tf卡中找uboot
441-467行
使能MMU(代码已经在内存中,内存打开可以使用)
跳转到内存中
481-491行
堆栈设置以及初始化
509行
start_armboot跳到c语言
汇编阶段完成,到C语言阶段
----------------------------
start.S具体的工作:
init_fnc_t *init_sequence[] = {
#if defined(CONFIG_ARCH_CPU_INIT)
arch_cpu_init, /* basic arch cpu dependent setup */
#endif
board_init, /* basic board dependent setup */
//#if defined(CONFIG_USE_IRQ)
interrupt_init, /* set up exceptions */
//#endif
//timer_init, /* initialize timer */
#ifdef CONFIG_FSL_ESDHC
//get_clocks,
#endif
env_init, /* initialize environment */
init_baudrate, /* initialze baudrate settings */
serial_init, /* serial communications setup */
console_init_f, /* stage 1 init of console */
off_charge, // xiebin.wang @ 20110531,for charger&power off device.
display_banner, /* say that we are here */
#if defined(CONFIG_DISPLAY_CPUINFO)
print_cpuinfo, /* display cpu info (and speed) */
#endif
#if defined(CONFIG_DISPLAY_BOARDINFO)
checkboard, /* display board info */
#endif
#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
//init_func_i2c,
#endif
dram_init, /* configure available RAM banks */
#if defined(CONFIG_CMD_PCI) || defined (CONFIG_PCI)
//arm_pci_init,
#endif
display_dram_config,
NULL,
};
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0) {
hang ();
}
}
u-boot启动内核:
s = getenv ("bootcmd");
获得环境变量bootcmd,bootcmd = nand read.jffs2 0x30007FC0 kernel;bootm 0x30007FC0
run_command (s, 0);
执行环境变量bootcmd里面的命令(nand read.jffs2 0x30007FC0 kernel),做到读出内核并运行(bootm 0x30007FC0)。
----------------
假如bootcount倒计时按下回车键,进入u-boot控制界面
len = readline (CONFIG_SYS_PROMPT);
rc = run_command (lastcommand, flag);
读出串口的数据,并执行命令。
cmd_tbl_t *cmdtp; cmd_tbl_t 结构体里哦面包含了指令名字、实现函数等。
(*sep == ';') && 可以使用;号隔开两个指令,分别处理
if ((argc = parse_line (finaltoken, argv)) == 0) 提取出指令字符串
cmdtp = find_cmd(argv[0]) 从指令表找到指令
在u-boot.lds中:所有指令存放在 .u-boot_cmd段,从__u_boot_cmd_start开始,到__u_boot_cmd_end结束。
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
find_cmd从__u_boot_cmd_start开始开始查找指令,找到后返回cmd_tbl_t 结构体指针。
if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) 调用这个指令的实现函数。
在正常情况下(没有在bootcount倒计时按下回车),会执行nand read.jffs2和bootm指令来读取flash的内核和启动内核,如果按下回车进入boot模式,则等待串口传送的指令。
.u-boot_cmd段中存放的具体内容,以bootm为例
U_BOOT_CMD(
bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
"boot application image from memory",
"[addr [arg ...]]\n - boot application image stored in memory\n"
"\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
"\t'arg' can be the address of an initrd image\n"
);
这是一个宏定义
command.h中有如下定义
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}
则
cmd_tbl_t __u_boot_cmd_bootm __attribute__ ((unused,section (".u_boot_cmd"))) =
{"bootm", CONFIG_SYS_MAXARGS, 1, do_bootm, "boot application image from memory", help}
以下代码是cmd_hello.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int i;
printf("helloworld!\n");
for(i=0;i
nand read.jffs2 0x30007fc0 kernel; kernel表示分区。从nand读出内核:从哪里读?从kernel分区读取;放到那里去?0x0007FC0
do_nand函数读取内核。
bootm 0x30007FC0命令:
do_bootm函数:1、读取头部;2、移动内核到加载地址去;3、启动do_bootm_linux
do_bootm_linux:1、设置一些启动参数;2、调到入口地址启动内核
static boot_os_fn *boot_os[] = {
#ifdef CONFIG_BOOTM_LINUX
[IH_OS_LINUX] = do_bootm_linux,
#endif
#ifdef CONFIG_BOOTM_NETBSD
[IH_OS_NETBSD] = do_bootm_netbsd,
#endif
#ifdef CONFIG_LYNXKDI
[IH_OS_LYNXOS] = do_bootm_lynxkdi,
#endif
#ifdef CONFIG_BOOTM_RTEMS
[IH_OS_RTEMS] = do_bootm_rtems,
#endif
#if defined(CONFIG_CMD_ELF)
[IH_OS_VXWORKS] = do_bootm_vxworks,
[IH_OS_QNX] = do_bootm_qnxelf,
#endif
#ifdef CONFIG_INTEGRITY
[IH_OS_INTEGRITY] = do_bootm_integrity,
#endif
};
do_bootm_linux函数在boot_os结构体数组内。
#if defined (CONFIG_SETUP_MEMORY_TAGS) || \
defined (CONFIG_CMDLINE_TAG) || \
defined (CONFIG_INITRD_TAG) || \
defined (CONFIG_SERIAL_TAG) || \
defined (CONFIG_REVISION_TAG) || \
defined (CONFIG_LCD) || \
defined (CONFIG_VFD)
setup_start_tag (bd);
#ifdef CONFIG_SERIAL_TAG
setup_serial_tag (¶ms);
#endif
#ifdef CONFIG_REVISION_TAG
setup_revision_tag (¶ms);
#endif
#ifdef CONFIG_SETUP_MEMORY_TAGS
setup_memory_tags (bd);
#endif
#ifdef CONFIG_CMDLINE_TAG
setup_commandline_tag (bd, commandline);
#endif
#ifdef CONFIG_INITRD_TAG
if (images->rd_start && images->rd_end)
setup_initrd_tag (bd, images->rd_start, images->rd_end);
#endif
#if defined (CONFIG_VFD) || defined (CONFIG_LCD)
setup_videolfb_tag ((gd_t *) gd);
#endif
setup_end_tag (bd);
#endif
为内核设置启动参数
static void setup_start_tag (bd_t *bd)
{
params = (struct tag *) bd->bi_boot_params;
params->hdr.tag = ATAG_CORE;
params->hdr.size = tag_size (tag_core);
params->u.core.flags = 0;
params->u.core.pagesize = 0;
params->u.core.rootdev = 0;
params = tag_next (params);
}
以setup_start_tag为例,设置参数。
theKernel (0, machid, bd->bi_boot_params);
跳转到入口地址,启动内核。 machid:机器ID;bd->bi_boot_params:上面设置的启动参数