2007-01-12 00:43:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2007-01-12 00:43:29 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <gpxe/list.h>
|
2007-06-09 18:32:27 +02:00
|
|
|
#include <gpxe/umalloc.h>
|
2007-01-12 00:43:29 +01:00
|
|
|
#include <gpxe/image.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Executable/loadable images
|
|
|
|
*
|
|
|
|
*/
|
2005-05-09 20:01:50 +02:00
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/** List of registered images */
|
|
|
|
struct list_head images = LIST_HEAD_INIT ( images );
|
2007-01-11 17:04:36 +01:00
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/** List of image types */
|
|
|
|
static struct image_type image_types[0]
|
|
|
|
__table_start ( struct image_type, image_types );
|
|
|
|
static struct image_type image_types_end[0]
|
|
|
|
__table_end ( struct image_type, image_types );
|
2005-05-09 20:01:50 +02:00
|
|
|
|
2007-06-09 18:32:27 +02:00
|
|
|
/**
|
|
|
|
* Free executable/loadable image
|
|
|
|
*
|
|
|
|
* @v refcnt Reference counter
|
|
|
|
*/
|
|
|
|
static void free_image ( struct refcnt *refcnt ) {
|
|
|
|
struct image *image = container_of ( refcnt, struct image, refcnt );
|
|
|
|
|
|
|
|
ufree ( image->data );
|
|
|
|
free ( image );
|
|
|
|
DBGC ( image, "IMAGE %p freed\n", image );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate executable/loadable image
|
|
|
|
*
|
|
|
|
* @ret image Executable/loadable image
|
|
|
|
*/
|
|
|
|
struct image * alloc_image ( void ) {
|
|
|
|
struct image *image;
|
|
|
|
|
|
|
|
image = malloc ( sizeof ( *image ) );
|
|
|
|
if ( image ) {
|
|
|
|
memset ( image, 0, sizeof ( *image ) );
|
|
|
|
image->refcnt.free = free_image;
|
|
|
|
}
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/**
|
|
|
|
* Register executable/loadable image
|
2005-05-17 15:38:24 +02:00
|
|
|
*
|
2007-01-12 00:43:29 +01:00
|
|
|
* @v image Executable/loadable image
|
|
|
|
* @ret rc Return status code
|
2005-05-17 15:38:24 +02:00
|
|
|
*/
|
2007-01-12 00:43:29 +01:00
|
|
|
int register_image ( struct image *image ) {
|
|
|
|
static unsigned int imgindex = 0;
|
2005-05-17 15:38:24 +02:00
|
|
|
|
2007-01-12 00:45:18 +01:00
|
|
|
/* Create image name if it doesn't already have one */
|
|
|
|
if ( ! image->name[0] ) {
|
|
|
|
snprintf ( image->name, sizeof ( image->name ), "img%d",
|
|
|
|
imgindex++ );
|
|
|
|
}
|
2007-01-12 00:43:29 +01:00
|
|
|
|
|
|
|
/* Add to image list */
|
2007-06-09 18:32:27 +02:00
|
|
|
image_get ( image );
|
2007-01-12 00:43:29 +01:00
|
|
|
list_add_tail ( &image->list, &images );
|
2007-01-12 17:20:51 +01:00
|
|
|
DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
|
|
|
|
image, user_to_phys ( image->data, 0 ),
|
|
|
|
user_to_phys ( image->data, image->len ), image->name );
|
2007-01-12 00:43:29 +01:00
|
|
|
|
|
|
|
return 0;
|
2005-05-17 15:38:24 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/**
|
|
|
|
* Unregister executable/loadable image
|
2005-05-09 20:01:50 +02:00
|
|
|
*
|
2007-01-12 00:43:29 +01:00
|
|
|
* @v image Executable/loadable image
|
2005-05-09 20:01:50 +02:00
|
|
|
*/
|
2007-01-12 00:43:29 +01:00
|
|
|
void unregister_image ( struct image *image ) {
|
|
|
|
list_del ( &image->list );
|
2007-06-09 18:32:27 +02:00
|
|
|
image_put ( image );
|
2007-01-12 00:43:29 +01:00
|
|
|
DBGC ( image, "IMAGE %p unregistered\n", image );
|
2005-05-09 20:01:50 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 07:03:02 +01:00
|
|
|
/**
|
|
|
|
* Find image by name
|
|
|
|
*
|
|
|
|
* @v name Image name
|
|
|
|
* @ret image Executable/loadable image, or NULL
|
|
|
|
*/
|
|
|
|
struct image * find_image ( const char *name ) {
|
|
|
|
struct image *image;
|
|
|
|
|
|
|
|
list_for_each_entry ( image, &images, list ) {
|
|
|
|
if ( strcmp ( image->name, name ) == 0 )
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-12 07:26:59 +01:00
|
|
|
/**
|
2007-01-12 10:46:10 +01:00
|
|
|
* Load executable/loadable image into memory
|
2007-01-12 07:26:59 +01:00
|
|
|
*
|
|
|
|
* @v image Executable/loadable image
|
2007-01-12 10:46:10 +01:00
|
|
|
* @v type Executable/loadable image type
|
|
|
|
* @ret rc Return status code
|
2007-01-12 07:26:59 +01:00
|
|
|
*/
|
2007-01-12 10:46:10 +01:00
|
|
|
static int image_load_type ( struct image *image, struct image_type *type ) {
|
|
|
|
int rc;
|
|
|
|
|
2007-01-14 15:59:36 +01:00
|
|
|
/* Check image is actually loadable */
|
|
|
|
if ( ! type->load )
|
|
|
|
return -ENOEXEC;
|
|
|
|
|
|
|
|
/* Try the image loader */
|
2007-01-12 10:46:10 +01:00
|
|
|
if ( ( rc = type->load ( image ) ) != 0 ) {
|
|
|
|
DBGC ( image, "IMAGE %p could not load as %s: %s\n",
|
|
|
|
image, type->name, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flag as loaded */
|
|
|
|
image->flags |= IMAGE_LOADED;
|
|
|
|
return 0;
|
2007-01-12 07:26:59 +01:00
|
|
|
}
|
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/**
|
|
|
|
* Load executable/loadable image into memory
|
2005-05-09 20:01:50 +02:00
|
|
|
*
|
2007-01-12 00:43:29 +01:00
|
|
|
* @v image Executable/loadable image
|
|
|
|
* @ret rc Return status code
|
2005-05-09 20:01:50 +02:00
|
|
|
*/
|
2007-01-12 00:43:29 +01:00
|
|
|
int image_load ( struct image *image ) {
|
2005-05-17 15:38:24 +02:00
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
assert ( image->type != NULL );
|
|
|
|
|
2007-01-12 10:46:10 +01:00
|
|
|
return image_load_type ( image, image->type );
|
2007-01-12 00:43:29 +01:00
|
|
|
}
|
2005-05-17 15:38:24 +02:00
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/**
|
|
|
|
* Autodetect image type and load executable/loadable image into memory
|
|
|
|
*
|
|
|
|
* @v image Executable/loadable image
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int image_autoload ( struct image *image ) {
|
|
|
|
struct image_type *type;
|
|
|
|
int rc;
|
2005-05-09 20:01:50 +02:00
|
|
|
|
2007-01-14 16:25:23 +01:00
|
|
|
/* If image already has a type, use it */
|
|
|
|
if ( image->type )
|
|
|
|
return image_load ( image );
|
|
|
|
|
|
|
|
/* Otherwise probe for a suitable type */
|
2007-01-12 00:43:29 +01:00
|
|
|
for ( type = image_types ; type < image_types_end ; type++ ) {
|
2007-01-12 09:57:51 +01:00
|
|
|
DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
|
2007-01-12 10:46:10 +01:00
|
|
|
rc = image_load_type ( image, type );
|
2007-01-12 00:43:29 +01:00
|
|
|
if ( image->type == NULL )
|
|
|
|
continue;
|
2007-01-12 10:46:10 +01:00
|
|
|
return rc;
|
2005-05-09 20:01:50 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
DBGC ( image, "IMAGE %p format not recognised\n", image );
|
|
|
|
return -ENOEXEC;
|
2005-05-09 20:01:50 +02:00
|
|
|
}
|
2006-05-16 17:00:36 +02:00
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
/**
|
|
|
|
* Execute loaded image
|
|
|
|
*
|
|
|
|
* @v image Loaded image
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int image_exec ( struct image *image ) {
|
|
|
|
int rc;
|
|
|
|
|
2007-01-12 07:03:02 +01:00
|
|
|
/* Image must be loaded first */
|
|
|
|
if ( ! ( image->flags & IMAGE_LOADED ) ) {
|
|
|
|
DBGC ( image, "IMAGE %p could not execute: not loaded\n",
|
|
|
|
image );
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2007-01-12 00:43:29 +01:00
|
|
|
assert ( image->type != NULL );
|
|
|
|
|
2007-01-14 15:59:36 +01:00
|
|
|
/* Check that image is actually executable */
|
|
|
|
if ( ! image->type->exec )
|
|
|
|
return -ENOEXEC;
|
|
|
|
|
2007-01-12 07:03:02 +01:00
|
|
|
/* Try executing the image */
|
2007-01-12 00:43:29 +01:00
|
|
|
if ( ( rc = image->type->exec ( image ) ) != 0 ) {
|
|
|
|
DBGC ( image, "IMAGE %p could not execute: %s\n",
|
|
|
|
image, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Well, some formats might return... */
|
|
|
|
return 0;
|
|
|
|
}
|