| 本帖最后由 qyihan 于 2013-12-3 11:33 编辑 
 【最新u-boot-2013.10移植】uboot移植(二):启动代码分析之start.S和crt0.S
 start.S
 /*
 * armboot - Startup Code for ARM920 CPU-core
 *
 * Copyright (c) 2001 Marius Gr枚ger <mag@sysgo.de>
 * Copyright (c) 2002 Alex Z眉pke <azu@sysgo.de>
 * Copyright (c) 2002 Gary Jennejohn <garyj@denx.de>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */
 
 #include <asm-offsets.h>
 #include <common.h>
 #include <config.h>
 
 /*
 *************************************************************************
 *
 * Jump vector table as in table 3.1 in [1]
 *
 *************************************************************************
 */
 
 
 .globl _start
 _start: b 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
 _software_interrupt: .word software_interrupt
 _prefetch_abort: .word prefetch_abort
 _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
 *
 *************************************************************************
 */
 
 .globl _TEXT_BASE
 _TEXT_BASE:
 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_TEXT_BASE)
 .word CONFIG_SPL_TEXT_BASE
 #else
 .word CONFIG_SYS_TEXT_BASE
 #endif
 
 /*
 * These are defined in the board-specific linker script.
 * Subtracting _start from them lets the linker put their
 * relative position in the executable instead of leaving
 * them null.
 */
 .globl _bss_start_ofs
 _bss_start_ofs:
 .word __bss_start - _start
 
 .globl _bss_end_ofs
 _bss_end_ofs:
 .word __bss_end - _start
 
 .globl _end_ofs
 _end_ofs:
 .word _end - _start
 
 #ifdef CONFIG_USE_IRQ
 /* 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
 
 /* IRQ stack memory (calculated at run-time) + 8 bytes */
 .globl IRQ_STACK_START_IN
 IRQ_STACK_START_IN:
 .word 0x0badc0de
 
 /*
 * 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
 
 #if defined(CONFIG_AT91RM9200DK) || defined(CONFIG_AT91RM9200EK)
 /*
 * relocate exception table
 */
 /*atmel的芯片相关的,不需要管*/
 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
 
 #ifdef CONFIG_S3C24X0
 /* turn off the watchdog */
 
 # if defined(CONFIG_S3C2400)
 # define pWTCON 0x15300000
 # define INTMSK 0x14400008 /* Interrupt-Controller base addresses */
 # define CLKDIVN 0x14800014 /* clock divisor register */
 #else
 # define pWTCON 0x53000000    /*2410的看门狗寄存器地址,和2440一样*/
 # define INTMSK 0x4A000008     /* Interrupt-Controller base addresses */
 /*INTMSK
 中断屏蔽寄存器的地址,一共32位,每一位都对应一个中断源,0打开,1屏蔽
 */
 # define INTSUBMSK 0x4A00001C   /*INTSUBMSK 这个寄存器有11位,每一位对应一个中断源,作用和INTMSK一样*/
 # define CLKDIVN 0x4C000014   /* clock divisor register */
 # endif
 
 /*关开门狗*/
 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
 
 /* FCLK:HCLK:PCLK = 1:2:4 */           /*设置时钟比例*/
 /* default FCLK is 120 MHz ! */
 ldr r0, =CLKDIVN
 mov r1, #3
 str r1, [r0]
 #endif /* CONFIG_S3C24X0 */
 
 /*
 * we do sys-critical inits only at reboot,
 * not when booting from ram!
 */
 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
 bl cpu_init_crit    /*进去之后,会关闭MMU,然后再进入路径board\samsung\smdk2410\lowlevel_init.S中的lowlevel_init,
 
 #endif主要做初始化SDRAM的工作*/
 
 bl _main        /*bl  _mian实际上是跳到了arch\arm\lib\crt0.S下
 
 的ENTRY(_main)里,下面列出crt0.S的代码*/
 /*------------------------------------------------------------------------------*/
 
 .globl c_runtime_cpu_setup
 c_runtime_cpu_setup:
 
 mov pc, lr
 
 /*
 *************************************************************************
 *
 * CPU_init_critical registers
 *
 * setup important registers
 * setup memory timing
 *
 *************************************************************************
 */
 
 
 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
 cpu_init_crit:
 /*
 * 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
 
 bl lowlevel_init
 
 mov lr, ip
 mov pc, lr
 #endif /* CONFIG_SKIP_LOWLEVEL_INIT */
 
 /*
 ………………………………
 
 
 crt0.S
 
 
 /*
 * crt0 - C-runtime startup Code for ARM U-Boot
 *
 * Copyright (c) 2012 Albert ARIBAUD <albert.u.boot@aribaud.net>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */
 
 #include <config.h>
 #include <asm-offsets.h>
 #include <linux/linkage.h>
 
 /*
 * entry point of crt0 sequence
 */
 
 ENTRY(_main)
 
 /*
 * Set up initial C runtime environment and call board_init_f(0).
 */
 
 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
 ldr sp, =(CONFIG_SPL_STACK)
 #else
 ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)    /*通过反汇编代码知道sp = 30000f60*/
 #endif
 /*通过反汇编代码知道sp又减去了0x98,在sp上面,存放了一个gd结构体*/
 bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
 sub sp, #GD_SIZE /* allocate one GD above SP */
 bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
 mov r9, sp /* GD is above SP */
 mov r0, #0         /*给board_init_f传递参数,给c函数传递的参数放在r0,r1,r2寄存器里*/
 
 
 bl  board_init_f    /*调用c函数,arch/arm/lib/board.c下的board_init_f函数*/
 /*在board.c的开始处,有这样一个宏DECLARE_GLOBAL_DATA_PTR; 它是定义arch/arm/include/asm/global_data.h中:
 #define DECLARE_GLOBAL_DATA_PTR     register volatile gd_t *gd asm ("r8")
 定义一个寄存器变量,如果去读gd,实际上就是去读r8
 即 gd 的地址是存在 r8寄存器的. 此时gd空间已经准备好, 可以交给 board_init_f 去初始化了.
 */
 
 #if ! defined(CONFIG_SPL_BUILD)
 
 /*
 * Set up intermediate environment (new sp and gd) and call
 * relocate_code(addr_moni). Trick here is that we'll return
 * 'here' but relocated.
 */
 /*
 * 重新设置sp,指向addr_sp
 * 设置返回地址
 */
 
 ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
 bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
 ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
 sub r9, r9, #GD_SIZE /* new GD is below bd */
 
 adr lr, here
 ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
 add lr, lr, r0
 ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
 /*调用重定位代码arch\arm\lib\relocate_code.S,详细分析见uboot移植三*/
 b relocate_code
 here:
 
 /* Set up final (full) environment */
 /*下面的代码是清除bss段*/
 
 bl  c_runtime_cpu_setup /* we still call old routine here */
 
 ldr r0, =__bss_start /* this is auto-relocated! */
 ldr r1, =__bss_end /* this is auto-relocated! */
 
 mov r2, #0x00000000 /* prepare zero to clear BSS */
 
 clbss_l:cmp r0, r1 /* while not at end of BSS */
 strlo r2, [r0] /* clear 32-bit BSS word */
 addlo r0, r0, #4 /* move to next */
 blo clbss_l
 /*
 * 从代码中找不到coloured_LED_init和red_led_on的定义,
 * 从反汇编看这两条语句是同一条指令:e1a0f00e mov pc, lr
 * 也就是说这两条指令什么也没做,又返回了
 * 所以这两个函数,我们可以自己来定义,用来做调试用
 */
 
 bl coloured_LED_init
 bl red_led_on
 
 /* call board_init_r(gd_t *id, ulong dest_addr) */
 mov r0, r9 /* gd_t */
 ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
 /* call board_init_r */
 /*调用c函数,进入第二阶段*/
 ldr pc, =board_init_r /* this is auto-relocated! */
 
 /* we should not return here. */
 
 #endif
 
 ENDPROC(_main)
 
 
 
 
 
 
 |