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/image.h>
30 * Executable/loadable images
34 /** List of registered images */
35 struct list_head images = LIST_HEAD_INIT ( images );
37 /** List of image types */
38 static struct image_type image_types[0]
39 __table_start ( struct image_type, image_types );
40 static struct image_type image_types_end[0]
41 __table_end ( struct image_type, image_types );
44 * Register executable/loadable image
46 * @v image Executable/loadable image
47 * @ret rc Return status code
49 int register_image ( struct image *image ) {
50 static unsigned int imgindex = 0;
52 /* Create image name if it doesn't already have one */
53 if ( ! image->name[0] ) {
54 snprintf ( image->name, sizeof ( image->name ), "img%d",
58 /* Add to image list */
59 list_add_tail ( &image->list, &images );
60 DBGC ( image, "IMAGE %p registered as %s\n", image, image->name );
66 * Unregister executable/loadable image
68 * @v image Executable/loadable image
70 void unregister_image ( struct image *image ) {
71 list_del ( &image->list );
72 DBGC ( image, "IMAGE %p unregistered\n", image );
76 * Load executable/loadable image into memory
78 * @v image Executable/loadable image
79 * @ret rc Return status code
81 int image_load ( struct image *image ) {
84 assert ( image->type != NULL );
86 if ( ( rc = image->type->load ( image ) ) != 0 ) {
87 DBGC ( image, "IMAGE %p could not load: %s\n",
88 image, strerror ( rc ) );
96 * Autodetect image type and load executable/loadable image into memory
98 * @v image Executable/loadable image
99 * @ret rc Return status code
101 int image_autoload ( struct image *image ) {
102 struct image_type *type;
105 for ( type = image_types ; type < image_types_end ; type++ ) {
106 rc = type->load ( image );
107 if ( image->type == NULL )
110 DBGC ( image, "IMAGE %p (%s) could not load: %s\n",
111 image, image->type->name, strerror ( rc ) );
117 DBGC ( image, "IMAGE %p format not recognised\n", image );
122 * Execute loaded image
124 * @v image Loaded image
125 * @ret rc Return status code
127 int image_exec ( struct image *image ) {
130 assert ( image->type != NULL );
132 if ( ( rc = image->type->exec ( image ) ) != 0 ) {
133 DBGC ( image, "IMAGE %p could not execute: %s\n",
134 image, strerror ( rc ) );
138 /* Well, some formats might return... */