reloc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <common.h>
  29. #include <asm/relocs.h>
  30. #include <asm/sections.h>
  31. /**
  32. * read_uint() - Read an unsigned integer from the buffer
  33. * @buf: pointer to a pointer to the reloc buffer
  34. *
  35. * Read one whole unsigned integer from the relocation data pointed to by @buf,
  36. * advancing @buf past the bytes encoding the integer.
  37. *
  38. * Returns: the integer read from @buf
  39. */
  40. static unsigned long read_uint(uint8_t **buf)
  41. {
  42. unsigned long val = 0;
  43. unsigned int shift = 0;
  44. uint8_t new;
  45. do {
  46. new = *(*buf)++;
  47. val |= (new & 0x7f) << shift;
  48. shift += 7;
  49. } while (new & 0x80);
  50. return val;
  51. }
  52. /**
  53. * apply_reloc() - Apply a single relocation
  54. * @type: the type of reloc (R_MIPS_*)
  55. * @addr: the address that the reloc should be applied to
  56. * @off: the relocation offset, ie. number of bytes we're moving U-Boot by
  57. *
  58. * Apply a single relocation of type @type at @addr. This function is
  59. * intentionally simple, and does the bare minimum needed to fixup the
  60. * relocated U-Boot - in particular, it does not check for overflows.
  61. */
  62. static void apply_reloc(unsigned int type, void *addr, long off)
  63. {
  64. uint32_t u32;
  65. switch (type) {
  66. case R_MIPS_26:
  67. u32 = *(uint32_t *)addr;
  68. u32 = (u32 & GENMASK(31, 26)) |
  69. ((u32 + (off >> 2)) & GENMASK(25, 0));
  70. *(uint32_t *)addr = u32;
  71. break;
  72. case R_MIPS_32:
  73. *(uint32_t *)addr += off;
  74. break;
  75. case R_MIPS_64:
  76. *(uint64_t *)addr += off;
  77. break;
  78. case R_MIPS_HI16:
  79. *(uint32_t *)addr += off >> 16;
  80. break;
  81. default:
  82. panic("Unhandled reloc type %u\n", type);
  83. }
  84. }
  85. /**
  86. * relocate_code() - Relocate U-Boot, generally from flash to DDR
  87. * @start_addr_sp: new stack pointer
  88. * @new_gd: pointer to relocated global data
  89. * @relocaddr: the address to relocate to
  90. *
  91. * Relocate U-Boot from its current location (generally in flash) to a new one
  92. * (generally in DDR). This function will copy the U-Boot binary & apply
  93. * relocations as necessary, then jump to board_init_r in the new build of
  94. * U-Boot. As such, this function does not return.
  95. */
  96. void relocate_code(ulong start_addr_sp, gd_t *new_gd, ulong relocaddr)
  97. {
  98. unsigned long addr, length, bss_len;
  99. uint8_t *buf, *bss_start;
  100. unsigned int type;
  101. long off;
  102. /*
  103. * Ensure that we're relocating by an offset which is a multiple of
  104. * 64KiB, ie. doesn't change the least significant 16 bits of any
  105. * addresses. This allows us to discard R_MIPS_LO16 relocs, saving
  106. * space in the U-Boot binary & complexity in handling them.
  107. */
  108. off = relocaddr - (unsigned long)__text_start;
  109. if (off & 0xffff)
  110. panic("Mis-aligned relocation\n");
  111. /* Copy U-Boot to RAM */
  112. length = __image_copy_end - __text_start;
  113. memcpy((void *)relocaddr, __text_start, length);
  114. /* Now apply relocations to the copy in RAM */
  115. buf = __rel_start;
  116. addr = relocaddr;
  117. while (true) {
  118. type = read_uint(&buf);
  119. if (type == R_MIPS_NONE)
  120. break;
  121. addr += read_uint(&buf) << 2;
  122. apply_reloc(type, (void *)addr, off);
  123. }
  124. /* Ensure the icache is coherent */
  125. flush_cache(relocaddr, length);
  126. /* Clear the .bss section */
  127. bss_start = (uint8_t *)((unsigned long)__bss_start + off);
  128. bss_len = (unsigned long)&__bss_end - (unsigned long)__bss_start;
  129. memset(bss_start, 0, bss_len);
  130. /* Jump to the relocated U-Boot */
  131. asm volatile(
  132. "move $29, %0\n"
  133. " move $4, %1\n"
  134. " move $5, %2\n"
  135. " move $31, $0\n"
  136. " jr %3"
  137. : /* no outputs */
  138. : "r"(start_addr_sp),
  139. "r"(new_gd),
  140. "r"(relocaddr),
  141. "r"((unsigned long)board_init_r + off));
  142. /* Since we jumped to the new U-Boot above, we won't get here */
  143. unreachable();
  144. }