s3c2440启动BL1代码

//start.S
.text
.global _start
_start:
    b reset
    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

undefined_instruction:
    nop
software_interrupt:
    nop
prefetch_abort:
    nop
data_abort:
    nop
not_used:
    nop
irq:
    nop
fiq:
    nop

reset:
    bl set_svc
    bl disable_watchdog
    bl disable_interrupt
    bl disable_mmu  
    bl clock_init   
    bl init_sdram   
    bl copy_to_ram
    bl init_stack
    bl clean_bss
    ldr pc,=gboot_main   @ go to C function
    @bl light_led



set_svc:
    mrs r0,cpsr
    bic r0,r0,#0x1f
    orr r0,r0,#0xd3
    msr cpsr,r0

    mov pc,lr

#define pWTCON 0x53000000
disable_watchdog:
    ldr r0,=pWTCON
    mov r1,#0x00
    str r1,[r0]

    mov pc,lr

disable_interrupt:
@ set cpsr I&F
    mrs r0,cpsr
    orr r0,r0,#0xc0
    msr cpsr,r0

    mov pc,lr

@ set MASK
    mvn r1,#0x00
    ldr r0,=0x4a000008
    str r1,[r0]

    mov pc,lr

disable_mmu:
    @ disable I/D cache
    mcr p15,0,r0,c7,c7,0

    @ read p15 to r0
    mrc p15,0,r0,c1,c0,0
    @ modify r0
    bic r0,r0,#0x00000007
    @ write r0 to p15
    mcr p15,0,r0,c1,c0,0 

    mov pc,lr

#define CLKDIVN 0x4c000014
#define MPLLCON 0x4c000004   
#define MPLL_405MHz ((127<<12)|(2<<4)|(1<<0))
clock_init:
    @ set FCLK:HCLK:PCLK = 1:4:8
    ldr r0,=CLKDIVN
    mov r1,#0x5
    str r1,[r0]

    @MMU_setAsyncBusMode
    mrc p15,0,r0,c1,c0,0
    orr r0,r0,#0xc0000000
    mcr p15,0,r0,c1,c0,0

    @ set FCLK = 405MHz
    ldr r0,=MPLLCON
    ldr r1,=MPLL_405MHz
    str r1,[r0]
    mov pc,lr

#define mem_contrl 0x48000000
init_sdram:
    ldr r0,=mem_contrl
    add r3,r0,#4*13
    adrl r1,mem_data

@ use loop to realize   
0:
    ldr r2,[r1],#4
    str r2,[r0],#4
    cmp r0,r3
    bne 0b           @ if r0 != r3 ,jump to before

    mov pc,lr

mem_data:
    .long 0x22000000   @BWSCON
    .long 0x00000700   @BANKCON0
    .long 0x00000700   @BANKCON1
    .long 0x00000700   @BANKCON2
    .long 0x00000700   @BANKCON3
    .long 0x00000700   @BANKCON4
    .long 0x00000700   @BANKCON5
    .long 0x00018001   @BANKCON6
    .long 0x00018001   @BANKCON7
    .long 0x008c04f5   @REFRESH
    .long 0x000000b1   @BANKSIZE
    .long 0x00000030   @MRSRB6
    .long 0x00000030   @MRSRB7


#define GPBCON 0x56000010
#define GPBDAT 0x56000014

light_led:
    ldr r0,=GPBCON
    ldr r1,=0x15400
    str r1,[r0]

    ldr r0,=GPBDAT
    ldr r1,=0x6bf
    str r1,[r0] 

    mov pc,lr

copy_to_ram:
    ldr r0,=0x00
    ldr r1,=0x30008000
    add r3,r0,#4*1024

copy_loop:
    ldr r2,[r0],#4
    str r2,[r1],#4
    cmp r0,r3
    bne copy_loop

    mov pc,lr

@ 0x30000000 + 64MB(0x4000000)
init_stack:
    ldr sp,=0x34000000
    mov pc,lr

clean_bss:
    ldr r0,=bss_start
    ldr r1,=bss_end
    cmp r0,r1
    moveq pc,lr          @ r0 == r1 ,back 
clean_loop:
    mov r2,#0
    str r2,[r0],#4
    cmp r0,r1
    bne clean_loop       @ r0 != r1 ,loop
    mov pc,lr
//main.c
int gboot_main()
{
    ...
    return 0;
}
//gboot.lds

OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
    . = 0x30008000;
    . = ALIGN(4);
    .text :
    {
        start.o(.text)
        *(.text)
    }
    . = ALIGN(4);
    .data :
    {
        *(data)
    }

    . = ALIGN(4);
    bss_start = .;
    .bss :
    {
        *(.bss)
    }
    bss_end = .;

}

//Makefile

all: start.o main.o
    arm-linux-ld -T gboot.lds -o gboot.elf $^
    arm-linux-objcopy -O binary gboot.elf gboot.bin

%.o: %.S
    arm-linux-gcc -nostdlib -g -c $^
%.o: %.c
    arm-linux-gcc -nostdlib -g -c $^


.PHONY: clean
clean:
    rm *.o *.elf *.bin

在Makefile文件中,如果没有 -nostdlib ,则会出现:

[root@localhost example]# make
arm-linux-gcc -g -c start.S
arm-linux-gcc -g -c main.c
arm-linux-ld -Tgboot.lds -o gboot.elf start.o main.o
main.o:(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'
make: *** [all] 错误 1

问题解决:arm-none-linux-gnueabi-gcc加上-nostdlib选项即可
机理
-nostdlib
不连接系统标准启动文件和标准库文件,只把指定的文件传递给连接器。
这个选项常用于编译内核、bootloader等程序,它们不需要启动文件、标准库文件。

C语言程序执行的第一条指令。并不是main函数。生产一个C程序的可执行文件时编译器通常会在我们的代码上加几个被称为启动文件的代码–crt1.o、crti.o、
crtend.o等,他们是标准库文件。这些代码设置C程序的堆栈等,然后调用main函数。他么依赖于操作系统,在裸板上无法执行,所以我们自己写一个。

问题已经找到答案了,我们自己写的crt0.S就是一个启动文件,他设置好堆栈后调用main函数。因此,我们不需要系统自带的启动文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值