elf.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. Copyright (c) 2001 William L. Pitts
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <cpu_func.h>
  8. #include <elf.h>
  9. #include <env.h>
  10. #include <net.h>
  11. #include <vxworks.h>
  12. #ifdef CONFIG_X86
  13. #include <vbe.h>
  14. #include <asm/e820.h>
  15. #include <linux/linkage.h>
  16. #endif
  17. /*
  18. * A very simple ELF64 loader, assumes the image is valid, returns the
  19. * entry point address.
  20. *
  21. * Note if U-Boot is 32-bit, the loader assumes the to segment's
  22. * physical address and size is within the lower 32-bit address space.
  23. */
  24. unsigned long load_elf64_image_phdr(unsigned long addr)
  25. {
  26. Elf64_Ehdr *ehdr; /* Elf header structure pointer */
  27. Elf64_Phdr *phdr; /* Program header structure pointer */
  28. int i;
  29. ehdr = (Elf64_Ehdr *)addr;
  30. phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
  31. /* Load each program header */
  32. for (i = 0; i < ehdr->e_phnum; ++i) {
  33. void *dst = (void *)(ulong)phdr->p_paddr;
  34. void *src = (void *)addr + phdr->p_offset;
  35. debug("Loading phdr %i to 0x%p (%lu bytes)\n",
  36. i, dst, (ulong)phdr->p_filesz);
  37. if (phdr->p_filesz)
  38. memcpy(dst, src, phdr->p_filesz);
  39. if (phdr->p_filesz != phdr->p_memsz)
  40. memset(dst + phdr->p_filesz, 0x00,
  41. phdr->p_memsz - phdr->p_filesz);
  42. flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
  43. roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
  44. ++phdr;
  45. }
  46. if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
  47. EF_PPC64_ELFV1_ABI)) {
  48. /*
  49. * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
  50. * descriptor pointer with the first double word being the
  51. * address of the entry point of the function.
  52. */
  53. uintptr_t addr = ehdr->e_entry;
  54. return *(Elf64_Addr *)addr;
  55. }
  56. return ehdr->e_entry;
  57. }
  58. unsigned long load_elf64_image_shdr(unsigned long addr)
  59. {
  60. Elf64_Ehdr *ehdr; /* Elf header structure pointer */
  61. Elf64_Shdr *shdr; /* Section header structure pointer */
  62. unsigned char *strtab = 0; /* String table pointer */
  63. unsigned char *image; /* Binary image pointer */
  64. int i; /* Loop counter */
  65. ehdr = (Elf64_Ehdr *)addr;
  66. /* Find the section header string table for output info */
  67. shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
  68. (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
  69. if (shdr->sh_type == SHT_STRTAB)
  70. strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
  71. /* Load each appropriate section */
  72. for (i = 0; i < ehdr->e_shnum; ++i) {
  73. shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
  74. (i * sizeof(Elf64_Shdr)));
  75. if (!(shdr->sh_flags & SHF_ALLOC) ||
  76. shdr->sh_addr == 0 || shdr->sh_size == 0) {
  77. continue;
  78. }
  79. if (strtab) {
  80. debug("%sing %s @ 0x%08lx (%ld bytes)\n",
  81. (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
  82. &strtab[shdr->sh_name],
  83. (unsigned long)shdr->sh_addr,
  84. (long)shdr->sh_size);
  85. }
  86. if (shdr->sh_type == SHT_NOBITS) {
  87. memset((void *)(uintptr_t)shdr->sh_addr, 0,
  88. shdr->sh_size);
  89. } else {
  90. image = (unsigned char *)addr + (ulong)shdr->sh_offset;
  91. memcpy((void *)(uintptr_t)shdr->sh_addr,
  92. (const void *)image, shdr->sh_size);
  93. }
  94. flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
  95. roundup((shdr->sh_addr + shdr->sh_size),
  96. ARCH_DMA_MINALIGN) -
  97. rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
  98. }
  99. if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
  100. EF_PPC64_ELFV1_ABI)) {
  101. /*
  102. * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
  103. * descriptor pointer with the first double word being the
  104. * address of the entry point of the function.
  105. */
  106. uintptr_t addr = ehdr->e_entry;
  107. return *(Elf64_Addr *)addr;
  108. }
  109. return ehdr->e_entry;
  110. }
  111. /*
  112. * A very simple ELF loader, assumes the image is valid, returns the
  113. * entry point address.
  114. *
  115. * The loader firstly reads the EFI class to see if it's a 64-bit image.
  116. * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
  117. */
  118. unsigned long load_elf_image_phdr(unsigned long addr)
  119. {
  120. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  121. Elf32_Phdr *phdr; /* Program header structure pointer */
  122. int i;
  123. ehdr = (Elf32_Ehdr *)addr;
  124. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  125. return load_elf64_image_phdr(addr);
  126. phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
  127. /* Load each program header */
  128. for (i = 0; i < ehdr->e_phnum; ++i) {
  129. void *dst = (void *)(uintptr_t)phdr->p_paddr;
  130. void *src = (void *)addr + phdr->p_offset;
  131. debug("Loading phdr %i to 0x%p (%i bytes)\n",
  132. i, dst, phdr->p_filesz);
  133. if (phdr->p_filesz)
  134. memcpy(dst, src, phdr->p_filesz);
  135. if (phdr->p_filesz != phdr->p_memsz)
  136. memset(dst + phdr->p_filesz, 0x00,
  137. phdr->p_memsz - phdr->p_filesz);
  138. flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
  139. roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
  140. ++phdr;
  141. }
  142. return ehdr->e_entry;
  143. }
  144. unsigned long load_elf_image_shdr(unsigned long addr)
  145. {
  146. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  147. Elf32_Shdr *shdr; /* Section header structure pointer */
  148. unsigned char *strtab = 0; /* String table pointer */
  149. unsigned char *image; /* Binary image pointer */
  150. int i; /* Loop counter */
  151. ehdr = (Elf32_Ehdr *)addr;
  152. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  153. return load_elf64_image_shdr(addr);
  154. /* Find the section header string table for output info */
  155. shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
  156. (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
  157. if (shdr->sh_type == SHT_STRTAB)
  158. strtab = (unsigned char *)(addr + shdr->sh_offset);
  159. /* Load each appropriate section */
  160. for (i = 0; i < ehdr->e_shnum; ++i) {
  161. shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
  162. (i * sizeof(Elf32_Shdr)));
  163. if (!(shdr->sh_flags & SHF_ALLOC) ||
  164. shdr->sh_addr == 0 || shdr->sh_size == 0) {
  165. continue;
  166. }
  167. if (strtab) {
  168. debug("%sing %s @ 0x%08lx (%ld bytes)\n",
  169. (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
  170. &strtab[shdr->sh_name],
  171. (unsigned long)shdr->sh_addr,
  172. (long)shdr->sh_size);
  173. }
  174. if (shdr->sh_type == SHT_NOBITS) {
  175. memset((void *)(uintptr_t)shdr->sh_addr, 0,
  176. shdr->sh_size);
  177. } else {
  178. image = (unsigned char *)addr + shdr->sh_offset;
  179. memcpy((void *)(uintptr_t)shdr->sh_addr,
  180. (const void *)image, shdr->sh_size);
  181. }
  182. flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
  183. roundup((shdr->sh_addr + shdr->sh_size),
  184. ARCH_DMA_MINALIGN) -
  185. rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
  186. }
  187. return ehdr->e_entry;
  188. }
  189. /*
  190. * Determine if a valid ELF image exists at the given memory location.
  191. * First look at the ELF header magic field, then make sure that it is
  192. * executable.
  193. */
  194. int valid_elf_image(unsigned long addr)
  195. {
  196. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  197. ehdr = (Elf32_Ehdr *)addr;
  198. if (!IS_ELF(*ehdr)) {
  199. printf("## No elf image at address 0x%08lx\n", addr);
  200. return 0;
  201. }
  202. if (ehdr->e_type != ET_EXEC) {
  203. printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
  204. return 0;
  205. }
  206. return 1;
  207. }