Просмотр исходного кода

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

tags/v0.9.3
Michael Brown 18 лет назад
Родитель
Сommit
c810baad37
3 измененных файлов: 25 добавлений и 28 удалений
  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 Просмотреть файл

@@ -27,6 +27,7 @@
27 27
 #include <elf.h>
28 28
 #include <gpxe/uaccess.h>
29 29
 #include <gpxe/segment.h>
30
+#include <gpxe/image.h>
30 31
 #include <gpxe/elf.h>
31 32
 
32 33
 typedef Elf32_Ehdr	Elf_Ehdr;
@@ -36,11 +37,11 @@ typedef Elf32_Off	Elf_Off;
36 37
 /**
37 38
  * Load ELF segment into memory
38 39
  *
39
- * @v elf		ELF file
40
+ * @v image		ELF file
40 41
  * @v phdr		ELF program header
41 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 45
 	physaddr_t dest;
45 46
 	userptr_t buffer;
46 47
 	int rc;
@@ -50,7 +51,7 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) {
50 51
 		return 0;
51 52
 
52 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 55
 		DBG ( "ELF segment outside ELF file\n" );
55 56
 		return -ENOEXEC;
56 57
 	}
@@ -81,7 +82,7 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) {
81 82
 	}
82 83
 
83 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 87
 	return 0;
87 88
 }
@@ -89,10 +90,10 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) {
89 90
 /**
90 91
  * Load ELF image into memory
91 92
  *
92
- * @v elf		ELF file
93
+ * @v image		ELF file
93 94
  * @ret rc		Return status code
94 95
  */
95
-int elf_load ( struct elf *elf ) {
96
+int elf_load ( struct image *image ) {
96 97
 	Elf_Ehdr ehdr;
97 98
 	Elf_Phdr phdr;
98 99
 	Elf_Off phoff;
@@ -100,7 +101,7 @@ int elf_load ( struct elf *elf ) {
100 101
 	int rc;
101 102
 
102 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 105
 	if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) {
105 106
 		DBG ( "Invalid ELF signature\n" );
106 107
 		return -ENOEXEC;
@@ -109,18 +110,24 @@ int elf_load ( struct elf *elf ) {
109 110
 	/* Read ELF program headers */
110 111
 	for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
111 112
 	      phoff += ehdr.e_phentsize, phnum-- ) {
112
-		if ( phoff > elf->len ) {
113
+		if ( phoff > image->len ) {
113 114
 			DBG ( "ELF program header %d outside ELF image\n",
114 115
 			      phnum );
115 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 120
 			return rc;
120 121
 	}
121 122
 
122 123
 	/* Fill in entry point address */
123
-	elf->entry = ehdr.e_entry;
124
+	image->entry = ehdr.e_entry;
124 125
 
125 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 Просмотреть файл

@@ -10,17 +10,6 @@
10 10
 
11 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 15
 #endif /* _GPXE_ELF_H */

+ 6
- 5
src/tests/tftptest.c Просмотреть файл

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

Загрузка…
Отмена
Сохранить