Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[image] Simplify image management commands and internal API
Remove the name, cmdline, and action parameters from imgdownload() and
imgdownload_string().  These functions now simply download and return
an image.

Add the function imgacquire(), which will interpret a "name or URI
string" parameter and return either an existing image or a newly
downloaded image.

Use imgacquire() to merge similar image-management commands that
currently differ only by whether they take the name of an existing
image or the URI of a new image to download.  For example, "chain" and
"imgexec" can now be merged.

Extend imgstat and imgfree commands to take an optional list of
images.

Remove the arbitrary restriction on the length of image names.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Mar 24, 2012
1 parent 4766b14 commit 1c127a6
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 370 deletions.
9 changes: 7 additions & 2 deletions src/arch/i386/core/runtime.c
Expand Up @@ -189,14 +189,18 @@ static int initrd_init ( void ) {
initrd_phys, ( initrd_phys + initrd_len ) );

/* Allocate image */
image = alloc_image();
image = alloc_image ( NULL );
if ( ! image ) {
DBGC ( colour, "RUNTIME could not allocate image for "
"initrd\n" );
rc = -ENOMEM;
goto err_alloc_image;
}
image_set_name ( image, "<INITRD>" );
if ( ( rc = image_set_name ( image, "<INITRD>" ) ) != 0 ) {
DBGC ( colour, "RUNTIME could not set image name: %s\n",
strerror ( rc ) );
goto err_set_name;
}

/* Allocate and copy initrd content */
image->data = umalloc ( initrd_len );
Expand Down Expand Up @@ -227,6 +231,7 @@ static int initrd_init ( void ) {

err_register_image:
err_umalloc:
err_set_name:
image_put ( image );
err_alloc_image:
return rc;
Expand Down
17 changes: 12 additions & 5 deletions src/arch/i386/interface/syslinux/comboot_call.c
Expand Up @@ -166,6 +166,8 @@ void comboot_force_text_mode ( void ) {
* Fetch kernel and optional initrd
*/
static int comboot_fetch_kernel ( char *kernel_file, char *cmdline ) {
struct image *kernel;
struct image *initrd;
char *initrd_file;
int rc;

Expand All @@ -184,8 +186,7 @@ static int comboot_fetch_kernel ( char *kernel_file, char *cmdline ) {
DBG ( "COMBOOT: fetching initrd '%s'\n", initrd_file );

/* Fetch initrd */
if ( ( rc = imgdownload_string ( initrd_file, NULL, NULL,
NULL ) ) != 0 ) {
if ( ( rc = imgdownload_string ( initrd_file, &initrd ) ) != 0){
DBG ( "COMBOOT: could not fetch initrd: %s\n",
strerror ( rc ) );
return rc;
Expand All @@ -198,14 +199,20 @@ static int comboot_fetch_kernel ( char *kernel_file, char *cmdline ) {

DBG ( "COMBOOT: fetching kernel '%s'\n", kernel_file );

/* Allocate and fetch kernel */
if ( ( rc = imgdownload_string ( kernel_file, NULL, cmdline,
image_replace ) ) != 0 ) {
/* Fetch kernel */
if ( ( rc = imgdownload_string ( kernel_file, &kernel ) ) != 0 ) {
DBG ( "COMBOOT: could not fetch kernel: %s\n",
strerror ( rc ) );
return rc;
}

/* Replace comboot image with kernel */
if ( ( rc = image_replace ( kernel ) ) != 0 ) {
DBG ( "COMBOOT: could not replace with kernel: %s\n",
strerror ( rc ) );
return rc;
}

return 0;
}

Expand Down
61 changes: 42 additions & 19 deletions src/core/image.c
Expand Up @@ -66,6 +66,7 @@ static int require_trusted_images_permanent = 0;
static void free_image ( struct refcnt *refcnt ) {
struct image *image = container_of ( refcnt, struct image, refcnt );

free ( image->name );
free ( image->cmdline );
uri_put ( image->uri );
ufree ( image->data );
Expand All @@ -77,37 +78,56 @@ static void free_image ( struct refcnt *refcnt ) {
/**
* Allocate executable image
*
* @v uri URI, or NULL
* @ret image Executable image
*/
struct image * alloc_image ( void ) {
struct image * alloc_image ( struct uri *uri ) {
const char *name;
struct image *image;
int rc;

/* Allocate image */
image = zalloc ( sizeof ( *image ) );
if ( image ) {
ref_init ( &image->refcnt, free_image );
if ( ! image )
goto err_alloc;

/* Initialise image */
ref_init ( &image->refcnt, free_image );
if ( uri ) {
image->uri = uri_get ( uri );
name = basename ( ( char * ) uri->path );
if ( ( rc = image_set_name ( image, name ) ) != 0 )
goto err_set_name;
}

return image;

err_set_name:
image_put ( image );
err_alloc:
return NULL;
}

/**
* Set image URI
* Set image name
*
* @v image Image
* @v URI New image URI
*
* If no name is set, the name will be updated to the base name of the
* URI path (if any).
* @v name New image name
* @ret rc Return status code
*/
void image_set_uri ( struct image *image, struct uri *uri ) {
const char *path = uri->path;
int image_set_name ( struct image *image, const char *name ) {
char *name_copy;

/* Replace URI reference */
uri_put ( image->uri );
image->uri = uri_get ( uri );
/* Duplicate name */
name_copy = strdup ( name );
if ( ! name_copy )
return -ENOMEM;

/* Replace existing name */
free ( image->name );
image->name = name_copy;

/* Set name if none already specified */
if ( path && ( ! image->name[0] ) )
image_set_name ( image, basename ( ( char * ) path ) );
return 0;
}

/**
Expand Down Expand Up @@ -137,11 +157,14 @@ int image_set_cmdline ( struct image *image, const char *cmdline ) {
*/
int register_image ( struct image *image ) {
static unsigned int imgindex = 0;
char name[8]; /* "imgXXXX" */
int rc;

/* Create image name if it doesn't already have one */
if ( ! image->name[0] ) {
snprintf ( image->name, sizeof ( image->name ), "img%d",
imgindex++ );
if ( ! image->name ) {
snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
if ( ( rc = image_set_name ( image, name ) ) != 0 )
return rc;
}

/* Avoid ending up with multiple "selected" images on
Expand Down
22 changes: 0 additions & 22 deletions src/core/parseopt.c
Expand Up @@ -114,28 +114,6 @@ int parse_netdev ( const char *text, struct net_device **netdev ) {
return 0;
}

/**
* Parse image name
*
* @v text Text
* @ret image Image
* @ret rc Return status code
*/
int parse_image ( const char *text, struct image **image ) {

/* Sanity check */
assert ( text != NULL );

/* Find network device */
*image = find_image ( text );
if ( ! *image ) {
printf ( "\"%s\": no such image\n", text );
return -ENOENT;
}

return 0;
}

/**
* Parse flag
*
Expand Down
5 changes: 3 additions & 2 deletions src/hci/commands/digest_cmd.c
Expand Up @@ -28,6 +28,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/crypto.h>
#include <ipxe/md5.h>
#include <ipxe/sha1.h>
#include <usr/imgmgmt.h>

/** @file
*
Expand Down Expand Up @@ -74,8 +75,8 @@ static int digest_exec ( int argc, char **argv,

for ( i = optind ; i < argc ; i++ ) {

/* find image */
if ( ( rc = parse_image ( argv[i], &image ) ) != 0 )
/* Acquire image */
if ( ( rc = imgacquire ( argv[i], &image ) ) != 0 )
continue;
offset = 0;
len = image->len;
Expand Down

0 comments on commit 1c127a6

Please sign in to comment.