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