blob: 04804524ed653dde1e7671d716dd7c99cdee81a3 [file] [log] [blame]
David Feng85fd5f12013-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 Glass631c1a22016-11-07 08:47:09 -070013#include <elf.h>
David Feng85fd5f12013-12-14 11:47:35 +080014#include <linux/linkage.h>
York Sun11b84ee2014-02-26 13:26:03 -080015#include <asm/macro.h>
David Feng85fd5f12013-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 Sun11b84ee2014-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 Feng85fd5f12013-12-14 11:47:35 +080027 /*
28 * Copy u-boot from flash to RAM
29 */
Stephen Warren81c21372017-11-02 18:11:27 -060030 adr x1, __image_copy_start /* x1 <- Run &__image_copy_start */
31 subs x9, x0, x1 /* x8 <- Run to copy offset */
David Feng85fd5f12013-12-14 11:47:35 +080032 b.eq relocate_done /* skip relocation */
Stephen Warren81c21372017-11-02 18:11:27 -060033 /*
34 * Don't ldr x1, __image_copy_start here, since if the code is already
35 * running at an address other than it was linked to, that instruction
36 * will load the relocated value of __image_copy_start. To
37 * correctly apply relocations, we need to know the linked value.
38 *
39 * Linked &__image_copy_start, which we know was at
40 * CONFIG_SYS_TEXT_BASE, which is stored in _TEXT_BASE, as a non-
41 * relocated value, since it isn't a symbol reference.
42 */
43 ldr x1, _TEXT_BASE /* x1 <- Linked &__image_copy_start */
44 subs x9, x0, x1 /* x9 <- Link to copy offset */
David Feng85fd5f12013-12-14 11:47:35 +080045
Stephen Warren81c21372017-11-02 18:11:27 -060046 adr x1, __image_copy_start /* x1 <- Run &__image_copy_start */
47 adr x2, __image_copy_end /* x2 <- Run &__image_copy_end */
David Feng85fd5f12013-12-14 11:47:35 +080048copy_loop:
49 ldp x10, x11, [x1], #16 /* copy from source address [x1] */
50 stp x10, x11, [x0], #16 /* copy to target address [x0] */
51 cmp x1, x2 /* until source end address [x2] */
52 b.lo copy_loop
York Sun11b84ee2014-02-26 13:26:03 -080053 str x0, [sp, #24]
David Feng85fd5f12013-12-14 11:47:35 +080054
55 /*
56 * Fix .rela.dyn relocations
57 */
Stephen Warren81c21372017-11-02 18:11:27 -060058 adr x2, __rel_dyn_start /* x2 <- Run &__rel_dyn_start */
59 adr x3, __rel_dyn_end /* x3 <- Run &__rel_dyn_end */
David Feng85fd5f12013-12-14 11:47:35 +080060fixloop:
61 ldp x0, x1, [x2], #16 /* (x0,x1) <- (SRC location, fixup) */
62 ldr x4, [x2], #8 /* x4 <- addend */
63 and x1, x1, #0xffffffff
Simon Glass631c1a22016-11-07 08:47:09 -070064 cmp x1, #R_AARCH64_RELATIVE
David Feng85fd5f12013-12-14 11:47:35 +080065 bne fixnext
66
67 /* relative fix: store addend plus offset at dest location */
68 add x0, x0, x9
69 add x4, x4, x9
70 str x4, [x0]
71fixnext:
72 cmp x2, x3
73 b.lo fixloop
74
75relocate_done:
York Sun11b84ee2014-02-26 13:26:03 -080076 switch_el x1, 3f, 2f, 1f
77 bl hang
783: mrs x0, sctlr_el3
79 b 0f
802: mrs x0, sctlr_el2
81 b 0f
821: mrs x0, sctlr_el1
830: tbz w0, #2, 5f /* skip flushing cache if disabled */
Masahiro Yamada93f19fe2017-02-04 12:30:06 +090084 tbz w0, #12, 4f /* skip invalidating i-cache if disabled */
York Sun11b84ee2014-02-26 13:26:03 -080085 ic iallu /* i-cache invalidate all */
86 isb sy
874: ldp x0, x1, [sp, #16]
88 bl __asm_flush_dcache_range
zijun_hu446c7d02017-09-23 13:30:58 +0800895: ldp x29, x30, [sp],#32
David Feng85fd5f12013-12-14 11:47:35 +080090 ret
91ENDPROC(relocate_code)