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 if ( ( rc = type->load ( image ) ) != 0 ) {
120 DBGC ( image, "IMAGE %p could not load as %s: %s\n",
121 image, type->name, strerror ( rc ) );
126 image->flags |= IMAGE_LOADED;
131 * Load executable/loadable image into memory
133 * @v image Executable/loadable image
134 * @ret rc Return status code
136 int image_load ( struct image *image ) {
138 assert ( image->type != NULL );
140 return image_load_type ( image, image->type );
144 * Autodetect image type and load executable/loadable image into memory
146 * @v image Executable/loadable image
147 * @ret rc Return status code
149 int image_autoload ( struct image *image ) {
150 struct image_type *type;
153 for ( type = image_types ; type < image_types_end ; type++ ) {
154 DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
155 rc = image_load_type ( image, type );
156 if ( image->type == NULL )
161 DBGC ( image, "IMAGE %p format not recognised\n", image );
166 * Execute loaded image
168 * @v image Loaded image
169 * @ret rc Return status code
171 int image_exec ( struct image *image ) {
174 /* Image must be loaded first */
175 if ( ! ( image->flags & IMAGE_LOADED ) ) {
176 DBGC ( image, "IMAGE %p could not execute: not loaded\n",
181 assert ( image->type != NULL );
183 /* Try executing the image */
184 if ( ( rc = image->type->exec ( image ) ) != 0 ) {
185 DBGC ( image, "IMAGE %p could not execute: %s\n",
186 image, strerror ( rc ) );
190 /* Well, some formats might return... */