vivi bootloader解析
vivi是韩国mizi公司设计的一款主要针s3c2410平台的bootloader,其特点是体积小,功能强大,运行效率高和使用方便。
vivi代码虽然比较小巧,但麻雀虽小,五脏俱全,用来学习bootloader还是不错的。代码在http://download.csdn.net/detail/yu4700/4388601可以下载到。
vivi源代码结构
包括arch/drivers/include/init/lib/scripts/util等几个目录
|-arch ------------------- 此目录主要包含所有vivi支持的目标板的子目录
| |- s3c2410 ------------- s3c2410的支持代码
| |- def-configs --------- 硬件平台配置文件
|-drivers ----------------- 包括了引导内核需要的设备的驱动程序(MTD和串口)
| |- mtd ----------------- MTD设备驱动程序
| |- serial -------------- 串口设备驱动程序
|-init -------------------- main.c文件和version.c文件
|-include ----------------- 头文件目录
|-lib --------------------- 公共接口代码
|-util -------------------- 工具的代码
|-scripts ----------------- 脚本文件
vivi启动过程分析
vivi启动分成两个过程,stage1和stage2。
stage1代码是在head.S文件中,这个文件使用汇编语言编写。
在地址0x0的地方存放着如下代码:
@ 0x00: Reset
b Reset
这句会跳转到Reset的地方运行,顺序如下:
1. 关闭watchdog
@ disable watch dog timer
mov r1, #0x53000000
mov r2, #0x0
str r2, [r1]
2. 禁用中断
@ disable all interrupts
mov r1, #INT_CTL_BASE
mov r2, #0xffffffff
str r2, [r1, #oINTMSK]
ldr r2, =0x7ff
str r2, [r1, #oINTSUBMSK]
3. 初始化系统时钟
@ initialise system clocks
mov r1, #CLK_CTL_BASE
mvn r2, #0xff000000
str r2, [r1, #oLOCKTIME]
@ldr r2, mpll_50mhz
@str r2, [r1, #oMPLLCON]
#ifndef CONFIG_S3C2410_MPORT1
@ 1:2:4
mov r1, #CLK_CTL_BASE
mov r2, #0x3
str r2, [r1, #oCLKDIVN]
mrc p15, 0, r1, c1, c0, 0
@ read ctrl register
orr r1, r1, #0xc0000000
@ Asynchronous
mcr p15, 0, r1, c1, c0, 0
@ write ctrl register
@ now, CPU clock is 200 Mhz
mov r1, #CLK_CTL_BASE
ldr r2, mpll_200mhz
str r2, [r1, #oMPLLCON]
#else
@ 1:2:2
mov r1, #CLK_CTL_BASE
ldr r2, clock_clkdivn
str r2, [r1, #oCLKDIVN]
mrc p15, 0, r1, c1, c0, 0 @ read ctrl register
orr r1, r1, #0xc0000000 @ Asynchronous
mcr p15, 0, r1, c1, c0, 0 @ write ctrl register
@ now, CPU clock is 100 Mhz
mov r1, #CLK_CTL_BASE
ldr r2, mpll_100mhz
str r2, [r1, #oMPLLCON]
#endif
bl memsetup
请注意,最后一句是跳转语句,跳转到内存初始化的地方,同时把下一句的地址保存到R14,当内存初始化结束后就可以用mov
pc, lr这句来返回。
4. 内存控制器配置
ENTRY(memsetup)
@ initialise the static memory
@ set memory control registers
mov r1, #MEM_CTL_BASE
adrl r2, mem_cfg_val
add r3, r1, #52
1: ldr r4, [r2], #4
str r4, [r1], #4
cmp r1, r3
bne 1b
mov pc, lr
5. 打开LED
@ All LED on
mov r1, #GPIO_CTL_BASE
add r1, r1, #oGPIO_F
ldr r2,=0x55aa
str r2, [r1, #oGPIO_CON]
mov r2, #0xff
str r2, [r1, #oGPIO_UP]
mov r2, #0x00
str r2, [r1, #oGPIO_DAT]
6. UART初始化
@ set GPIO for UART
mov r1, #GPIO_CTL_BASE
add r1, r1, #oGPIO_H
ldr r2, gpio_con_uart
str r2, [r1, #oGPIO_CON]
ldr r2, gpio_up_uart
str r2, [r1, #oGPIO_UP]
bl InitUART -------------- 同样是跳转到InitUART
初始化UART代码如下:
@ Initialize UART
@
@ r0 = number of UART port
InitUART:
ldr r1, SerBase
mov r2, #0x0
str r2, [r1, #oUFCON]
str r2, [r1, #oUMCON]
mov r2, #0x3
str r2, [r1, #oULCON]
ldr r2, =0x245
str r2, [r1, #oUCON]
#define UART_BRD ((50000000 / (UART_BAUD_RATE * 16)) - 1)
mov r2, #UART_BRD
str r2, [r1, #oUBRDIV]
mov r3, #100
mov r2, #0x0
1: sub r3, r3, #0x1
tst r2, r3
bne 1b
mov pc, lr
7. 加载stage2的代码
在copy之前会有内存检测
@
@ copy_myself: copy vivi to ram
@
copy_myself:
mov r10, lr
@ reset NAND
mov r1, #NAND_CTL_BASE
ldr r2, =0xf830
@ initial value
str r2, [r1, #oNFCONF]
ldr r2, [r1, #oNFCONF]
bic r2, r2, #0x800
@ enable chip
str r2, [r1, #oNFCONF]
mov r2, #0xff
@ RESET command
strb r2, [r1, #oNFCMD]
mov r3, #0
@ wait
1: add r3, r3, #0x1
cmp r3, #0xa
blt 1b
2: ldr r2, [r1, #oNFSTAT] @ wait ready
tst r2, #0x1
beq 2b
ldr r2, [r1, #oNFCONF]
orr r2, r2, #0x800
@ disable chip
str r2, [r1, #oNFCONF]
@ get read to call C functions (for nand_read())
ldr sp, DW_STACK_START
@ setup stack pointer
mov fp, #0
@ no previous frame, so fp=0
@ copy vivi to RAM
ldr r0, =VIVI_RAM_BASE
mov r1, #0x0
mov r2, #0x20000
bl nand_read_ll
tst r0, #0x0
beq ok_nand_read
ok_nand_read:
@ verify
mov r0, #0
ldr r1, =0x33f00000
mov r2, #0x400
@ 4 bytes * 1024 = 4K-bytes
go_next:
ldr r3, [r0], #4
ldr r4, [r1], #4
teq r3, r4
bne notmatch
subs r2, r2, #4
beq done_nand_read
bne go_next
notmatch:
#ifdef CONFIG_DEBUG_LL
sub r0, r0, #4
ldr r1, SerBase
bl PrintHexWord
ldr r0, STR_FAIL
ldr r1, SerBase
补充:综合编程 , 其他综合 ,