blob: 803525156314e2b4b59a73d296965f773095041a [file] [log] [blame]
Albert ARIBAUD9580b322013-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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Albert ARIBAUD9580b322013-05-19 01:48:15 +00007 */
8
9#include <linux/linkage.h>
10
11/*
12 * void relocate_code(addr_moni)
13 *
14 * This function relocates the monitor code.
15 *
16 * NOTE:
17 * To prevent the code below from containing references with an R_ARM_ABS32
18 * relocation record type, we never refer to linker-defined symbols directly.
19 * Instead, we declare literals which contain their relative location with
20 * respect to relocate_code, and at run time, add relocate_code back to them.
21 */
22
23ENTRY(relocate_code)
Albert ARIBAUD1fc34f12013-06-11 14:17:35 +020024 ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
Jeroen Hofstee2deb1ba2013-09-21 14:04:40 +020025 subs r4, r0, r1 /* r4 <- relocation offset */
Albert ARIBAUD9580b322013-05-19 01:48:15 +000026 beq relocate_done /* skip relocation */
Albert ARIBAUDc53687e2013-06-11 14:17:33 +020027 ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */
Albert ARIBAUD9580b322013-05-19 01:48:15 +000028
29copy_loop:
Albert ARIBAUD1fc34f12013-06-11 14:17:35 +020030 ldmia r1!, {r10-r11} /* copy from source address [r1] */
31 stmia r0!, {r10-r11} /* copy to target address [r0] */
32 cmp r1, r2 /* until source end address [r2] */
Albert ARIBAUD9580b322013-05-19 01:48:15 +000033 blo copy_loop
34
35 /*
36 * fix .rel.dyn relocations
37 */
Albert ARIBAUDaf3ff162013-06-11 14:17:34 +020038 ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
39 ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
Albert ARIBAUD9580b322013-05-19 01:48:15 +000040fixloop:
Albert ARIBAUD1fc34f12013-06-11 14:17:35 +020041 ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
42 and r1, r1, #0xff
43 cmp r1, #23 /* relative fixup? */
44 bne fixnext
45
Albert ARIBAUD9580b322013-05-19 01:48:15 +000046 /* relative fix: increase location by offset */
Jeroen Hofstee2deb1ba2013-09-21 14:04:40 +020047 add r0, r0, r4
Albert ARIBAUD9580b322013-05-19 01:48:15 +000048 ldr r1, [r0]
Jeroen Hofstee2deb1ba2013-09-21 14:04:40 +020049 add r1, r1, r4
Albert ARIBAUD9580b322013-05-19 01:48:15 +000050 str r1, [r0]
Albert ARIBAUD1fc34f12013-06-11 14:17:35 +020051fixnext:
Albert ARIBAUD9580b322013-05-19 01:48:15 +000052 cmp r2, r3
53 blo fixloop
54
55relocate_done:
56
Mike Dunn4a717422013-06-21 09:12:28 -070057#ifdef __XSCALE__
58 /*
59 * On xscale, icache must be invalidated and write buffers drained,
60 * even with cache disabled - 4.2.7 of xscale core developer's manual
61 */
62 mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
63 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
64#endif
65
Albert ARIBAUD9580b322013-05-19 01:48:15 +000066 /* ARMv4- don't know bx lr but the assembler fails to see that */
67
68#ifdef __ARM_ARCH_4__
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020069 mov pc, lr
Albert ARIBAUD9580b322013-05-19 01:48:15 +000070#else
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020071 bx lr
Albert ARIBAUD9580b322013-05-19 01:48:15 +000072#endif
73
Albert ARIBAUD9580b322013-05-19 01:48:15 +000074ENDPROC(relocate_code)