#include <assert.h>
#include <vsprintf.h>
#include <gpxe/list.h>
+#include <gpxe/emalloc.h>
#include <gpxe/image.h>
/** @file
DBGC ( image, "IMAGE %p unregistered\n", image );
}
+/**
+ * Move image to start of list of registered images
+ *
+ * @v image Executable/loadable image
+ *
+ * Move the image to the start of the image list. This makes it
+ * easier to keep track of which of the images marked as loaded is
+ * likely to still be valid.
+ */
+void promote_image ( struct image *image ) {
+ list_del ( &image->list );
+ list_add ( &image->list, &images );
+}
+
/**
* Find image by name
*
}
/**
- * Free loaded image
+ * Load executable/loadable image into memory
*
* @v image Executable/loadable image
- *
- * This releases the memory being used to store the image; it does not
- * release the @c struct @c image itself, nor does it unregister the
- * image.
+ * @v type Executable/loadable image type
+ * @ret rc Return status code
*/
-void free_image ( struct image *image ) {
- efree ( image->data );
- image->data = UNULL;
- image->len = 0;
+static int image_load_type ( struct image *image, struct image_type *type ) {
+ int rc;
+
+ 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;
}
/**
* @ret rc Return status code
*/
int image_load ( struct image *image ) {
- int rc;
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;
- }
-
- image->flags |= IMAGE_LOADED;
- return 0;
+ return image_load_type ( image, image->type );
}
/**
for ( type = image_types ; type < image_types_end ; type++ ) {
DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
- rc = type->load ( image );
+ rc = image_load_type ( image, type );
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;
- }
- image->flags |= IMAGE_LOADED;
- return 0;
+ return rc;
}
DBGC ( image, "IMAGE %p format not recognised\n", image );
#include <errno.h>
#include <vsprintf.h>
#include <gpxe/image.h>
+#include <gpxe/emalloc.h>
#include <usr/fetch.h>
#include <usr/imgmgmt.h>
return 0;
err:
- free_image ( image );
+ efree ( image->data );
free ( image );
return rc;
}
* @ret rc Return status code
*/
int imgload ( struct image *image ) {
- return image_autoload ( image );
+ int rc;
+
+ /* Try to load image */
+ if ( ( rc = image_autoload ( image ) ) != 0 )
+ return rc;
+
+ /* If we succeed, move the image to the start of the list */
+ promote_image ( image );
+
+ return 0;
}
/**
*/
void imgfree ( struct image *image ) {
unregister_image ( image );
- free_image ( image );
+ efree ( image->data );
free ( image );
}