1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
stage2.lds
ENTRY(start)
OUTPUT_FORMAT(elf32-littlearm)
OUTPUT_ARCH(arm)
STARTUP(crt0.o)

MEMORY
{
    DRAM  : ORIGIN = 0x60000000, LENGTH = 0x01000000
    IRAM  : ORIGIN = 0x00000000, LENGTH = 0x00002000
}

SECTIONS
{
    .text : {
      *(.init.text)
      _relocstart = .;
      *(.text*)
      *(.glue_7*)
      _relocend = .;
    } > IRAM AT > DRAM

    .stack 0x60800000 (NOLOAD) : {
      *(.stack)
      stackbegin = .;
      . += 0x200;
      stackend = .;
      irqstackbegin = .;
      . += 0x200;
      irqstackend = .;
    } > DRAM
}


crt0.S

//
// startup code
//
//

#define PSR_MODE  0x0000001f
#define PSR_USR_MODE  0x00000010
#define PSR_IRQ_MODE  0x00000012
#define PSR_SVC_MODE  0x00000013

#define PSR_INT_MASK  0x000000c0
#define PSR_FIQ_DIS  0x00000040
#define PSR_IRQ_DIS  0x00000080

.section .init.text,"ax",%progbits
.global start
.extern _interrupt_disable

// -----------------------------------------------------
//  startup code (setup stacks, branch to main)
// -----------------------------------------------------
start:
  // setup IRQ stack
  mov    r0, #(PSR_IRQ_MODE|PSR_FIQ_DIS|PSR_IRQ_DIS)
  msr    cpsr, r0
  ldr    sp,=irqstackend

  // setup SVC stack
  mov    r0, #(PSR_SVC_MODE|PSR_FIQ_DIS|PSR_IRQ_DIS)
  msr    cpsr, r0
  ldr    sp,=stackend

  // disbale interrupts
  bl    _interrupt_disable

        // turn on power
        mov             r0, #0x18000000
        add             r0, r0, #0x1C000

        // remap
        ldr             r1,=0xdeadbeef
        str             r1, [r0, #4]

        // relocate itself
        ldr    r0,=_relocstart
        ldr    r1,=_relocend
  ldr    r2,=0x0
1:
  cmp    r1,r0
        ldrhi    r3,[r0],#4
  strhi    r3,[r2],#4
  bhi    1b
  
  // continue running in SVC (supervisor mode)
  b    main

Makefile
TARGET  = stage2

TOOLCHAIN  = arm-elf-eabi-

CC    = $(TOOLCHAIN)gcc
CPP    = $(TOOLCHAIN)cpp
LD    = $(TOOLCHAIN)gcc
AS    = $(TOOLCHAIN)as
OBJCOPY  = $(TOOLCHAIN)objcopy
OBJDUMP  = $(TOOLCHAIN)objdump

CFLAGS  = -Wundef -marm -mcpu=arm7tdmi -nostdlib -mfpu=fpa -O0 -c
#ASFLAGS  = -mcpu=arm926ej-s

OBJS  = crt0.o main.o
LDSCRIPT= stage2.lds

#LIBDIRS  = -L../arm/lib/gcc/arm-elf/4.1.0/ -L../lib
#LIBS  = -lgcc
LIBS    =
LDFLAGS  = -Wundef -marm -mcpu=arm7tdmi -T$(LDSCRIPT) -nostartfiles  \
  -mfpu=fpa -nostdlib -Xlinker -Map=$(TARGET).map

all    : $(TARGET).bin
    ls -ls $(TARGET).bin

%.o    : %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) $(INCDIRS) $< -o $@

%.o    : %.S
    $(CC) $(CFLAGS) -c $< -o $@

$(TARGET).elf : $(OBJS)
    $(LD) $(LDFLAGS) $(OBJS) $(LIBDIRS) $(LIBS) -o $(TARGET).elf

$(TARGET).bin : $(TARGET).elf
    $(OBJCOPY) -O binary $(TARGET).elf $(TARGET).bin

dasm  : $(TARGET).bin
    $(OBJDUMP) -m arm -D $(TARGET).elf | cat > $(TARGET).asm

clean  :
    rm -f $(OBJS)
    rm -f $(TARGET).elf
    rm -f $(TARGET).bin
    rm -f $(TARGET).asm
    rm -f $(TARGET).map

error
wodz@wodz-r61:~/Datasheets/rockchip/rk27load/stage2wo$ make
arm-elf-eabi-gcc -Wundef -marm -mcpu=arm7tdmi -nostdlib -mfpu=fpa -O0 -c -c crt0.S -o crt0.o
arm-elf-eabi-gcc -Wundef -marm -mcpu=arm7tdmi -nostdlib -mfpu=fpa -O0 -c -c main.S -o main.o
arm-elf-eabi-gcc -Wundef -marm -mcpu=arm7tdmi -Tstage2.lds -nostartfiles -mfpu=fpa -nostdlib -Xlinker -Map=stage2.map crt0.o main.o   -o stage2.elf
crt0.o: In function `start':
(.init.text+0x0): multiple definition of `start'
crt0.o:(.init.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [stage2.elf] Błąd 1