2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/list.h>
26 #include <gpxe/emalloc.h>
27 #include <gpxe/image.h>
31 * Executable/loadable images
35 /** List of registered images */
36 struct list_head images = LIST_HEAD_INIT ( images );
38 /** List of image types */
39 static struct image_type image_types[0]
40 __table_start ( struct image_type, image_types );
41 static struct image_type image_types_end[0]
42 __table_end ( struct image_type, image_types );
45 * Register executable/loadable image
47 * @v image Executable/loadable image
48 * @ret rc Return status code
50 int register_image ( struct image *image ) {
51 static unsigned int imgindex = 0;
53 /* Create image name if it doesn't already have one */
54 if ( ! image->name[0] ) {
55 snprintf ( image->name, sizeof ( image->name ), "img%d",
59 /* Add to image list */
60 list_add_tail ( &image->list, &images );
61 DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
62 image, user_to_phys ( image->data, 0 ),
63 user_to_phys ( image->data, image->len ), image->name );
69 * Unregister executable/loadable image
71 * @v image Executable/loadable image
73 void unregister_image ( struct image *image ) {
74 list_del ( &image->list );
75 DBGC ( image, "IMAGE %p unregistered\n", image );
79 * Move image to start of list of registered images
81 * @v image Executable/loadable image
83 * Move the image to the start of the image list. This makes it
84 * easier to keep track of which of the images marked as loaded is
85 * likely to still be valid.
87 void promote_image ( struct image *image ) {
88 list_del ( &image->list );
89 list_add ( &image->list, &images );
96 * @ret image Executable/loadable image, or NULL
98 struct image * find_image ( const char *name ) {
101 list_for_each_entry ( image, &images, list ) {
102 if ( strcmp ( image->name, name ) == 0 )
110 * Load executable/loadable image into memory
112 * @v image Executable/loadable image
113 * @v type Executable/loadable image type
114 * @ret rc Return status code
116 static int image_load_type ( struct image *image, struct image_type *type ) {
119 /* Check image is actually loadable */
123 /* Try the image loader */
124 if ( ( rc = type->load ( image ) ) != 0 ) {
125 DBGC ( image, "IMAGE %p could not load as %s: %s\n",
126 image, type->name, strerror ( rc ) );
131 image->flags |= IMAGE_LOADED;
136 * Load executable/loadable image into memory
138 * @v image Executable/loadable image
139 * @ret rc Return status code
141 int image_load ( struct image *image ) {
143 assert ( image->type != NULL );
145 return image_load_type ( image, image->type );
149 * Autodetect image type and load executable/loadable image into memory
151 * @v image Executable/loadable image
152 * @ret rc Return status code
154 int image_autoload ( struct image *image ) {
155 struct image_type *type;
158 for ( type = image_types ; type < image_types_end ; type++ ) {
159 DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
160 rc = image_load_type ( image, type );
161 if ( image->type == NULL )
166 DBGC ( image, "IMAGE %p format not recognised\n", image );
171 * Execute loaded image
173 * @v image Loaded image
174 * @ret rc Return status code
176 int image_exec ( struct image *image ) {
179 /* Image must be loaded first */
180 if ( ! ( image->flags & IMAGE_LOADED ) ) {
181 DBGC ( image, "IMAGE %p could not execute: not loaded\n",
186 assert ( image->type != NULL );
188 /* Check that image is actually executable */
189 if ( ! image->type->exec )
192 /* Try executing the image */
193 if ( ( rc = image->type->exec ( image ) ) != 0 ) {
194 DBGC ( image, "IMAGE %p could not execute: %s\n",
195 image, strerror ( rc ) );
199 /* Well, some formats might return... */