Some shuffling around of the image management code; this needs tidying up.
return 0;
/* Create cpio header before non-prebuilt images */
- if ( filename[0] ) {
+ if ( filename && filename[0] ) {
size_t name_len = ( strlen ( filename ) + 1 );
DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
unsigned int insert;
physaddr_t start;
physaddr_t end;
+ char *cmdline;
unsigned int i;
/* Add each image as a multiboot module */
( ( count - insert ) * sizeof ( *module ) ));
module->mod_start = start;
module->mod_end = end;
- module->string = virt_to_phys ( module_image->cmdline);
+ cmdline = ( module_image->cmdline ?
+ module_image->cmdline : "" );
+ module->string = virt_to_phys ( cmdline );
module->reserved = 0;
/* We promise to page-align modules */
*/
static int multiboot_exec ( struct image *image ) {
physaddr_t entry = image->priv.phys;
+ char *cmdline;
/* Populate multiboot information structure */
memset ( &mbinfo, 0, sizeof ( mbinfo ) );
MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
multiboot_build_memmap ( image, &mbinfo, mbmemmap,
( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) );
- mbinfo.cmdline = virt_to_phys ( image->cmdline );
+ cmdline = ( image->cmdline ? image->cmdline : "" );
+ mbinfo.cmdline = virt_to_phys ( cmdline );
mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
mbinfo.mods_addr = virt_to_phys ( mbmodules );
#include <stdio.h>
#include <errno.h>
#include <assert.h>
+#include <libgen.h>
#include <gpxe/list.h>
#include <gpxe/umalloc.h>
+#include <gpxe/uri.h>
#include <gpxe/image.h>
/** @file
static void free_image ( struct refcnt *refcnt ) {
struct image *image = container_of ( refcnt, struct image, refcnt );
+ uri_put ( image->uri );
ufree ( image->data );
free ( image );
DBGC ( image, "IMAGE %p freed\n", image );
return image;
}
+/**
+ * Set image URI
+ *
+ * @v image Image
+ * @v URI New image URI
+ * @ret rc Return status code
+ *
+ * If no name is set, the name will be updated to the base name of the
+ * URI path (if any).
+ */
+int image_set_uri ( struct image *image, struct uri *uri ) {
+ const char *path = uri->path;
+
+ /* Replace URI reference */
+ uri_put ( image->uri );
+ image->uri = uri_get ( uri );
+
+ /* Set name if none already specified */
+ if ( path && ( ! image->name[0] ) )
+ image_set_name ( image, basename ( ( char * ) path ) );
+
+ return 0;
+}
+
+/**
+ * Set image command line
+ *
+ * @v image Image
+ * @v cmdline New image command line
+ * @ret rc Return status code
+ */
+int image_set_cmdline ( struct image *image, const char *cmdline ) {
+ free ( image->cmdline );
+ image->cmdline = strdup ( cmdline );
+ if ( ! image->cmdline )
+ return -ENOMEM;
+ return 0;
+}
+
/**
* Register executable/loadable image
*
/* Well, some formats might return... */
return 0;
}
+
+/**
+ * Register and autoload an image
+ *
+ * @v image Image
+ * @ret rc Return status code
+ */
+int register_and_autoload_image ( struct image *image ) {
+ int rc;
+
+ if ( ( rc = register_image ( image ) ) != 0 )
+ return rc;
+
+ if ( ( rc = image_autoload ( image ) ) != 0 )
+ return rc;
+
+ return 0;
+}
+
+/**
+ * Register and autoexec an image
+ *
+ * @v image Image
+ * @ret rc Return status code
+ */
+int register_and_autoexec_image ( struct image *image ) {
+ int rc;
+
+ if ( ( rc = register_and_autoload_image ( image ) ) != 0 )
+ return rc;
+
+ if ( ( rc = image_exec ( image ) ) != 0 )
+ return rc;
+
+ return 0;
+}
*
*/
-/**
- * Print image description
- *
- */
+enum image_action {
+ IMG_FETCH = 0,
+ IMG_LOAD,
+ IMG_EXEC,
+};
/**
* Fill in image command line
* @v image Image
* @v nargs Argument count
* @v args Argument list
+ * @ret rc Return status code
*/
-static void imgfill_cmdline ( struct image *image, unsigned int nargs,
- char **args ) {
+static int imgfill_cmdline ( struct image *image, unsigned int nargs,
+ char **args ) {
+ char buf[256];
size_t used = 0;
- image->cmdline[0] = '\0';
- while ( ( used < sizeof ( image->cmdline ) ) && nargs-- ) {
- used += snprintf ( &image->cmdline[used],
- ( sizeof ( image->cmdline ) - used ),
- "%s%s", ( used ? " " : "" ), *(args++) );
+ memset ( buf, 0, sizeof ( buf ) );
+ while ( ( used < sizeof ( buf ) ) && nargs-- ) {
+ used += snprintf ( &buf[used], ( sizeof ( buf ) - used ),
+ " %s", *(args++) );
}
+
+ return image_set_cmdline ( image, &buf[1] );
}
/**
*
* @v argv Argument list
*/
-static void imgfetch_core_syntax ( char **argv, int load ) {
+static void imgfetch_core_syntax ( char **argv, enum image_action action ) {
+ static const char *actions[] = {
+ [IMG_FETCH] = "Fetch",
+ [IMG_LOAD] = "Fetch and load",
+ [IMG_EXEC] = "Fetch and execute",
+ };
+
printf ( "Usage:\n"
" %s [-n|--name <name>] filename [arguments...]\n"
"\n"
"%s executable/loadable image\n",
- argv[0], ( load ? "Fetch and load" : "Fetch" ) );
+ argv[0], actions[action] );
}
/**
* @v argv Argument list
* @ret rc Return status code
*/
-static int imgfetch_core_exec ( struct image_type *image_type, int load,
+static int imgfetch_core_exec ( struct image_type *image_type,
+ enum image_action action,
int argc, char **argv ) {
static struct option longopts[] = {
{ "help", 0, NULL, 'h' },
struct image *image;
const char *name = NULL;
char *filename;
+ int ( * image_register ) ( struct image *image );
int c;
int rc;
/* Display help text */
default:
/* Unrecognised/invalid option */
- imgfetch_core_syntax ( argv, load );
+ imgfetch_core_syntax ( argv, action );
return -EINVAL;
}
}
/* Need at least a filename remaining after the options */
if ( optind == argc ) {
- imgfetch_core_syntax ( argv, load );
+ imgfetch_core_syntax ( argv, action );
return -EINVAL;
}
filename = argv[optind++];
}
/* Fill in image name */
- if ( name )
- strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
+ if ( name ) {
+ if ( ( rc = image_set_name ( image, name ) ) != 0 )
+ return rc;
+ }
/* Set image type (if specified) */
image->type = image_type;
/* Fill in command line */
- imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
+ if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
+ &argv[optind] ) ) != 0 )
+ return rc;
/* Fetch the image */
- if ( ( rc = imgfetch ( image, filename, load ) ) != 0 ) {
+ switch ( action ) {
+ case IMG_FETCH:
+ image_register = register_image;
+ break;
+ case IMG_LOAD:
+ image_register = register_and_autoload_image;
+ break;
+ case IMG_EXEC:
+ image_register = register_and_autoexec_image;
+ break;
+ default:
+ assert ( 0 );
+ return -EINVAL;
+ }
+ if ( ( rc = imgfetch ( image, filename, image_register ) ) != 0 ) {
printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
image_put ( image );
return rc;
static int imgfetch_exec ( int argc, char **argv ) {
int rc;
- if ( ( rc = imgfetch_core_exec ( NULL, 0, argc, argv ) ) != 0 )
+ if ( ( rc = imgfetch_core_exec ( NULL, IMG_FETCH,
+ argc, argv ) ) != 0 )
return rc;
return 0;
static int kernel_exec ( int argc, char **argv ) {
int rc;
- if ( ( rc = imgfetch_core_exec ( NULL, 1, argc, argv ) ) != 0 )
+ if ( ( rc = imgfetch_core_exec ( NULL, IMG_LOAD, argc, argv ) ) != 0 )
return rc;
return 0;
static int initrd_exec ( int argc, char **argv ) {
int rc;
- if ( ( rc = imgfetch_core_exec ( &initrd_image_type, 0,
+ if ( ( rc = imgfetch_core_exec ( &initrd_image_type, IMG_FETCH,
argc, argv ) ) != 0 )
return rc;
struct image *image;
const char *name;
int c;
+ int rc;
/* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
printf ( "No such image: %s\n", name );
return 1;
}
- imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
+ if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
+ &argv[optind] ) ) != 0 )
+ return rc;
+
return 0;
}
#include <gpxe/uaccess.h>
#include <gpxe/refcnt.h>
+struct uri;
struct image_type;
-/** Maximum length of a command line */
-#define CMDLINE_MAX 128
-
/** An executable or loadable image */
struct image {
/** Reference count */
struct refcnt refcnt;
- /** Name */
- char name[16];
/** List of registered images */
struct list_head list;
+
+ /** URI of image */
+ struct uri *uri;
+ /** Name */
+ char name[16];
/** Flags */
unsigned int flags;
/** Command line to pass to image */
- char cmdline[CMDLINE_MAX];
+ char *cmdline;
/** Raw file image */
userptr_t data;
/** Length of raw file image */
list_for_each_entry ( (image), &images, list )
extern struct image * alloc_image ( void );
+extern int image_set_uri ( struct image *image, struct uri *uri );
+extern int image_set_cmdline ( struct image *image, const char *cmdline );
extern int register_image ( struct image *image );
extern void unregister_image ( struct image *image );
extern void promote_image ( struct image *image );
extern int image_load ( struct image *image );
extern int image_autoload ( struct image *image );
extern int image_exec ( struct image *image );
+extern int register_and_autoload_image ( struct image *image );
+extern int register_and_autoexec_image ( struct image *image );
/**
* Increment reference count on an image
ref_put ( &image->refcnt );
}
+/**
+ * Set image name
+ *
+ * @v image Image
+ * @v name New image name
+ * @ret rc Return status code
+ */
+static inline int image_set_name ( struct image *image, const char *name ) {
+ strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
+ return 0;
+}
+
#endif /* _GPXE_IMAGE_H */
* @v uri URI
* @ret has_relative_path URI has a relative path
*
- * An relative path begins with something other than a '/'. Note that
+ * A relative path begins with something other than a '/'. Note that
* this is a separate concept from a relative URI. Note also that a
* URI may not have a path at all.
*/
/**
* Decrement URI reference count
*
- * @v uri URI
+ * @v uri URI, or NULL
*/
static inline __attribute__ (( always_inline )) void
uri_put ( struct uri *uri ) {
struct image;
-extern int imgfetch ( struct image *image, const char *filename, int load );
+extern int imgfetch ( struct image *image, const char *uri_string,
+ int ( * image_register ) ( struct image *image ) );
extern int imgload ( struct image *image );
extern int imgexec ( struct image *image );
extern struct image * imgautoselect ( void );
printf ( "Out of memory\n" );
return -ENOMEM;
}
- if ( ( rc = imgfetch ( image, filename, 0 ) ) != 0 ) {
+ if ( ( rc = imgfetch ( image, filename,
+ register_and_autoexec_image ) ) != 0 ) {
printf ( "Could not retrieve %s: %s\n",
filename, strerror ( rc ) );
image_put ( image );
return rc;
}
- if ( ( rc = imgload ( image ) ) != 0 ) {
- printf ( "Could not load %s: %s\n", image->name,
- strerror ( rc ) );
- image_put ( image );
- return rc;
- }
- if ( ( rc = imgexec ( image ) ) != 0 ) {
- printf ( "Could not execute %s: %s\n", image->name,
- strerror ( rc ) );
- image_put ( image );
- return rc;
- }
+ image_put ( image );
return 0;
}
#include <gpxe/downloader.h>
#include <gpxe/monojob.h>
#include <gpxe/open.h>
+#include <gpxe/uri.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
*
* @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
* @v name Name for image, or NULL
- * @ret new_image Newly created image
+ * @v register_image Image registration routine
* @ret rc Return status code
*/
-int imgfetch ( struct image *image, const char *uri_string, int load ) {
+int imgfetch ( struct image *image, const char *uri_string,
+ int ( * image_register ) ( struct image *image ) ) {
+ struct uri *uri;
int rc;
- if ( ( rc = create_downloader ( &monojob, image,
- ( load ? imgfetch_autoload :
- register_image ),
- LOCATION_URI_STRING,
- uri_string ) ) == 0 )
+ if ( ! ( uri = parse_uri ( uri_string ) ) )
+ return -ENOMEM;
+
+ image_set_uri ( image, uri );
+
+ if ( ( rc = create_downloader ( &monojob, image, image_register,
+ LOCATION_URI, uri ) ) == 0 )
rc = monojob_wait();
+ uri_put ( uri );
return rc;
}
printf ( " [%s]", image->type->name );
if ( image->flags & IMAGE_LOADED )
printf ( " [LOADED]" );
- if ( image->cmdline[0] )
+ if ( image->cmdline )
printf ( " \"%s\"", image->cmdline );
printf ( "\n" );
}