2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 * A "pure" ELF image is not a bootable image. There are various
32 * bootable formats based upon ELF (e.g. Multiboot), which share
33 * common ELF-related functionality.
38 #include <ipxe/uaccess.h>
39 #include <ipxe/segment.h>
40 #include <ipxe/image.h>
44 * Load ELF segment into memory
47 * @v phdr ELF program header
48 * @v dest Destination address
49 * @ret rc Return status code
51 static int elf_load_segment ( struct image *image, Elf_Phdr *phdr,
53 userptr_t buffer = phys_to_user ( dest );
56 DBGC ( image, "ELF %p loading segment [%x,%x) to [%lx,%lx,%lx)\n",
57 image, phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
58 dest, ( dest + phdr->p_filesz ), ( dest + phdr->p_memsz ) );
60 /* Verify and prepare segment */
61 if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
62 phdr->p_memsz ) ) != 0 ) {
63 DBGC ( image, "ELF %p could not prepare segment: %s\n",
64 image, strerror ( rc ) );
68 /* Copy image to segment */
69 memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
78 * @v ehdr ELF executable header
79 * @v phdr ELF program header
80 * @v process Segment processor
81 * @ret entry Entry point, if found
82 * @ret max Maximum used address
83 * @ret rc Return status code
85 static int elf_segment ( struct image *image, Elf_Ehdr *ehdr, Elf_Phdr *phdr,
86 int ( * process ) ( struct image *image,
87 Elf_Phdr *phdr, physaddr_t dest ),
88 physaddr_t *entry, physaddr_t *max ) {
91 unsigned long e_offset;
94 /* Do nothing for non-PT_LOAD segments */
95 if ( phdr->p_type != PT_LOAD )
98 /* Check segment lies within image */
99 if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
100 DBGC ( image, "ELF %p segment outside image\n", image );
104 /* Find start address: use physical address for preference,
105 * fall back to virtual address if no physical address
108 dest = phdr->p_paddr;
110 dest = phdr->p_vaddr;
112 DBGC ( image, "ELF %p segment loads to physical address 0\n",
116 end = ( dest + phdr->p_memsz );
118 /* Update maximum used address, if applicable */
122 /* Process segment */
123 if ( ( rc = process ( image, phdr, dest ) ) != 0 )
126 /* Set execution address, if it lies within this segment */
127 if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
128 *entry = ehdr->e_entry;
129 DBGC ( image, "ELF %p found physical entry point at %lx\n",
131 } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
134 *entry = ( dest + e_offset );
135 DBGC ( image, "ELF %p found virtual entry point at %lx"
136 " (virt %lx)\n", image, *entry,
137 ( ( unsigned long ) ehdr->e_entry ) );
145 * Process ELF segments
148 * @v ehdr ELF executable header
149 * @v process Segment processor
150 * @ret entry Entry point, if found
151 * @ret max Maximum used address
152 * @ret rc Return status code
154 int elf_segments ( struct image *image, Elf_Ehdr *ehdr,
155 int ( * process ) ( struct image *image, Elf_Phdr *phdr,
157 physaddr_t *entry, physaddr_t *max ) {
163 /* Initialise maximum used address */
166 /* Invalidate entry point */
169 /* Read and process ELF program headers */
170 for ( phoff = ehdr->e_phoff , phnum = ehdr->e_phnum ; phnum ;
171 phoff += ehdr->e_phentsize, phnum-- ) {
172 if ( phoff > image->len ) {
173 DBGC ( image, "ELF %p program header %d outside "
174 "image\n", image, phnum );
177 copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
178 if ( ( rc = elf_segment ( image, ehdr, &phdr, process,
179 entry, max ) ) != 0 )
183 /* Check for a valid execution address */
185 DBGC ( image, "ELF %p entry point %lx outside image\n",
186 image, ( ( unsigned long ) ehdr->e_entry ) );
194 * Load ELF image into memory
197 * @ret entry Entry point
198 * @ret max Maximum used address
199 * @ret rc Return status code
201 int elf_load ( struct image *image, physaddr_t *entry, physaddr_t *max ) {
202 static const uint8_t e_ident[] = {
207 [EI_CLASS] = ELFCLASS,
212 /* Read ELF header */
213 copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
214 if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
215 sizeof ( e_ident ) ) != 0 ) {
216 DBGC ( image, "ELF %p has invalid signature\n", image );
220 /* Load ELF segments into memory */
221 if ( ( rc = elf_segments ( image, &ehdr, elf_load_segment,
222 entry, max ) ) != 0 )