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