1 // SPDX-License-Identifier: BSD-2-Clause
3 Copyright (c) 2001 William L. Pitts
16 #include <linux/linkage.h>
20 * A very simple ELF64 loader, assumes the image is valid, returns the
21 * entry point address.
23 * Note if U-Boot is 32-bit, the loader assumes the to segment's
24 * physical address and size is within the lower 32-bit address space.
26 unsigned long load_elf64_image_phdr(unsigned long addr)
28 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
29 Elf64_Phdr *phdr; /* Program header structure pointer */
32 ehdr = (Elf64_Ehdr *)addr;
33 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
35 /* Load each program header */
36 for (i = 0; i < ehdr->e_phnum; ++i) {
37 void *dst = (void *)(ulong)phdr->p_paddr;
38 void *src = (void *)addr + phdr->p_offset;
40 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
41 i, dst, (ulong)phdr->p_filesz);
43 memcpy(dst, src, phdr->p_filesz);
44 if (phdr->p_filesz != phdr->p_memsz)
45 memset(dst + phdr->p_filesz, 0x00,
46 phdr->p_memsz - phdr->p_filesz);
47 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
48 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
52 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
53 EF_PPC64_ELFV1_ABI)) {
55 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
56 * descriptor pointer with the first double word being the
57 * address of the entry point of the function.
59 uintptr_t addr = ehdr->e_entry;
61 return *(Elf64_Addr *)addr;
67 unsigned long load_elf64_image_shdr(unsigned long addr)
69 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
70 Elf64_Shdr *shdr; /* Section header structure pointer */
71 unsigned char *strtab = 0; /* String table pointer */
72 unsigned char *image; /* Binary image pointer */
73 int i; /* Loop counter */
75 ehdr = (Elf64_Ehdr *)addr;
77 /* Find the section header string table for output info */
78 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
79 (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
81 if (shdr->sh_type == SHT_STRTAB)
82 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
84 /* Load each appropriate section */
85 for (i = 0; i < ehdr->e_shnum; ++i) {
86 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
87 (i * sizeof(Elf64_Shdr)));
89 if (!(shdr->sh_flags & SHF_ALLOC) ||
90 shdr->sh_addr == 0 || shdr->sh_size == 0) {
95 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
96 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
97 &strtab[shdr->sh_name],
98 (unsigned long)shdr->sh_addr,
102 if (shdr->sh_type == SHT_NOBITS) {
103 memset((void *)(uintptr_t)shdr->sh_addr, 0,
106 image = (unsigned char *)addr + (ulong)shdr->sh_offset;
107 memcpy((void *)(uintptr_t)shdr->sh_addr,
108 (const void *)image, shdr->sh_size);
110 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
111 roundup((shdr->sh_addr + shdr->sh_size),
113 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
116 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
117 EF_PPC64_ELFV1_ABI)) {
119 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
120 * descriptor pointer with the first double word being the
121 * address of the entry point of the function.
123 uintptr_t addr = ehdr->e_entry;
125 return *(Elf64_Addr *)addr;
128 return ehdr->e_entry;
132 * A very simple ELF loader, assumes the image is valid, returns the
133 * entry point address.
135 * The loader firstly reads the EFI class to see if it's a 64-bit image.
136 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
138 unsigned long load_elf_image_phdr(unsigned long addr)
140 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
141 Elf32_Phdr *phdr; /* Program header structure pointer */
144 ehdr = (Elf32_Ehdr *)addr;
145 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
146 return load_elf64_image_phdr(addr);
148 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
150 /* Load each program header */
151 for (i = 0; i < ehdr->e_phnum; ++i) {
152 void *dst = (void *)(uintptr_t)phdr->p_paddr;
153 void *src = (void *)addr + phdr->p_offset;
155 debug("Loading phdr %i to 0x%p (%i bytes)\n",
156 i, dst, phdr->p_filesz);
158 memcpy(dst, src, phdr->p_filesz);
159 if (phdr->p_filesz != phdr->p_memsz)
160 memset(dst + phdr->p_filesz, 0x00,
161 phdr->p_memsz - phdr->p_filesz);
162 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
163 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
167 return ehdr->e_entry;
170 unsigned long load_elf_image_shdr(unsigned long addr)
172 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
173 Elf32_Shdr *shdr; /* Section header structure pointer */
174 unsigned char *strtab = 0; /* String table pointer */
175 unsigned char *image; /* Binary image pointer */
176 int i; /* Loop counter */
178 ehdr = (Elf32_Ehdr *)addr;
179 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
180 return load_elf64_image_shdr(addr);
182 /* Find the section header string table for output info */
183 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
184 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
186 if (shdr->sh_type == SHT_STRTAB)
187 strtab = (unsigned char *)(addr + shdr->sh_offset);
189 /* Load each appropriate section */
190 for (i = 0; i < ehdr->e_shnum; ++i) {
191 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
192 (i * sizeof(Elf32_Shdr)));
194 if (!(shdr->sh_flags & SHF_ALLOC) ||
195 shdr->sh_addr == 0 || shdr->sh_size == 0) {
200 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
201 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
202 &strtab[shdr->sh_name],
203 (unsigned long)shdr->sh_addr,
204 (long)shdr->sh_size);
207 if (shdr->sh_type == SHT_NOBITS) {
208 memset((void *)(uintptr_t)shdr->sh_addr, 0,
211 image = (unsigned char *)addr + shdr->sh_offset;
212 memcpy((void *)(uintptr_t)shdr->sh_addr,
213 (const void *)image, shdr->sh_size);
215 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
216 roundup((shdr->sh_addr + shdr->sh_size),
218 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
221 return ehdr->e_entry;
225 * Determine if a valid ELF image exists at the given memory location.
226 * First look at the ELF header magic field, then make sure that it is
229 int valid_elf_image(unsigned long addr)
231 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
233 ehdr = (Elf32_Ehdr *)addr;
235 if (!IS_ELF(*ehdr)) {
236 printf("## No elf image at address 0x%08lx\n", addr);
240 if (ehdr->e_type != ET_EXEC) {
241 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);