1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * relocate - common relocation function for AArch64 U-Boot
6 * Albert ARIBAUD <albert.u.boot@aribaud.net>
7 * David Feng <fenghua@phytium.com.cn>
10 #include <asm-offsets.h>
13 #include <linux/linkage.h>
14 #include <asm/macro.h>
17 * void relocate_code (addr_moni)
19 * This function relocates the monitor code.
20 * x0 holds the destination address.
23 stp x29, x30, [sp, #-32]! /* create a stack frame */
27 * Copy u-boot from flash to RAM
29 adr x1, __image_copy_start /* x1 <- Run &__image_copy_start */
30 subs x9, x0, x1 /* x8 <- Run to copy offset */
31 b.eq relocate_done /* skip relocation */
33 * Don't ldr x1, __image_copy_start here, since if the code is already
34 * running at an address other than it was linked to, that instruction
35 * will load the relocated value of __image_copy_start. To
36 * correctly apply relocations, we need to know the linked value.
38 * Linked &__image_copy_start, which we know was at
39 * CONFIG_SYS_TEXT_BASE, which is stored in _TEXT_BASE, as a non-
40 * relocated value, since it isn't a symbol reference.
42 ldr x1, _TEXT_BASE /* x1 <- Linked &__image_copy_start */
43 subs x9, x0, x1 /* x9 <- Link to copy offset */
45 adr x1, __image_copy_start /* x1 <- Run &__image_copy_start */
46 adr x2, __image_copy_end /* x2 <- Run &__image_copy_end */
48 ldp x10, x11, [x1], #16 /* copy from source address [x1] */
49 stp x10, x11, [x0], #16 /* copy to target address [x0] */
50 cmp x1, x2 /* until source end address [x2] */
55 * Fix .rela.dyn relocations
57 adr x2, __rel_dyn_start /* x2 <- Run &__rel_dyn_start */
58 adr x3, __rel_dyn_end /* x3 <- Run &__rel_dyn_end */
60 ldp x0, x1, [x2], #16 /* (x0,x1) <- (SRC location, fixup) */
61 ldr x4, [x2], #8 /* x4 <- addend */
62 and x1, x1, #0xffffffff
63 cmp x1, #R_AARCH64_RELATIVE
66 /* relative fix: store addend plus offset at dest location */
75 switch_el x1, 3f, 2f, 1f
82 0: tbz w0, #2, 5f /* skip flushing cache if disabled */
83 tbz w0, #12, 4f /* skip invalidating i-cache if disabled */
84 ic iallu /* i-cache invalidate all */
86 4: ldp x0, x1, [sp, #16]
87 bl __asm_flush_dcache_range
88 5: ldp x29, x30, [sp],#32
90 ENDPROC(relocate_code)