blob: 67c8af2f35a5722a1facc7126f1b2017bb0f92f5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Paul Burton703ec9d2017-06-19 11:53:47 -07002/*
3 * MIPS Relocation
4 *
5 * Copyright (c) 2017 Imagination Technologies Ltd.
6 *
Paul Burton703ec9d2017-06-19 11:53:47 -07007 * 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>
Simon Glass1eb69ae2019-11-14 12:57:39 -070030#include <cpu_func.h>
Simon Glass94133872019-12-28 10:44:45 -070031#include <init.h>
Paul Burton703ec9d2017-06-19 11:53:47 -070032#include <asm/relocs.h>
33#include <asm/sections.h>
Simon Glasscd93d622020-05-10 11:40:13 -060034#include <linux/bitops.h>
Paul Burton703ec9d2017-06-19 11:53:47 -070035
36/**
37 * read_uint() - Read an unsigned integer from the buffer
38 * @buf: pointer to a pointer to the reloc buffer
39 *
40 * Read one whole unsigned integer from the relocation data pointed to by @buf,
41 * advancing @buf past the bytes encoding the integer.
42 *
43 * Returns: the integer read from @buf
44 */
45static unsigned long read_uint(uint8_t **buf)
46{
47 unsigned long val = 0;
48 unsigned int shift = 0;
49 uint8_t new;
50
51 do {
52 new = *(*buf)++;
53 val |= (new & 0x7f) << shift;
54 shift += 7;
55 } while (new & 0x80);
56
57 return val;
58}
59
60/**
61 * apply_reloc() - Apply a single relocation
62 * @type: the type of reloc (R_MIPS_*)
63 * @addr: the address that the reloc should be applied to
64 * @off: the relocation offset, ie. number of bytes we're moving U-Boot by
65 *
66 * Apply a single relocation of type @type at @addr. This function is
67 * intentionally simple, and does the bare minimum needed to fixup the
68 * relocated U-Boot - in particular, it does not check for overflows.
69 */
Stefan Roesec3728132020-06-05 10:29:25 +020070static void apply_reloc(unsigned int type, void *addr, long off, uint8_t *buf)
Paul Burton703ec9d2017-06-19 11:53:47 -070071{
72 uint32_t u32;
73
74 switch (type) {
75 case R_MIPS_26:
76 u32 = *(uint32_t *)addr;
77 u32 = (u32 & GENMASK(31, 26)) |
78 ((u32 + (off >> 2)) & GENMASK(25, 0));
79 *(uint32_t *)addr = u32;
80 break;
81
82 case R_MIPS_32:
83 *(uint32_t *)addr += off;
84 break;
85
86 case R_MIPS_64:
87 *(uint64_t *)addr += off;
88 break;
89
90 case R_MIPS_HI16:
91 *(uint32_t *)addr += off >> 16;
92 break;
93
94 default:
Stefan Roesec3728132020-06-05 10:29:25 +020095 panic("Unhandled reloc type %u (@ %p), bss used before relocation?\n",
96 type, buf);
Paul Burton703ec9d2017-06-19 11:53:47 -070097 }
98}
99
100/**
101 * relocate_code() - Relocate U-Boot, generally from flash to DDR
102 * @start_addr_sp: new stack pointer
103 * @new_gd: pointer to relocated global data
104 * @relocaddr: the address to relocate to
105 *
106 * Relocate U-Boot from its current location (generally in flash) to a new one
107 * (generally in DDR). This function will copy the U-Boot binary & apply
108 * relocations as necessary, then jump to board_init_r in the new build of
109 * U-Boot. As such, this function does not return.
110 */
111void relocate_code(ulong start_addr_sp, gd_t *new_gd, ulong relocaddr)
112{
113 unsigned long addr, length, bss_len;
114 uint8_t *buf, *bss_start;
115 unsigned int type;
116 long off;
117
118 /*
119 * Ensure that we're relocating by an offset which is a multiple of
120 * 64KiB, ie. doesn't change the least significant 16 bits of any
121 * addresses. This allows us to discard R_MIPS_LO16 relocs, saving
122 * space in the U-Boot binary & complexity in handling them.
123 */
124 off = relocaddr - (unsigned long)__text_start;
125 if (off & 0xffff)
126 panic("Mis-aligned relocation\n");
127
128 /* Copy U-Boot to RAM */
129 length = __image_copy_end - __text_start;
130 memcpy((void *)relocaddr, __text_start, length);
131
132 /* Now apply relocations to the copy in RAM */
133 buf = __rel_start;
134 addr = relocaddr;
135 while (true) {
136 type = read_uint(&buf);
137 if (type == R_MIPS_NONE)
138 break;
139
140 addr += read_uint(&buf) << 2;
Stefan Roesec3728132020-06-05 10:29:25 +0200141 apply_reloc(type, (void *)addr, off, buf);
Paul Burton703ec9d2017-06-19 11:53:47 -0700142 }
143
144 /* Ensure the icache is coherent */
145 flush_cache(relocaddr, length);
146
147 /* Clear the .bss section */
148 bss_start = (uint8_t *)((unsigned long)__bss_start + off);
149 bss_len = (unsigned long)&__bss_end - (unsigned long)__bss_start;
150 memset(bss_start, 0, bss_len);
151
152 /* Jump to the relocated U-Boot */
153 asm volatile(
154 "move $29, %0\n"
155 " move $4, %1\n"
156 " move $5, %2\n"
157 " move $31, $0\n"
158 " jr %3"
159 : /* no outputs */
160 : "r"(start_addr_sp),
161 "r"(new_gd),
162 "r"(relocaddr),
163 "r"((unsigned long)board_init_r + off));
164
165 /* Since we jumped to the new U-Boot above, we won't get here */
166 unreachable();
167}