david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/core/image.c

141 lines
3.3 KiB
C
Raw Normal View History

/*
* 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>
#include <errno.h>
#include <assert.h>
#include <vsprintf.h>
#include <gpxe/list.h>
#include <gpxe/image.h>
/** @file
*
* Executable/loadable images
*
*/
2005-05-09 20:01:50 +02:00
/** List of registered images */
struct list_head images = LIST_HEAD_INIT ( images );
/** 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
/**
* Register executable/loadable image
2005-05-17 15:38:24 +02:00
*
* @v image Executable/loadable image
* @ret rc Return status code
2005-05-17 15:38:24 +02: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++ );
}
/* Add to image list */
list_add_tail ( &image->list, &images );
DBGC ( image, "IMAGE %p registered as %s\n", image, image->name );
return 0;
2005-05-17 15:38:24 +02:00
}
/**
* Unregister executable/loadable image
2005-05-09 20:01:50 +02:00
*
* @v image Executable/loadable image
2005-05-09 20:01:50 +02:00
*/
void unregister_image ( struct image *image ) {
list_del ( &image->list );
DBGC ( image, "IMAGE %p unregistered\n", image );
2005-05-09 20:01:50 +02:00
}
/**
* Load executable/loadable image into memory
2005-05-09 20:01:50 +02:00
*
* @v image Executable/loadable image
* @ret rc Return status code
2005-05-09 20:01:50 +02:00
*/
int image_load ( struct image *image ) {
int rc;
2005-05-17 15:38:24 +02:00
assert ( image->type != NULL );
if ( ( rc = image->type->load ( image ) ) != 0 ) {
DBGC ( image, "IMAGE %p could not load: %s\n",
image, strerror ( rc ) );
return rc;
2005-05-17 15:38:24 +02:00
}
return 0;
}
2005-05-17 15:38:24 +02: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
for ( type = image_types ; type < image_types_end ; type++ ) {
rc = type->load ( image );
if ( image->type == NULL )
continue;
if ( rc != 0 ) {
DBGC ( image, "IMAGE %p (%s) could not load: %s\n",
image, image->type->name, strerror ( rc ) );
return rc;
}
return 0;
2005-05-09 20:01:50 +02:00
}
DBGC ( image, "IMAGE %p format not recognised\n", image );
return -ENOEXEC;
2005-05-09 20:01:50 +02:00
}
/**
* Execute loaded image
*
* @v image Loaded image
* @ret rc Return status code
*/
int image_exec ( struct image *image ) {
int rc;
assert ( image->type != NULL );
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;
}