| 
				
			 | 
			
			
				
				@@ -23,6 +23,7 @@ 
			 | 
		
		
	
		
			
			| 
				23
			 | 
			
				23
			 | 
			
			
				
				 #include <assert.h> 
			 | 
		
		
	
		
			
			| 
				24
			 | 
			
				24
			 | 
			
			
				
				 #include <vsprintf.h> 
			 | 
		
		
	
		
			
			| 
				25
			 | 
			
				25
			 | 
			
			
				
				 #include <gpxe/list.h> 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				26
			 | 
			
			
				
				+#include <gpxe/emalloc.h> 
			 | 
		
		
	
		
			
			| 
				26
			 | 
			
				27
			 | 
			
			
				
				 #include <gpxe/image.h> 
			 | 
		
		
	
		
			
			| 
				27
			 | 
			
				28
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				28
			 | 
			
				29
			 | 
			
			
				
				 /** @file 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -72,6 +73,20 @@ void unregister_image ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				72
			 | 
			
				73
			 | 
			
			
				
				 	DBGC ( image, "IMAGE %p unregistered\n", image ); 
			 | 
		
		
	
		
			
			| 
				73
			 | 
			
				74
			 | 
			
			
				
				 } 
			 | 
		
		
	
		
			
			| 
				74
			 | 
			
				75
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				76
			 | 
			
			
				
				+/** 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				77
			 | 
			
			
				
				+ * Move image to start of list of registered images 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				78
			 | 
			
			
				
				+ * 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				79
			 | 
			
			
				
				+ * @v image		Executable/loadable image 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				80
			 | 
			
			
				
				+ * 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				81
			 | 
			
			
				
				+ * Move the image to the start of the image list.  This makes it 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				82
			 | 
			
			
				
				+ * easier to keep track of which of the images marked as loaded is 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				83
			 | 
			
			
				
				+ * likely to still be valid. 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				84
			 | 
			
			
				
				+ */ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				85
			 | 
			
			
				
				+void promote_image ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				86
			 | 
			
			
				
				+	list_del ( &image->list ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				87
			 | 
			
			
				
				+	list_add ( &image->list, &images ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				88
			 | 
			
			
				
				+} 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				89
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				75
			 | 
			
				90
			 | 
			
			
				
				 /** 
			 | 
		
		
	
		
			
			| 
				76
			 | 
			
				91
			 | 
			
			
				
				  * Find image by name 
			 | 
		
		
	
		
			
			| 
				77
			 | 
			
				92
			 | 
			
			
				
				  * 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -90,18 +105,24 @@ struct image * find_image ( const char *name ) { 
			 | 
		
		
	
		
			
			| 
				90
			 | 
			
				105
			 | 
			
			
				
				 } 
			 | 
		
		
	
		
			
			| 
				91
			 | 
			
				106
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				92
			 | 
			
				107
			 | 
			
			
				
				 /** 
			 | 
		
		
	
		
			
			| 
				93
			 | 
			
				
			 | 
			
			
				
				- * Free loaded image 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				108
			 | 
			
			
				
				+ * Load executable/loadable image into memory 
			 | 
		
		
	
		
			
			| 
				94
			 | 
			
				109
			 | 
			
			
				
				  * 
			 | 
		
		
	
		
			
			| 
				95
			 | 
			
				110
			 | 
			
			
				
				  * @v image		Executable/loadable image 
			 | 
		
		
	
		
			
			| 
				96
			 | 
			
				
			 | 
			
			
				
				- * 
			 | 
		
		
	
		
			
			| 
				97
			 | 
			
				
			 | 
			
			
				
				- * This releases the memory being used to store the image; it does not 
			 | 
		
		
	
		
			
			| 
				98
			 | 
			
				
			 | 
			
			
				
				- * release the @c struct @c image itself, nor does it unregister the 
			 | 
		
		
	
		
			
			| 
				99
			 | 
			
				
			 | 
			
			
				
				- * image. 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				111
			 | 
			
			
				
				+ * @v type		Executable/loadable image type 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				112
			 | 
			
			
				
				+ * @ret rc		Return status code 
			 | 
		
		
	
		
			
			| 
				100
			 | 
			
				113
			 | 
			
			
				
				  */ 
			 | 
		
		
	
		
			
			| 
				101
			 | 
			
				
			 | 
			
			
				
				-void free_image ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				102
			 | 
			
				
			 | 
			
			
				
				-	efree ( image->data ); 
			 | 
		
		
	
		
			
			| 
				103
			 | 
			
				
			 | 
			
			
				
				-	image->data = UNULL; 
			 | 
		
		
	
		
			
			| 
				104
			 | 
			
				
			 | 
			
			
				
				-	image->len = 0; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				114
			 | 
			
			
				
				+static int image_load_type ( struct image *image, struct image_type *type ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				115
			 | 
			
			
				
				+	int rc; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				116
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				117
			 | 
			
			
				
				+	if ( ( rc = type->load ( image ) ) != 0 ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				118
			 | 
			
			
				
				+		DBGC ( image, "IMAGE %p could not load as %s: %s\n", 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				119
			 | 
			
			
				
				+		       image, type->name, strerror ( rc ) ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				120
			 | 
			
			
				
				+		return rc; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				121
			 | 
			
			
				
				+	} 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				122
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				123
			 | 
			
			
				
				+	/* Flag as loaded */ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				124
			 | 
			
			
				
				+	image->flags |= IMAGE_LOADED; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				125
			 | 
			
			
				
				+	return 0; 
			 | 
		
		
	
		
			
			| 
				105
			 | 
			
				126
			 | 
			
			
				
				 } 
			 | 
		
		
	
		
			
			| 
				106
			 | 
			
				127
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				107
			 | 
			
				128
			 | 
			
			
				
				 /** 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -111,18 +132,10 @@ void free_image ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				111
			 | 
			
				132
			 | 
			
			
				
				  * @ret rc		Return status code 
			 | 
		
		
	
		
			
			| 
				112
			 | 
			
				133
			 | 
			
			
				
				  */ 
			 | 
		
		
	
		
			
			| 
				113
			 | 
			
				134
			 | 
			
			
				
				 int image_load ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				114
			 | 
			
				
			 | 
			
			
				
				-	int rc; 
			 | 
		
		
	
		
			
			| 
				115
			 | 
			
				135
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				116
			 | 
			
				136
			 | 
			
			
				
				 	assert ( image->type != NULL ); 
			 | 
		
		
	
		
			
			| 
				117
			 | 
			
				137
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				118
			 | 
			
				
			 | 
			
			
				
				-	if ( ( rc = image->type->load ( image ) ) != 0 ) { 
			 | 
		
		
	
		
			
			| 
				119
			 | 
			
				
			 | 
			
			
				
				-		DBGC ( image, "IMAGE %p could not load: %s\n", 
			 | 
		
		
	
		
			
			| 
				120
			 | 
			
				
			 | 
			
			
				
				-		       image, strerror ( rc ) ); 
			 | 
		
		
	
		
			
			| 
				121
			 | 
			
				
			 | 
			
			
				
				-		return rc; 
			 | 
		
		
	
		
			
			| 
				122
			 | 
			
				
			 | 
			
			
				
				-	} 
			 | 
		
		
	
		
			
			| 
				123
			 | 
			
				
			 | 
			
			
				
				- 
			 | 
		
		
	
		
			
			| 
				124
			 | 
			
				
			 | 
			
			
				
				-	image->flags |= IMAGE_LOADED; 
			 | 
		
		
	
		
			
			| 
				125
			 | 
			
				
			 | 
			
			
				
				-	return 0; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				138
			 | 
			
			
				
				+	return image_load_type ( image, image->type ); 
			 | 
		
		
	
		
			
			| 
				126
			 | 
			
				139
			 | 
			
			
				
				 } 
			 | 
		
		
	
		
			
			| 
				127
			 | 
			
				140
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				128
			 | 
			
				141
			 | 
			
			
				
				 /** 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -137,16 +150,10 @@ int image_autoload ( struct image *image ) { 
			 | 
		
		
	
		
			
			| 
				137
			 | 
			
				150
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				138
			 | 
			
				151
			 | 
			
			
				
				 	for ( type = image_types ; type < image_types_end ; type++ ) { 
			 | 
		
		
	
		
			
			| 
				139
			 | 
			
				152
			 | 
			
			
				
				 		DBGC ( image, "IMAGE %p trying type %s\n", image, type->name ); 
			 | 
		
		
	
		
			
			| 
				140
			 | 
			
				
			 | 
			
			
				
				-		rc = type->load ( image ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				153
			 | 
			
			
				
				+		rc = image_load_type ( image, type ); 
			 | 
		
		
	
		
			
			| 
				141
			 | 
			
				154
			 | 
			
			
				
				 		if ( image->type == NULL ) 
			 | 
		
		
	
		
			
			| 
				142
			 | 
			
				155
			 | 
			
			
				
				 			continue; 
			 | 
		
		
	
		
			
			| 
				143
			 | 
			
				
			 | 
			
			
				
				-		if ( rc != 0 ) { 
			 | 
		
		
	
		
			
			| 
				144
			 | 
			
				
			 | 
			
			
				
				-			DBGC ( image, "IMAGE %p (%s) could not load: %s\n", 
			 | 
		
		
	
		
			
			| 
				145
			 | 
			
				
			 | 
			
			
				
				-			       image, image->type->name, strerror ( rc ) ); 
			 | 
		
		
	
		
			
			| 
				146
			 | 
			
				
			 | 
			
			
				
				-			return rc; 
			 | 
		
		
	
		
			
			| 
				147
			 | 
			
				
			 | 
			
			
				
				-		} 
			 | 
		
		
	
		
			
			| 
				148
			 | 
			
				
			 | 
			
			
				
				-		image->flags |= IMAGE_LOADED; 
			 | 
		
		
	
		
			
			| 
				149
			 | 
			
				
			 | 
			
			
				
				-		return 0; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				156
			 | 
			
			
				
				+		return rc; 
			 | 
		
		
	
		
			
			| 
				150
			 | 
			
				157
			 | 
			
			
				
				 	} 
			 | 
		
		
	
		
			
			| 
				151
			 | 
			
				158
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				152
			 | 
			
				159
			 | 
			
			
				
				 	DBGC ( image, "IMAGE %p format not recognised\n", image ); 
			 |