xref: /openbmc/u-boot/arch/mips/lib/reloc.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2703ec9ddSPaul Burton /*
3703ec9ddSPaul Burton  * MIPS Relocation
4703ec9ddSPaul Burton  *
5703ec9ddSPaul Burton  * Copyright (c) 2017 Imagination Technologies Ltd.
6703ec9ddSPaul Burton  *
7703ec9ddSPaul Burton  * Relocation data, found in the .rel section, is generated by the mips-relocs
8703ec9ddSPaul Burton  * tool & contains a record of all locations in the U-Boot binary that need to
9703ec9ddSPaul Burton  * be fixed up during relocation.
10703ec9ddSPaul Burton  *
11703ec9ddSPaul Burton  * The data is a sequence of unsigned integers, which are of somewhat arbitrary
12703ec9ddSPaul Burton  * size. This is achieved by encoding integers as a sequence of bytes, each of
13703ec9ddSPaul Burton  * which contains 7 bits of data with the most significant bit indicating
14703ec9ddSPaul Burton  * whether any further bytes need to be read. The least significant bits of the
15703ec9ddSPaul Burton  * integer are found in the first byte - ie. it somewhat resembles little
16703ec9ddSPaul Burton  * endian.
17703ec9ddSPaul Burton  *
18703ec9ddSPaul Burton  * Each pair of two integers represents a relocation that must be applied. The
19703ec9ddSPaul Burton  * first integer represents the type of relocation as a standard ELF relocation
20703ec9ddSPaul Burton  * type (ie. R_MIPS_*). The second integer represents the offset at which to
21703ec9ddSPaul Burton  * apply the relocation, relative to the previous relocation or for the first
22703ec9ddSPaul Burton  * relocation the start of the relocated .text section.
23703ec9ddSPaul Burton  *
24703ec9ddSPaul Burton  * The end of the relocation data is indicated when type R_MIPS_NONE (0) is
25703ec9ddSPaul Burton  * read, at which point no further integers should be read. That is, the
26703ec9ddSPaul Burton  * terminating R_MIPS_NONE reloc includes no offset.
27703ec9ddSPaul Burton  */
28703ec9ddSPaul Burton 
29703ec9ddSPaul Burton #include <common.h>
30703ec9ddSPaul Burton #include <asm/relocs.h>
31703ec9ddSPaul Burton #include <asm/sections.h>
32703ec9ddSPaul Burton 
33703ec9ddSPaul Burton /**
34703ec9ddSPaul Burton  * read_uint() - Read an unsigned integer from the buffer
35703ec9ddSPaul Burton  * @buf: pointer to a pointer to the reloc buffer
36703ec9ddSPaul Burton  *
37703ec9ddSPaul Burton  * Read one whole unsigned integer from the relocation data pointed to by @buf,
38703ec9ddSPaul Burton  * advancing @buf past the bytes encoding the integer.
39703ec9ddSPaul Burton  *
40703ec9ddSPaul Burton  * Returns: the integer read from @buf
41703ec9ddSPaul Burton  */
read_uint(uint8_t ** buf)42703ec9ddSPaul Burton static unsigned long read_uint(uint8_t **buf)
43703ec9ddSPaul Burton {
44703ec9ddSPaul Burton 	unsigned long val = 0;
45703ec9ddSPaul Burton 	unsigned int shift = 0;
46703ec9ddSPaul Burton 	uint8_t new;
47703ec9ddSPaul Burton 
48703ec9ddSPaul Burton 	do {
49703ec9ddSPaul Burton 		new = *(*buf)++;
50703ec9ddSPaul Burton 		val |= (new & 0x7f) << shift;
51703ec9ddSPaul Burton 		shift += 7;
52703ec9ddSPaul Burton 	} while (new & 0x80);
53703ec9ddSPaul Burton 
54703ec9ddSPaul Burton 	return val;
55703ec9ddSPaul Burton }
56703ec9ddSPaul Burton 
57703ec9ddSPaul Burton /**
58703ec9ddSPaul Burton  * apply_reloc() - Apply a single relocation
59703ec9ddSPaul Burton  * @type: the type of reloc (R_MIPS_*)
60703ec9ddSPaul Burton  * @addr: the address that the reloc should be applied to
61703ec9ddSPaul Burton  * @off: the relocation offset, ie. number of bytes we're moving U-Boot by
62703ec9ddSPaul Burton  *
63703ec9ddSPaul Burton  * Apply a single relocation of type @type at @addr. This function is
64703ec9ddSPaul Burton  * intentionally simple, and does the bare minimum needed to fixup the
65703ec9ddSPaul Burton  * relocated U-Boot - in particular, it does not check for overflows.
66703ec9ddSPaul Burton  */
apply_reloc(unsigned int type,void * addr,long off)67703ec9ddSPaul Burton static void apply_reloc(unsigned int type, void *addr, long off)
68703ec9ddSPaul Burton {
69703ec9ddSPaul Burton 	uint32_t u32;
70703ec9ddSPaul Burton 
71703ec9ddSPaul Burton 	switch (type) {
72703ec9ddSPaul Burton 	case R_MIPS_26:
73703ec9ddSPaul Burton 		u32 = *(uint32_t *)addr;
74703ec9ddSPaul Burton 		u32 = (u32 & GENMASK(31, 26)) |
75703ec9ddSPaul Burton 		      ((u32 + (off >> 2)) & GENMASK(25, 0));
76703ec9ddSPaul Burton 		*(uint32_t *)addr = u32;
77703ec9ddSPaul Burton 		break;
78703ec9ddSPaul Burton 
79703ec9ddSPaul Burton 	case R_MIPS_32:
80703ec9ddSPaul Burton 		*(uint32_t *)addr += off;
81703ec9ddSPaul Burton 		break;
82703ec9ddSPaul Burton 
83703ec9ddSPaul Burton 	case R_MIPS_64:
84703ec9ddSPaul Burton 		*(uint64_t *)addr += off;
85703ec9ddSPaul Burton 		break;
86703ec9ddSPaul Burton 
87703ec9ddSPaul Burton 	case R_MIPS_HI16:
88703ec9ddSPaul Burton 		*(uint32_t *)addr += off >> 16;
89703ec9ddSPaul Burton 		break;
90703ec9ddSPaul Burton 
91703ec9ddSPaul Burton 	default:
92703ec9ddSPaul Burton 		panic("Unhandled reloc type %u\n", type);
93703ec9ddSPaul Burton 	}
94703ec9ddSPaul Burton }
95703ec9ddSPaul Burton 
96703ec9ddSPaul Burton /**
97703ec9ddSPaul Burton  * relocate_code() - Relocate U-Boot, generally from flash to DDR
98703ec9ddSPaul Burton  * @start_addr_sp: new stack pointer
99703ec9ddSPaul Burton  * @new_gd: pointer to relocated global data
100703ec9ddSPaul Burton  * @relocaddr: the address to relocate to
101703ec9ddSPaul Burton  *
102703ec9ddSPaul Burton  * Relocate U-Boot from its current location (generally in flash) to a new one
103703ec9ddSPaul Burton  * (generally in DDR). This function will copy the U-Boot binary & apply
104703ec9ddSPaul Burton  * relocations as necessary, then jump to board_init_r in the new build of
105703ec9ddSPaul Burton  * U-Boot. As such, this function does not return.
106703ec9ddSPaul Burton  */
relocate_code(ulong start_addr_sp,gd_t * new_gd,ulong relocaddr)107703ec9ddSPaul Burton void relocate_code(ulong start_addr_sp, gd_t *new_gd, ulong relocaddr)
108703ec9ddSPaul Burton {
109703ec9ddSPaul Burton 	unsigned long addr, length, bss_len;
110703ec9ddSPaul Burton 	uint8_t *buf, *bss_start;
111703ec9ddSPaul Burton 	unsigned int type;
112703ec9ddSPaul Burton 	long off;
113703ec9ddSPaul Burton 
114703ec9ddSPaul Burton 	/*
115703ec9ddSPaul Burton 	 * Ensure that we're relocating by an offset which is a multiple of
116703ec9ddSPaul Burton 	 * 64KiB, ie. doesn't change the least significant 16 bits of any
117703ec9ddSPaul Burton 	 * addresses. This allows us to discard R_MIPS_LO16 relocs, saving
118703ec9ddSPaul Burton 	 * space in the U-Boot binary & complexity in handling them.
119703ec9ddSPaul Burton 	 */
120703ec9ddSPaul Burton 	off = relocaddr - (unsigned long)__text_start;
121703ec9ddSPaul Burton 	if (off & 0xffff)
122703ec9ddSPaul Burton 		panic("Mis-aligned relocation\n");
123703ec9ddSPaul Burton 
124703ec9ddSPaul Burton 	/* Copy U-Boot to RAM */
125703ec9ddSPaul Burton 	length = __image_copy_end - __text_start;
126703ec9ddSPaul Burton 	memcpy((void *)relocaddr, __text_start, length);
127703ec9ddSPaul Burton 
128703ec9ddSPaul Burton 	/* Now apply relocations to the copy in RAM */
129703ec9ddSPaul Burton 	buf = __rel_start;
130703ec9ddSPaul Burton 	addr = relocaddr;
131703ec9ddSPaul Burton 	while (true) {
132703ec9ddSPaul Burton 		type = read_uint(&buf);
133703ec9ddSPaul Burton 		if (type == R_MIPS_NONE)
134703ec9ddSPaul Burton 			break;
135703ec9ddSPaul Burton 
136703ec9ddSPaul Burton 		addr += read_uint(&buf) << 2;
137703ec9ddSPaul Burton 		apply_reloc(type, (void *)addr, off);
138703ec9ddSPaul Burton 	}
139703ec9ddSPaul Burton 
140703ec9ddSPaul Burton 	/* Ensure the icache is coherent */
141703ec9ddSPaul Burton 	flush_cache(relocaddr, length);
142703ec9ddSPaul Burton 
143703ec9ddSPaul Burton 	/* Clear the .bss section */
144703ec9ddSPaul Burton 	bss_start = (uint8_t *)((unsigned long)__bss_start + off);
145703ec9ddSPaul Burton 	bss_len = (unsigned long)&__bss_end - (unsigned long)__bss_start;
146703ec9ddSPaul Burton 	memset(bss_start, 0, bss_len);
147703ec9ddSPaul Burton 
148703ec9ddSPaul Burton 	/* Jump to the relocated U-Boot */
149703ec9ddSPaul Burton 	asm volatile(
150703ec9ddSPaul Burton 		       "move	$29, %0\n"
151703ec9ddSPaul Burton 		"	move	$4, %1\n"
152703ec9ddSPaul Burton 		"	move	$5, %2\n"
153703ec9ddSPaul Burton 		"	move	$31, $0\n"
154703ec9ddSPaul Burton 		"	jr	%3"
155703ec9ddSPaul Burton 		: /* no outputs */
156703ec9ddSPaul Burton 		: "r"(start_addr_sp),
157703ec9ddSPaul Burton 		  "r"(new_gd),
158703ec9ddSPaul Burton 		  "r"(relocaddr),
159703ec9ddSPaul Burton 		  "r"((unsigned long)board_init_r + off));
160703ec9ddSPaul Burton 
161703ec9ddSPaul Burton 	/* Since we jumped to the new U-Boot above, we won't get here */
162703ec9ddSPaul Burton 	unreachable();
163703ec9ddSPaul Burton }
164