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
20 FILE_LICENCE ( GPL2_OR_LATER );
27 * A "pure" ELF image is not a bootable image. There are various
28 * bootable formats based upon ELF (e.g. Multiboot), which share
29 * common ELF-related functionality.
34 #include <ipxe/uaccess.h>
35 #include <ipxe/segment.h>
36 #include <ipxe/image.h>
39 typedef Elf32_Ehdr Elf_Ehdr;
40 typedef Elf32_Phdr Elf_Phdr;
41 typedef Elf32_Off Elf_Off;
42 #define ELFCLASS ELFCLASS32
45 * Load ELF segment into memory
48 * @v phdr ELF program header
49 * @v ehdr ELF executable header
50 * @ret entry Entry point, if found
51 * @ret max Maximum used address
52 * @ret rc Return status code
54 static int elf_load_segment ( struct image *image, Elf_Phdr *phdr,
55 Elf_Ehdr *ehdr, physaddr_t *entry,
60 unsigned long e_offset;
63 /* Do nothing for non-PT_LOAD segments */
64 if ( phdr->p_type != PT_LOAD )
67 /* Check segment lies within image */
68 if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
69 DBGC ( image, "ELF %p segment outside image\n", image );
73 /* Find start address: use physical address for preference,
74 * fall back to virtual address if no physical address
81 DBGC ( image, "ELF %p segment loads to physical address 0\n",
85 buffer = phys_to_user ( dest );
86 end = ( dest + phdr->p_memsz );
88 DBGC ( image, "ELF %p loading segment [%x,%x) to [%x,%x,%x)\n", image,
89 phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
90 phdr->p_paddr, ( phdr->p_paddr + phdr->p_filesz ),
91 ( phdr->p_paddr + phdr->p_memsz ) );
93 /* Verify and prepare segment */
94 if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
95 phdr->p_memsz ) ) != 0 ) {
96 DBGC ( image, "ELF %p could not prepare segment: %s\n",
97 image, strerror ( rc ) );
101 /* Update maximum used address, if applicable */
105 /* Copy image to segment */
106 memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
108 /* Set execution address, if it lies within this segment */
109 if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
110 *entry = ehdr->e_entry;
111 DBGC ( image, "ELF %p found physical entry point at %lx\n",
113 } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
116 *entry = ( dest + e_offset );
117 DBGC ( image, "ELF %p found virtual entry point at %lx"
118 " (virt %lx)\n", image, *entry,
119 ( ( unsigned long ) ehdr->e_entry ) );
127 * Load ELF image into memory
130 * @ret entry Entry point
131 * @ret max Maximum used address
132 * @ret rc Return status code
134 int elf_load ( struct image *image, physaddr_t *entry, physaddr_t *max ) {
135 static const uint8_t e_ident[] = {
140 [EI_CLASS] = ELFCLASS,
148 /* Read ELF header */
149 copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
150 if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
151 sizeof ( e_ident ) ) != 0 ) {
152 DBGC ( image, "ELF %p has invalid signature\n", image );
156 /* Initialise maximum used address */
159 /* Invalidate entry point */
162 /* Read ELF program headers */
163 for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
164 phoff += ehdr.e_phentsize, phnum-- ) {
165 if ( phoff > image->len ) {
166 DBGC ( image, "ELF %p program header %d outside "
167 "image\n", image, phnum );
170 copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
171 if ( ( rc = elf_load_segment ( image, &phdr, &ehdr,
172 entry, max ) ) != 0 ) {
177 /* Check for a valid execution address */
179 DBGC ( image, "ELF %p entry point %lx outside image\n",
180 image, ( ( unsigned long ) ehdr.e_entry ) );