Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relocate - common relocation function for ARM U-Boot |
| 3 | * |
| 4 | * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Georges Savoundararadj | 58623d1 | 2014-10-28 23:16:11 +0100 | [diff] [blame] | 9 | #include <asm-offsets.h> |
| 10 | #include <config.h> |
Simon Glass | 631c1a2 | 2016-11-07 08:47:09 -0700 | [diff] [blame] | 11 | #include <elf.h> |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 12 | #include <linux/linkage.h> |
rev13@wp.pl | b3b57e8 | 2015-03-01 12:44:39 +0100 | [diff] [blame] | 13 | #ifdef CONFIG_CPU_V7M |
| 14 | #include <asm/armv7m.h> |
| 15 | #endif |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
Albert ARIBAUD | bd6e56f | 2014-11-13 17:59:15 +0100 | [diff] [blame] | 18 | * 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 | |
| 28 | ENTRY(relocate_vectors) |
| 29 | |
rev13@wp.pl | b3b57e8 | 2015-03-01 12:44:39 +0100 | [diff] [blame] | 30 | #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 ARIBAUD | bd6e56f | 2014-11-13 17:59:15 +0100 | [diff] [blame] | 39 | #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.pl | b3b57e8 | 2015-03-01 12:44:39 +0100 | [diff] [blame] | 63 | #endif |
Albert ARIBAUD | bd6e56f | 2014-11-13 17:59:15 +0100 | [diff] [blame] | 64 | bx lr |
| 65 | |
| 66 | ENDPROC(relocate_vectors) |
| 67 | |
| 68 | /* |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 69 | * 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 | |
| 80 | ENTRY(relocate_code) |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 81 | ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */ |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 82 | subs r4, r0, r1 /* r4 <- relocation offset */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 83 | beq relocate_done /* skip relocation */ |
Albert ARIBAUD | c53687e | 2013-06-11 14:17:33 +0200 | [diff] [blame] | 84 | ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 85 | |
| 86 | copy_loop: |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 87 | ldmia r1!, {r10-r11} /* copy from source address [r1] */ |
| 88 | stmia r0!, {r10-r11} /* copy to target address [r0] */ |
| 89 | cmp r1, r2 /* until source end address [r2] */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 90 | blo copy_loop |
| 91 | |
| 92 | /* |
| 93 | * fix .rel.dyn relocations |
| 94 | */ |
Albert ARIBAUD | af3ff16 | 2013-06-11 14:17:34 +0200 | [diff] [blame] | 95 | ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */ |
| 96 | ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 97 | fixloop: |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 98 | ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */ |
| 99 | and r1, r1, #0xff |
Simon Glass | 631c1a2 | 2016-11-07 08:47:09 -0700 | [diff] [blame] | 100 | cmp r1, #R_ARM_RELATIVE |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 101 | bne fixnext |
| 102 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 103 | /* relative fix: increase location by offset */ |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 104 | add r0, r0, r4 |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 105 | ldr r1, [r0] |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 106 | add r1, r1, r4 |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 107 | str r1, [r0] |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 108 | fixnext: |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 109 | cmp r2, r3 |
| 110 | blo fixloop |
| 111 | |
| 112 | relocate_done: |
| 113 | |
Mike Dunn | 4a71742 | 2013-06-21 09:12:28 -0700 | [diff] [blame] | 114 | #ifdef __XSCALE__ |
| 115 | /* |
| 116 | * On xscale, icache must be invalidated and write buffers drained, |
| 117 | * even with cache disabled - 4.2.7 of xscale core developer's manual |
| 118 | */ |
| 119 | mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */ |
| 120 | mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */ |
| 121 | #endif |
| 122 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 123 | /* ARMv4- don't know bx lr but the assembler fails to see that */ |
| 124 | |
| 125 | #ifdef __ARM_ARCH_4__ |
Albert ARIBAUD | da75866 | 2014-11-13 17:59:14 +0100 | [diff] [blame] | 126 | mov pc, lr |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 127 | #else |
Albert ARIBAUD | da75866 | 2014-11-13 17:59:14 +0100 | [diff] [blame] | 128 | bx lr |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 131 | ENDPROC(relocate_code) |