Purge warnings from prism2 drivers
[gpxe.git] / src / image / elf.c
blob75c976eaf101d8a0b60f76e94ed3062fd4666b51
1 /*
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.
19 /**
20 * @file
22 * ELF image format
26 #include <errno.h>
27 #include <elf.h>
28 #include <gpxe/uaccess.h>
29 #include <gpxe/segment.h>
30 #include <gpxe/image.h>
31 #include <gpxe/elf.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;
39 /**
40 * Execute ELF image
42 * @v image ELF file
43 * @ret rc Return status code
45 static int elf_exec ( struct image *image __unused ) {
46 return -ENOTSUP;
49 /**
50 * Load ELF segment into memory
52 * @v image ELF file
53 * @v phdr ELF program header
54 * @ret rc Return status code
56 static int elf_load_segment ( struct image *image, Elf_Phdr *phdr ) {
57 physaddr_t dest;
58 userptr_t buffer;
59 int rc;
61 /* Do nothing for non-PT_LOAD segments */
62 if ( phdr->p_type != PT_LOAD )
63 return 0;
65 /* Check segment lies within image */
66 if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
67 DBG ( "ELF segment outside ELF file\n" );
68 return -ENOEXEC;
71 /* Find start address: use physical address for preference,
72 * fall back to virtual address if no physical address
73 * supplied.
75 dest = phdr->p_paddr;
76 if ( ! dest )
77 dest = phdr->p_vaddr;
78 if ( ! dest ) {
79 DBG ( "ELF segment loads to physical address 0\n" );
80 return -ENOEXEC;
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 ) );
93 return rc;
96 /* Copy image to segment */
97 memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
99 return 0;
103 * Load ELF image into memory
105 * @v image ELF file
106 * @ret rc Return status code
108 int elf_load ( struct image *image ) {
109 Elf_Ehdr ehdr;
110 Elf_Phdr phdr;
111 Elf_Off phoff;
112 unsigned int phnum;
113 int rc;
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" );
119 return -ENOEXEC;
122 /* This is an ELF image, valid or otherwise */
123 if ( ! image->type )
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",
131 phnum );
132 return -ENOEXEC;
134 copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
135 if ( ( rc = elf_load_segment ( image, &phdr ) ) != 0 )
136 return rc;
139 /* Record execution entry point in image private data field */
140 image->priv.phys = ehdr.e_entry;
142 return 0;
145 /** ELF image type */
146 struct image_type elf_image_type __image_type ( PROBE_NORMAL ) = {
147 .name = "ELF",
148 .load = elf_load,
149 .exec = elf_exec,