image.h File Reference

Executable/loadable images. More...

#include <gpxe/tables.h>
#include <gpxe/list.h>
#include <gpxe/uaccess.h>
#include <gpxe/refcnt.h>

Go to the source code of this file.

Data Structures

struct  image
 An executable or loadable image. More...
struct  image_type
 An executable or loadable image type. More...

Defines

#define IMAGE_LOADED   0x0001
 Image is loaded.
#define PROBE_MULTIBOOT   01
 Multiboot image probe priority.
#define PROBE_NORMAL   02
 Normal image probe priority.
#define PROBE_PXE   03
 PXE image probe priority.
#define IMAGE_TYPES   __table ( struct image_type, "image_types" )
 Executable or loadable image type table.
#define __image_type(probe_order)   __table_entry ( IMAGE_TYPES, probe_order )
 An executable or loadable image type.
#define for_each_image(image)   list_for_each_entry ( (image), &images, list )
 Iterate over all registered images.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int have_images (void)
 Test for existence of images.
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.
void promote_image (struct image *image)
struct imagefind_image (const char *name)
 Find image by name.
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.
static struct imageimage_get (struct image *image)
 Increment reference count on an image.
static void image_put (struct image *image)
 Decrement reference count on an image.
static int image_set_name (struct image *image, const char *name)
 Set image name.

Variables

struct list_head images
 List of registered images.


Detailed Description

Executable/loadable images.

Definition in file image.h.


Define Documentation

#define IMAGE_LOADED   0x0001

Image is loaded.

Definition at line 69 of file image.h.

Referenced by image_exec(), image_load_type(), imgautoselect(), and imgstat().

#define PROBE_MULTIBOOT   01

Multiboot image probe priority.

Multiboot images are also valid executables in another format (e.g. ELF), so we must perform the multiboot probe first.

Definition at line 113 of file image.h.

#define PROBE_NORMAL   02

Normal image probe priority.

Definition at line 118 of file image.h.

#define PROBE_PXE   03

PXE image probe priority.

PXE images have no signature checks, so will claim all image files. They must therefore be tried last in the probe order list.

Definition at line 126 of file image.h.

#define IMAGE_TYPES   __table ( struct image_type, "image_types" )

Executable or loadable image type table.

Definition at line 129 of file image.h.

Referenced by image_autoload().

#define __image_type ( probe_order   )     __table_entry ( IMAGE_TYPES, probe_order )

An executable or loadable image type.

Definition at line 132 of file image.h.

#define for_each_image ( image   )     list_for_each_entry ( (image), &images, list )

Iterate over all registered images.

Definition at line 137 of file image.h.

Referenced by bzimage_load_initrds(), imgautoselect(), imgstat_exec(), main(), and multiboot_build_module_list().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static int have_images ( void   )  [inline, static]

Test for existence of images.

Return values:
existence Some images exist

Definition at line 145 of file image.h.

References images, and list_empty().

Referenced by main().

00145                                        {
00146         return ( ! list_empty ( &images ) );
00147 }

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 }

void promote_image ( struct image image  ) 

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 }

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 }

static struct image* image_get ( struct image image  )  [static, read]

Increment reference count on an image.

Parameters:
image Image
Return values:
image Image

Definition at line 168 of file image.h.

References ref_get(), and image::refcnt.

Referenced by comboot_fetch_kernel(), create_downloader(), image_exec(), and register_image().

00168                                                                {
00169         ref_get ( &image->refcnt );
00170         return image;
00171 }

static void image_put ( struct image image  )  [inline, static]

Decrement reference count on an image.

Parameters:
image Image

Definition at line 178 of file image.h.

References ref_put(), and image::refcnt.

Referenced by boot_next_server_and_filename(), comboot_fetch_kernel(), downloader_free(), free_image(), image_exec(), imgfetch_core_exec(), and unregister_image().

00178                                                      {
00179         ref_put ( &image->refcnt );
00180 }

static int image_set_name ( struct image image,
const char *  name 
) [inline, static]

Set image name.

Parameters:
image Image
name New image name
Return values:
rc Return status code

Definition at line 189 of file image.h.

References image::name, and strncpy().

Referenced by image_set_uri(), and imgfetch_core_exec().

00189                                                                            {
00190         strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
00191         return 0;
00192 }


Variable Documentation

struct list_head 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:50 2010 for gPXE by  doxygen 1.5.7.1