blob: fdba004363af38de84ad7d8cd33db556aa6de4d7 [file] [log] [blame]
David Feng0ae76532013-12-14 11:47:35 +08001/*
2 * relocate - common relocation function for AArch64 U-Boot
3 *
4 * (C) Copyright 2013
5 * Albert ARIBAUD <albert.u.boot@aribaud.net>
6 * David Feng <fenghua@phytium.com.cn>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <asm-offsets.h>
12#include <config.h>
Simon Glassc70f74a2016-11-07 08:47:09 -070013#include <elf.h>
David Feng0ae76532013-12-14 11:47:35 +080014#include <linux/linkage.h>
York Sun83571bc2014-02-26 13:26:03 -080015#include <asm/macro.h>
David Feng0ae76532013-12-14 11:47:35 +080016
17/*
18 * void relocate_code (addr_moni)
19 *
20 * This function relocates the monitor code.
21 * x0 holds the destination address.
22 */
23ENTRY(relocate_code)
York Sun83571bc2014-02-26 13:26:03 -080024 stp x29, x30, [sp, #-32]! /* create a stack frame */
25 mov x29, sp
26 str x0, [sp, #16]
David Feng0ae76532013-12-14 11:47:35 +080027 /*
28 * Copy u-boot from flash to RAM
29 */
30 ldr x1, =__image_copy_start /* x1 <- SRC &__image_copy_start */
31 subs x9, x0, x1 /* x9 <- relocation offset */
32 b.eq relocate_done /* skip relocation */
33 ldr x2, =__image_copy_end /* x2 <- SRC &__image_copy_end */
34
35copy_loop:
36 ldp x10, x11, [x1], #16 /* copy from source address [x1] */
37 stp x10, x11, [x0], #16 /* copy to target address [x0] */
38 cmp x1, x2 /* until source end address [x2] */
39 b.lo copy_loop
York Sun83571bc2014-02-26 13:26:03 -080040 str x0, [sp, #24]
David Feng0ae76532013-12-14 11:47:35 +080041
42 /*
43 * Fix .rela.dyn relocations
44 */
45 ldr x2, =__rel_dyn_start /* x2 <- SRC &__rel_dyn_start */
46 ldr x3, =__rel_dyn_end /* x3 <- SRC &__rel_dyn_end */
47fixloop:
48 ldp x0, x1, [x2], #16 /* (x0,x1) <- (SRC location, fixup) */
49 ldr x4, [x2], #8 /* x4 <- addend */
50 and x1, x1, #0xffffffff
Simon Glassc70f74a2016-11-07 08:47:09 -070051 cmp x1, #R_AARCH64_RELATIVE
David Feng0ae76532013-12-14 11:47:35 +080052 bne fixnext
53
54 /* relative fix: store addend plus offset at dest location */
55 add x0, x0, x9
56 add x4, x4, x9
57 str x4, [x0]
58fixnext:
59 cmp x2, x3
60 b.lo fixloop
61
62relocate_done:
York Sun83571bc2014-02-26 13:26:03 -080063 switch_el x1, 3f, 2f, 1f
64 bl hang
653: mrs x0, sctlr_el3
66 b 0f
672: mrs x0, sctlr_el2
68 b 0f
691: mrs x0, sctlr_el1
700: tbz w0, #2, 5f /* skip flushing cache if disabled */
Masahiro Yamada1f4f5e52017-02-04 12:30:06 +090071 tbz w0, #12, 4f /* skip invalidating i-cache if disabled */
York Sun83571bc2014-02-26 13:26:03 -080072 ic iallu /* i-cache invalidate all */
73 isb sy
744: ldp x0, x1, [sp, #16]
75 bl __asm_flush_dcache_range
zijun_hu7baf952f2017-09-23 13:30:58 +0800765: ldp x29, x30, [sp],#32
David Feng0ae76532013-12-14 11:47:35 +080077 ret
78ENDPROC(relocate_code)