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 <- current position of code */
- 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 <- size of armboot */
- add r2, r0, r2 /* r2 <- source end address */
- @ 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.分析过程参照了一位网友的文章,很有参考价值,给我很大帮助。我只是按照配置->编译->启动过程
的这么一个认识过程来分析,可能会显得比较混乱。
2.博客链接:
阅读(1158) | 评论(0) | 转发(0) |