relocate.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * (C) Copyright 2008-2011
  3. * Graeme Russ, <graeme.russ@gmail.com>
  4. *
  5. * (C) Copyright 2002
  6. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  7. *
  8. * (C) Copyright 2002
  9. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Marius Groeger <mgroeger@sysgo.de>
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  17. #include <common.h>
  18. #include <inttypes.h>
  19. #include <asm/u-boot-x86.h>
  20. #include <asm/relocate.h>
  21. #include <asm/sections.h>
  22. #include <elf.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. int copy_uboot_to_ram(void)
  25. {
  26. size_t len = (size_t)&__data_end - (size_t)&__text_start;
  27. memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
  28. return 0;
  29. }
  30. int clear_bss(void)
  31. {
  32. ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
  33. size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
  34. memset((void *)dst_addr, 0x00, len);
  35. return 0;
  36. }
  37. /*
  38. * This function has more error checking than you might expect. Please see
  39. * the commit message for more informaiton.
  40. */
  41. int do_elf_reloc_fixups(void)
  42. {
  43. Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
  44. Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
  45. Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
  46. Elf32_Addr *offset_ptr_ram;
  47. /* The size of the region of u-boot that runs out of RAM. */
  48. uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
  49. if (re_src == re_end)
  50. panic("No relocation data");
  51. do {
  52. /* Get the location from the relocation entry */
  53. offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
  54. /* Check that the location of the relocation is in .text */
  55. if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE &&
  56. offset_ptr_rom > last_offset) {
  57. /* Switch to the in-RAM version */
  58. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  59. gd->reloc_off);
  60. /* Check that the target points into .text */
  61. if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
  62. *offset_ptr_ram <=
  63. (CONFIG_SYS_TEXT_BASE + size)) {
  64. *offset_ptr_ram += gd->reloc_off;
  65. } else {
  66. debug(" %p: rom reloc %x, ram %p, value %x,"
  67. " limit %" PRIXPTR "\n", re_src,
  68. re_src->r_offset, offset_ptr_ram,
  69. *offset_ptr_ram,
  70. CONFIG_SYS_TEXT_BASE + size);
  71. }
  72. } else {
  73. debug(" %p: rom reloc %x, last %p\n", re_src,
  74. re_src->r_offset, last_offset);
  75. }
  76. last_offset = offset_ptr_rom;
  77. } while (++re_src < re_end);
  78. return 0;
  79. }