[U-Boot] [RFC 12/14] x86: Move relocation code out of board.c

Graeme Russ
2011-12-23T12:25:49Z

---
arch/x86/lib/Makefile | 1 +
arch/x86/lib/board.c | 113 -----------------------------------
arch/x86/lib/relocate.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 151 insertions(+), 113 deletions(-)
create mode 100644 arch/x86/lib/relocate.c

diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 7820895..57b6896 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -40,6 +40,7 @@ COBJS-$(CONFIG_SYS_GENERIC_TIMER) += pcat_timer.o
COBJS-$(CONFIG_PCI) += pci.o
COBJS-$(CONFIG_PCI) += pci_type1.o
COBJS-$(CONFIG_SYS_X86_REALMODE) += realmode.o
+COBJS-y += relocate.o
COBJS-y += string.o
COBJS-$(CONFIG_SYS_X86_ISR_TIMER) += timer.o
COBJS-$(CONFIG_VIDEO) += video_bios.o
diff --git a/arch/x86/lib/board.c b/arch/x86/lib/board.c
index e8274bf..a6596ef 100644
--- a/arch/x86/lib/board.c
+++ b/arch/x86/lib/board.c
@@ -41,7 +41,6 @@
#include <ide.h>
#include <serial.h>
#include <asm/u-boot-x86.h>
-#include <elf.h>

#ifdef CONFIG_BITBANGMII
#include <miiphy.h>
@@ -124,11 +123,6 @@ static void display_flash_config(ulong size)
*/
typedef int (init_fnc_t) (void);

-static int calculate_relocation_address(gd_t *);
-static int copy_uboot_to_ram(gd_t *);
-static int clear_bss(gd_t *);
-static int do_elf_reloc_fixups(gd_t *);
-
init_fnc_t *init_sequence_f[] = {
cpu_init_f,
board_early_init_f,
@@ -155,85 +149,6 @@ init_fnc_t *init_sequence_r[] = {

gd_t *gd;

-static int calculate_relocation_address(gd_t *id)
-{
- ulong text_start = (ulong)&__text_start;
- ulong bss_end = (ulong)&__bss_end;
- ulong dest_addr;
- ulong rel_offset;
-
- /* Calculate destination RAM Address and relocation offset */
- dest_addr = (ulong)id;
- dest_addr -= CONFIG_SYS_STACK_SIZE;
- dest_addr -= (bss_end - text_start);
-
- /*
- * Round destination address down to 16-byte boundary to keep
- * IDT and GDT 16-byte aligned
- */
- dest_addr &= ~15;
-
- rel_offset = dest_addr - text_start;
-
- id->relocaddr = dest_addr;
- id->reloc_off = rel_offset;
-
- return 0;
-}
-
-static int copy_uboot_to_ram(gd_t *id)
-{
- size_t len = (size_t)&__data_end - (size_t)&__text_start;
-
- memcpy((void *)id->relocaddr, (void *)&__text_start, len);
-
- return 0;
-}
-
-static int clear_bss(gd_t *id)
-{
- ulong dst_addr = (ulong)&__bss_start + id->reloc_off;
- size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
-
- memset((void *)dst_addr, 0x00, len);
-
- return 0;
-}
-
-static int do_elf_reloc_fixups(gd_t *id)
-{
- Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
- Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
-
- Elf32_Addr *offset_ptr_rom;
- Elf32_Addr *offset_ptr_ram;
-
- /* The size of the region of u-boot that runs out of RAM. */
- uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
-
- do {
- /* Get the location from the relocation entry */
- offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
-
- /* Check that the location of the relocation is in .text */
- if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
-
- /* Switch to the in-RAM version */
- offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
- id->reloc_off);
-
- /* Check that the target points into .text */
- if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
- *offset_ptr_ram <
- (CONFIG_SYS_TEXT_BASE + size)) {
- *offset_ptr_ram += id->reloc_off;
- }
- }
- } while (re_src++ < re_end);
-
- return 0;
-}
-
/* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
void board_init_f(ulong boot_flags)
{
@@ -254,34 +169,6 @@ void board_init_f(ulong boot_flags)
;
}

-typedef void (board_init_r_t) (gd_t *, ulong);
-
-void relocate_code(ulong stack_ptr, gd_t *id, ulong reloc_addr)
-{
- board_init_r_t *board_init_r_func;
-
- /* We are running from flash, but the stack is now in SDRAM */
-
- /* gd is still in CAR - Copy it into SDRAM */
- memcpy(id, gd, sizeof(gd_t));
-
- if (init_cache() != 0)
- hang();
-
- calculate_relocation_address(id);
- copy_uboot_to_ram(id);
- clear_bss(id);
- do_elf_reloc_fixups(id);
-
- board_init_r_func = board_init_r;
- board_init_r_func += id->reloc_off;
- board_init_r_func(id, id->relocaddr);
-
- /* NOTREACHED - relocate_code() does not return */
- while (1)
- ;
-}
-
void board_init_r(gd_t *id, ulong dest_addr)
{
#if defined(CONFIG_CMD_NET)
diff --git a/arch/x86/lib/relocate.c b/arch/x86/lib/relocate.c
new file mode 100644
index 0000000..f8c0b3f
--- /dev/null
+++ b/arch/x86/lib/relocate.c
@@ -0,0 +1,150 @@
+/*
+ * (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>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <asm/u-boot-x86.h>
+#include <elf.h>
+
+static int calculate_relocation_address(gd_t *);
+static int copy_uboot_to_ram(gd_t *);
+static int clear_bss(gd_t *);
+static int do_elf_reloc_fixups(gd_t *);
+
+static int calculate_relocation_address(gd_t *id)
+{
+ ulong text_start = (ulong)&__text_start;
+ ulong bss_end = (ulong)&__bss_end;
+ ulong dest_addr;
+ ulong rel_offset;
+
+ /* Calculate destination RAM Address and relocation offset */
+ dest_addr = (ulong)id;
+ dest_addr -= CONFIG_SYS_STACK_SIZE;
+ dest_addr -= (bss_end - text_start);
+
+ /*
+ * Round destination address down to 16-byte boundary to keep
+ * IDT and GDT 16-byte aligned
+ */
+ dest_addr &= ~15;
+
+ rel_offset = dest_addr - text_start;
+
+ id->relocaddr = dest_addr;
+ id->reloc_off = rel_offset;
+
+ return 0;
+}
+
+static int copy_uboot_to_ram(gd_t *id)
+{
+ size_t len = (size_t)&__data_end - (size_t)&__text_start;
+
+ memcpy((void *)id->relocaddr, (void *)&__text_start, len);
+
+ return 0;
+}
+
+static int clear_bss(gd_t *id)
+{
+ ulong dst_addr = (ulong)&__bss_start + id->reloc_off;
+ size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
+
+ memset((void *)dst_addr, 0x00, len);
+
+ return 0;
+}
+
+static int do_elf_reloc_fixups(gd_t *id)
+{
+ Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
+ Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
+
+ Elf32_Addr *offset_ptr_rom;
+ Elf32_Addr *offset_ptr_ram;
+
+ /* The size of the region of u-boot that runs out of RAM. */
+ uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
+
+ do {
+ /* Get the location from the relocation entry */
+ offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
+
+ /* Check that the location of the relocation is in .text */
+ if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
+
+ /* Switch to the in-RAM version */
+ offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
+ id->reloc_off);
+
+ /* Check that the target points into .text */
+ if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
+ *offset_ptr_ram <
+ (CONFIG_SYS_TEXT_BASE + size)) {
+ *offset_ptr_ram += id->reloc_off;
+ }
+ }
+ } while (re_src++ < re_end);
+
+ return 0;
+}
+
+typedef void (board_init_r_t) (gd_t *, ulong);
+
+__attribute__ ((__noreturn__))
+void relocate_code(ulong dummy_1, gd_t *id, ulong dummy_2)
+{
+ board_init_r_t *board_init_r_func;
+
+ /* We are running from flash, but the stack is now in SDRAM */
+
+ /* gd is still in CAR - Copy it into SDRAM */
+ memcpy(id, gd, sizeof(gd_t));
+
+ if (init_cache() != 0)
+ hang();
+
+ calculate_relocation_address(id);
+ copy_uboot_to_ram(id);
+ clear_bss(id);
+ do_elf_reloc_fixups(id);
+
+ board_init_r_func = board_init_r;
+ board_init_r_func += id->reloc_off;
+ board_init_r_func(id, id->relocaddr);
+
+ /* NOTREACHED - relocate_code() does not return */
+ while (1)
+ ;
+}
--
1.7.5.2.317.g391b14