reloc.c 4.7 KB

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