image.c File Reference

Executable/loadable images. More...

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#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>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static void free_image (struct refcnt *refcnt)
 Free executable/loadable image.
struct imagealloc_image (void)
 Allocate executable/loadable image.
int image_set_uri (struct image *image, struct uri *uri)
 Set image URI.
int image_set_cmdline (struct image *image, const char *cmdline)
 Set image command line.
int register_image (struct image *image)
 Register executable/loadable image.
void unregister_image (struct image *image)
 Unregister executable/loadable image.
struct imagefind_image (const char *name)
 Find image by name.
static int image_load_type (struct image *image, struct image_type *type)
 Load executable/loadable image into memory.
int image_load (struct image *image)
 Load executable/loadable image into memory.
int image_autoload (struct image *image)
 Autodetect image type and load executable/loadable image into memory.
int image_exec (struct image *image)
 Execute loaded image.
int register_and_autoload_image (struct image *image)
 Register and autoload an image.
int register_and_autoexec_image (struct image *image)
 Register and autoexec an image.

Variables

struct list_head images = LIST_HEAD_INIT ( images )
 List of registered images.


Detailed Description

Executable/loadable images.

Definition in file image.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static void free_image ( struct refcnt refcnt  )  [static]

Free executable/loadable image.

Parameters:
refcnt Reference counter

Definition at line 47 of file image.c.

References container_of, image::data, DBGC, free(), image_put(), image::replacement, ufree(), and image::uri.

Referenced by alloc_image().

00047                                                  {
00048         struct image *image = container_of ( refcnt, struct image, refcnt );
00049 
00050         uri_put ( image->uri );
00051         ufree ( image->data );
00052         image_put ( image->replacement );
00053         free ( image );
00054         DBGC ( image, "IMAGE %p freed\n", image );
00055 }

struct image* alloc_image ( void   )  [read]

Allocate executable/loadable image.

Return values:
image Executable/loadable image

Definition at line 62 of file image.c.

References refcnt::free, free_image(), image::refcnt, and zalloc().

Referenced by boot_next_server_and_filename(), comboot_fetch_kernel(), and imgfetch_core_exec().

00062                                     {
00063         struct image *image;
00064 
00065         image = zalloc ( sizeof ( *image ) );
00066         if ( image ) {
00067                 image->refcnt.free = free_image;
00068         }
00069         return image;
00070 }

int image_set_uri ( struct image image,
struct uri uri 
)

Set image URI.

Parameters:
image Image
URI New image URI
Return values:
rc Return status code
If no name is set, the name will be updated to the base name of the URI path (if any).

Definition at line 82 of file image.c.

References basename(), image_set_name(), image::name, uri::path, and image::uri.

Referenced by imgfetch().

00082                                                            {
00083         const char *path = uri->path;
00084 
00085         /* Replace URI reference */
00086         uri_put ( image->uri );
00087         image->uri = uri_get ( uri );
00088 
00089         /* Set name if none already specified */
00090         if ( path && ( ! image->name[0] ) )
00091                 image_set_name ( image, basename ( ( char * ) path ) );
00092 
00093         return 0;
00094 }

int image_set_cmdline ( struct image image,
const char *  cmdline 
)

Set image command line.

Parameters:
image Image
cmdline New image command line
Return values:
rc Return status code

Definition at line 103 of file image.c.

References image::cmdline, ENOMEM, free(), and strdup().

Referenced by comboot_fetch_kernel(), and imgfill_cmdline().

00103                                                                    {
00104         free ( image->cmdline );
00105         image->cmdline = strdup ( cmdline );
00106         if ( ! image->cmdline )
00107                 return -ENOMEM;
00108         return 0;
00109 }

int register_image ( struct image image  ) 

Register executable/loadable image.

Parameters:
image Executable/loadable image
Return values:
rc Return status code

Definition at line 117 of file image.c.

References image::data, DBGC, image_get(), image::len, image::list, list_add_tail, image::name, snprintf(), and user_to_phys().

Referenced by comboot_fetch_kernel(), create_downloader(), embedded_init(), imgfetch_core_exec(), register_and_autoload_image(), and script_exec().

00117                                            {
00118         static unsigned int imgindex = 0;
00119 
00120         /* Create image name if it doesn't already have one */
00121         if ( ! image->name[0] ) {
00122                 snprintf ( image->name, sizeof ( image->name ), "img%d",
00123                            imgindex++ );
00124         }
00125 
00126         /* Add to image list */
00127         image_get ( image );
00128         list_add_tail ( &image->list, &images );
00129         DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
00130                image, user_to_phys ( image->data, 0 ),
00131                user_to_phys ( image->data, image->len ), image->name );
00132 
00133         return 0;
00134 }

void unregister_image ( struct image image  ) 

Unregister executable/loadable image.

Parameters:
image Executable/loadable image

Definition at line 141 of file image.c.

References DBGC, image_put(), image::list, and list_del.

Referenced by com32_exec(), comboot_exec(), imgfree(), and script_exec().

00141                                               {
00142         DBGC ( image, "IMAGE %p unregistered\n", image );
00143         list_del ( &image->list );
00144         image_put ( image );
00145 }

struct image* find_image ( const char *  name  )  [read]

Find image by name.

Parameters:
name Image name
Return values:
image Executable/loadable image, or NULL

Definition at line 153 of file image.c.

References image::list, list_for_each_entry, image::name, NULL, and strcmp().

Referenced by digest_exec(), imgargs_exec(), imgexec_exec(), imgfree_exec(), and imgload_exec().

00153                                                {
00154         struct image *image;
00155 
00156         list_for_each_entry ( image, &images, list ) {
00157                 if ( strcmp ( image->name, name ) == 0 )
00158                         return image;
00159         }
00160 
00161         return NULL;
00162 }

static int image_load_type ( struct image image,
struct image_type type 
) [static]

Load executable/loadable image into memory.

Parameters:
image Executable/loadable image
type Executable/loadable image type
Return values:
rc Return status code

Definition at line 171 of file image.c.

References DBGC, ENOEXEC, image::flags, IMAGE_LOADED, image_type::load, image_type::name, and strerror().

Referenced by image_autoload(), and image_load().

00171                                                                             {
00172         int rc;
00173 
00174         /* Check image is actually loadable */
00175         if ( ! type->load )
00176                 return -ENOEXEC;
00177 
00178         /* Try the image loader */
00179         if ( ( rc = type->load ( image ) ) != 0 ) {
00180                 DBGC ( image, "IMAGE %p could not load as %s: %s\n",
00181                        image, type->name, strerror ( rc ) );
00182                 return rc;
00183         }
00184 
00185         /* Flag as loaded */
00186         image->flags |= IMAGE_LOADED;
00187         return 0;
00188 }

int image_load ( struct image image  ) 

Load executable/loadable image into memory.

Parameters:
image Executable/loadable image
Return values:
rc Return status code

Definition at line 196 of file image.c.

References assert, image_load_type(), NULL, and image::type.

Referenced by image_autoload().

00196                                        {
00197 
00198         assert ( image->type != NULL );
00199 
00200         return image_load_type ( image, image->type );
00201 }

int image_autoload ( struct image image  ) 

Autodetect image type and load executable/loadable image into memory.

Parameters:
image Executable/loadable image
Return values:
rc Return status code

Definition at line 209 of file image.c.

References DBGC, ENOEXEC, for_each_table_entry, image_load(), image_load_type(), IMAGE_TYPES, image_type::name, NULL, and image::type.

Referenced by com32_exec(), comboot_exec(), embedded_init(), imgload(), and register_and_autoload_image().

00209                                            {
00210         struct image_type *type;
00211         int rc;
00212 
00213         /* If image already has a type, use it */
00214         if ( image->type )
00215                 return image_load ( image );
00216 
00217         /* Otherwise probe for a suitable type */
00218         for_each_table_entry ( type, IMAGE_TYPES ) {
00219                 DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
00220                 rc = image_load_type ( image, type );
00221                 if ( image->type == NULL )
00222                         continue;
00223                 return rc;
00224         }
00225 
00226         DBGC ( image, "IMAGE %p format not recognised\n", image );
00227         return -ENOEXEC;
00228 }

int image_exec ( struct image image  ) 

Execute loaded image.

Parameters:
image Loaded image
Return values:
rc Return status code

Definition at line 236 of file image.c.

References assert, churi(), cwuri, DBGC, ENOEXEC, ENOTTY, image_type::exec, image::flags, image_exec(), image_get(), IMAGE_LOADED, image_put(), NULL, image::replacement, strerror(), image::type, and image::uri.

Referenced by image_exec(), imgexec(), main(), and register_and_autoexec_image().

00236                                        {
00237         struct image *replacement;
00238         struct uri *old_cwuri;
00239         int rc;
00240 
00241         /* Image must be loaded first */
00242         if ( ! ( image->flags & IMAGE_LOADED ) ) {
00243                 DBGC ( image, "IMAGE %p could not execute: not loaded\n",
00244                        image );
00245                 return -ENOTTY;
00246         }
00247 
00248         assert ( image->type != NULL );
00249 
00250         /* Check that image is actually executable */
00251         if ( ! image->type->exec )
00252                 return -ENOEXEC;
00253 
00254         /* Switch current working directory to be that of the image itself */
00255         old_cwuri = uri_get ( cwuri );
00256         churi ( image->uri );
00257 
00258         /* Take out a temporary reference to the image.  This allows
00259          * the image to unregister itself if necessary, without
00260          * automatically freeing itself.
00261          */
00262         image_get ( image );
00263 
00264         /* Try executing the image */
00265         if ( ( rc = image->type->exec ( image ) ) != 0 ) {
00266                 DBGC ( image, "IMAGE %p could not execute: %s\n",
00267                        image, strerror ( rc ) );
00268                 /* Do not return yet; we still have clean-up to do */
00269         }
00270 
00271         /* Pick up replacement image before we drop the original
00272          * image's temporary reference.
00273          */
00274         replacement = image->replacement;
00275 
00276         /* Drop temporary reference to the original image */
00277         image_put ( image );
00278 
00279         /* Reset current working directory */
00280         churi ( old_cwuri );
00281         uri_put ( old_cwuri );
00282 
00283         /* Tail-recurse into replacement image, if one exists */
00284         if ( replacement ) {
00285                 DBGC ( image, "IMAGE %p replacing self with IMAGE %p\n",
00286                        image, replacement );
00287                 if ( ( rc = image_exec ( replacement ) ) != 0 )
00288                         return rc;
00289         }
00290 
00291         return rc;
00292 }

int register_and_autoload_image ( struct image image  ) 

Register and autoload an image.

Parameters:
image Image
Return values:
rc Return status code

Definition at line 300 of file image.c.

References image_autoload(), and register_image().

Referenced by boot_next_server_and_filename(), imgfetch_core_exec(), and register_and_autoexec_image().

00300                                                         {
00301         int rc;
00302 
00303         if ( ( rc = register_image ( image ) ) != 0 )
00304                 return rc;
00305 
00306         if ( ( rc = image_autoload ( image ) ) != 0 )
00307                 return rc;
00308 
00309         return 0;
00310 }

int register_and_autoexec_image ( struct image image  ) 

Register and autoexec an image.

Parameters:
image Image
Return values:
rc Return status code

Definition at line 318 of file image.c.

References image_exec(), and register_and_autoload_image().

Referenced by imgfetch_core_exec().

00318                                                         {
00319         int rc;
00320 
00321         if ( ( rc = register_and_autoload_image ( image ) ) != 0 )
00322                 return rc;
00323 
00324         if ( ( rc = image_exec ( image ) ) != 0 )
00325                 return rc;
00326 
00327         return 0;
00328 }


Variable Documentation

struct list_head images = LIST_HEAD_INIT ( images )

List of registered images.

Definition at line 40 of file image.c.

Referenced by have_images(), and imgfree_exec().


Generated on Tue Apr 6 20:01:14 2010 for gPXE by  doxygen 1.5.7.1