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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <gpxe/uaccess.h>
29 #include <gpxe/segment.h>
30 #include <gpxe/image.h>
33 struct image_type elf_image_type
__image_type ( PROBE_NORMAL
);
35 typedef Elf32_Ehdr Elf_Ehdr
;
36 typedef Elf32_Phdr Elf_Phdr
;
37 typedef Elf32_Off Elf_Off
;
43 * @ret rc Return status code
45 static int elf_exec ( struct image
*image __unused
) {
50 * Load ELF segment into memory
53 * @v phdr ELF program header
54 * @ret rc Return status code
56 static int elf_load_segment ( struct image
*image
, Elf_Phdr
*phdr
) {
61 /* Do nothing for non-PT_LOAD segments */
62 if ( phdr
->p_type
!= PT_LOAD
)
65 /* Check segment lies within image */
66 if ( ( phdr
->p_offset
+ phdr
->p_filesz
) > image
->len
) {
67 DBG ( "ELF segment outside ELF file\n" );
71 /* Find start address: use physical address for preference,
72 * fall back to virtual address if no physical address
79 DBG ( "ELF segment loads to physical address 0\n" );
82 buffer
= phys_to_user ( dest
);
84 DBG ( "ELF loading segment [%lx,%lx) to [%lx,%lx,%lx)\n",
85 phdr
->p_offset
, ( phdr
->p_offset
+ phdr
->p_filesz
),
86 phdr
->p_paddr
, ( phdr
->p_paddr
+ phdr
->p_filesz
),
87 ( phdr
->p_paddr
+ phdr
->p_memsz
) );
89 /* Verify and prepare segment */
90 if ( ( rc
= prep_segment ( buffer
, phdr
->p_filesz
,
91 phdr
->p_memsz
) ) != 0 ) {
92 DBG ( "ELF could not prepare segment: %s\n", strerror ( rc
) );
96 /* Copy image to segment */
97 memcpy_user ( buffer
, 0, image
->data
, phdr
->p_offset
, phdr
->p_filesz
);
103 * Load ELF image into memory
106 * @ret rc Return status code
108 int elf_load ( struct image
*image
) {
115 /* Read ELF header */
116 copy_from_user ( &ehdr
, image
->data
, 0, sizeof ( ehdr
) );
117 if ( memcmp ( &ehdr
.e_ident
[EI_MAG0
], ELFMAG
, SELFMAG
) != 0 ) {
118 DBG ( "Invalid ELF signature\n" );
122 /* This is an ELF image, valid or otherwise */
124 image
->type
= &elf_image_type
;
126 /* Read ELF program headers */
127 for ( phoff
= ehdr
.e_phoff
, phnum
= ehdr
.e_phnum
; phnum
;
128 phoff
+= ehdr
.e_phentsize
, phnum
-- ) {
129 if ( phoff
> image
->len
) {
130 DBG ( "ELF program header %d outside ELF image\n",
134 copy_from_user ( &phdr
, image
->data
, phoff
, sizeof ( phdr
) );
135 if ( ( rc
= elf_load_segment ( image
, &phdr
) ) != 0 )
139 /* Record execution entry point in image private data field */
140 image
->priv
.phys
= ehdr
.e_entry
;
145 /** ELF image type */
146 struct image_type elf_image_type
__image_type ( PROBE_NORMAL
) = {