1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
4 */
5
6 #include <common.h>
7 #include <elf.h>
8 #include <log.h>
9 #include <asm-generic/sections.h>
10 #include <asm/global_data.h>
11
12 extern ulong __image_copy_start;
13 extern ulong __ivt_start;
14 extern ulong __ivt_end;
15 extern ulong __text_end;
16
17 DECLARE_GLOBAL_DATA_PTR;
18
copy_uboot_to_ram(void)19 int copy_uboot_to_ram(void)
20 {
21 size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
22
23 if (gd->flags & GD_FLG_SKIP_RELOC)
24 return 0;
25
26 memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
27
28 return 0;
29 }
30
clear_bss(void)31 int clear_bss(void)
32 {
33 ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
34 size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
35
36 memset((void *)dst_addr, 0x00, len);
37
38 return 0;
39 }
40
41 /*
42 * Base functionality is taken from x86 version with added ARC-specifics
43 */
do_elf_reloc_fixups(void)44 int do_elf_reloc_fixups(void)
45 {
46 Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
47 Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
48
49 if (gd->flags & GD_FLG_SKIP_RELOC)
50 return 0;
51
52 debug("Section .rela.dyn is located at %08x-%08x\n",
53 (unsigned int)re_src, (unsigned int)re_end);
54
55 Elf32_Addr *offset_ptr_rom;
56 Elf32_Addr *offset_ptr_ram;
57
58 do {
59 /* Get the location from the relocation entry */
60 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
61
62 /* Check that the location of the relocation is in .text */
63 if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
64 offset_ptr_rom < (Elf32_Addr *)&__image_copy_end) {
65 unsigned int val, do_swap = 0;
66 /* Switch to the in-RAM version */
67 offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
68 gd->reloc_off);
69
70 #ifdef __LITTLE_ENDIAN__
71 /* If location in ".text" section swap value */
72 if (((u32)offset_ptr_rom >= (u32)&__text_start &&
73 (u32)offset_ptr_rom <= (u32)&__text_end)
74 #if defined(__ARC700__) || defined(__ARC600__)
75 || ((u32)offset_ptr_rom >= (u32)&__ivt_start &&
76 (u32)offset_ptr_rom <= (u32)&__ivt_end)
77 #endif
78 )
79 do_swap = 1;
80 #endif
81
82 debug("Patching value @ %08x (relocated to %08x)%s\n",
83 (unsigned int)offset_ptr_rom,
84 (unsigned int)offset_ptr_ram,
85 do_swap ? ", middle-endian encoded" : "");
86
87 /*
88 * Use "memcpy" because target location might be
89 * 16-bit aligned on ARC so we may need to read
90 * byte-by-byte. On attempt to read entire word by
91 * CPU throws an exception
92 */
93 memcpy(&val, offset_ptr_ram, sizeof(int));
94
95 if (do_swap)
96 val = (val << 16) | (val >> 16);
97
98 /* Check that the target points into executable */
99 if (val < (unsigned int)&__image_copy_start ||
100 val > (unsigned int)&__image_copy_end) {
101 /* TODO: Use panic() instead of debug()
102 *
103 * For some reason GCC might generate
104 * fake relocation even for LD/SC of constant
105 * inderectly. See an example below:
106 * ----------------------->8--------------------
107 * static int setup_mon_len(void)
108 * {
109 * gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
110 * return 0;
111 * }
112 * ----------------------->8--------------------
113 *
114 * And that's what we get in the binary:
115 * ----------------------->8--------------------
116 * 10005cb4 <setup_mon_len>:
117 * 10005cb4: 193c 3f80 0003 2f80 st 0x32f80,[r25,60]
118 * 10005cb8: R_ARC_RELATIVE *ABS*-0x10000000
119 * 10005cbc: 7fe0 j_s.d [blink]
120 * 10005cbe: 700c mov_s r0,0
121 * ----------------------->8--------------------
122 */
123 debug("Relocation target %08x points outside of image\n",
124 val);
125 }
126
127 val += gd->reloc_off;
128
129 if (do_swap)
130 val = (val << 16) | (val >> 16);
131
132 memcpy(offset_ptr_ram, &val, sizeof(int));
133 }
134 } while (++re_src < re_end);
135
136 return 0;
137 }
138