blob: cd2bab6cde7fb4e9c19cedc611122b91267d2aab [file] [log] [blame]
Albert ARIBAUD3da0e572013-05-19 01:48:15 +00001/*
2 * relocate - common relocation function for ARM U-Boot
3 *
4 * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <linux/linkage.h>
26
27/*
28 * void relocate_code(addr_moni)
29 *
30 * This function relocates the monitor code.
31 *
32 * NOTE:
33 * To prevent the code below from containing references with an R_ARM_ABS32
34 * relocation record type, we never refer to linker-defined symbols directly.
35 * Instead, we declare literals which contain their relative location with
36 * respect to relocate_code, and at run time, add relocate_code back to them.
37 */
38
39ENTRY(relocate_code)
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020040 ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
41 subs r9, r0, r1 /* r9 <- relocation offset */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000042 beq relocate_done /* skip relocation */
Albert ARIBAUDd026dec2013-06-11 14:17:33 +020043 ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000044
45copy_loop:
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020046 ldmia r1!, {r10-r11} /* copy from source address [r1] */
47 stmia r0!, {r10-r11} /* copy to target address [r0] */
48 cmp r1, r2 /* until source end address [r2] */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000049 blo copy_loop
50
51 /*
52 * fix .rel.dyn relocations
53 */
Albert ARIBAUD47bd65e2013-06-11 14:17:34 +020054 ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
55 ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000056fixloop:
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020057 ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
58 and r1, r1, #0xff
59 cmp r1, #23 /* relative fixup? */
60 bne fixnext
61
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000062 /* relative fix: increase location by offset */
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020063 add r0, r0, r9
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000064 ldr r1, [r0]
65 add r1, r1, r9
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000066 str r1, [r0]
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +020067fixnext:
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000068 cmp r2, r3
69 blo fixloop
70
71relocate_done:
72
Mike Dunn9dc8fef2013-06-21 09:12:28 -070073#ifdef __XSCALE__
74 /*
75 * On xscale, icache must be invalidated and write buffers drained,
76 * even with cache disabled - 4.2.7 of xscale core developer's manual
77 */
78 mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
79 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
80#endif
81
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000082 /* ARMv4- don't know bx lr but the assembler fails to see that */
83
84#ifdef __ARM_ARCH_4__
85 mov pc, lr
86#else
87 bx lr
88#endif
89
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000090ENDPROC(relocate_code)