1 // SPDX-License-Identifier: BSD-2-Clause
3 Copyright (c) 2001 William L. Pitts
15 #include <linux/linkage.h>
19 * A very simple ELF64 loader, assumes the image is valid, returns the
20 * entry point address.
22 * Note if U-Boot is 32-bit, the loader assumes the to segment's
23 * physical address and size is within the lower 32-bit address space.
25 unsigned long load_elf64_image_phdr(unsigned long addr)
27 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
28 Elf64_Phdr *phdr; /* Program header structure pointer */
31 ehdr = (Elf64_Ehdr *)addr;
32 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
34 /* Load each program header */
35 for (i = 0; i < ehdr->e_phnum; ++i) {
36 void *dst = (void *)(ulong)phdr->p_paddr;
37 void *src = (void *)addr + phdr->p_offset;
39 debug("Loading phdr %i to 0x%p (%lu bytes)\n",
40 i, dst, (ulong)phdr->p_filesz);
42 memcpy(dst, src, phdr->p_filesz);
43 if (phdr->p_filesz != phdr->p_memsz)
44 memset(dst + phdr->p_filesz, 0x00,
45 phdr->p_memsz - phdr->p_filesz);
46 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
47 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
51 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
52 EF_PPC64_ELFV1_ABI)) {
54 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
55 * descriptor pointer with the first double word being the
56 * address of the entry point of the function.
58 uintptr_t addr = ehdr->e_entry;
60 return *(Elf64_Addr *)addr;
66 unsigned long load_elf64_image_shdr(unsigned long addr)
68 Elf64_Ehdr *ehdr; /* Elf header structure pointer */
69 Elf64_Shdr *shdr; /* Section header structure pointer */
70 unsigned char *strtab = 0; /* String table pointer */
71 unsigned char *image; /* Binary image pointer */
72 int i; /* Loop counter */
74 ehdr = (Elf64_Ehdr *)addr;
76 /* Find the section header string table for output info */
77 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
78 (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
80 if (shdr->sh_type == SHT_STRTAB)
81 strtab = (unsigned char *)(addr + (ulong)shdr->sh_offset);
83 /* Load each appropriate section */
84 for (i = 0; i < ehdr->e_shnum; ++i) {
85 shdr = (Elf64_Shdr *)(addr + (ulong)ehdr->e_shoff +
86 (i * sizeof(Elf64_Shdr)));
88 if (!(shdr->sh_flags & SHF_ALLOC) ||
89 shdr->sh_addr == 0 || shdr->sh_size == 0) {
94 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
95 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
96 &strtab[shdr->sh_name],
97 (unsigned long)shdr->sh_addr,
101 if (shdr->sh_type == SHT_NOBITS) {
102 memset((void *)(uintptr_t)shdr->sh_addr, 0,
105 image = (unsigned char *)addr + (ulong)shdr->sh_offset;
106 memcpy((void *)(uintptr_t)shdr->sh_addr,
107 (const void *)image, shdr->sh_size);
109 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
110 roundup((shdr->sh_addr + shdr->sh_size),
112 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
115 if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
116 EF_PPC64_ELFV1_ABI)) {
118 * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
119 * descriptor pointer with the first double word being the
120 * address of the entry point of the function.
122 uintptr_t addr = ehdr->e_entry;
124 return *(Elf64_Addr *)addr;
127 return ehdr->e_entry;
131 * A very simple ELF loader, assumes the image is valid, returns the
132 * entry point address.
134 * The loader firstly reads the EFI class to see if it's a 64-bit image.
135 * If yes, call the ELF64 loader. Otherwise continue with the ELF32 loader.
137 unsigned long load_elf_image_phdr(unsigned long addr)
139 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
140 Elf32_Phdr *phdr; /* Program header structure pointer */
143 ehdr = (Elf32_Ehdr *)addr;
144 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
145 return load_elf64_image_phdr(addr);
147 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
149 /* Load each program header */
150 for (i = 0; i < ehdr->e_phnum; ++i) {
151 void *dst = (void *)(uintptr_t)phdr->p_paddr;
152 void *src = (void *)addr + phdr->p_offset;
154 debug("Loading phdr %i to 0x%p (%i bytes)\n",
155 i, dst, phdr->p_filesz);
157 memcpy(dst, src, phdr->p_filesz);
158 if (phdr->p_filesz != phdr->p_memsz)
159 memset(dst + phdr->p_filesz, 0x00,
160 phdr->p_memsz - phdr->p_filesz);
161 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
162 roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
166 return ehdr->e_entry;
169 unsigned long load_elf_image_shdr(unsigned long addr)
171 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
172 Elf32_Shdr *shdr; /* Section header structure pointer */
173 unsigned char *strtab = 0; /* String table pointer */
174 unsigned char *image; /* Binary image pointer */
175 int i; /* Loop counter */
177 ehdr = (Elf32_Ehdr *)addr;
178 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
179 return load_elf64_image_shdr(addr);
181 /* Find the section header string table for output info */
182 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
183 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
185 if (shdr->sh_type == SHT_STRTAB)
186 strtab = (unsigned char *)(addr + shdr->sh_offset);
188 /* Load each appropriate section */
189 for (i = 0; i < ehdr->e_shnum; ++i) {
190 shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
191 (i * sizeof(Elf32_Shdr)));
193 if (!(shdr->sh_flags & SHF_ALLOC) ||
194 shdr->sh_addr == 0 || shdr->sh_size == 0) {
199 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
200 (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
201 &strtab[shdr->sh_name],
202 (unsigned long)shdr->sh_addr,
203 (long)shdr->sh_size);
206 if (shdr->sh_type == SHT_NOBITS) {
207 memset((void *)(uintptr_t)shdr->sh_addr, 0,
210 image = (unsigned char *)addr + shdr->sh_offset;
211 memcpy((void *)(uintptr_t)shdr->sh_addr,
212 (const void *)image, shdr->sh_size);
214 flush_cache(rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN),
215 roundup((shdr->sh_addr + shdr->sh_size),
217 rounddown(shdr->sh_addr, ARCH_DMA_MINALIGN));
220 return ehdr->e_entry;
224 * Determine if a valid ELF image exists at the given memory location.
225 * First look at the ELF header magic field, then make sure that it is
228 int valid_elf_image(unsigned long addr)
230 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
232 ehdr = (Elf32_Ehdr *)addr;
234 if (!IS_ELF(*ehdr)) {
235 printf("## No elf image at address 0x%08lx\n", addr);
239 if (ehdr->e_type != ET_EXEC) {
240 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);