blob: 5102bfabde4acd4351173364e9111ea74efcc1e4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +00002/*
3 * relocate - common relocation function for ARM U-Boot
4 *
5 * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert ARIBAUD3da0e572013-05-19 01:48:15 +00006 */
7
Georges Savoundararadj3ff46cc2014-10-28 23:16:11 +01008#include <asm-offsets.h>
Vikas Manochad22336a2018-08-31 16:57:06 -07009#include <asm/assembler.h>
Georges Savoundararadj3ff46cc2014-10-28 23:16:11 +010010#include <config.h>
Simon Glassc70f74a2016-11-07 08:47:09 -070011#include <elf.h>
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000012#include <linux/linkage.h>
rev13@wp.pl12d8a722015-03-01 12:44:39 +010013#ifdef CONFIG_CPU_V7M
14#include <asm/armv7m.h>
15#endif
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000016
17/*
Albert ARIBAUDdb544b92014-11-13 17:59:15 +010018 * Default/weak exception vectors relocation routine
19 *
20 * This routine covers the standard ARM cases: normal (0x00000000),
21 * high (0xffff0000) and VBAR. SoCs which do not comply with any of
22 * the standard cases must provide their own, strong, version.
23 */
24
25 .section .text.relocate_vectors,"ax",%progbits
26 .weak relocate_vectors
27
28ENTRY(relocate_vectors)
29
rev13@wp.pl12d8a722015-03-01 12:44:39 +010030#ifdef CONFIG_CPU_V7M
31 /*
32 * On ARMv7-M we only have to write the new vector address
33 * to VTOR register.
34 */
35 ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
36 ldr r1, =V7M_SCB_BASE
37 str r0, [r1, V7M_SCB_VTOR]
38#else
Albert ARIBAUDdb544b92014-11-13 17:59:15 +010039#ifdef CONFIG_HAS_VBAR
40 /*
41 * If the ARM processor has the security extensions,
42 * use VBAR to relocate the exception vectors.
43 */
44 ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
45 mcr p15, 0, r0, c12, c0, 0 /* Set VBAR */
46#else
47 /*
48 * Copy the relocated exception vectors to the
49 * correct address
50 * CP15 c1 V bit gives us the location of the vectors:
51 * 0x00000000 or 0xFFFF0000.
52 */
53 ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
54 mrc p15, 0, r2, c1, c0, 0 /* V bit (bit[13]) in CP15 c1 */
55 ands r2, r2, #(1 << 13)
56 ldreq r1, =0x00000000 /* If V=0 */
57 ldrne r1, =0xFFFF0000 /* If V=1 */
58 ldmia r0!, {r2-r8,r10}
59 stmia r1!, {r2-r8,r10}
60 ldmia r0!, {r2-r8,r10}
61 stmia r1!, {r2-r8,r10}
62#endif
rev13@wp.pl12d8a722015-03-01 12:44:39 +010063#endif
Albert ARIBAUDdb544b92014-11-13 17:59:15 +010064 bx lr
65
66ENDPROC(relocate_vectors)
67
68/*
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000069 * void relocate_code(addr_moni)
70 *
71 * This function relocates the monitor code.
72 *
73 * NOTE:
74 * To prevent the code below from containing references with an R_ARM_ABS32
75 * relocation record type, we never refer to linker-defined symbols directly.
76 * Instead, we declare literals which contain their relative location with
77 * respect to relocate_code, and at run time, add relocate_code back to them.
78 */
79
80ENTRY(relocate_code)
Andre Przywara81e712a2022-07-12 12:00:23 +010081relocate_base:
82 adr r3, relocate_base
Chia-Wei Wangcd82f192021-08-03 10:50:10 +080083 ldr r1, _image_copy_start_ofs
84 add r1, r3 /* r1 <- Run &__image_copy_start */
85 subs r4, r0, r1 /* r4 <- Run to copy offset */
86 beq relocate_done /* skip relocation */
87 ldr r1, _image_copy_start_ofs
88 add r1, r3 /* r1 <- Run &__image_copy_start */
89 ldr r2, _image_copy_end_ofs
90 add r2, r3 /* r2 <- Run &__image_copy_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000091copy_loop:
Chia-Wei Wangcd82f192021-08-03 10:50:10 +080092 ldmia r1!, {r10-r11} /* copy from source address [r1] */
93 stmia r0!, {r10-r11} /* copy to target address [r0] */
94 cmp r1, r2 /* until source end address [r2] */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +000095 blo copy_loop
96
97 /*
98 * fix .rel.dyn relocations
99 */
Chia-Wei Wangcd82f192021-08-03 10:50:10 +0800100 ldr r1, _rel_dyn_start_ofs
101 add r2, r1, r3 /* r2 <- Run &__rel_dyn_start */
102 ldr r1, _rel_dyn_end_ofs
103 add r3, r1, r3 /* r3 <- Run &__rel_dyn_end */
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000104fixloop:
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +0200105 ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
106 and r1, r1, #0xff
Simon Glassc70f74a2016-11-07 08:47:09 -0700107 cmp r1, #R_ARM_RELATIVE
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +0200108 bne fixnext
109
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000110 /* relative fix: increase location by offset */
Jeroen Hofsteea81872f2013-09-21 14:04:40 +0200111 add r0, r0, r4
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000112 ldr r1, [r0]
Jeroen Hofsteea81872f2013-09-21 14:04:40 +0200113 add r1, r1, r4
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000114 str r1, [r0]
Albert ARIBAUDfbf87b12013-06-11 14:17:35 +0200115fixnext:
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000116 cmp r2, r3
117 blo fixloop
118
119relocate_done:
120
Mike Dunn9dc8fef2013-06-21 09:12:28 -0700121#ifdef __XSCALE__
122 /*
123 * On xscale, icache must be invalidated and write buffers drained,
124 * even with cache disabled - 4.2.7 of xscale core developer's manual
125 */
126 mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
127 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
128#endif
129
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000130 /* ARMv4- don't know bx lr but the assembler fails to see that */
131
132#ifdef __ARM_ARCH_4__
Albert ARIBAUD28970ef2014-11-13 17:59:14 +0100133 mov pc, lr
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000134#else
Albert ARIBAUD28970ef2014-11-13 17:59:14 +0100135 bx lr
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000136#endif
137
Albert ARIBAUD3da0e572013-05-19 01:48:15 +0000138ENDPROC(relocate_code)
Chia-Wei Wangcd82f192021-08-03 10:50:10 +0800139
140_image_copy_start_ofs:
141 .word __image_copy_start - relocate_code
142_image_copy_end_ofs:
143 .word __image_copy_end - relocate_code
144_rel_dyn_start_ofs:
145 .word __rel_dyn_start - relocate_code
146_rel_dyn_end_ofs:
147 .word __rel_dyn_end - relocate_code