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 | |
| 9 | #include <linux/linkage.h> |
| 10 | |
| 11 | /* |
| 12 | * void relocate_code(addr_moni) |
| 13 | * |
| 14 | * This function relocates the monitor code. |
| 15 | * |
| 16 | * NOTE: |
| 17 | * To prevent the code below from containing references with an R_ARM_ABS32 |
| 18 | * relocation record type, we never refer to linker-defined symbols directly. |
| 19 | * Instead, we declare literals which contain their relative location with |
| 20 | * respect to relocate_code, and at run time, add relocate_code back to them. |
| 21 | */ |
| 22 | |
| 23 | ENTRY(relocate_code) |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 24 | ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */ |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 25 | subs r4, r0, r1 /* r4 <- relocation offset */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 26 | beq relocate_done /* skip relocation */ |
Albert ARIBAUD | c53687e | 2013-06-11 14:17:33 +0200 | [diff] [blame] | 27 | ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 28 | |
| 29 | copy_loop: |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 30 | ldmia r1!, {r10-r11} /* copy from source address [r1] */ |
| 31 | stmia r0!, {r10-r11} /* copy to target address [r0] */ |
| 32 | cmp r1, r2 /* until source end address [r2] */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 33 | blo copy_loop |
| 34 | |
| 35 | /* |
| 36 | * fix .rel.dyn relocations |
| 37 | */ |
Albert ARIBAUD | af3ff16 | 2013-06-11 14:17:34 +0200 | [diff] [blame] | 38 | ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */ |
| 39 | ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */ |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 40 | fixloop: |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 41 | ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */ |
| 42 | and r1, r1, #0xff |
| 43 | cmp r1, #23 /* relative fixup? */ |
| 44 | bne fixnext |
| 45 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 46 | /* relative fix: increase location by offset */ |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 47 | add r0, r0, r4 |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 48 | ldr r1, [r0] |
Jeroen Hofstee | 2deb1ba | 2013-09-21 14:04:40 +0200 | [diff] [blame] | 49 | add r1, r1, r4 |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 50 | str r1, [r0] |
Albert ARIBAUD | 1fc34f1 | 2013-06-11 14:17:35 +0200 | [diff] [blame] | 51 | fixnext: |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 52 | cmp r2, r3 |
| 53 | blo fixloop |
| 54 | |
| 55 | relocate_done: |
| 56 | |
Mike Dunn | 4a71742 | 2013-06-21 09:12:28 -0700 | [diff] [blame] | 57 | #ifdef __XSCALE__ |
| 58 | /* |
| 59 | * On xscale, icache must be invalidated and write buffers drained, |
| 60 | * even with cache disabled - 4.2.7 of xscale core developer's manual |
| 61 | */ |
| 62 | mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */ |
| 63 | mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */ |
| 64 | #endif |
| 65 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 66 | /* ARMv4- don't know bx lr but the assembler fails to see that */ |
| 67 | |
| 68 | #ifdef __ARM_ARCH_4__ |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 69 | mov pc, lr |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 70 | #else |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 71 | bx lr |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Albert ARIBAUD | 9580b32 | 2013-05-19 01:48:15 +0000 | [diff] [blame] | 74 | ENDPROC(relocate_code) |