iPXE
Macros | Functions | Variables
image.c File Reference

Executable images. More...

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <libgen.h>
#include <syslog.h>
#include <ipxe/list.h>
#include <ipxe/umalloc.h>
#include <ipxe/uri.h>
#include <ipxe/image.h>

Go to the source code of this file.

Macros

#define EACCES_UNTRUSTED   __einfo_error ( EINFO_EACCES_UNTRUSTED )
 
#define EINFO_EACCES_UNTRUSTED   __einfo_uniqify ( EINFO_EACCES, 0x01, "Untrusted image" )
 
#define EACCES_PERMANENT   __einfo_error ( EINFO_EACCES_PERMANENT )
 
#define EINFO_EACCES_PERMANENT   __einfo_uniqify ( EINFO_EACCES, 0x02, "Trust requirement is permanent" )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void free_image (struct refcnt *refcnt)
 Free executable image. More...
 
struct imagealloc_image (struct uri *uri)
 Allocate executable image. More...
 
int image_set_uri (struct image *image, struct uri *uri)
 Set image URI. More...
 
int image_set_name (struct image *image, const char *name)
 Set image name. More...
 
int image_set_cmdline (struct image *image, const char *cmdline)
 Set image command line. More...
 
int image_set_len (struct image *image, size_t len)
 Set image length. More...
 
int image_set_data (struct image *image, userptr_t data, size_t len)
 Set image data. More...
 
static int image_probe (struct image *image)
 Determine image type. More...
 
int register_image (struct image *image)
 Register executable image. More...
 
void unregister_image (struct image *image)
 Unregister executable image. More...
 
struct imagefind_image (const char *name)
 Find image by name. More...
 
int image_exec (struct image *image)
 Execute image. More...
 
int image_replace (struct image *replacement)
 Set replacement image. More...
 
int image_select (struct image *image)
 Select image for execution. More...
 
struct imageimage_find_selected (void)
 Find selected image. More...
 
int image_set_trust (int require_trusted, int permanent)
 Change image trust requirement. More...
 
struct imageimage_memory (const char *name, userptr_t data, size_t len)
 Create registered image from block of memory. More...
 
const char * image_argument (struct image *image, const char *key)
 Find argument within image command line. More...
 

Variables

struct list_head images = LIST_HEAD_INIT ( images )
 List of registered images. More...
 
struct imagecurrent_image
 Currently-executing image. More...
 
static int require_trusted_images = 0
 Current image trust requirement. More...
 
static int require_trusted_images_permanent = 0
 Prevent changes to image trust requirement. More...
 

Detailed Description

Executable images.

Definition in file image.c.

Macro Definition Documentation

◆ EACCES_UNTRUSTED

#define EACCES_UNTRUSTED   __einfo_error ( EINFO_EACCES_UNTRUSTED )

Definition at line 47 of file image.c.

◆ EINFO_EACCES_UNTRUSTED

#define EINFO_EACCES_UNTRUSTED   __einfo_uniqify ( EINFO_EACCES, 0x01, "Untrusted image" )

Definition at line 49 of file image.c.

◆ EACCES_PERMANENT

#define EACCES_PERMANENT   __einfo_error ( EINFO_EACCES_PERMANENT )

Definition at line 51 of file image.c.

◆ EINFO_EACCES_PERMANENT

#define EINFO_EACCES_PERMANENT   __einfo_uniqify ( EINFO_EACCES, 0x02, "Trust requirement is permanent" )

Definition at line 53 of file image.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ free_image()

static void free_image ( struct refcnt refcnt)
static

Free executable image.

Parameters
refcntReference counter

Definition at line 73 of file image.c.

73  {
74  struct image *image = container_of ( refcnt, struct image, refcnt );
75 
76  DBGC ( image, "IMAGE %s freed\n", image->name );
77  free ( image->name );
78  free ( image->cmdline );
79  uri_put ( image->uri );
80  ufree ( image->data );
82  free ( image );
83 }
userptr_t data
Raw file image.
Definition: image.h:41
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:39
A reference counter.
Definition: refcnt.h:26
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct uri * uri
URI of image.
Definition: image.h:32
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
static __always_inline void ufree(userptr_t userptr)
Free external memory.
Definition: umalloc.h:65
struct image * replacement
Replacement image.
Definition: image.h:60
char * name
Name.
Definition: image.h:34

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

Referenced by alloc_image().

◆ alloc_image()

struct image* alloc_image ( struct uri uri)

Allocate executable image.

Parameters
uriURI, or NULL
Return values
imageExecutable image

Definition at line 91 of file image.c.

91  {
92  struct image *image;
93  int rc;
94 
95  /* Allocate image */
96  image = zalloc ( sizeof ( *image ) );
97  if ( ! image )
98  goto err_alloc;
99 
100  /* Initialise image */
102  if ( uri && ( ( rc = image_set_uri ( image, uri ) ) != 0 ) )
103  goto err_set_uri;
104 
105  return image;
106 
107  err_set_uri:
108  image_put ( image );
109  err_alloc:
110  return NULL;
111 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int image_set_uri(struct image *image, struct uri *uri)
Set image URI.
Definition: image.c:120
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
An executable image.
Definition: image.h:24
static void free_image(struct refcnt *refcnt)
Free executable image.
Definition: image.c:73
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
A Uniform Resource Identifier.
Definition: uri.h:64
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct refcnt refcnt
Reference count.
Definition: image.h:26

References free_image(), image_put(), image_set_uri(), NULL, rc, ref_init, image::refcnt, and zalloc().

Referenced by image_extract(), image_memory(), and imgdownload().

◆ image_set_uri()

int image_set_uri ( struct image image,
struct uri uri 
)

Set image URI.

Parameters
imageImage
uriNew image URI
Return values
rcReturn status code

Definition at line 120 of file image.c.

120  {
121  const char *name;
122  int rc;
123 
124  /* Set name, if image does not already have one */
125  if ( uri->path && ( ! ( image->name && image->name[0] ) ) ) {
126  name = basename ( ( char * ) uri->path );
127  if ( ( rc = image_set_name ( image, name ) ) != 0 )
128  return rc;
129  }
130 
131  /* Update image URI */
132  uri_put ( image->uri );
133  image->uri = uri_get ( uri );
134 
135  return 0;
136 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:194
An executable image.
Definition: image.h:24
const char * path
Path (after URI decoding)
Definition: uri.h:80
char * basename(char *path)
Return base name from path.
Definition: basename.c:42
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:145
struct uri * uri
URI of image.
Definition: image.h:32
A Uniform Resource Identifier.
Definition: uri.h:64
char * name
Name.
Definition: image.h:34

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

Referenced by alloc_image(), and downloader_vredirect().

◆ image_set_name()

int image_set_name ( struct image image,
const char *  name 
)

Set image name.

Parameters
imageImage
nameNew image name
Return values
rcReturn status code

Definition at line 145 of file image.c.

145  {
146  char *name_copy;
147 
148  /* Duplicate name */
149  name_copy = strdup ( name );
150  if ( ! name_copy )
151  return -ENOMEM;
152 
153  /* Replace existing name */
154  free ( image->name );
155  image->name = name_copy;
156 
157  return 0;
158 }
const char * name
Definition: ath9k_hw.c:1984
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * strdup(const char *src)
Duplicate string.
Definition: string.c:380
char * name
Name.
Definition: image.h:34

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

Referenced by image_extract(), image_memory(), image_set_uri(), imgsingle_exec(), and register_image().

◆ image_set_cmdline()

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

Set image command line.

Parameters
imageImage
cmdlineNew image command line, or NULL
Return values
rcReturn status code

Definition at line 167 of file image.c.

167  {
168 
169  free ( image->cmdline );
170  image->cmdline = NULL;
171  if ( cmdline ) {
172  image->cmdline = strdup ( cmdline );
173  if ( ! image->cmdline )
174  return -ENOMEM;
175  }
176  return 0;
177 }
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:39
#define ENOMEM
Not enough space.
Definition: errno.h:534
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * strdup(const char *src)
Duplicate string.
Definition: string.c:380
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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

Referenced by image_clear_cmdline(), image_extract_exec(), and imgsingle_exec().

◆ image_set_len()

int image_set_len ( struct image image,
size_t  len 
)

Set image length.

Parameters
imageImage
lenLength of image data
Return values
rcReturn status code

Definition at line 186 of file image.c.

186  {
187  userptr_t new;
188 
189  /* (Re)allocate image data */
190  new = urealloc ( image->data, len );
191  if ( ! new )
192  return -ENOMEM;
193  image->data = new;
194  image->len = len;
195 
196  return 0;
197 }
userptr_t data
Raw file image.
Definition: image.h:41
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
userptr_t urealloc(userptr_t userptr, size_t new_size)
Reallocate external memory.
size_t len
Length of raw file image.
Definition: image.h:43
uint32_t len
Length.
Definition: ena.h:14
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References image::data, ENOMEM, len, image::len, and urealloc().

Referenced by gzip_extract(), image_set_data(), and zlib_deflate().

◆ image_set_data()

int image_set_data ( struct image image,
userptr_t  data,
size_t  len 
)

Set image data.

Parameters
imageImage
dataImage data
lenLength of image data
Return values
rcReturn status code

Definition at line 207 of file image.c.

207  {
208  int rc;
209 
210  /* Set image length */
211  if ( ( rc = image_set_len ( image, len ) ) != 0 )
212  return rc;
213 
214  /* Copy in new image data */
215  memcpy_user ( image->data, 0, data, 0, len );
216 
217  return 0;
218 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
An executable image.
Definition: image.h:24
int image_set_len(struct image *image, size_t len)
Set image length.
Definition: image.c:186
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void memcpy_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers.

References data, image::data, image_set_len(), len, memcpy_user(), and rc.

Referenced by image_memory().

◆ image_probe()

static int image_probe ( struct image image)
static

Determine image type.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 226 of file image.c.

226  {
227  struct image_type *type;
228  int rc;
229 
230  /* Try each type in turn */
232  if ( ( rc = type->probe ( image ) ) == 0 ) {
233  image->type = type;
234  DBGC ( image, "IMAGE %s is %s\n",
235  image->name, type->name );
236  return 0;
237  }
238  DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
239  type->name, strerror ( rc ) );
240  }
241 
242  DBGC ( image, "IMAGE %s format not recognised\n", image->name );
243  return -ENOTSUP;
244 }
#define IMAGE_TYPES
Executable image type table.
Definition: image.h:148
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct image_type * type
Image type, if known.
Definition: image.h:46
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
An executable image type.
Definition: image.h:76
An executable image.
Definition: image.h:24
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
char * name
Name.
Definition: image.h:34

References DBGC, ENOTSUP, for_each_table_entry, IMAGE_TYPES, image::name, rc, strerror(), type, and image::type.

Referenced by register_image().

◆ register_image()

int register_image ( struct image image)

Register executable image.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 252 of file image.c.

252  {
253  static unsigned int imgindex = 0;
254  char name[8]; /* "imgXXXX" */
255  int rc;
256 
257  /* Create image name if it doesn't already have one */
258  if ( ! image->name ) {
259  snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
260  if ( ( rc = image_set_name ( image, name ) ) != 0 )
261  return rc;
262  }
263 
264  /* Avoid ending up with multiple "selected" images on
265  * re-registration
266  */
267  if ( image_find_selected() )
269 
270  /* Add to image list */
271  image_get ( image );
273  list_add_tail ( &image->list, &images );
274  DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
275  image->name, user_to_phys ( image->data, 0 ),
276  user_to_phys ( image->data, image->len ) );
277 
278  /* Try to detect image type, if applicable. Ignore failures,
279  * since we expect to handle some unrecognised images
280  * (e.g. kernel initrds, multiboot modules, random files
281  * provided via our EFI virtual filesystem, etc).
282  */
283  if ( ! image->type )
284  image_probe ( image );
285 
286  return 0;
287 }
unsigned int flags
Flags.
Definition: image.h:36
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
userptr_t data
Raw file image.
Definition: image.h:41
struct image_type * type
Image type, if known.
Definition: image.h:46
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:212
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
static int image_probe(struct image *image)
Determine image type.
Definition: image.c:226
#define IMAGE_SELECTED
Image is selected for execution.
Definition: image.h:67
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:64
size_t len
Length of raw file image.
Definition: image.h:43
struct list_head images
List of registered images.
Definition: image.c:57
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:145
struct image * image_find_selected(void)
Find selected image.
Definition: image.c:502
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
struct list_head list
List of registered images.
Definition: image.h:29
char * name
Name.
Definition: image.h:34

References image::data, DBGC, image::flags, image_find_selected(), image_get(), image_probe(), IMAGE_REGISTERED, IMAGE_SELECTED, image_set_name(), images, image::len, image::list, list_add_tail, image::name, name, rc, snprintf(), image::type, and user_to_phys().

Referenced by asn1_okx(), cmdline_init(), efi_cmdline_init(), embedded_init(), image_exec(), image_extract(), image_memory(), imgdownload(), pixbuf_okx(), and test_init().

◆ unregister_image()

void unregister_image ( struct image image)

Unregister executable image.

Parameters
imageExecutable image

Definition at line 294 of file image.c.

294  {
295 
296  /* Do nothing unless image is registered */
297  if ( ! ( image->flags & IMAGE_REGISTERED ) )
298  return;
299 
300  DBGC ( image, "IMAGE %s unregistered\n", image->name );
301  list_del ( &image->list );
303  image_put ( image );
304 }
unsigned int flags
Flags.
Definition: image.h:36
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:64
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
struct list_head list
List of registered images.
Definition: image.h:29
char * name
Name.
Definition: image.h:34

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

Referenced by asn1_okx(), bzimage_exec(), cert_exec(), com32_exec_loop(), comboot_exec_loop(), console_exec(), gzip_okx(), image_exec(), image_extract(), image_extract_exec(), imgextract_exec(), imgfree_exec(), imgverify_exec(), pixbuf_okx(), and zlib_okx().

◆ find_image()

struct image* find_image ( const char *  name)

Find image by name.

Parameters
nameImage name
Return values
imageExecutable image, or NULL

Definition at line 312 of file image.c.

312  {
313  struct image *image;
314 
315  for_each_image ( image ) {
316  if ( strcmp ( image->name, name ) == 0 )
317  return image;
318  }
319 
320  return NULL;
321 }
const char * name
Definition: ath9k_hw.c:1984
An executable image.
Definition: image.h:24
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:157
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References for_each_image, image::name, name, NULL, and strcmp().

Referenced by imgacquire(), and imgmulti_exec().

◆ image_exec()

int image_exec ( struct image image)

Execute image.

Parameters
imageExecutable image
Return values
rcReturn status code

The image must already be registered. Note that executing an image may cause it to unregister itself. The caller must therefore assume that the image pointer becomes invalid.

Definition at line 333 of file image.c.

333  {
334  struct image *saved_current_image;
335  struct image *replacement = NULL;
336  struct uri *old_cwuri;
337  int rc;
338 
339  /* Sanity check */
341 
342  /* Switch current working directory to be that of the image
343  * itself, if applicable
344  */
345  old_cwuri = uri_get ( cwuri );
346  if ( image->uri )
347  churi ( image->uri );
348 
349  /* Preserve record of any currently-running image */
350  saved_current_image = current_image;
351 
352  /* Take out a temporary reference to the image, so that it
353  * does not get freed when temporarily unregistered.
354  */
356 
357  /* Check that this image can be executed */
358  if ( ! ( image->type && image->type->exec ) ) {
359  rc = -ENOEXEC;
360  goto err;
361  }
362 
363  /* Check that image is trusted (if applicable) */
364  if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
365  DBGC ( image, "IMAGE %s is not trusted\n", image->name );
366  rc = -EACCES_UNTRUSTED;
367  goto err;
368  }
369 
370  /* Record boot attempt */
371  syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
372 
373  /* Temporarily unregister the image during its execution */
375 
376  /* Try executing the image */
377  if ( ( rc = image->type->exec ( image ) ) != 0 ) {
378  DBGC ( image, "IMAGE %s could not execute: %s\n",
379  image->name, strerror ( rc ) );
380  /* Do not return yet; we still have clean-up to do */
381  }
382 
383  /* Record result of boot attempt */
384  if ( rc == 0 ) {
385  syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
386  image->name );
387  } else {
388  syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
389  image->name, strerror ( rc ) );
390  }
391 
392  /* Re-register image (unless due to be replaced) */
393  if ( ! image->replacement )
394  register_image ( image );
395 
396  /* Pick up replacement image before we drop the original
397  * image's temporary reference. The replacement image must
398  * already be registered, so we don't need to hold a temporary
399  * reference (which would complicate the tail-recursion).
400  */
401  replacement = image->replacement;
402  if ( replacement )
403  assert ( replacement->flags & IMAGE_REGISTERED );
404 
405  err:
406  /* Unregister image if applicable */
409 
410  /* Debug message for tail-recursion. Placed here because the
411  * image_put() may end up freeing the image.
412  */
413  if ( replacement ) {
414  DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
415  image->name, replacement->name );
416  }
417 
418  /* Drop temporary reference to the original image */
419  image_put ( image );
420 
421  /* Restore previous currently-running image */
422  current_image = saved_current_image;
423 
424  /* Reset current working directory */
425  churi ( old_cwuri );
426  uri_put ( old_cwuri );
427 
428  /* Tail-recurse into replacement image, if one exists */
429  if ( replacement )
430  return image_exec ( replacement );
431 
432  return rc;
433 }
unsigned int flags
Flags.
Definition: image.h:36
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct image * current_image
Currently-executing image.
Definition: image.c:60
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:194
#define EACCES_UNTRUSTED
Definition: image.c:47
#define ENOEXEC
Exec format error.
Definition: errno.h:519
struct image_type * type
Image type, if known.
Definition: image.h:46
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:212
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define LOG_ERR
Error: error conditions.
Definition: syslog.h:35
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:73
int(* exec)(struct image *image)
Execute image.
Definition: image.h:94
int image_exec(struct image *image)
Execute image.
Definition: image.c:333
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:45
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:64
int register_image(struct image *image)
Register executable image.
Definition: image.c:252
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:70
struct uri * uri
URI of image.
Definition: image.h:32
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:294
#define syslog(priority, fmt,...)
Write message to system log.
Definition: syslog.h:93
struct image * replacement
Replacement image.
Definition: image.h:60
A Uniform Resource Identifier.
Definition: uri.h:64
static int require_trusted_images
Current image trust requirement.
Definition: image.c:63
struct uri * cwuri
Current working URI.
Definition: cwuri.c:38
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition: syslog.h:41

References assert(), churi(), current_image, cwuri, DBGC, EACCES_UNTRUSTED, ENOEXEC, image_type::exec, image::flags, IMAGE_AUTO_UNREGISTER, image_exec(), image_get(), image_put(), IMAGE_REGISTERED, IMAGE_TRUSTED, LOG_ERR, LOG_NOTICE, image::name, NULL, rc, register_image(), image::replacement, require_trusted_images, strerror(), syslog, image::type, unregister_image(), image::uri, uri_get(), and uri_put().

Referenced by image_exec(), image_extract_exec(), imgexec(), ipxe(), and uriboot().

◆ image_replace()

int image_replace ( struct image replacement)

Set replacement image.

Parameters
replacementReplacement image
Return values
rcReturn status code

The replacement image must already be registered, and must remain registered until the currently-executing image returns.

Definition at line 444 of file image.c.

444  {
445  struct image *image = current_image;
446  int rc;
447 
448  /* Sanity check */
450 
451  /* Fail unless there is a currently-executing image */
452  if ( ! image ) {
453  rc = -ENOTTY;
454  DBGC ( replacement, "IMAGE %s cannot replace non-existent "
455  "image: %s\n", replacement->name, strerror ( rc ) );
456  return rc;
457  }
458 
459  /* Check that the replacement image can be executed */
460  if ( ! ( replacement->type && replacement->type->exec ) )
461  return -ENOEXEC;
462 
463  /* Clear any existing replacement */
465 
466  /* Set replacement */
468  DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
469  image->name, replacement->name );
470 
471  return 0;
472 }
unsigned int flags
Flags.
Definition: image.h:36
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct image * current_image
Currently-executing image.
Definition: image.c:60
#define ENOEXEC
Exec format error.
Definition: errno.h:519
struct image_type * type
Image type, if known.
Definition: image.h:46
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:212
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
int(* exec)(struct image *image)
Execute image.
Definition: image.h:94
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:64
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
struct image * replacement
Replacement image.
Definition: image.h:60
char * name
Name.
Definition: image.h:34

References assert(), current_image, DBGC, ENOEXEC, ENOTTY, image_type::exec, image::flags, image_get(), image_put(), IMAGE_REGISTERED, image::name, rc, image::replacement, strerror(), and image::type.

Referenced by comboot_fetch_kernel(), and imgexec().

◆ image_select()

int image_select ( struct image image)

Select image for execution.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 480 of file image.c.

480  {
481  struct image *tmp;
482 
483  /* Unselect all other images */
484  for_each_image ( tmp )
485  tmp->flags &= ~IMAGE_SELECTED;
486 
487  /* Check that this image can be executed */
488  if ( ! ( image->type && image->type->exec ) )
489  return -ENOEXEC;
490 
491  /* Mark image as selected */
493 
494  return 0;
495 }
unsigned int flags
Flags.
Definition: image.h:36
#define ENOEXEC
Exec format error.
Definition: errno.h:519
struct image_type * type
Image type, if known.
Definition: image.h:46
An executable image.
Definition: image.h:24
int(* exec)(struct image *image)
Execute image.
Definition: image.h:94
unsigned long tmp
Definition: linux_pci.h:53
#define IMAGE_SELECTED
Image is selected for execution.
Definition: image.h:67
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:157

References ENOEXEC, image_type::exec, image::flags, for_each_image, IMAGE_SELECTED, tmp, and image::type.

Referenced by embedded_init(), and imgselect().

◆ image_find_selected()

struct image* image_find_selected ( void  )

Find selected image.

Return values
imageExecutable image, or NULL

Definition at line 502 of file image.c.

502  {
503  struct image *image;
504 
505  for_each_image ( image ) {
506  if ( image->flags & IMAGE_SELECTED )
507  return image;
508  }
509  return NULL;
510 }
unsigned int flags
Flags.
Definition: image.h:36
An executable image.
Definition: image.h:24
#define IMAGE_SELECTED
Image is selected for execution.
Definition: image.h:67
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:157
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References image::flags, for_each_image, IMAGE_SELECTED, and NULL.

Referenced by imgsingle_exec(), and register_image().

◆ image_set_trust()

int image_set_trust ( int  require_trusted,
int  permanent 
)

Change image trust requirement.

Parameters
require_trustedRequire trusted images
permanentMake trust requirement permanent
Return values
rcReturn status code

Definition at line 519 of file image.c.

519  {
520 
521  /* Update trust requirement, if permitted to do so */
523  require_trusted_images = require_trusted;
525  }
526 
527  /* Fail if we attempted to change the trust requirement but
528  * were not permitted to do so.
529  */
530  if ( require_trusted_images != require_trusted )
531  return -EACCES_PERMANENT;
532 
533  return 0;
534 }
static int require_trusted_images_permanent
Prevent changes to image trust requirement.
Definition: image.c:66
#define EACCES_PERMANENT
Definition: image.c:51
static int require_trusted_images
Current image trust requirement.
Definition: image.c:63

References EACCES_PERMANENT, require_trusted_images, and require_trusted_images_permanent.

Referenced by imgtrust_exec().

◆ image_memory()

struct image* image_memory ( const char *  name,
userptr_t  data,
size_t  len 
)

Create registered image from block of memory.

Parameters
nameName
dataImage data
lenLength
Return values
imageImage, or NULL on error

Definition at line 544 of file image.c.

544  {
545  struct image *image;
546  int rc;
547 
548  /* Allocate image */
549  image = alloc_image ( NULL );
550  if ( ! image ) {
551  rc = -ENOMEM;
552  goto err_alloc_image;
553  }
554 
555  /* Set name */
556  if ( ( rc = image_set_name ( image, name ) ) != 0 )
557  goto err_set_name;
558 
559  /* Set data */
560  if ( ( rc = image_set_data ( image, data, len ) ) != 0 )
561  goto err_set_data;
562 
563  /* Register image */
564  if ( ( rc = register_image ( image ) ) != 0 )
565  goto err_register;
566 
567  /* Drop local reference to image */
568  image_put ( image );
569 
570  return image;
571 
572  err_register:
573  err_set_data:
574  err_set_name:
575  image_put ( image );
576  err_alloc_image:
577  return NULL;
578 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
int register_image(struct image *image)
Register executable image.
Definition: image.c:252
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:145
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:222
int image_set_data(struct image *image, userptr_t data, size_t len)
Set image data.
Definition: image.c:207
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:91
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References alloc_image(), data, ENOMEM, image_put(), image_set_data(), image_set_name(), len, name, NULL, rc, and register_image().

Referenced by efi_autoexec_startup(), gzip_okx(), imgmem(), initrd_init(), and zlib_okx().

◆ image_argument()

const char* image_argument ( struct image image,
const char *  key 
)

Find argument within image command line.

Parameters
imageImage
keyArgument search key (including trailing delimiter)
Return values
valueArgument value, or NULL if not found

Definition at line 587 of file image.c.

587  {
588  const char *cmdline = image->cmdline;
589  const char *search;
590  const char *match;
591  const char *next;
592 
593  /* Find argument */
594  for ( search = cmdline ; search ; search = next ) {
595 
596  /* Find next occurrence, if any */
597  match = strstr ( search, key );
598  if ( ! match )
599  break;
600  next = ( match + strlen ( key ) );
601 
602  /* Check preceding delimiter, if any */
603  if ( ( match == cmdline ) || isspace ( match[-1] ) )
604  return next;
605  }
606 
607  return NULL;
608 }
uint32_t next
Next descriptor address.
Definition: myson.h:18
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:39
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:309
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
union @382 key
Sense key.
Definition: scsi.h:18

References cmdline, image::cmdline, isspace(), key, next, NULL, strlen(), and strstr().

Referenced by bzimage_parse_cmdline(), and cpio_parse_cmdline().

Variable Documentation

◆ images

struct list_head images = LIST_HEAD_INIT ( images )

List of registered images.

Definition at line 57 of file image.c.

Referenced by first_image(), have_images(), initrd_dump(), initrd_reshuffle(), initrd_squash_high(), initrd_swap(), and register_image().

◆ current_image

struct image* current_image

Currently-executing image.

Definition at line 60 of file image.c.

Referenced by goto_exec(), image_exec(), and image_replace().

◆ require_trusted_images

int require_trusted_images = 0
static

Current image trust requirement.

Definition at line 63 of file image.c.

Referenced by image_exec(), and image_set_trust().

◆ require_trusted_images_permanent

int require_trusted_images_permanent = 0
static

Prevent changes to image trust requirement.

Definition at line 66 of file image.c.

Referenced by image_set_trust().