uboot的启动流程:
看一幅图:
1.第一阶段:start.s的内容:
点击(此处)折叠或打开
#include @该文件是第二步中mkconfig文件执行时创建的。include/config.h
#include
#include @在include目录下。
/*
*************************************************************************
*
* Jump vector table as in table 3.1 in [1]
*
*************************************************************************
*/
@这是一个异常跳转表,
.globl _start
_start:
b start_code @start_code才是真正的启动代码
ldr pc, _undefined_instruction
ldr pc, _software_interrupt
ldr pc, _prefetch_abort
ldr pc, _data_abort
ldr pc, _not_used
ldr pc, _irq
ldr pc, _fiq
_undefined_instruction: .word undefined_instruction @在标号_undefined_instruction处存储了一个变量,该变量也是一个标号地址
_software_interrupt: .word software_interrupt @执行上面的ldr pc, _undefined_instruction;指令会将变量undefined_instruction的值装入PC
_prefetch_abort: .word prefetch_abort @pc指向一个地址,CPU从该地址中取指执行
_data_abort: .word data_abort
_not_used: .word not_used
_irq: .word irq
_fiq: .word fiq
.balignl 16,0xdeadbeef
/*
*************************************************************************
*
* Startup Code (called from the ARM reset exception vector)
*
* do important init only if we don't start from memory!
* relocate armboot to ram
* setup stack
* jump to second stage
*
*************************************************************************
*/
_TEXT_BASE:
.word TEXT_BASE @变量TEXT_BASE由连接脚本指定,
.globl _armboot_start
_armboot_start:
.word _start@_start是在起始代码处定义的标号,当跳到_armboot_start后还是会跳转到_start
/*
* These are defined in the board-specific linker script.
*/
.globl _bss_start
_bss_start:
.word __bss_start @这是board/mini2440/u-boot.lds脚本中的变量,
.globl _bss_end
_bss_end:
.word_end @这也是board/mini2440/u-boot.lds脚本中的变量,
#ifdef CONFIG_USE_IRQ @这个宏在include/configs/$(board_name).h中定义。可以取消
/* IRQ stack memory (calculated at run-time) */
.globl IRQ_STACK_START
IRQ_STACK_START:
.word 0x0badc0de
/* IRQ stack memory (calculated at run-time) */
.globl FIQ_STACK_START
FIQ_STACK_START:
.word 0x0badc0de
#endif
/*
* the actual start code
*/
start_code: @这是真正的启动代码
/* set the cpu to SVC32 mode*/
mrs r0,cpsr
bic r0,r0,#0x1f
orr r0,r0,#0xd3
msr cpsr,r0
bl coloured_LED_init @这个函数在status_led.h中被提升了作用域,但是在哪里面实现??status_led.h文件
bl red_LED_on @同上
@下面这些宏开关也是在include/configs/$(board_name).h中定义??
#if defined(CONFIG_AT91RM9200DK) || defined(CONFIG_AT91RM9200EK) || defined(CONFIG_AT91RM9200DF)
/*
* relocate exception table
*/
ldr r0, =_start
ldr r1, =0x0
mov r2, #16
copyex:
subs r2, r2, #1
ldr r3, [r0], #4
str r3, [r1], #4
bne copyex
#endif
@下面这些宏开关是在include/configs/$(board_name).h中定义
#if defined(CONFIG_S3C2400) || defined(CONFIG_S3C2410)||defined(CONFIG_S3C2440)
/* turn off the watchdog */
# if defined(CONFIG_S3C2400)
# define pWTCON 0x15300000
# define INTMSK 0x14400008 /* Interupt-Controller base addresses */
# define CLKDIVN 0x14800014 /* clock divisor register */
#else
# define pWTCON 0x53000000
# define INTMSK 0x4A000008 /* Interupt-Controller base addresses */
# define INTSUBMSK 0x4A00001C
# define CLKDIVN 0x4C000014 /* clock divisor register */
# endif
#define CLK_CTL_BASE 0x4c000000
#define MDIV_405 0x7f<<12
#define PSDIV_405 0x21
#define UPLL_MDIV_48 0x38<<12
#define UPLL_PSDIV_48 0x22
#define MDIV_200 0xa1<<12
#define PSDIV_200 0x31
ldr r0, =pWTCON
mov r1, #0x0
str r1, [r0]
/*
* mask all IRQs by setting all bits in the INTMR - default
*/
mov r1, #0xffffffff
ldr r0, =INTMSK
str r1, [r0]
# if defined(CONFIG_S3C2410)
ldr r1, =0x3ff
ldr r0, =INTSUBMSK
str r1, [r0]
# endif
/*add by gray*/
#if defined(CONFIG_S3C2440)
ldr r1, =0x7fff
ldr r0, =INTSUBMSK
str r1, [r0]
#endif
/*add by gray,这里是修改后的,其实可以在uboot启动的第二阶段start_armboot()函数里调用board_init()函数时重置CPU钟
对于S3C2440,MPLL(clk)=(2 * m * Fin) / p * 2^s,
m = MDIV + 8, p = PDIV + 2, s = SDIV.
这里MDIV = 0x7f, PDIV = 0x2 ,SDIV = 0x1
*/
#if defined(CONFIG_S3C2440)
/*FCLK:HCLK:PCLK=1:2:4*/
ldr r0, =CLKDIVN
mov r1, #5
str r1,[r0]
mrc p15,0,r1,c1,c0,0 /*read ctrl reg*/
orr r1,r1,#0xc0000000 /*ASYN*/
mcr p15,0,r1,c1,c0,0 /*write ctrl reg*/
mov r1,#CLK_CTL_BASE
mov r2,#UPLL_MDIV_48
add r2, r2,#UPLL_PSDIV_48
str r2,[r1,#0x08] /*write UPLL first,48MHZ*/
mov r2,#MDIV_405
add r2,r2,#PSDIV_405 /*mpll_405MHZ*/
str r2,[r1,#0x04] /*MPLLCON*/
#else
/*F:H:P=1:2:4*/
ldr r0, =CLKDIVN
mov r1, #3
str r1,[r0]
mrc p15,0,r1,c1,c0,0 /*read ctrl reg*/
orr r1,r1,#0xc0000000
mcr p15,0,r1,c1,c0,0
mov r1,#CLK_CTL_BASE
mov r2,#MDIV_200
add r2,r2,#PSDIV_200 /*mpll_200MHZ*/
str r2,[r1,#0x04] /*MPLLCON*/
#endif
#endif /*CONFIG_S3C2400 || CONFIG_S3C2410 || CONFIG_S3C2440*/
/*
* we do sys-critical inits only at reboot,
* not when booting from ram!
*/
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
bl cpu_init_crit @如果没有定义CONFIG_SKIP_LOWLEVEL_INIT,就进行严格的初始化,调用lowlevel_init函数
#endif
#ifndef CONFIG_AT91RM9200 @AT91RM9200评估板,基于ARM920T内核
#ifndef CONFIG_SKIP_RELOCATE_UBOOT
/* relocate U-Boot to RAM */
relocate:
adr r0, _start /* r0
ldr r1, _TEXT_BASE /* test if we run from flash or RAM */
cmp r0, r1 /* don't reloc during debug */
beq stack_setup /*如果_start与_TEXT_BASE相等,就直接跳去stack_setup进行栈的设置, 否则就要进行代码的拷贝,把代码拷贝到内存中*/
ldr r2, _armboot_start
ldr r3, _bss_start @计算代码段大小=_bss_start-_armboot_start
sub r2, r3, r2 /* r2
add r2, r0, r2 /* r2
@ r2 = _start的地址+代码段大小
copy_loop:
ldmia r0!, {r3-r10} /* copy from source address [r0] */
stmia r1!, {r3-r10} /* copy to target address [r1] */
cmp r0, r2 /* until source end addreee [r2] */
ble copy_loop
#endif /* CONFIG_SKIP_RELOCATE_UBOOT */
#endif
/* Set up the stack,设置栈 */
stack_setup:
ldr r0, _TEXT_BASE /*upper 128 KiB: relocated uboot */
sub r0, r0, #CFG_MALLOC_LEN /* malloc area,这是堆区 */
sub r0, r0, #CFG_GBL_DATA_SIZE /* bdinfo */
#ifdef CONFIG_USE_IRQ @如果定义了IRQ,就要为IRQ,FIQ分配中断栈。
sub r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
#endif
sub sp, r0, #12 /* leave 3 words for abort-stack */
clear_bss: @清除bss段,_bss_start 和_bss_end就是u-boot.lds脚本传入的变量
ldr r0, _bss_start /* find start of bss segment */
ldr r1, _bss_end /* stop here */
mov r2, #0x00000000 /* clear */
clbss_l:str r2, [r0] /* clear loop... */
add r0, r0, #4
cmp r0, r1
ble clbss_l
ldr pc, _start_armboot
_start_armboot: .word start_armboot @跳到start_armboot()函数执行,进入uboot的第二阶段
/*
*************************************************************************
*
* CPU_init_critical registers
*
* setup important registers
* setup memory timing
*
*************************************************************************
*/
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
cpu_init_crit: @进行严格的初始化,关闭I/D caches,MMU,
/*
* flush v4 I/D caches
*/
mov r0, #0
mcr p15, 0, r0, c7, c7, 0 /* flush v3/v4 cache */
mcr p15, 0, r0, c8, c7, 0 /* flush v4 TLB */
/*
* disable MMU stuff and caches
*/
mrc p15, 0, r0, c1, c0, 0
bic r0, r0, #0x00002300 @ clear bits 13, 9:8 (--V- --RS)
bic r0, r0, #0x00000087 @ clear bits 7, 2:0 (B--- -CAM)
orr r0, r0, #0x00000002 @ set bit 2 (A) Align
orr r0, r0, #0x00001000 @ set bit 12 (I) I-Cache
mcr p15, 0, r0, c1, c0, 0
/*
* before relocating, we have to setup RAM timing
* because memory timing is board-dependend, you will
* find a lowlevel_init.S in your board directory.
*/
mov ip, lr
#if defined(CONFIG_AT91RM9200DK) || defined(CONFIG_AT91RM9200EK) || defined(CONFIG_AT91RM9200DF)
#else
bl lowlevel_init @这个函数是重点。。在lowlevel_init.S中
#endif
mov lr, ip
mov pc, lr
#endif /* CONFIG_SKIP_LOWLEVEL_INIT */
lowlevel_init.S的内容:
_TEXT_BASE:
.word TEXT_BASE
.globl lowlevel_init
lowlevel_init: @很明显,是初始化SDRAM
/* memory control configuration */
/* make r0 relative the current location so that it */
/* reads SMRDATA out of FLASH rather than memory ! */
ldr r0, =SMRDATA
ldr r1, _TEXT_BASE
sub r0, r0, r1
ldr r1, =BWSCON /* Bus Width Status Controller */
add r2, r0, #13*4
0:
ldr r3, [r0], #4
str r3, [r1], #4
cmp r2, r0
bne 0b
/* everything is fine now */
mov pc, lr
.ltorg
2.进入uboot启动的第二阶段:start_armboot()
点击(此处)折叠或打开
start_armboot()在board.c中,处于lib_arm/下,在进入lib_arm目录下编译LIBS目标时依赖board.o文件
void start_armboot (void)
{
init_fnc_t **init_fnc_ptr;
char *s;
#if !defined(CFG_NO_FLASH) || defined (CONFIG_VFD) || defined(CONFIG_LCD)
ulong size;
#endif
#if defined(CONFIG_VFD) || defined(CONFIG_LCD)
unsigned long addr;
#endif
//gd是个寄存器变量,ARM平台对应R8寄存器
/* Pointer is writable since we allocated a register for it
*_armboot_start是代码的起始地址,链接脚本指定为0x33f80000,CFG_MALLOC_LEN=192k,
*所以gd的起始地址=0x33F50000
*/
gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));
/* compiler optimization barrier needed for GCC >= 3.4 */
__asm__ __volatile__("": : :"memory");
memset ((void*)gd, 0, sizeof (gd_t));//清空gd_t结构体
gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));//bd_t结构体
memset (gd->bd, 0, sizeof (bd_t));
gd->flags |= GD_FLG_RELOC;
monitor_flash_len = _bss_start - _armboot_start;//uboot代码的长度
//对结构体init_fnc_t *init_sequence[]数组中的函数指针挨个调用。
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
if ((*init_fnc_ptr)() != 0) {
hang ();
}
}
/* 初始化flash*/
size = flash_init ();
。。。。。。
/* 初始化一个堆空间 */
mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);
nand_init(); /* go init the NAND */
/* 初始化环境变量,将环境参数读入内存指定位置 */
env_relocate ();
/* must do this after the framebuffer is allocated,串口初始化 */
serial_initialize();
/* IP Address */
gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
/* MAC Address,省略 */
devices_init (); /* get the devices list going. */
jumptable_init (); /*跳转表初始化 */
console_init_r (); /* fully init console as a device */
/* 使能异常中断 */
enable_interrupts ();
/* Perform network card initialisation if necessary */
#ifdef CONFIG_DRIVER_CS8900
cs8900_get_enetaddr (gd->bd->bi_enetaddr);
#endif
/* main_loop() can return to retry autoboot, if so just run it again. */
for (;;) {
main_loop ();
}
/* NOTREACHED - no way out of command loop except booting */
}
init_fnc_t *init_sequence[] = {
cpu_init, /* 基本的处理器相关配置,初始化IRQ/FIQ模式的栈 -- cpu/arm920t/cpu.c */
board_init, /* 基本的板级相关配置,设置系统时钟等 -- board/smdk2410/smdk2410.c */
interrupt_init, /* 初始化定时器处理 -- cpu/arm920t/s3c24x0/interrupt.c */
env_init, /* 初始化环境变量,检查Flash上的环境参数是否有效 -- common/env_flash.c */
init_baudrate, /* 初始化波特率设置 -- lib_arm/board.c */
serial_init, /* 串口通讯设置 -- cpu/arm920t/s3c24x0/serial.c */
console_init_f, /* 控制台初始化阶段1 -- common/console.c */
display_banner, /* 打印u-boot信息 -- lib_arm/board.c */
dram_init, /* 配置可用的RAM,检测系统内存映射 -- board/smdk2410/smdk2410.c */
display_dram_config, /* 显示RAM的配置大小 -- lib_arm/board.c */
NULL,
};
下面逐个分析初始化函数:
1.int cpu_init (void)
{
/* setup up stacks if necessary */
#ifdef CONFIG_USE_IRQ
IRQ_STACK_START = _armboot_start - CFG_MALLOC_LEN - CFG_GBL_DATA_SIZE - 4;
FIQ_STACK_START = IRQ_STACK_START - CONFIG_STACKSIZE_IRQ;
#endif
return 0;
}
2.int board_init (void)//设置系统时钟,配置引脚,使能缓存
{
S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
/* to reduce PLL lock time, adjust the LOCKTIME register */
clk_power->LOCKTIME = 0xFFFFFF;
/* configure MPLL */
clk_power->MPLLCON = ((M_MDIV << 12) + (M_PDIV << 4) + M_SDIV);
......
/* configure UPLL */
clk_power->UPLLCON = ((U_M_MDIV << 12) + (U_M_PDIV << 4) + U_M_SDIV);
......
/* set up the I/O ports */
gpio->GPACON = 0x007FFFFF;
gpio->GPBCON = 0x00044555;
gpio->GPBUP = 0x000007FF;
gpio->GPCCON = 0xAAAAAAAA;
gpio->GPCUP = 0x0000FFFF;
gpio->GPDCON = 0xAAAAAAAA;
gpio->GPDUP = 0x0000FFFF;
gpio->GPECON = 0xAAAAAAAA;
gpio->GPEUP = 0x0000FFFF;
gpio->GPFCON = 0x000055AA;
gpio->GPFUP = 0x000000FF;
gpio->GPGCON = 0xFF95FFBA;
gpio->GPGUP = 0x0000FFFF;
gpio->GPHCON = 0x002AFAAA;
gpio->GPHUP = 0x000007FF;
/* arch number of SMDK2410-Board */
gd->bd->bi_arch_number = MACH_TYPE_SMDK2410;
/* address of boot parameters */
gd->bd->bi_boot_params = 0x30000100;//为什么是这个地址
icache_enable();//下面这两个函数都是设置协处理器的相关寄存器,使能缓存
dcache_enable();
}
3.int interrupt_init (void)//初始化2410的PWM timer 4,使其能自动装载计数值,
//恒定的产生时间中断信号,但是中断被屏蔽了用不上。
{
S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
/* use PWM Timer 4 because it has no output */
/* prescaler for Timer 4 is 16 */
timers->TCFG0 = 0x0f00;
if (timer_load_val == 0)
{
/*
* for 10 ms clock period @ PCLK with 4 bit divider = 1/2
* (default) and prescaler = 16. Should be 10390
* @33.25MHz and 15625 @ 50 MHz
*/
timer_load_val = get_PCLK()/(2 * 16 * 100);
}
/* load value for 10 ms timeout */
lastdec = timers->TCNTB4 = timer_load_val;
/* auto load, manual update of Timer 4 */
timers->TCON = (timers->TCON & ~0x0700000) | 0x600000;
/* auto load, start Timer 4 */
timers->TCON = (timers->TCON & ~0x0700000) | 0x500000;
timestamp = 0;
return (0);
}
4.int env_init(void)
{
int crc1_ok = 0, crc2_ok = 0;
uchar flag1 = flash_addr->flags;
uchar flag2 = flash_addr_new->flags;
ulong addr_default = (ulong)&default_environment[0];
ulong addr1 = (ulong)&(flash_addr->data);
ulong addr2 = (ulong)&(flash_addr_new->data);
crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
if (crc1_ok && ! crc2_ok) {
gd->env_addr = addr1;
gd->env_valid = 1;
} else if (! crc1_ok && crc2_ok) {
gd->env_addr = addr2;
gd->env_valid = 1;
} else if (! crc1_ok && ! crc2_ok) {
gd->env_addr = addr_default;
gd->env_valid = 0;
} else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
gd->env_addr = addr1;
gd->env_valid = 1;
} else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
gd->env_addr = addr2;
gd->env_valid = 1;
} else if (flag1 == flag2) {
gd->env_addr = addr1;
gd->env_valid = 2;
} else if (flag1 == 0xFF) {
gd->env_addr = addr1;
gd->env_valid = 2;
} else if (flag2 == 0xFF) {
gd->env_addr = addr2;
gd->env_valid = 2;
}
return (0);
}
PS:
uboot的重要的数据结构
1)gd 全局数据变量指针,它保存了u-boot运行需要的全局数据,类型定义:
typedef struct global_data {
bd_t *bd; //board data pointor板子数据指针
unsigned long flags; //指示标志,如设备已经初始化标志等。
unsigned long baudrate; //串口波特率
unsigned long have_console; /* 串口初始化标志*/
unsigned long reloc_off; /* 重定位偏移,就是实际定向的位置与编译连接时指定的位置之差,一般为0 */
unsigned long env_addr; /* 环境参数地址*/
unsigned long env_valid; /* 环境参数CRC检验有效标志 */
unsigned long fb_base; /* base address of frame buffer */
#ifdef CONFIG_VFD
unsigned char vfd_type; /* display type */
#endif
void **jt; /* 跳转表,1.1.6中用来函数调用地址登记 */
} gd_t;
2)bd 板子数据指针。板子很多重要的参数。 类型定义如下:
typedef struct bd_info {
int bi_baudrate; /* 串口波特率 */
unsigned long bi_ip_addr; /* IP 地址 */
unsigned char bi_enetaddr[6]; /* MAC地址*/
struct environment_s *bi_env;
ulong bi_arch_number; /* unique id for this board */
ulong bi_boot_params; /* 启动参数 */
struct /* RAM 配置 */
{
ulong start;
ulong size;
}bi_dram[CONFIG_NR_DRAM_BANKS];
} bd_t;
3)环境变量指针 env_t *env_ptr = (env_t *)(&environment[0]);(common/env_flash.c)
env_ptr指向环境参数区,系统启动时默认的环境参数environment[],定义在common/environment.c中。
参数解释:
bootdelay 定义执行自动启动的等候秒数
baudrate 定义串口控制台的波特率
netmask 定义以太网接口的掩码
ethaddr 定义以太网接口的MAC地址
bootfile 定义缺省的下载文件
bootargs 定义传递给Linux内核的命令行参数
bootcmd 定义自动启动时执行的几条命令
serverip 定义tftp服务器端的IP地址
ipaddr 定义本地的IP地址
stdin 定义标准输入设备,一般是串口
stdout 定义标准输出设备,一般是串口
stderr 定义标准出错信息输出设备,一般是串口
4)设备相关:
标准IO设备数组?evice_t *stdio_devices[] = { NULL, NULL, NULL };
设备列表 list_t devlist = 0;
device_t的定义:include\devices.h中:
typedef struct {
int flags; /* Device flags: input/output/system */
int ext; /* Supported extensions */
char name[16]; /* Device name */
/* GENERAL functions */
int (*start) (void); /* To start the device */
int (*stop) (void); /* To stop the device */
/* 输出函数 */
void (*putc) (const char c); /* To put a char */
void (*puts) (const char *s); /* To put a string (accelerator) */
/* 输入函数 */
int (*tstc) (void); /* To test if a char is ready... */
int (*getc) (void); /* To get that char */
/* Other functions */
void *priv; /* Private extensions */
} device_t;
u-boot把可以用为控制台输入输出的设备添加到设备列表devlist,并把当前用作标准IO的设备指针加入stdio_devices数组中。
在调用标准IO函数如printf()时将调用stdio_devices数组对应设备的IO函数如putc()。
5)命令相关的数据结构,后面介绍。
6)与具体设备有关的数据结构,
如flash_info_t flash_info[CFG_MAX_FLASH_BANKS];记录nor flash的信息。
nand_info_t nand_info[CFG_MAX_NAND_DEVICE]; nand flash块设备信息
5.static int init_baudrate (void)
{
char tmp[64]; /* long enough for environment variables */
int i = getenv_r ("baudrate", tmp, sizeof (tmp));//环境变量存于tmp缓冲中
gd->bd->bi_baudrate = gd->baudrate = (i > 0)
? (int) simple_strtoul (tmp, NULL, 10)
: CONFIG_BAUDRATE;
return (0);
}
6.int serial_init()实际调用下面这个函数,在移植时这个函数是第一个要修改的,因为要用到串口输出信息
static int serial_init_dev(const int dev_index)
{
S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
/* FIFO enable, Tx/Rx FIFO clear */
uart->UFCON = 0x07; //这里使用的是串口0.
uart->UMCON = 0x0;
/* Normal,No parity,1 stop,8 bit */
uart->ULCON = 0x3;
/*
* tx=level,rx=edge,disable timeout int.,enable rx error int.,
* normal,interrupt or polling
*/
uart->UCON = 0x245;
#ifdef CONFIG_HWFLOW
uart->UMCON = 0x1; /* RTS up */
#endif
/* FIXME: This is sooooooooooooooooooo ugly */
#if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
/* we need auto hw flow control on the gsm and gps port */
if (dev_index == 0 || dev_index == 1)
uart->UMCON = 0x10;
#endif
_serial_setbrg(dev_index);
return (0);
}
7.由于标准设备还没有初始化(gd->flags & GD_FLG_DEVINIT=0),这时控制台使用串口作为控制台
函数只有一句:gd->have_console = 1;
int console_init_f (void)
{
gd->have_console = 1; //将标志位置1
#ifdef CONFIG_SILENT_CONSOLE
if (getenv("silent") != NULL)
gd->flags |= GD_FLG_SILENT;
#endif
return (0);
}
8.int dram_init (void)
{//PHYS_SDRAM_1在整个include/configs.h文件中有很多定义,只在一个地方define为0x30000000
//PHYS_SDRAM_1_SIZE定义为64MB
gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
return 0;
}
9. ulong flash_init (void)
这里smdk2410的默认flash是AMD的,mini2440的板子是SST39F1601,如果NOR Flash符合CFI接口标准,可以使用drivers/cfi_flash.c
里面的接口函数,否则就得重写。如果使用cfi_flash.c,需要修改include/configs/$(board_name).h,在里面增加:
#define CFG_FLASH_CFI_DRIVER 1
在board/$(board_name)/Makefile中去掉flash.o
在《嵌入式linux完全手册》上P273页有说明。
首先是有一个变量flash_info_t flash_info[CFG_MAX_FLASH_BANKS]来记录flash的信息。flash_info_t定义:
typedef struct {
ulong size; /* 总大小BYTE */
ushort sector_count; /* 总的sector数*/
ulong flash_id; /* combined device & manufacturer code */
ulong start[CFG_MAX_FLASH_SECT]; /* 每个sector的起始物理地址。 */
uchar protect[CFG_MAX_FLASH_SECT]; /* 每个sector的保护状态,如果置1,在执行erase操作的时候将跳过对应sector*/
#ifdef CFG_FLASH_CFI //我不管CFI接口。
.....
#endif
} flash_info_t;
flash_init()的操作就是读取ID号,ID号指明了生产商和设备号,根据这些信息设置size,sector_count,flash_id.
以及start[]、protect[]。
10.mem_malloc_init()
11.nand_init()
12.env_relocate()
13.devices_init (); /* get the devices list going. */定义于common/devices.c
14.jumptable_init ()
15.console_init_r (); /* fully init console as a device ,前面结构体中的是前期控制台初始化,这是后期*/
主要过程:查看环境参数stdin,stdout,stderr中对标准IO的指定的设备名称,再按照环境指定的名称搜索devlist,将搜到的设备指针赋给标准IO数组stdio_devices[]。置gd->flag标志GD_FLG_DEVINIT。这个标志影响putc,getc函数的实现,未定义此标志时直接由串口serial_getc和serial_putc实现,定义以后通过标准设备数组stdio_devices[]中的putc和getc来实现IO。
下面是相关代码:
void putc (const char c)
{
#ifdef CONFIG_SILENT_CONSOLE
if (gd->flags & GD_FLG_SILENT)//GD_FLG_SILENT无输出标志
return;
#endif
if (gd->flags & GD_FLG_DEVINIT) {//设备list已经初始化
/* Send to the standard output */
fputc (stdout, c);
} else {
/* Send directly to the handler */
serial_putc (c);//未初始化时直接从串口输出。
}
}
void fputc (int file, const char c)
{
if (file < MAX_FILES)
stdio_devices[file]->putc (c);
}
为什么要使用devlist,std_device[]?
为了更灵活地实现标准IO重定向,任何可以作为标准IO的设备,如USB键盘,LCD屏,串口等都可以对应一个device_t的结构体变量,只需要实现getc和putc等函数,就能加入到devlist列表中去,也就可以被assign为标准IO设备std_device中去。如函数
int console_assign (int file, char *devname); /* Assign the console 重定向标准输入输出*/
这个函数功能就是把名为devname的设备重定向为标准IO文件file(stdin,stdout,stderr)。其执行过程是在devlist中查找devname的设备,返回这个设备的device_t指针,并把指针值赋给std_device[file]。
16.enable_interrupts ();
(1)首先,需要设置系统时钟、初始化串口,只要这两个设置好,就能从串口看到打印信息。
board_init函数设置MPLL,改变系统时钟,这是一个开发板相关的函数。
serial_init函数用来初始化串口,设置UART控制器,是CPU相关的函数,
(2)检测系统内存映射
对于特定的开发板,内存分布是比较明确的,dram_init函数指定了目标开发板的内存起始地址为0x30000000,大小为64M
(3)U-boot命令的格式
uboot中的每个命令都是通过U_BOOT_CMD宏来定义,格式:U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help")
这个宏定义在include/command.h中,
(4)为内核设置启动参数
uboot是通过标记列表向内核传递参数,设置内存标记、命令标记的函数setup_memory_tags、setup_commandline_tag,
在lib_arm/armlinux.c中定义实现。如果要定义一个命令,需要在include/configs/$(board_name).h中增加一个关于
命令的配置项:
如: #define CONFIG_CMDLINE_TAG 1
对于arm架构的CPU,都是通过lib_arm/armlinux.c中的do_bootm_linux函数来启动内核,在这个函数中,设置标记列表,最后通过
the_kernel(0,bd->bi_arch_number,bd->bi_boot_params)调用内核。
PS:
1.分析过程参照了一位网友的文章,很有参考价值,给我很大帮助。我只是按照配置->编译->启动过程
的这么一个认识过程来分析,可能会显得比较混乱。