From c810baad37673647f6b5683e3f50e7f392c482e2 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 11 Jan 2007 16:10:32 +0000 Subject: [PATCH] Use generic "struct image" rather than "struct elf". --- src/image/elf.c | 29 ++++++++++++++++++----------- src/include/gpxe/elf.h | 13 +------------ src/tests/tftptest.c | 11 ++++++----- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/image/elf.c b/src/image/elf.c index c76c8f5e..5233a64c 100644 --- a/src/image/elf.c +++ b/src/image/elf.c @@ -27,6 +27,7 @@ #include #include #include +#include #include typedef Elf32_Ehdr Elf_Ehdr; @@ -36,11 +37,11 @@ typedef Elf32_Off Elf_Off; /** * Load ELF segment into memory * - * @v elf ELF file + * @v image ELF file * @v phdr ELF program header * @ret rc Return status code */ -static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) { +static int elf_load_segment ( struct image *image, Elf_Phdr *phdr ) { physaddr_t dest; userptr_t buffer; int rc; @@ -50,7 +51,7 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) { return 0; /* Check segment lies within image */ - if ( ( phdr->p_offset + phdr->p_filesz ) > elf->len ) { + if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) { DBG ( "ELF segment outside ELF file\n" ); return -ENOEXEC; } @@ -81,7 +82,7 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) { } /* Copy image to segment */ - copy_user ( buffer, 0, elf->image, phdr->p_offset, phdr->p_filesz ); + copy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz ); return 0; } @@ -89,10 +90,10 @@ static int elf_load_segment ( struct elf *elf, Elf_Phdr *phdr ) { /** * Load ELF image into memory * - * @v elf ELF file + * @v image ELF file * @ret rc Return status code */ -int elf_load ( struct elf *elf ) { +int elf_load ( struct image *image ) { Elf_Ehdr ehdr; Elf_Phdr phdr; Elf_Off phoff; @@ -100,7 +101,7 @@ int elf_load ( struct elf *elf ) { int rc; /* Read ELF header */ - copy_from_user ( &ehdr, elf->image, 0, sizeof ( ehdr ) ); + copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) ); if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) { DBG ( "Invalid ELF signature\n" ); return -ENOEXEC; @@ -109,18 +110,24 @@ int elf_load ( struct elf *elf ) { /* Read ELF program headers */ for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ; phoff += ehdr.e_phentsize, phnum-- ) { - if ( phoff > elf->len ) { + if ( phoff > image->len ) { DBG ( "ELF program header %d outside ELF image\n", phnum ); return -ENOEXEC; } - copy_from_user ( &phdr, elf->image, phoff, sizeof ( phdr ) ); - if ( ( rc = elf_load_segment ( elf, &phdr ) ) != 0 ) + copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) ); + if ( ( rc = elf_load_segment ( image, &phdr ) ) != 0 ) return rc; } /* Fill in entry point address */ - elf->entry = ehdr.e_entry; + image->entry = ehdr.e_entry; return 0; } + +/** ELF image type */ +struct image_type elf_image_type __image_type = { + .name = "ELF", + .load = elf_load, +}; diff --git a/src/include/gpxe/elf.h b/src/include/gpxe/elf.h index 8abbb02e..db28a60a 100644 --- a/src/include/gpxe/elf.h +++ b/src/include/gpxe/elf.h @@ -10,17 +10,6 @@ #include -/** An ELF file */ -struct elf { - /** ELF file image */ - userptr_t image; - /** Length of ELF file image */ - size_t len; - - /** Entry point */ - physaddr_t entry; -}; - -extern int elf_load ( struct elf *elf ); +extern int elf_load ( struct image *image ); #endif /* _GPXE_ELF_H */ diff --git a/src/tests/tftptest.c b/src/tests/tftptest.c index 57f51502..7bdec78d 100644 --- a/src/tests/tftptest.c +++ b/src/tests/tftptest.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include "pxe.h" @@ -14,7 +15,7 @@ int test_tftp ( struct net_device *netdev, struct sockaddr_tcpip *target, const char *filename ) { struct tftp_session tftp; struct buffer buffer; - struct elf elf; + struct image image; uint16_t fbms; int rc; @@ -32,11 +33,11 @@ int test_tftp ( struct net_device *netdev, struct sockaddr_tcpip *target, if ( ( rc = async_wait ( tftp_get ( &tftp ) ) ) != 0 ) return rc; - elf.image = buffer.addr; - elf.len = buffer.len; - if ( ( rc = elf_load ( &elf ) ) == 0 ) { + image.data = buffer.addr; + image.len = buffer.len; + if ( ( rc = elf_load ( &image ) ) == 0 ) { printf ( "Got valid ELF image: execaddr at %lx\n", - elf.entry ); + image.entry ); return 0; }