Prepare v2023.10
[platform/kernel/u-boot.git] / arch / x86 / lib / relocate.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008-2011
4  * Graeme Russ, <graeme.russ@gmail.com>
5  *
6  * (C) Copyright 2002
7  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8  *
9  * (C) Copyright 2002
10  * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
11  *
12  * (C) Copyright 2002
13  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14  * Marius Groeger <mgroeger@sysgo.de>
15  */
16
17 #include <common.h>
18 #include <log.h>
19 #include <relocate.h>
20 #include <asm/global_data.h>
21 #include <asm/u-boot-x86.h>
22 #include <asm/sections.h>
23 #include <elf.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 int copy_uboot_to_ram(void)
28 {
29         size_t len = (uintptr_t)&__data_end - (uintptr_t)&__text_start;
30
31         if (gd->flags & GD_FLG_SKIP_RELOC)
32                 return 0;
33         memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
34
35         return 0;
36 }
37
38 #ifndef CONFIG_EFI_APP
39 int clear_bss(void)
40 {
41         ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
42         size_t len = (uintptr_t)&__bss_end - (uintptr_t)&__bss_start;
43
44         if (gd->flags & GD_FLG_SKIP_RELOC)
45                 return 0;
46         memset((void *)dst_addr, 0x00, len);
47
48         return 0;
49 }
50 #endif
51
52 #if CONFIG_IS_ENABLED(X86_64)
53 static void do_elf_reloc_fixups64(unsigned int text_base, uintptr_t size,
54                                   Elf64_Rela *re_src, Elf64_Rela *re_end)
55 {
56         Elf64_Addr *offset_ptr_rom, *last_offset = NULL;
57         Elf64_Addr *offset_ptr_ram;
58
59         do {
60                 unsigned long long type = ELF64_R_TYPE(re_src->r_info);
61
62                 if (type != R_X86_64_RELATIVE) {
63                         printf("%s: unsupported relocation type 0x%llx "
64                                "at %p, ", __func__, type, re_src);
65                         printf("offset = 0x%llx\n", re_src->r_offset);
66                         continue;
67                 }
68
69                 /* Get the location from the relocation entry */
70                 offset_ptr_rom = (Elf64_Addr *)(uintptr_t)re_src->r_offset;
71
72                 /* Check that the location of the relocation is in .text */
73                 if (offset_ptr_rom >= (Elf64_Addr *)(uintptr_t)text_base &&
74                     offset_ptr_rom > last_offset) {
75                         /* Switch to the in-RAM version */
76                         offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +
77                                                         gd->reloc_off);
78
79                         /* Check that the target points into .text */
80                         if (*offset_ptr_ram >= text_base &&
81                             *offset_ptr_ram <= text_base + size) {
82                                 *offset_ptr_ram = gd->reloc_off +
83                                                         re_src->r_addend;
84                         } else {
85                                 debug("   %p: %lx: rom reloc %lx, ram %p, value %lx, limit %lX\n",
86                                       re_src, (ulong)re_src->r_info,
87                                       (ulong)re_src->r_offset, offset_ptr_ram,
88                                       (ulong)*offset_ptr_ram, text_base + size);
89                         }
90                 } else {
91                         debug("   %p: %lx: rom reloc %lx, last %p\n", re_src,
92                               (ulong)re_src->r_info, (ulong)re_src->r_offset,
93                               last_offset);
94                 }
95                 last_offset = offset_ptr_rom;
96
97         } while (++re_src < re_end);
98 }
99 #else
100 static void do_elf_reloc_fixups32(unsigned int text_base, uintptr_t size,
101                                   Elf32_Rel *re_src, Elf32_Rel *re_end)
102 {
103         Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
104         Elf32_Addr *offset_ptr_ram;
105
106         do {
107                 unsigned int type = ELF32_R_TYPE(re_src->r_info);
108
109                 if (type != R_386_RELATIVE) {
110                         printf("%s: unsupported relocation type 0x%x "
111                                "at %p, ", __func__, type, re_src);
112                         printf("offset = 0x%x\n", re_src->r_offset);
113                         continue;
114                 }
115
116                 /* Get the location from the relocation entry */
117                 offset_ptr_rom = (Elf32_Addr *)(uintptr_t)re_src->r_offset;
118
119                 /* Check that the location of the relocation is in .text */
120                 if (offset_ptr_rom >= (Elf32_Addr *)(uintptr_t)text_base &&
121                     offset_ptr_rom > last_offset) {
122
123                         /* Switch to the in-RAM version */
124                         offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
125                                                         gd->reloc_off);
126
127                         /* Check that the target points into .text */
128                         if (*offset_ptr_ram >= text_base &&
129                             *offset_ptr_ram <= text_base + size) {
130                                 *offset_ptr_ram += gd->reloc_off;
131                         } else {
132                                 debug("   %p: rom reloc %x, ram %p, value %x, limit %lX\n",
133                                       re_src, re_src->r_offset, offset_ptr_ram,
134                                       *offset_ptr_ram, text_base + size);
135                         }
136                 } else {
137                         debug("   %p: rom reloc %x, last %p\n", re_src,
138                                re_src->r_offset, last_offset);
139                 }
140                 last_offset = offset_ptr_rom;
141
142         } while (++re_src < re_end);
143 }
144 #endif
145
146 /*
147  * This function has more error checking than you might expect. Please see
148  * this commit message for more information:
149  *    62f7970a x86: Add error checking to x86 relocation code
150  */
151 int do_elf_reloc_fixups(void)
152 {
153         void *re_src = (void *)(&__rel_dyn_start);
154         void *re_end = (void *)(&__rel_dyn_end);
155         uint text_base;
156
157         /* The size of the region of u-boot that runs out of RAM. */
158         uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
159
160         if (gd->flags & GD_FLG_SKIP_RELOC)
161                 return 0;
162         if (re_src == re_end)
163                 panic("No relocation data");
164
165 #ifdef CONFIG_TEXT_BASE
166         text_base = CONFIG_TEXT_BASE;
167 #else
168         panic("No CONFIG_TEXT_BASE");
169 #endif
170 #if CONFIG_IS_ENABLED(X86_64)
171         do_elf_reloc_fixups64(text_base, size, re_src, re_end);
172 #else
173         do_elf_reloc_fixups32(text_base, size, re_src, re_end);
174 #endif
175
176         return 0;
177 }