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