2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-12-23 04:57:58 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2008-2011
|
|
|
|
* Graeme Russ, <graeme.russ@gmail.com>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2002
|
|
|
|
* Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2002
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2002
|
|
|
|
* Sysgo Real-Time Solutions, GmbH <www.elinos.com>
|
|
|
|
* Marius Groeger <mgroeger@sysgo.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-03-31 14:40:38 +00:00
|
|
|
#include <relocate.h>
|
2011-12-23 04:57:58 +00:00
|
|
|
#include <asm/u-boot-x86.h>
|
2013-03-05 14:39:54 +00:00
|
|
|
#include <asm/sections.h>
|
2011-12-23 04:57:58 +00:00
|
|
|
#include <elf.h>
|
|
|
|
|
2013-04-17 16:13:33 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2011-12-23 10:14:22 +00:00
|
|
|
int copy_uboot_to_ram(void)
|
2011-12-23 04:57:58 +00:00
|
|
|
{
|
2017-01-16 14:03:55 +00:00
|
|
|
size_t len = (uintptr_t)&__data_end - (uintptr_t)&__text_start;
|
2011-12-23 04:57:58 +00:00
|
|
|
|
2015-08-04 18:33:44 +00:00
|
|
|
if (gd->flags & GD_FLG_SKIP_RELOC)
|
|
|
|
return 0;
|
2011-12-23 04:57:58 +00:00
|
|
|
memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-23 10:14:22 +00:00
|
|
|
int clear_bss(void)
|
2011-12-23 04:57:58 +00:00
|
|
|
{
|
|
|
|
ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
|
2017-01-16 14:03:55 +00:00
|
|
|
size_t len = (uintptr_t)&__bss_end - (uintptr_t)&__bss_start;
|
2011-12-23 04:57:58 +00:00
|
|
|
|
2015-08-04 18:33:44 +00:00
|
|
|
if (gd->flags & GD_FLG_SKIP_RELOC)
|
|
|
|
return 0;
|
2011-12-23 04:57:58 +00:00
|
|
|
memset((void *)dst_addr, 0x00, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:03:54 +00:00
|
|
|
#if CONFIG_IS_ENABLED(X86_64)
|
|
|
|
static void do_elf_reloc_fixups64(unsigned int text_base, uintptr_t size,
|
|
|
|
Elf64_Rela *re_src, Elf64_Rela *re_end)
|
|
|
|
{
|
|
|
|
Elf64_Addr *offset_ptr_rom, *last_offset = NULL;
|
|
|
|
Elf64_Addr *offset_ptr_ram;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Get the location from the relocation entry */
|
|
|
|
offset_ptr_rom = (Elf64_Addr *)(uintptr_t)re_src->r_offset;
|
|
|
|
|
|
|
|
/* Check that the location of the relocation is in .text */
|
|
|
|
if (offset_ptr_rom >= (Elf64_Addr *)(uintptr_t)text_base &&
|
|
|
|
offset_ptr_rom > last_offset) {
|
|
|
|
/* Switch to the in-RAM version */
|
|
|
|
offset_ptr_ram = (Elf64_Addr *)((ulong)offset_ptr_rom +
|
|
|
|
gd->reloc_off);
|
|
|
|
|
|
|
|
/* Check that the target points into .text */
|
|
|
|
if (*offset_ptr_ram >= text_base &&
|
|
|
|
*offset_ptr_ram <= text_base + size) {
|
|
|
|
*offset_ptr_ram = gd->reloc_off +
|
|
|
|
re_src->r_addend;
|
|
|
|
} else {
|
2018-08-06 11:47:40 +00:00
|
|
|
debug(" %p: %lx: rom reloc %lx, ram %p, value %lx, limit %lX\n",
|
2017-01-16 14:03:54 +00:00
|
|
|
re_src, (ulong)re_src->r_info,
|
|
|
|
(ulong)re_src->r_offset, offset_ptr_ram,
|
|
|
|
(ulong)*offset_ptr_ram, text_base + size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug(" %p: %lx: rom reloc %lx, last %p\n", re_src,
|
|
|
|
(ulong)re_src->r_info, (ulong)re_src->r_offset,
|
|
|
|
last_offset);
|
|
|
|
}
|
|
|
|
last_offset = offset_ptr_rom;
|
|
|
|
|
|
|
|
} while (++re_src < re_end);
|
|
|
|
}
|
|
|
|
#else
|
2017-01-16 14:03:53 +00:00
|
|
|
static void do_elf_reloc_fixups32(unsigned int text_base, uintptr_t size,
|
|
|
|
Elf32_Rel *re_src, Elf32_Rel *re_end)
|
2011-12-23 04:57:58 +00:00
|
|
|
{
|
2013-02-28 19:26:16 +00:00
|
|
|
Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
|
2011-12-23 04:57:58 +00:00
|
|
|
Elf32_Addr *offset_ptr_ram;
|
2014-11-15 01:18:23 +00:00
|
|
|
|
2011-12-23 04:57:58 +00:00
|
|
|
do {
|
|
|
|
/* Get the location from the relocation entry */
|
2017-01-16 14:03:53 +00:00
|
|
|
offset_ptr_rom = (Elf32_Addr *)(uintptr_t)re_src->r_offset;
|
2011-12-23 04:57:58 +00:00
|
|
|
|
|
|
|
/* Check that the location of the relocation is in .text */
|
2017-01-16 14:03:53 +00:00
|
|
|
if (offset_ptr_rom >= (Elf32_Addr *)(uintptr_t)text_base &&
|
2015-08-04 18:33:49 +00:00
|
|
|
offset_ptr_rom > last_offset) {
|
2011-12-23 04:57:58 +00:00
|
|
|
|
|
|
|
/* Switch to the in-RAM version */
|
|
|
|
offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
|
|
|
|
gd->reloc_off);
|
|
|
|
|
|
|
|
/* Check that the target points into .text */
|
2015-08-04 18:33:49 +00:00
|
|
|
if (*offset_ptr_ram >= text_base &&
|
|
|
|
*offset_ptr_ram <= text_base + size) {
|
2011-12-23 04:57:58 +00:00
|
|
|
*offset_ptr_ram += gd->reloc_off;
|
2013-02-28 19:26:16 +00:00
|
|
|
} else {
|
2018-08-06 11:47:40 +00:00
|
|
|
debug(" %p: rom reloc %x, ram %p, value %x, limit %lX\n",
|
|
|
|
re_src, re_src->r_offset, offset_ptr_ram,
|
|
|
|
*offset_ptr_ram, text_base + size);
|
2011-12-23 04:57:58 +00:00
|
|
|
}
|
2013-02-28 19:26:16 +00:00
|
|
|
} else {
|
|
|
|
debug(" %p: rom reloc %x, last %p\n", re_src,
|
|
|
|
re_src->r_offset, last_offset);
|
2011-12-23 04:57:58 +00:00
|
|
|
}
|
2013-02-28 19:26:16 +00:00
|
|
|
last_offset = offset_ptr_rom;
|
|
|
|
|
2012-10-23 18:04:43 +00:00
|
|
|
} while (++re_src < re_end);
|
2017-01-16 14:03:53 +00:00
|
|
|
}
|
2017-01-16 14:03:54 +00:00
|
|
|
#endif
|
2017-01-16 14:03:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function has more error checking than you might expect. Please see
|
|
|
|
* this commit message for more information:
|
|
|
|
* 62f7970a x86: Add error checking to x86 relocation code
|
|
|
|
*/
|
|
|
|
int do_elf_reloc_fixups(void)
|
|
|
|
{
|
|
|
|
void *re_src = (void *)(&__rel_dyn_start);
|
|
|
|
void *re_end = (void *)(&__rel_dyn_end);
|
|
|
|
uint text_base;
|
|
|
|
|
|
|
|
/* The size of the region of u-boot that runs out of RAM. */
|
|
|
|
uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
|
|
|
|
|
|
|
|
if (gd->flags & GD_FLG_SKIP_RELOC)
|
|
|
|
return 0;
|
|
|
|
if (re_src == re_end)
|
|
|
|
panic("No relocation data");
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYS_TEXT_BASE
|
|
|
|
text_base = CONFIG_SYS_TEXT_BASE;
|
|
|
|
#else
|
|
|
|
panic("No CONFIG_SYS_TEXT_BASE");
|
|
|
|
#endif
|
2017-01-16 14:03:54 +00:00
|
|
|
#if CONFIG_IS_ENABLED(X86_64)
|
|
|
|
do_elf_reloc_fixups64(text_base, size, re_src, re_end);
|
|
|
|
#else
|
2017-01-16 14:03:53 +00:00
|
|
|
do_elf_reloc_fixups32(text_base, size, re_src, re_end);
|
2017-01-16 14:03:54 +00:00
|
|
|
#endif
|
2011-12-23 04:57:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|