/**
* The "imgfetch"/"module"/"kernel" command body
*
+ * @v image_type Image type to assign (or NULL)
+ * @v load Image will be automatically loaded after fetching
* @v argc Argument count
* @v argv Argument list
- * @v load Image will be automatically loaded after fetching
- * @ret image Fetched image
* @ret rc Return status code
*/
-static int imgfetch_core_exec ( int argc, char **argv, int load,
- struct image **image ) {
+static int imgfetch_core_exec ( struct image_type *image_type, int load,
+ int argc, char **argv ) {
static struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ "name", required_argument, NULL, 'n' },
{ NULL, 0, NULL, 0 },
};
+ struct image *image;
const char *name = NULL;
char *filename;
int c;
if ( ! name )
name = basename ( filename );
+ /* Allocate image */
+ image = alloc_image();
+ if ( ! image ) {
+ printf ( "%s\n", strerror ( -ENOMEM ) );
+ return -ENOMEM;
+ }
+
+ /* Fill in image name */
+ if ( name )
+ strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
+
+ /* Set image type (if specified) */
+ image->type = image_type;
+
+ /* Fill in command line */
+ imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
+
+ printf ( "name = %s, filename = %s\n", name, filename );
+
/* Fetch the image */
- if ( ( rc = imgfetch ( filename, name, image ) ) != 0 ) {
+ if ( ( rc = imgfetch ( image, filename, load ) ) != 0 ) {
printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
+ image_put ( image );
return rc;
}
- /* Fill in command line */
- imgfill_cmdline ( *image, ( argc - optind ), &argv[optind] );
-
+ image_put ( image );
return 0;
}
* @ret rc Exit code
*/
static int imgfetch_exec ( int argc, char **argv ) {
- struct image *image;
int rc;
- if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) ) != 0 )
- return 1;
+ if ( ( rc = imgfetch_core_exec ( NULL, 0, argc, argv ) ) != 0 )
+ return rc;
return 0;
}
* @ret rc Exit code
*/
static int kernel_exec ( int argc, char **argv ) {
- struct image *image;
int rc;
- if ( ( rc = imgfetch_core_exec ( argc, argv, 1, &image ) ) != 0 )
- return 1;
-
- /* Load image */
- if ( ( rc = imgload ( image ) ) != 0 ) {
- printf ( "Could not load %s: %s\n", image->name,
- strerror ( rc ) );
- return 1;
- }
+ if ( ( rc = imgfetch_core_exec ( NULL, 1, argc, argv ) ) != 0 )
+ return rc;
return 0;
}
* @ret rc Exit code
*/
static int initrd_exec ( int argc, char **argv ) {
- struct image *image;
int rc;
- if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) ) != 0 )
- return 1;
-
- /* Mark image as an intird */
- image->type = &initrd_image_type;
+ if ( ( rc = imgfetch_core_exec ( &initrd_image_type, 0,
+ argc, argv ) ) != 0 )
+ return rc;
return 0;
}
}
if ( ( rc = imgload ( image ) ) != 0 ) {
printf ( "Could not load %s: %s\n", name, strerror ( rc ) );
- return 1;
+ return rc;
}
return 0;
return;
}
printf ( "Booting \"%s\"\n", filename );
- if ( ( rc = imgfetch ( filename, NULL, &image ) ) != 0 ) {
+ image = alloc_image();
+ if ( ! image ) {
+ printf ( "Out of memory\n" );
+ return;
+ }
+ if ( ( rc = imgfetch ( image, filename, 0 ) ) != 0 ) {
printf ( "Could not retrieve %s: %s\n",
filename, strerror ( rc ) );
+ image_put ( image );
return;
}
if ( ( rc = imgload ( image ) ) != 0 ) {
printf ( "Could not load %s: %s\n", image->name,
strerror ( rc ) );
+ image_put ( image );
return;
}
if ( ( rc = imgexec ( image ) ) != 0 ) {
printf ( "Could not execute %s: %s\n", image->name,
strerror ( rc ) );
+ image_put ( image );
return;
}
}
#include <stdio.h>
#include <errno.h>
#include <gpxe/image.h>
-#include <gpxe/umalloc.h>
-#include <gpxe/download.h>
+#include <gpxe/downloader.h>
+#include <gpxe/monojob.h>
+#include <gpxe/open.h>
#include <usr/imgmgmt.h>
/** @file
*
*/
+static int imgfetch_autoload ( struct image *image ) {
+ int rc;
+
+ if ( ( rc = register_image ( image ) ) != 0 )
+ return rc;
+
+ if ( ( rc = image_autoload ( image ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
/**
* Fetch an image
*
* @ret new_image Newly created image
* @ret rc Return status code
*/
-int imgfetch ( const char *uri_string, const char *name,
- struct image **new_image ) {
- struct image *image;
- struct async async;
+int imgfetch ( struct image *image, const char *uri_string, int load ) {
int rc;
- /* Allocate new image */
- image = malloc ( sizeof ( *image ) );
- if ( ! image )
- return -ENOMEM;
- memset ( image, 0, sizeof ( *image ) );
+ printf ( "uri_string = %s\n", uri_string );
- /* Fill in image name */
- if ( name )
- strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
+ if ( ( rc = create_downloader ( &monojob, image,
+ ( load ? imgfetch_autoload :
+ register_image ),
+ LOCATION_URI_STRING,
+ uri_string ) ) == 0 )
+ rc = monojob_wait();
- /* Download the file */
- if ( ( rc = async_block_progress ( &async,
- start_download ( uri_string, &async,
- &image->data,
- &image->len )))!=0)
- goto err;
-
- /* Register the image */
- if ( ( rc = register_image ( image ) ) != 0 )
- goto err;
-
- *new_image = image;
- return 0;
-
- err:
- ufree ( image->data );
- free ( image );
return rc;
}
if ( ( rc = image_autoload ( image ) ) != 0 )
return rc;
- /* If we succeed, move the image to the start of the list */
-#warning "No longer exists"
- // promote_image ( image );
-
return 0;
}
*/
void imgfree ( struct image *image ) {
unregister_image ( image );
- ufree ( image->data );
- free ( image );
}