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:
parent
9fd6a0418f
commit
d4947c05b2
|
@ -190,7 +190,7 @@ static size_t bzimage_load_initrd ( struct image *image,
|
|||
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",
|
||||
|
|
|
@ -135,6 +135,7 @@ multiboot_build_module_list ( struct image *image,
|
|||
unsigned int insert;
|
||||
physaddr_t start;
|
||||
physaddr_t end;
|
||||
char *cmdline;
|
||||
unsigned int i;
|
||||
|
||||
/* Add each image as a multiboot module */
|
||||
|
@ -169,7 +170,9 @@ multiboot_build_module_list ( struct image *image,
|
|||
( ( 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 */
|
||||
|
@ -222,6 +225,7 @@ static struct multiboot_module __bss16_array ( mbmodules, [MAX_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 ) );
|
||||
|
@ -229,7 +233,8 @@ static int multiboot_exec ( struct image *image ) {
|
|||
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 );
|
||||
|
|
|
@ -22,8 +22,10 @@
|
|||
#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
|
||||
|
@ -49,6 +51,7 @@ static struct image_type image_types_end[0]
|
|||
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 );
|
||||
|
@ -69,6 +72,45 @@ struct image * alloc_image ( void ) {
|
|||
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
|
||||
*
|
||||
|
@ -220,3 +262,39 @@ int image_exec ( struct image *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;
|
||||
}
|
||||
|
|
|
@ -33,10 +33,11 @@
|
|||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Print image description
|
||||
*
|
||||
*/
|
||||
enum image_action {
|
||||
IMG_FETCH = 0,
|
||||
IMG_LOAD,
|
||||
IMG_EXEC,
|
||||
};
|
||||
|
||||
/**
|
||||
* Fill in image command line
|
||||
|
@ -44,17 +45,20 @@
|
|||
* @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] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,12 +66,18 @@ static void imgfill_cmdline ( struct image *image, unsigned int nargs,
|
|||
*
|
||||
* @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] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,7 +89,8 @@ static void imgfetch_core_syntax ( char **argv, int load ) {
|
|||
* @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' },
|
||||
|
@ -89,6 +100,7 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
|
|||
struct image *image;
|
||||
const char *name = NULL;
|
||||
char *filename;
|
||||
int ( * image_register ) ( struct image *image );
|
||||
int c;
|
||||
int rc;
|
||||
|
||||
|
@ -104,14 +116,14 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
|
|||
/* 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++];
|
||||
|
@ -126,17 +138,35 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
|
|||
}
|
||||
|
||||
/* 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;
|
||||
|
@ -156,7 +186,8 @@ static int imgfetch_core_exec ( struct image_type *image_type, int load,
|
|||
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;
|
||||
|
@ -172,7 +203,7 @@ static int imgfetch_exec ( int argc, char **argv ) {
|
|||
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;
|
||||
|
@ -188,7 +219,7 @@ static int kernel_exec ( int argc, char **argv ) {
|
|||
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;
|
||||
|
||||
|
@ -286,6 +317,7 @@ static int imgargs_exec ( int argc, char **argv ) {
|
|||
struct image *image;
|
||||
const char *name;
|
||||
int c;
|
||||
int rc;
|
||||
|
||||
/* Parse options */
|
||||
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 );
|
||||
return 1;
|
||||
}
|
||||
imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
|
||||
if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
|
||||
&argv[optind] ) ) != 0 )
|
||||
return rc;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -13,25 +13,26 @@
|
|||
#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 */
|
||||
|
@ -114,6 +115,8 @@ extern struct list_head images;
|
|||
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 );
|
||||
|
@ -121,6 +124,8 @@ struct image * find_image ( const char *name );
|
|||
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
|
||||
|
@ -142,4 +147,16 @@ static inline void image_put ( struct image *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 */
|
||||
|
|
|
@ -94,7 +94,7 @@ static inline int uri_has_absolute_path ( struct uri *uri ) {
|
|||
* @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.
|
||||
*/
|
||||
|
@ -117,7 +117,7 @@ uri_get ( struct uri *uri ) {
|
|||
/**
|
||||
* Decrement URI reference count
|
||||
*
|
||||
* @v uri URI
|
||||
* @v uri URI, or NULL
|
||||
*/
|
||||
static inline __attribute__ (( always_inline )) void
|
||||
uri_put ( struct uri *uri ) {
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
|
||||
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 );
|
||||
|
|
|
@ -60,25 +60,15 @@ static int boot_filename ( const char *filename ) {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <gpxe/downloader.h>
|
||||
#include <gpxe/monojob.h>
|
||||
#include <gpxe/open.h>
|
||||
#include <gpxe/uri.h>
|
||||
#include <usr/imgmgmt.h>
|
||||
|
||||
/** @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
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
@ -118,7 +112,7 @@ void imgstat ( struct image *image ) {
|
|||
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" );
|
||||
}
|
||||
|
|
Reference in New Issue