Browse Source

Use generic "struct image" rather than "struct elf".

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
c810baad37
3 changed files with 25 additions and 28 deletions
  1. 18
    11
      src/image/elf.c
  2. 1
    12
      src/include/gpxe/elf.h
  3. 6
    5
      src/tests/tftptest.c

+ 18
- 11
src/image/elf.c View File

27
 #include <elf.h>
27
 #include <elf.h>
28
 #include <gpxe/uaccess.h>
28
 #include <gpxe/uaccess.h>
29
 #include <gpxe/segment.h>
29
 #include <gpxe/segment.h>
30
+#include <gpxe/image.h>
30
 #include <gpxe/elf.h>
31
 #include <gpxe/elf.h>
31
 
32
 
32
 typedef Elf32_Ehdr	Elf_Ehdr;
33
 typedef Elf32_Ehdr	Elf_Ehdr;
36
 /**
37
 /**
37
  * Load ELF segment into memory
38
  * Load ELF segment into memory
38
  *
39
  *
39
- * @v elf		ELF file
40
+ * @v image		ELF file
40
  * @v phdr		ELF program header
41
  * @v phdr		ELF program header
41
  * @ret rc		Return status code
42
  * @ret rc		Return status code
42
  */
43
  */
43
-static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) {
44
+static int elf_load_segment ( struct image *image, Elf_Phdr *phdr ) {
44
 	physaddr_t dest;
45
 	physaddr_t dest;
45
 	userptr_t buffer;
46
 	userptr_t buffer;
46
 	int rc;
47
 	int rc;
50
 		return 0;
51
 		return 0;
51
 
52
 
52
 	/* Check segment lies within image */
53
 	/* Check segment lies within image */
53
-	if ( ( phdr->p_offset + phdr->p_filesz ) > elf->len ) {
54
+	if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
54
 		DBG ( "ELF segment outside ELF file\n" );
55
 		DBG ( "ELF segment outside ELF file\n" );
55
 		return -ENOEXEC;
56
 		return -ENOEXEC;
56
 	}
57
 	}
81
 	}
82
 	}
82
 
83
 
83
 	/* Copy image to segment */
84
 	/* Copy image to segment */
84
-	copy_user ( buffer, 0, elf->image, phdr->p_offset, phdr->p_filesz );
85
+	copy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
85
 
86
 
86
 	return 0;
87
 	return 0;
87
 }
88
 }
89
 /**
90
 /**
90
  * Load ELF image into memory
91
  * Load ELF image into memory
91
  *
92
  *
92
- * @v elf		ELF file
93
+ * @v image		ELF file
93
  * @ret rc		Return status code
94
  * @ret rc		Return status code
94
  */
95
  */
95
-int elf_load ( struct elf *elf ) {
96
+int elf_load ( struct image *image ) {
96
 	Elf_Ehdr ehdr;
97
 	Elf_Ehdr ehdr;
97
 	Elf_Phdr phdr;
98
 	Elf_Phdr phdr;
98
 	Elf_Off phoff;
99
 	Elf_Off phoff;
100
 	int rc;
101
 	int rc;
101
 
102
 
102
 	/* Read ELF header */
103
 	/* Read ELF header */
103
-	copy_from_user ( &ehdr, elf->image, 0, sizeof ( ehdr ) );
104
+	copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
104
 	if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) {
105
 	if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) {
105
 		DBG ( "Invalid ELF signature\n" );
106
 		DBG ( "Invalid ELF signature\n" );
106
 		return -ENOEXEC;
107
 		return -ENOEXEC;
109
 	/* Read ELF program headers */
110
 	/* Read ELF program headers */
110
 	for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
111
 	for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
111
 	      phoff += ehdr.e_phentsize, phnum-- ) {
112
 	      phoff += ehdr.e_phentsize, phnum-- ) {
112
-		if ( phoff > elf->len ) {
113
+		if ( phoff > image->len ) {
113
 			DBG ( "ELF program header %d outside ELF image\n",
114
 			DBG ( "ELF program header %d outside ELF image\n",
114
 			      phnum );
115
 			      phnum );
115
 			return -ENOEXEC;
116
 			return -ENOEXEC;
116
 		}
117
 		}
117
-		copy_from_user ( &phdr, elf->image, phoff, sizeof ( phdr ) );
118
-		if ( ( rc = elf_load_segment ( elf, &phdr ) ) != 0 )
118
+		copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
119
+		if ( ( rc = elf_load_segment ( image, &phdr ) ) != 0 )
119
 			return rc;
120
 			return rc;
120
 	}
121
 	}
121
 
122
 
122
 	/* Fill in entry point address */
123
 	/* Fill in entry point address */
123
-	elf->entry = ehdr.e_entry;
124
+	image->entry = ehdr.e_entry;
124
 
125
 
125
 	return 0;
126
 	return 0;
126
 }
127
 }
128
+
129
+/** ELF image type */
130
+struct image_type elf_image_type __image_type = {
131
+	.name = "ELF",
132
+	.load = elf_load,
133
+};

+ 1
- 12
src/include/gpxe/elf.h View File

10
 
10
 
11
 #include <elf.h>
11
 #include <elf.h>
12
 
12
 
13
-/** An ELF file */
14
-struct elf {
15
-	/** ELF file image */
16
-	userptr_t image;
17
-	/** Length of ELF file image */
18
-	size_t len;
19
-
20
-	/** Entry point */
21
-	physaddr_t entry;
22
-};
23
-
24
-extern int elf_load ( struct elf *elf );
13
+extern int elf_load ( struct image *image );
25
 
14
 
26
 #endif /* _GPXE_ELF_H */
15
 #endif /* _GPXE_ELF_H */

+ 6
- 5
src/tests/tftptest.c View File

6
 #include <gpxe/async.h>
6
 #include <gpxe/async.h>
7
 #include <gpxe/uaccess.h>
7
 #include <gpxe/uaccess.h>
8
 #include <gpxe/buffer.h>
8
 #include <gpxe/buffer.h>
9
+#include <gpxe/image.h>
9
 #include <gpxe/elf.h>
10
 #include <gpxe/elf.h>
10
 #include <bios.h>
11
 #include <bios.h>
11
 #include "pxe.h"
12
 #include "pxe.h"
14
 		const char *filename ) {
15
 		const char *filename ) {
15
 	struct tftp_session tftp;
16
 	struct tftp_session tftp;
16
 	struct buffer buffer;
17
 	struct buffer buffer;
17
-	struct elf elf;
18
+	struct image image;
18
 	uint16_t fbms;
19
 	uint16_t fbms;
19
 	int rc;
20
 	int rc;
20
 
21
 
32
 	if ( ( rc = async_wait ( tftp_get ( &tftp ) ) ) != 0 )
33
 	if ( ( rc = async_wait ( tftp_get ( &tftp ) ) ) != 0 )
33
 		return rc;
34
 		return rc;
34
 
35
 
35
-	elf.image = buffer.addr;
36
-	elf.len = buffer.len;
37
-	if ( ( rc = elf_load ( &elf ) ) == 0 ) {
36
+	image.data = buffer.addr;
37
+	image.len = buffer.len;
38
+	if ( ( rc = elf_load ( &image ) ) == 0 ) {
38
 		printf ( "Got valid ELF image: execaddr at %lx\n",
39
 		printf ( "Got valid ELF image: execaddr at %lx\n",
39
-			 elf.entry );
40
+			 image.entry );
40
 		return 0;
41
 		return 0;
41
 	}
42
 	}
42
 
43
 

Loading…
Cancel
Save