xref: /rk3399_rockchip-uboot/arch/mips/lib/reloc.c (revision 0ddc9c172279f686077310fbcfa5baf10c719188)
1*703ec9ddSPaul Burton /*
2*703ec9ddSPaul Burton  * MIPS Relocation
3*703ec9ddSPaul Burton  *
4*703ec9ddSPaul Burton  * Copyright (c) 2017 Imagination Technologies Ltd.
5*703ec9ddSPaul Burton  *
6*703ec9ddSPaul Burton  * SPDX-License-Identifier:	GPL-2.0+
7*703ec9ddSPaul Burton  *
8*703ec9ddSPaul Burton  * Relocation data, found in the .rel section, is generated by the mips-relocs
9*703ec9ddSPaul Burton  * tool & contains a record of all locations in the U-Boot binary that need to
10*703ec9ddSPaul Burton  * be fixed up during relocation.
11*703ec9ddSPaul Burton  *
12*703ec9ddSPaul Burton  * The data is a sequence of unsigned integers, which are of somewhat arbitrary
13*703ec9ddSPaul Burton  * size. This is achieved by encoding integers as a sequence of bytes, each of
14*703ec9ddSPaul Burton  * which contains 7 bits of data with the most significant bit indicating
15*703ec9ddSPaul Burton  * whether any further bytes need to be read. The least significant bits of the
16*703ec9ddSPaul Burton  * integer are found in the first byte - ie. it somewhat resembles little
17*703ec9ddSPaul Burton  * endian.
18*703ec9ddSPaul Burton  *
19*703ec9ddSPaul Burton  * Each pair of two integers represents a relocation that must be applied. The
20*703ec9ddSPaul Burton  * first integer represents the type of relocation as a standard ELF relocation
21*703ec9ddSPaul Burton  * type (ie. R_MIPS_*). The second integer represents the offset at which to
22*703ec9ddSPaul Burton  * apply the relocation, relative to the previous relocation or for the first
23*703ec9ddSPaul Burton  * relocation the start of the relocated .text section.
24*703ec9ddSPaul Burton  *
25*703ec9ddSPaul Burton  * The end of the relocation data is indicated when type R_MIPS_NONE (0) is
26*703ec9ddSPaul Burton  * read, at which point no further integers should be read. That is, the
27*703ec9ddSPaul Burton  * terminating R_MIPS_NONE reloc includes no offset.
28*703ec9ddSPaul Burton  */
29*703ec9ddSPaul Burton 
30*703ec9ddSPaul Burton #include <common.h>
31*703ec9ddSPaul Burton #include <asm/relocs.h>
32*703ec9ddSPaul Burton #include <asm/sections.h>
33*703ec9ddSPaul Burton 
34*703ec9ddSPaul Burton /**
35*703ec9ddSPaul Burton  * read_uint() - Read an unsigned integer from the buffer
36*703ec9ddSPaul Burton  * @buf: pointer to a pointer to the reloc buffer
37*703ec9ddSPaul Burton  *
38*703ec9ddSPaul Burton  * Read one whole unsigned integer from the relocation data pointed to by @buf,
39*703ec9ddSPaul Burton  * advancing @buf past the bytes encoding the integer.
40*703ec9ddSPaul Burton  *
41*703ec9ddSPaul Burton  * Returns: the integer read from @buf
42*703ec9ddSPaul Burton  */
read_uint(uint8_t ** buf)43*703ec9ddSPaul Burton static unsigned long read_uint(uint8_t **buf)
44*703ec9ddSPaul Burton {
45*703ec9ddSPaul Burton 	unsigned long val = 0;
46*703ec9ddSPaul Burton 	unsigned int shift = 0;
47*703ec9ddSPaul Burton 	uint8_t new;
48*703ec9ddSPaul Burton 
49*703ec9ddSPaul Burton 	do {
50*703ec9ddSPaul Burton 		new = *(*buf)++;
51*703ec9ddSPaul Burton 		val |= (new & 0x7f) << shift;
52*703ec9ddSPaul Burton 		shift += 7;
53*703ec9ddSPaul Burton 	} while (new & 0x80);
54*703ec9ddSPaul Burton 
55*703ec9ddSPaul Burton 	return val;
56*703ec9ddSPaul Burton }
57*703ec9ddSPaul Burton 
58*703ec9ddSPaul Burton /**
59*703ec9ddSPaul Burton  * apply_reloc() - Apply a single relocation
60*703ec9ddSPaul Burton  * @type: the type of reloc (R_MIPS_*)
61*703ec9ddSPaul Burton  * @addr: the address that the reloc should be applied to
62*703ec9ddSPaul Burton  * @off: the relocation offset, ie. number of bytes we're moving U-Boot by
63*703ec9ddSPaul Burton  *
64*703ec9ddSPaul Burton  * Apply a single relocation of type @type at @addr. This function is
65*703ec9ddSPaul Burton  * intentionally simple, and does the bare minimum needed to fixup the
66*703ec9ddSPaul Burton  * relocated U-Boot - in particular, it does not check for overflows.
67*703ec9ddSPaul Burton  */
apply_reloc(unsigned int type,void * addr,long off)68*703ec9ddSPaul Burton static void apply_reloc(unsigned int type, void *addr, long off)
69*703ec9ddSPaul Burton {
70*703ec9ddSPaul Burton 	uint32_t u32;
71*703ec9ddSPaul Burton 
72*703ec9ddSPaul Burton 	switch (type) {
73*703ec9ddSPaul Burton 	case R_MIPS_26:
74*703ec9ddSPaul Burton 		u32 = *(uint32_t *)addr;
75*703ec9ddSPaul Burton 		u32 = (u32 & GENMASK(31, 26)) |
76*703ec9ddSPaul Burton 		      ((u32 + (off >> 2)) & GENMASK(25, 0));
77*703ec9ddSPaul Burton 		*(uint32_t *)addr = u32;
78*703ec9ddSPaul Burton 		break;
79*703ec9ddSPaul Burton 
80*703ec9ddSPaul Burton 	case R_MIPS_32:
81*703ec9ddSPaul Burton 		*(uint32_t *)addr += off;
82*703ec9ddSPaul Burton 		break;
83*703ec9ddSPaul Burton 
84*703ec9ddSPaul Burton 	case R_MIPS_64:
85*703ec9ddSPaul Burton 		*(uint64_t *)addr += off;
86*703ec9ddSPaul Burton 		break;
87*703ec9ddSPaul Burton 
88*703ec9ddSPaul Burton 	case R_MIPS_HI16:
89*703ec9ddSPaul Burton 		*(uint32_t *)addr += off >> 16;
90*703ec9ddSPaul Burton 		break;
91*703ec9ddSPaul Burton 
92*703ec9ddSPaul Burton 	default:
93*703ec9ddSPaul Burton 		panic("Unhandled reloc type %u\n", type);
94*703ec9ddSPaul Burton 	}
95*703ec9ddSPaul Burton }
96*703ec9ddSPaul Burton 
97*703ec9ddSPaul Burton /**
98*703ec9ddSPaul Burton  * relocate_code() - Relocate U-Boot, generally from flash to DDR
99*703ec9ddSPaul Burton  * @start_addr_sp: new stack pointer
100*703ec9ddSPaul Burton  * @new_gd: pointer to relocated global data
101*703ec9ddSPaul Burton  * @relocaddr: the address to relocate to
102*703ec9ddSPaul Burton  *
103*703ec9ddSPaul Burton  * Relocate U-Boot from its current location (generally in flash) to a new one
104*703ec9ddSPaul Burton  * (generally in DDR). This function will copy the U-Boot binary & apply
105*703ec9ddSPaul Burton  * relocations as necessary, then jump to board_init_r in the new build of
106*703ec9ddSPaul Burton  * U-Boot. As such, this function does not return.
107*703ec9ddSPaul Burton  */
relocate_code(ulong start_addr_sp,gd_t * new_gd,ulong relocaddr)108*703ec9ddSPaul Burton void relocate_code(ulong start_addr_sp, gd_t *new_gd, ulong relocaddr)
109*703ec9ddSPaul Burton {
110*703ec9ddSPaul Burton 	unsigned long addr, length, bss_len;
111*703ec9ddSPaul Burton 	uint8_t *buf, *bss_start;
112*703ec9ddSPaul Burton 	unsigned int type;
113*703ec9ddSPaul Burton 	long off;
114*703ec9ddSPaul Burton 
115*703ec9ddSPaul Burton 	/*
116*703ec9ddSPaul Burton 	 * Ensure that we're relocating by an offset which is a multiple of
117*703ec9ddSPaul Burton 	 * 64KiB, ie. doesn't change the least significant 16 bits of any
118*703ec9ddSPaul Burton 	 * addresses. This allows us to discard R_MIPS_LO16 relocs, saving
119*703ec9ddSPaul Burton 	 * space in the U-Boot binary & complexity in handling them.
120*703ec9ddSPaul Burton 	 */
121*703ec9ddSPaul Burton 	off = relocaddr - (unsigned long)__text_start;
122*703ec9ddSPaul Burton 	if (off & 0xffff)
123*703ec9ddSPaul Burton 		panic("Mis-aligned relocation\n");
124*703ec9ddSPaul Burton 
125*703ec9ddSPaul Burton 	/* Copy U-Boot to RAM */
126*703ec9ddSPaul Burton 	length = __image_copy_end - __text_start;
127*703ec9ddSPaul Burton 	memcpy((void *)relocaddr, __text_start, length);
128*703ec9ddSPaul Burton 
129*703ec9ddSPaul Burton 	/* Now apply relocations to the copy in RAM */
130*703ec9ddSPaul Burton 	buf = __rel_start;
131*703ec9ddSPaul Burton 	addr = relocaddr;
132*703ec9ddSPaul Burton 	while (true) {
133*703ec9ddSPaul Burton 		type = read_uint(&buf);
134*703ec9ddSPaul Burton 		if (type == R_MIPS_NONE)
135*703ec9ddSPaul Burton 			break;
136*703ec9ddSPaul Burton 
137*703ec9ddSPaul Burton 		addr += read_uint(&buf) << 2;
138*703ec9ddSPaul Burton 		apply_reloc(type, (void *)addr, off);
139*703ec9ddSPaul Burton 	}
140*703ec9ddSPaul Burton 
141*703ec9ddSPaul Burton 	/* Ensure the icache is coherent */
142*703ec9ddSPaul Burton 	flush_cache(relocaddr, length);
143*703ec9ddSPaul Burton 
144*703ec9ddSPaul Burton 	/* Clear the .bss section */
145*703ec9ddSPaul Burton 	bss_start = (uint8_t *)((unsigned long)__bss_start + off);
146*703ec9ddSPaul Burton 	bss_len = (unsigned long)&__bss_end - (unsigned long)__bss_start;
147*703ec9ddSPaul Burton 	memset(bss_start, 0, bss_len);
148*703ec9ddSPaul Burton 
149*703ec9ddSPaul Burton 	/* Jump to the relocated U-Boot */
150*703ec9ddSPaul Burton 	asm volatile(
151*703ec9ddSPaul Burton 		       "move	$29, %0\n"
152*703ec9ddSPaul Burton 		"	move	$4, %1\n"
153*703ec9ddSPaul Burton 		"	move	$5, %2\n"
154*703ec9ddSPaul Burton 		"	move	$31, $0\n"
155*703ec9ddSPaul Burton 		"	jr	%3"
156*703ec9ddSPaul Burton 		: /* no outputs */
157*703ec9ddSPaul Burton 		: "r"(start_addr_sp),
158*703ec9ddSPaul Burton 		  "r"(new_gd),
159*703ec9ddSPaul Burton 		  "r"(relocaddr),
160*703ec9ddSPaul Burton 		  "r"((unsigned long)board_init_r + off));
161*703ec9ddSPaul Burton 
162*703ec9ddSPaul Burton 	/* Since we jumped to the new U-Boot above, we won't get here */
163*703ec9ddSPaul Burton 	unreachable();
164*703ec9ddSPaul Burton }
165