xref: /rk3399_rockchip-uboot/arch/arc/lib/relocate.c (revision 9bef24d0dea30447951e1cd0074be502ca408fc0)
122723828SAlexey Brodkin /*
222723828SAlexey Brodkin  * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
322723828SAlexey Brodkin  *
422723828SAlexey Brodkin  * SPDX-License-Identifier:	GPL-2.0+
522723828SAlexey Brodkin  */
622723828SAlexey Brodkin 
722723828SAlexey Brodkin #include <common.h>
822723828SAlexey Brodkin #include <elf.h>
9*9bef24d0SAlexey Brodkin #include <asm-generic/sections.h>
10*9bef24d0SAlexey Brodkin 
11*9bef24d0SAlexey Brodkin extern ulong __image_copy_start;
12*9bef24d0SAlexey Brodkin extern ulong __ivt_end;
1322723828SAlexey Brodkin 
1422723828SAlexey Brodkin DECLARE_GLOBAL_DATA_PTR;
1522723828SAlexey Brodkin 
copy_uboot_to_ram(void)163fb80163SAlexey Brodkin int copy_uboot_to_ram(void)
173fb80163SAlexey Brodkin {
183fb80163SAlexey Brodkin 	size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
193fb80163SAlexey Brodkin 
203fb80163SAlexey Brodkin 	memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
213fb80163SAlexey Brodkin 
223fb80163SAlexey Brodkin 	return 0;
233fb80163SAlexey Brodkin }
243fb80163SAlexey Brodkin 
clear_bss(void)253fb80163SAlexey Brodkin int clear_bss(void)
263fb80163SAlexey Brodkin {
273fb80163SAlexey Brodkin 	ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
283fb80163SAlexey Brodkin 	size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
293fb80163SAlexey Brodkin 
303fb80163SAlexey Brodkin 	memset((void *)dst_addr, 0x00, len);
313fb80163SAlexey Brodkin 
323fb80163SAlexey Brodkin 	return 0;
333fb80163SAlexey Brodkin }
343fb80163SAlexey Brodkin 
3522723828SAlexey Brodkin /*
3622723828SAlexey Brodkin  * Base functionality is taken from x86 version with added ARC-specifics
3722723828SAlexey Brodkin  */
do_elf_reloc_fixups(void)3822723828SAlexey Brodkin int do_elf_reloc_fixups(void)
3922723828SAlexey Brodkin {
4022723828SAlexey Brodkin 	Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
4122723828SAlexey Brodkin 	Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
4222723828SAlexey Brodkin 
43ffffcd15SAlexey Brodkin 	debug("Section .rela.dyn is located at %08x-%08x\n",
44ffffcd15SAlexey Brodkin 	      (unsigned int)re_src, (unsigned int)re_end);
45ffffcd15SAlexey Brodkin 
4622723828SAlexey Brodkin 	Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
4722723828SAlexey Brodkin 	Elf32_Addr *offset_ptr_ram;
4822723828SAlexey Brodkin 
4922723828SAlexey Brodkin 	do {
5022723828SAlexey Brodkin 		/* Get the location from the relocation entry */
5122723828SAlexey Brodkin 		offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
5222723828SAlexey Brodkin 
5322723828SAlexey Brodkin 		/* Check that the location of the relocation is in .text */
541c91a3d9SAlexey Brodkin 		if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
5522723828SAlexey Brodkin 		    offset_ptr_rom > last_offset) {
5622723828SAlexey Brodkin 			unsigned int val;
5722723828SAlexey Brodkin 			/* Switch to the in-RAM version */
5822723828SAlexey Brodkin 			offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
5922723828SAlexey Brodkin 							gd->reloc_off);
6022723828SAlexey Brodkin 
61ffffcd15SAlexey Brodkin 			debug("Patching value @ %08x (relocated to %08x)\n",
62ffffcd15SAlexey Brodkin 			      (unsigned int)offset_ptr_rom,
63ffffcd15SAlexey Brodkin 			      (unsigned int)offset_ptr_ram);
64ffffcd15SAlexey Brodkin 
6522723828SAlexey Brodkin 			/*
6622723828SAlexey Brodkin 			 * Use "memcpy" because target location might be
6722723828SAlexey Brodkin 			 * 16-bit aligned on ARC so we may need to read
6822723828SAlexey Brodkin 			 * byte-by-byte. On attempt to read entire word by
6922723828SAlexey Brodkin 			 * CPU throws an exception
7022723828SAlexey Brodkin 			 */
7122723828SAlexey Brodkin 			memcpy(&val, offset_ptr_ram, sizeof(int));
7222723828SAlexey Brodkin 
7336ae5cd2SAlexey Brodkin #ifdef __LITTLE_ENDIAN__
7422723828SAlexey Brodkin 			/* If location in ".text" section swap value */
7522723828SAlexey Brodkin 			if ((unsigned int)offset_ptr_rom <
7620a58ac0SIgor Guryanov 			    (unsigned int)&__ivt_end)
7722723828SAlexey Brodkin 				val = (val << 16) | (val >> 16);
7836ae5cd2SAlexey Brodkin #endif
7922723828SAlexey Brodkin 
801c91a3d9SAlexey Brodkin 			/* Check that the target points into executable */
811c91a3d9SAlexey Brodkin 			if (val >= (unsigned int)&__image_copy_start && val <=
821c91a3d9SAlexey Brodkin 			    (unsigned int)&__image_copy_end) {
8322723828SAlexey Brodkin 				val += gd->reloc_off;
8436ae5cd2SAlexey Brodkin #ifdef __LITTLE_ENDIAN__
8522723828SAlexey Brodkin 				/* If location in ".text" section swap value */
8622723828SAlexey Brodkin 				if ((unsigned int)offset_ptr_rom <
8720a58ac0SIgor Guryanov 				    (unsigned int)&__ivt_end)
8822723828SAlexey Brodkin 					val = (val << 16) | (val >> 16);
8936ae5cd2SAlexey Brodkin #endif
9022723828SAlexey Brodkin 				memcpy(offset_ptr_ram, &val, sizeof(int));
9122723828SAlexey Brodkin 			}
9222723828SAlexey Brodkin 		}
9322723828SAlexey Brodkin 		last_offset = offset_ptr_rom;
9422723828SAlexey Brodkin 
9522723828SAlexey Brodkin 	} while (++re_src < re_end);
9622723828SAlexey Brodkin 
9722723828SAlexey Brodkin 	return 0;
9822723828SAlexey Brodkin }
99