Allow images to hold references to the originating URI.

Some shuffling around of the image management code; this needs tidying up.
This commit is contained in:
Michael Brown
2007-08-02 20:18:32 +01:00
parent 9fd6a0418f
commit d4947c05b2
9 changed files with 189 additions and 69 deletions

View File

@@ -190,7 +190,7 @@ static size_t bzimage_load_initrd ( struct image *image,
return 0; return 0;
/* Create cpio header before non-prebuilt images */ /* Create cpio header before non-prebuilt images */
if ( filename[0] ) { if ( filename && filename[0] ) {
size_t name_len = ( strlen ( filename ) + 1 ); size_t name_len = ( strlen ( filename ) + 1 );
DBGC ( image, "bzImage %p inserting initrd %p as %s\n", DBGC ( image, "bzImage %p inserting initrd %p as %s\n",

View File

@@ -135,6 +135,7 @@ multiboot_build_module_list ( struct image *image,
unsigned int insert; unsigned int insert;
physaddr_t start; physaddr_t start;
physaddr_t end; physaddr_t end;
char *cmdline;
unsigned int i; unsigned int i;
/* Add each image as a multiboot module */ /* Add each image as a multiboot module */
@@ -169,7 +170,9 @@ multiboot_build_module_list ( struct image *image,
( ( count - insert ) * sizeof ( *module ) )); ( ( count - insert ) * sizeof ( *module ) ));
module->mod_start = start; module->mod_start = start;
module->mod_end = end; 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; module->reserved = 0;
/* We promise to page-align modules */ /* We promise to page-align modules */
@@ -222,6 +225,7 @@ static struct multiboot_module __bss16_array ( mbmodules, [MAX_MODULES] );
*/ */
static int multiboot_exec ( struct image *image ) { static int multiboot_exec ( struct image *image ) {
physaddr_t entry = image->priv.phys; physaddr_t entry = image->priv.phys;
char *cmdline;
/* Populate multiboot information structure */ /* Populate multiboot information structure */
memset ( &mbinfo, 0, sizeof ( mbinfo ) ); memset ( &mbinfo, 0, sizeof ( mbinfo ) );
@@ -229,7 +233,8 @@ static int multiboot_exec ( struct image *image ) {
MBI_FLAG_CMDLINE | MBI_FLAG_MODS ); MBI_FLAG_CMDLINE | MBI_FLAG_MODS );
multiboot_build_memmap ( image, &mbinfo, mbmemmap, multiboot_build_memmap ( image, &mbinfo, mbmemmap,
( sizeof(mbmemmap) / sizeof(mbmemmap[0]) ) ); ( 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, mbinfo.mods_count = multiboot_build_module_list ( image, mbmodules,
( sizeof(mbmodules) / sizeof(mbmodules[0]) ) ); ( sizeof(mbmodules) / sizeof(mbmodules[0]) ) );
mbinfo.mods_addr = virt_to_phys ( mbmodules ); mbinfo.mods_addr = virt_to_phys ( mbmodules );

View File

@@ -22,8 +22,10 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <libgen.h>
#include <gpxe/list.h> #include <gpxe/list.h>
#include <gpxe/umalloc.h> #include <gpxe/umalloc.h>
#include <gpxe/uri.h>
#include <gpxe/image.h> #include <gpxe/image.h>
/** @file /** @file
@@ -49,6 +51,7 @@ static struct image_type image_types_end[0]
static void free_image ( struct refcnt *refcnt ) { static void free_image ( struct refcnt *refcnt ) {
struct image *image = container_of ( refcnt, struct image, refcnt ); struct image *image = container_of ( refcnt, struct image, refcnt );
uri_put ( image->uri );
ufree ( image->data ); ufree ( image->data );
free ( image ); free ( image );
DBGC ( image, "IMAGE %p freed\n", image ); DBGC ( image, "IMAGE %p freed\n", image );
@@ -69,6 +72,45 @@ struct image * alloc_image ( void ) {
return 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 * Register executable/loadable image
* *
@@ -220,3 +262,39 @@ int image_exec ( struct image *image ) {
/* Well, some formats might return... */ /* Well, some formats might return... */
return 0; 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;
}

View File

@@ -33,10 +33,11 @@
* *
*/ */
/** enum image_action {
* Print image description IMG_FETCH = 0,
* IMG_LOAD,
*/ IMG_EXEC,
};
/** /**
* Fill in image command line * Fill in image command line
@@ -44,17 +45,20 @@
* @v image Image * @v image Image
* @v nargs Argument count * @v nargs Argument count
* @v args Argument list * @v args Argument list
* @ret rc Return status code
*/ */
static void imgfill_cmdline ( struct image *image, unsigned int nargs, static int imgfill_cmdline ( struct image *image, unsigned int nargs,
char **args ) { char **args ) {
char buf[256];
size_t used = 0; size_t used = 0;
image->cmdline[0] = '\0'; memset ( buf, 0, sizeof ( buf ) );
while ( ( used < sizeof ( image->cmdline ) ) && nargs-- ) { while ( ( used < sizeof ( buf ) ) && nargs-- ) {
used += snprintf ( &image->cmdline[used], used += snprintf ( &buf[used], ( sizeof ( buf ) - used ),
( sizeof ( image->cmdline ) - used ), " %s", *(args++) );
"%s%s", ( used ? " " : "" ), *(args++) );
} }
return image_set_cmdline ( image, &buf[1] );
} }
/** /**
@@ -62,12 +66,18 @@ static void imgfill_cmdline ( struct image *image, unsigned int nargs,
* *
* @v argv Argument list * @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" printf ( "Usage:\n"
" %s [-n|--name <name>] filename [arguments...]\n" " %s [-n|--name <name>] filename [arguments...]\n"
"\n" "\n"
"%s executable/loadable image\n", "%s executable/loadable image\n",
argv[0], ( load ? "Fetch and load" : "Fetch" ) ); argv[0], actions[action] );
} }
/** /**
@@ -79,7 +89,8 @@ static void imgfetch_core_syntax ( char **argv, int load ) {
* @v argv Argument list * @v argv Argument list
* @ret rc Return status code * @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 ) { int argc, char **argv ) {
static struct option longopts[] = { static struct option longopts[] = {
{ "help", 0, NULL, 'h' }, { "help", 0, NULL, 'h' },
@@ -89,6 +100,7 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
struct image *image; struct image *image;
const char *name = NULL; const char *name = NULL;
char *filename; char *filename;
int ( * image_register ) ( struct image *image );
int c; int c;
int rc; int rc;
@@ -104,14 +116,14 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
/* Display help text */ /* Display help text */
default: default:
/* Unrecognised/invalid option */ /* Unrecognised/invalid option */
imgfetch_core_syntax ( argv, load ); imgfetch_core_syntax ( argv, action );
return -EINVAL; return -EINVAL;
} }
} }
/* Need at least a filename remaining after the options */ /* Need at least a filename remaining after the options */
if ( optind == argc ) { if ( optind == argc ) {
imgfetch_core_syntax ( argv, load ); imgfetch_core_syntax ( argv, action );
return -EINVAL; return -EINVAL;
} }
filename = argv[optind++]; filename = argv[optind++];
@@ -126,17 +138,35 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
} }
/* Fill in image name */ /* Fill in image name */
if ( name ) if ( name ) {
strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) ); if ( ( rc = image_set_name ( image, name ) ) != 0 )
return rc;
}
/* Set image type (if specified) */ /* Set image type (if specified) */
image->type = image_type; image->type = image_type;
/* Fill in command line */ /* 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 */ /* 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 ) ); printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
image_put ( image ); image_put ( image );
return rc; return rc;
@@ -156,7 +186,8 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
static int imgfetch_exec ( int argc, char **argv ) { static int imgfetch_exec ( int argc, char **argv ) {
int rc; 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 rc;
return 0; return 0;
@@ -172,7 +203,7 @@ static int imgfetch_exec ( int argc, char **argv ) {
static int kernel_exec ( int argc, char **argv ) { static int kernel_exec ( int argc, char **argv ) {
int rc; 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 rc;
return 0; return 0;
@@ -188,7 +219,7 @@ static int kernel_exec ( int argc, char **argv ) {
static int initrd_exec ( int argc, char **argv ) { static int initrd_exec ( int argc, char **argv ) {
int rc; int rc;
if ( ( rc = imgfetch_core_exec ( &initrd_image_type, 0, if ( ( rc = imgfetch_core_exec ( &initrd_image_type, IMG_FETCH,
argc, argv ) ) != 0 ) argc, argv ) ) != 0 )
return rc; return rc;
@@ -286,6 +317,7 @@ static int imgargs_exec ( int argc, char **argv ) {
struct image *image; struct image *image;
const char *name; const char *name;
int c; int c;
int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
@@ -312,7 +344,10 @@ static int imgargs_exec ( int argc, char **argv ) {
printf ( "No such image: %s\n", name ); printf ( "No such image: %s\n", name );
return 1; return 1;
} }
imgfill_cmdline ( image, ( argc - optind ), &argv[optind] ); if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
&argv[optind] ) ) != 0 )
return rc;
return 0; return 0;
} }

View File

@@ -13,25 +13,26 @@
#include <gpxe/uaccess.h> #include <gpxe/uaccess.h>
#include <gpxe/refcnt.h> #include <gpxe/refcnt.h>
struct uri;
struct image_type; struct image_type;
/** Maximum length of a command line */
#define CMDLINE_MAX 128
/** An executable or loadable image */ /** An executable or loadable image */
struct image { struct image {
/** Reference count */ /** Reference count */
struct refcnt refcnt; struct refcnt refcnt;
/** Name */
char name[16];
/** List of registered images */ /** List of registered images */
struct list_head list; struct list_head list;
/** URI of image */
struct uri *uri;
/** Name */
char name[16];
/** Flags */ /** Flags */
unsigned int flags; unsigned int flags;
/** Command line to pass to image */ /** Command line to pass to image */
char cmdline[CMDLINE_MAX]; char *cmdline;
/** Raw file image */ /** Raw file image */
userptr_t data; userptr_t data;
/** Length of raw file image */ /** Length of raw file image */
@@ -114,6 +115,8 @@ extern struct list_head images;
list_for_each_entry ( (image), &images, list ) list_for_each_entry ( (image), &images, list )
extern struct image * alloc_image ( void ); 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 int register_image ( struct image *image );
extern void unregister_image ( struct image *image ); extern void unregister_image ( struct image *image );
extern void promote_image ( struct image *image ); extern void promote_image ( struct image *image );
@@ -121,6 +124,8 @@ struct image * find_image ( const char *name );
extern int image_load ( struct image *image ); extern int image_load ( struct image *image );
extern int image_autoload ( struct image *image ); extern int image_autoload ( struct image *image );
extern int image_exec ( 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 * Increment reference count on an image
@@ -142,4 +147,16 @@ static inline void image_put ( struct image *image ) {
ref_put ( &image->refcnt ); 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 */ #endif /* _GPXE_IMAGE_H */

View File

@@ -94,7 +94,7 @@ static inline int uri_has_absolute_path ( struct uri *uri ) {
* @v uri URI * @v uri URI
* @ret has_relative_path URI has a relative path * @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 * this is a separate concept from a relative URI. Note also that a
* URI may not have a path at all. * URI may not have a path at all.
*/ */
@@ -117,7 +117,7 @@ uri_get ( struct uri *uri ) {
/** /**
* Decrement URI reference count * Decrement URI reference count
* *
* @v uri URI * @v uri URI, or NULL
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) void
uri_put ( struct uri *uri ) { uri_put ( struct uri *uri ) {

View File

@@ -9,7 +9,8 @@
struct image; 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 imgload ( struct image *image );
extern int imgexec ( struct image *image ); extern int imgexec ( struct image *image );
extern struct image * imgautoselect ( void ); extern struct image * imgautoselect ( void );

View File

@@ -60,25 +60,15 @@ static int boot_filename ( const char *filename ) {
printf ( "Out of memory\n" ); printf ( "Out of memory\n" );
return -ENOMEM; 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", printf ( "Could not retrieve %s: %s\n",
filename, strerror ( rc ) ); filename, strerror ( rc ) );
image_put ( image ); image_put ( image );
return rc; 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; return 0;
} }

View File

@@ -24,6 +24,7 @@
#include <gpxe/downloader.h> #include <gpxe/downloader.h>
#include <gpxe/monojob.h> #include <gpxe/monojob.h>
#include <gpxe/open.h> #include <gpxe/open.h>
#include <gpxe/uri.h>
#include <usr/imgmgmt.h> #include <usr/imgmgmt.h>
/** @file /** @file
@@ -32,36 +33,29 @@
* *
*/ */
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 * Fetch an image
* *
* @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz") * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
* @v name Name for image, or NULL * @v name Name for image, or NULL
* @ret new_image Newly created image * @v register_image Image registration routine
* @ret rc Return status code * @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; int rc;
if ( ( rc = create_downloader ( &monojob, image, if ( ! ( uri = parse_uri ( uri_string ) ) )
( load ? imgfetch_autoload : return -ENOMEM;
register_image ),
LOCATION_URI_STRING, image_set_uri ( image, uri );
uri_string ) ) == 0 )
if ( ( rc = create_downloader ( &monojob, image, image_register,
LOCATION_URI, uri ) ) == 0 )
rc = monojob_wait(); rc = monojob_wait();
uri_put ( uri );
return rc; return rc;
} }
@@ -118,7 +112,7 @@ void imgstat ( struct image *image ) {
printf ( " [%s]", image->type->name ); printf ( " [%s]", image->type->name );
if ( image->flags & IMAGE_LOADED ) if ( image->flags & IMAGE_LOADED )
printf ( " [LOADED]" ); printf ( " [LOADED]" );
if ( image->cmdline[0] ) if ( image->cmdline )
printf ( " \"%s\"", image->cmdline ); printf ( " \"%s\"", image->cmdline );
printf ( "\n" ); printf ( "\n" );
} }