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/uaccess.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)
 
 FILE_SECBOOT (PERMITTED)
 
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...
 
char * image_strip_suffix (struct image *image)
 Strip dot suffix from image name, if present. 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, const void *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...
 
struct imagefind_image_tag (struct image_tag *tag)
 Find image by tag. 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...
 
int image_set_trust (int require_trusted, int permanent)
 Change image trust requirement. More...
 
struct imageimage_memory (const char *name, const void *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 image_tag selected_image __image_tag
 Image selected for execution. 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 49 of file image.c.

◆ EINFO_EACCES_UNTRUSTED

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

Definition at line 51 of file image.c.

◆ EACCES_PERMANENT

#define EACCES_PERMANENT   __einfo_error ( EINFO_EACCES_PERMANENT )

Definition at line 53 of file image.c.

◆ EINFO_EACCES_PERMANENT

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

Definition at line 55 of file image.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ free_image()

void free_image ( struct refcnt refcnt)

Free executable image.

Parameters
refcntReference counter

Image consumers must call image_put() rather than calling free_image() directly. This function is exposed for use only by static images.

Definition at line 86 of file image.c.

86  {
87  struct image *image = container_of ( refcnt, struct image, refcnt );
88  struct image_tag *tag;
89 
90  /* Sanity check: free_image() should not be called directly on
91  * dynamically allocated images.
92  */
93  assert ( refcnt->count < 0 );
94  DBGC ( image, "IMAGE %s freed\n", image->name );
95 
96  /* Clear any tag weak references */
98  if ( tag->image == image )
99  tag->image = NULL;
100  }
101 
102  /* Free dynamic allocations used by both static and dynamic images */
103  free ( image->cmdline );
104  uri_put ( image->uri );
106 
107  /* Free image name, if dynamically allocated */
108  if ( ! ( image->flags & IMAGE_STATIC_NAME ) )
109  free ( image->name );
110 
111  /* Free image data and image itself, if dynamically allocated */
112  if ( ! ( image->flags & IMAGE_STATIC ) ) {
113  ufree ( image->rwdata );
114  free ( image );
115  }
116 }
static __always_inline void ufree(void *ptr)
Free external memory.
Definition: umalloc.h:68
unsigned int flags
Flags.
Definition: image.h:40
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:206
An image tag.
Definition: image.h:173
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:43
A reference counter.
Definition: refcnt.h:27
int count
Current reference count.
Definition: refcnt.h:33
#define IMAGE_STATIC
Image is statically allocated.
Definition: image.h:89
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:36
#define IMAGE_TAGS
Image tag table.
Definition: image.h:181
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:386
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:250
void * rwdata
Writable data.
Definition: image.h:53
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition: image.h:92
struct image * replacement
Replacement image.
Definition: image.h:73
uint64_t tag
Identity tag.
Definition: edd.h:31
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References assert(), image::cmdline, container_of, refcnt::count, DBGC, image::flags, for_each_table_entry, free, image_put(), IMAGE_STATIC, IMAGE_STATIC_NAME, IMAGE_TAGS, image::name, NULL, image::replacement, image::rwdata, tag, ufree(), image::uri, and uri_put().

Referenced by alloc_image(), cmdline_image_free(), efi_cmdline_free(), and lkrn_exec().

◆ alloc_image()

struct image* alloc_image ( struct uri uri)

Allocate executable image.

Parameters
uriURI, or NULL
Return values
imageExecutable image

Definition at line 124 of file image.c.

124  {
125  struct image *image;
126  int rc;
127 
128  /* Allocate image */
129  image = zalloc ( sizeof ( *image ) );
130  if ( ! image )
131  goto err_alloc;
132 
133  /* Initialise image */
135  if ( uri && ( ( rc = image_set_uri ( image, uri ) ) != 0 ) )
136  goto err_set_uri;
137 
138  return image;
139 
140  err_set_uri:
141  image_put ( image );
142  err_alloc:
143  return NULL;
144 }
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:153
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
An executable image.
Definition: image.h:24
void free_image(struct refcnt *refcnt)
Free executable image.
Definition: image.c:86
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:250
A Uniform Resource Identifier.
Definition: uri.h:65
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
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 cpio_okx(), 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 153 of file image.c.

153  {
154  const char *name;
155  int rc;
156 
157  /* Set name, if image does not already have one */
158  if ( ! ( image->name && image->name[0] ) ) {
159  name = ( uri->path ? uri->path : uri->opaque );
160  if ( name ) {
161  name = basename ( ( char * ) name );
162  if ( ( rc = image_set_name ( image, name ) ) != 0 )
163  return rc;
164  }
165  }
166 
167  /* Update image URI */
168  uri_put ( image->uri );
169  image->uri = uri_get ( uri );
170 
171  return 0;
172 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:206
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:195
An executable image.
Definition: image.h:24
const char * path
Path (after URI decoding)
Definition: uri.h:81
char * basename(char *path)
Return base name from path.
Definition: basename.c:43
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:181
struct uri * uri
URI of image.
Definition: image.h:32
const char * opaque
Opaque part.
Definition: uri.h:71
A Uniform Resource Identifier.
Definition: uri.h:65
char * name
Name.
Definition: image.h:38

References basename(), image_set_name(), image::name, name, uri::opaque, 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 181 of file image.c.

181  {
182  char *name_copy;
183 
184  /* Duplicate name */
185  name_copy = strdup ( name );
186  if ( ! name_copy )
187  return -ENOMEM;
188 
189  /* Free existing name, if not statically allocated */
190  if ( ! ( image->flags & IMAGE_STATIC_NAME ) )
191  free ( image->name );
192 
193  /* Replace existing name */
194  image->name = name_copy;
196 
197  return 0;
198 }
unsigned int flags
Flags.
Definition: image.h:40
const char * name
Definition: ath9k_hw.c:1986
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:535
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
char * strdup(const char *src)
Duplicate string.
Definition: string.c:394
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition: image.h:92
char * name
Name.
Definition: image.h:38

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

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

◆ image_strip_suffix()

char* image_strip_suffix ( struct image image)

Strip dot suffix from image name, if present.

Parameters
imageImage
Return values
sepPosition of old dot separator, or NULL

Definition at line 206 of file image.c.

206  {
207  char *dot;
208 
209  /* Locate and strip suffix, if present */
210  if ( image->name &&
211  ( ( dot = strrchr ( image->name, '.' ) ) != NULL ) ) {
212  *dot = '\0';
213  return dot;
214  }
215 
216  return NULL;
217 }
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:290
An executable image.
Definition: image.h:24
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References image::name, NULL, and strrchr().

Referenced by cms_decrypt(), and image_extract().

◆ 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 226 of file image.c.

226  {
227 
228  free ( image->cmdline );
229  image->cmdline = NULL;
230  if ( cmdline ) {
231  image->cmdline = strdup ( cmdline );
232  if ( ! image->cmdline )
233  return -ENOMEM;
234  }
235  return 0;
236 }
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:43
#define ENOMEM
Not enough space.
Definition: errno.h:535
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
char * strdup(const char *src)
Duplicate string.
Definition: string.c:394
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

Referenced by cpio_okx(), 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 245 of file image.c.

245  {
246  void *new;
247 
248  /* Refuse to reallocate static images */
249  if ( image->flags & IMAGE_STATIC )
250  return -ENOTTY;
251 
252  /* (Re)allocate image data */
253  new = urealloc ( image->rwdata, len );
254  if ( ! new )
255  return -ENOMEM;
256  image->rwdata = new;
257  image->len = len;
258 
259  return 0;
260 }
unsigned int flags
Flags.
Definition: image.h:40
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:535
#define IMAGE_STATIC
Image is statically allocated.
Definition: image.h:89
ring len
Length.
Definition: dwmac.h:231
size_t len
Length of raw file image.
Definition: image.h:56
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:595
void * rwdata
Writable data.
Definition: image.h:53
void * urealloc(void *ptr, size_t new_size)
Reallocate external memory.

References ENOMEM, ENOTTY, image::flags, IMAGE_STATIC, image::len, len, image::rwdata, and urealloc().

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

◆ image_set_data()

int image_set_data ( struct image image,
const void *  data,
size_t  len 
)

Set image data.

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

Definition at line 270 of file image.c.

270  {
271  int rc;
272 
273  /* Set image length */
274  if ( ( rc = image_set_len ( image, len ) ) != 0 )
275  return rc;
276 
277  /* Copy in new image data */
278  memcpy ( image->rwdata, data, len );
279 
280  return 0;
281 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
int image_set_len(struct image *image, size_t len)
Set image length.
Definition: image.c:245
void * rwdata
Writable data.
Definition: image.h:53
uint8_t data[48]
Additional event data.
Definition: ena.h:22

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

Referenced by image_memory(), and zimg_extract().

◆ image_probe()

static int image_probe ( struct image image)
static

Determine image type.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 289 of file image.c.

289  {
290  struct image_type *type;
291  int rc;
292 
293  /* Try each type in turn */
295  if ( ( rc = type->probe ( image ) ) == 0 ) {
296  image->type = type;
297  DBGC ( image, "IMAGE %s is %s\n",
298  image->name, type->name );
299  return 0;
300  }
301  DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
302  type->name, strerror ( rc ) );
303  }
304 
305  DBGC ( image, "IMAGE %s format not recognised\n", image->name );
306  return -ENOTSUP;
307 }
#define IMAGE_TYPES
Executable image type table.
Definition: image.h:167
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct image_type * type
Image type, if known.
Definition: image.h:59
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
An executable image type.
Definition: image.h:95
An executable image.
Definition: image.h:24
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:386
char * name
Name.
Definition: image.h:38

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 315 of file image.c.

315  {
316  static unsigned int imgindex = 0;
317  char name[8]; /* "imgXXXX" */
318  int rc;
319 
320  /* Sanity checks */
321  if ( image->flags & IMAGE_STATIC ) {
322  assert ( ( image->name == NULL ) ||
323  ( image->flags & IMAGE_STATIC_NAME ) );
324  assert ( image->cmdline == NULL );
325  }
326 
327  /* Create image name if it doesn't already have one */
328  if ( ! image->name ) {
329  snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
330  if ( ( rc = image_set_name ( image, name ) ) != 0 )
331  return rc;
332  }
333 
334  /* Add to image list */
335  image_get ( image );
337  list_add_tail ( &image->list, &images );
338  DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
339  image->name, virt_to_phys ( image->data ),
340  ( virt_to_phys ( image->data ) + image->len ) );
341 
342  /* Try to detect image type, if applicable. Ignore failures,
343  * since we expect to handle some unrecognised images
344  * (e.g. kernel initrds, multiboot modules, random files
345  * provided via our EFI virtual filesystem, etc).
346  */
347  if ( ! image->type )
348  image_probe ( image );
349 
350  return 0;
351 }
unsigned int flags
Flags.
Definition: image.h:40
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
const void * data
Read-only data.
Definition: image.h:51
struct image_type * type
Image type, if known.
Definition: image.h:59
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:240
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:43
static int image_probe(struct image *image)
Determine image type.
Definition: image.c:289
#define IMAGE_STATIC
Image is statically allocated.
Definition: image.h:89
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:94
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:77
size_t len
Length of raw file image.
Definition: image.h:56
struct list_head images
List of registered images.
Definition: image.c:59
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:181
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition: image.h:92
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:383
struct list_head list
List of registered images.
Definition: image.h:29
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References assert(), image::cmdline, image::data, DBGC, image::flags, image_get(), image_probe(), IMAGE_REGISTERED, image_set_name(), IMAGE_STATIC, IMAGE_STATIC_NAME, images, image::len, image::list, list_add_tail, image::name, name, NULL, rc, snprintf(), and image::type.

Referenced by asn1_okx(), cmdline_init(), cms_decrypt(), efi_cmdline_init(), efi_image_exec(), 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 358 of file image.c.

358  {
359 
360  /* Do nothing unless image is registered */
361  if ( ! ( image->flags & IMAGE_REGISTERED ) )
362  return;
363 
364  DBGC ( image, "IMAGE %s unregistered\n", image->name );
365  list_del ( &image->list );
367  image_put ( image );
368 }
unsigned int flags
Flags.
Definition: image.h:40
#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:120
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:77
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:250
struct list_head list
List of registered images.
Definition: image.h:29
char * name
Name.
Definition: image.h:38

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

Referenced by asn1_okx(), bzimage_exec(), cert_exec(), cms_decrypt(), com32_exec_loop(), comboot_exec_loop(), console_exec(), efi_autoexec_load(), efi_image_exec(), fdt_test_exec(), gzip_okx(), image_exec(), image_extract(), image_extract_exec(), imgdecrypt_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 376 of file image.c.

376  {
377  struct image *image;
378 
379  for_each_image ( image ) {
380  if ( strcmp ( image->name, name ) == 0 )
381  return image;
382  }
383 
384  return NULL;
385 }
const char * name
Definition: ath9k_hw.c:1986
An executable image.
Definition: image.h:24
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:191
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:174
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

Referenced by imgacquire(), and imgmulti_exec().

◆ find_image_tag()

struct image* find_image_tag ( struct image_tag tag)

Find image by tag.

Parameters
tagImage tag
Return values
imageExecutable image, or NULL

Definition at line 393 of file image.c.

393  {
394  struct image *image;
395 
396  for_each_image ( image ) {
397  if ( tag->image == image )
398  return image;
399  }
400 
401  return NULL;
402 }
An executable image.
Definition: image.h:24
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:191
uint64_t tag
Identity tag.
Definition: edd.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References for_each_image, NULL, and tag.

Referenced by efi_file_open(), efi_image_exec(), fdt_create(), imgsingle_exec(), and shim_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 414 of file image.c.

414  {
415  struct image *saved_current_image;
416  struct image *replacement = NULL;
417  struct uri *old_cwuri;
418  int rc;
419 
420  /* Sanity check */
422 
423  /* Switch current working directory to be that of the image
424  * itself, if applicable
425  */
426  old_cwuri = uri_get ( cwuri );
427  if ( image->uri )
428  churi ( image->uri );
429 
430  /* Set as currently running image */
431  saved_current_image = image_tag ( image, &current_image );
432 
433  /* Take out a temporary reference to the image, so that it
434  * does not get freed when temporarily unregistered.
435  */
436  image_get ( image );
437 
438  /* Check that this image can be executed */
439  if ( ! ( image->type && image->type->exec ) ) {
440  rc = -ENOEXEC;
441  goto err;
442  }
443 
444  /* Check that image is trusted (if applicable) */
445  if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
446  DBGC ( image, "IMAGE %s is not trusted\n", image->name );
447  rc = -EACCES_UNTRUSTED;
448  goto err;
449  }
450 
451  /* Record boot attempt */
452  syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
453 
454  /* Temporarily unregister the image during its execution */
456 
457  /* Try executing the image */
458  if ( ( rc = image->type->exec ( image ) ) != 0 ) {
459  DBGC ( image, "IMAGE %s could not execute: %s\n",
460  image->name, strerror ( rc ) );
461  /* Do not return yet; we still have clean-up to do */
462  }
463 
464  /* Record result of boot attempt */
465  if ( rc == 0 ) {
466  syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
467  image->name );
468  } else {
469  syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
470  image->name, strerror ( rc ) );
471  }
472 
473  /* Re-register image (unless due to be replaced) */
474  if ( ! image->replacement )
475  register_image ( image );
476 
477  /* Pick up replacement image before we drop the original
478  * image's temporary reference. The replacement image must
479  * already be registered, so we don't need to hold a temporary
480  * reference (which would complicate the tail-recursion).
481  */
483  if ( replacement )
484  assert ( replacement->flags & IMAGE_REGISTERED );
485 
486  /* Clear any recorded replacement image */
489 
490  err:
491  /* Unregister image if applicable */
494 
495  /* Debug message for tail-recursion. Placed here because the
496  * image_put() may end up freeing the image.
497  */
498  if ( replacement ) {
499  DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
500  image->name, replacement->name );
501  }
502 
503  /* Drop temporary reference to the original image */
504  image_put ( image );
505 
506  /* Restore previous currently-running image */
507  image_tag ( saved_current_image, &current_image );
508 
509  /* Reset current working directory */
510  churi ( old_cwuri );
511  uri_put ( old_cwuri );
512 
513  /* Tail-recurse into replacement image, if one exists */
514  if ( replacement )
515  return image_exec ( replacement );
516 
517  return rc;
518 }
unsigned int flags
Flags.
Definition: image.h:40
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:206
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:195
#define EACCES_UNTRUSTED
Definition: image.c:49
#define ENOEXEC
Exec format error.
Definition: errno.h:520
struct image_type * type
Image type, if known.
Definition: image.h:59
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:240
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define LOG_ERR
Error: error conditions.
Definition: syslog.h:36
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:83
int(* exec)(struct image *image)
Execute image.
Definition: image.h:113
int image_exec(struct image *image)
Execute image.
Definition: image.c:414
struct image_tag current_image
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:46
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const char * replacement
Definition: editstring.h:54
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:77
int register_image(struct image *image)
Register executable image.
Definition: image.c:315
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:80
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:250
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:358
#define syslog(priority, fmt,...)
Write message to system log.
Definition: syslog.h:94
struct image * replacement
Replacement image.
Definition: image.h:73
A Uniform Resource Identifier.
Definition: uri.h:65
static int require_trusted_images
Current image trust requirement.
Definition: image.c:72
struct uri * cwuri
Current working URI.
Definition: cwuri.c:39
static struct image * image_tag(struct image *image, struct image_tag *tag)
Tag image.
Definition: image.h:297
char * name
Name.
Definition: image.h:38
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition: syslog.h:42

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_tag(), IMAGE_TRUSTED, LOG_ERR, LOG_NOTICE, image::name, NULL, rc, register_image(), replacement, image::replacement, require_trusted_images, strerror(), syslog, image::type, unregister_image(), image::uri, uri_get(), and uri_put().

Referenced by image_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 529 of file image.c.

529  {
530  struct image *image = current_image.image;
531  int rc;
532 
533  /* Sanity check */
534  assert ( replacement->flags & IMAGE_REGISTERED );
535 
536  /* Fail unless there is a currently-executing image */
537  if ( ! image ) {
538  rc = -ENOTTY;
539  DBGC ( replacement, "IMAGE %s cannot replace non-existent "
540  "image: %s\n", replacement->name, strerror ( rc ) );
541  return rc;
542  }
543 
544  /* Check that the replacement image can be executed */
545  if ( ! ( replacement->type && replacement->type->exec ) )
546  return -ENOEXEC;
547 
548  /* Clear any existing replacement */
550 
551  /* Set replacement */
553  DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
554  image->name, replacement->name );
555 
556  return 0;
557 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOEXEC
Exec format error.
Definition: errno.h:520
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:240
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
struct image * image
Image (weak reference, nullified when image is freed)
Definition: image.h:177
struct image_tag current_image
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const char * replacement
Definition: editstring.h:54
#define IMAGE_REGISTERED
Image is registered.
Definition: image.h:77
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:250
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:595
struct image * replacement
Replacement image.
Definition: image.h:73
char * name
Name.
Definition: image.h:38

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

Referenced by comboot_fetch_kernel(), image_extract_exec(), 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 565 of file image.c.

565  {
566 
567  /* Check that this image can be executed */
568  if ( ! ( image->type && image->type->exec ) )
569  return -ENOEXEC;
570 
571  /* Mark image as selected */
573 
574  return 0;
575 }
struct image_tag selected_image
#define ENOEXEC
Exec format error.
Definition: errno.h:520
struct image_type * type
Image type, if known.
Definition: image.h:59
An executable image.
Definition: image.h:24
int(* exec)(struct image *image)
Execute image.
Definition: image.h:113
static struct image * image_tag(struct image *image, struct image_tag *tag)
Tag image.
Definition: image.h:297

References ENOEXEC, image_type::exec, image_tag(), selected_image, and image::type.

Referenced by embedded_init(), and imgselect().

◆ 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 584 of file image.c.

584  {
585 
586  /* Update trust requirement, if permitted to do so */
588  require_trusted_images = require_trusted;
590  }
591 
592  /* Fail if we attempted to change the trust requirement but
593  * were not permitted to do so.
594  */
595  if ( require_trusted_images != require_trusted )
596  return -EACCES_PERMANENT;
597 
598  return 0;
599 }
static int require_trusted_images_permanent
Prevent changes to image trust requirement.
Definition: image.c:75
#define EACCES_PERMANENT
Definition: image.c:53
static int require_trusted_images
Current image trust requirement.
Definition: image.c:72

References EACCES_PERMANENT, require_trusted_images, and require_trusted_images_permanent.

Referenced by imgtrust_exec().

◆ image_memory()

struct image* image_memory ( const char *  name,
const void *  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 609 of file image.c.

610  {
611  struct image *image;
612  int rc;
613 
614  /* Allocate image */
615  image = alloc_image ( NULL );
616  if ( ! image ) {
617  rc = -ENOMEM;
618  goto err_alloc_image;
619  }
620 
621  /* Set name */
622  if ( ( rc = image_set_name ( image, name ) ) != 0 )
623  goto err_set_name;
624 
625  /* Set data */
626  if ( ( rc = image_set_data ( image, data, len ) ) != 0 )
627  goto err_set_data;
628 
629  /* Register image */
630  if ( ( rc = register_image ( image ) ) != 0 )
631  goto err_register;
632 
633  /* Drop local reference to image */
634  image_put ( image );
635 
636  return image;
637 
638  err_register:
639  err_set_data:
640  err_set_name:
641  image_put ( image );
642  err_alloc_image:
643  return NULL;
644 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:535
ring len
Length.
Definition: dwmac.h:231
int register_image(struct image *image)
Register executable image.
Definition: image.c:315
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:181
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:250
int image_set_data(struct image *image, const void *data, size_t len)
Set image data.
Definition: image.c:270
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:124
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

Referenced by fdt_test_exec(), 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 653 of file image.c.

653  {
654  const char *cmdline = image->cmdline;
655  const char *search;
656  const char *match;
657  const char *next;
658 
659  /* Find argument */
660  for ( search = cmdline ; search ; search = next ) {
661 
662  /* Find next occurrence, if any */
663  match = strstr ( search, key );
664  if ( ! match )
665  break;
666  next = ( match + strlen ( key ) );
667 
668  /* Check preceding delimiter, if any */
669  if ( ( match == cmdline ) || isspace ( match[-1] ) )
670  return next;
671  }
672 
673  return NULL;
674 }
An executable image.
Definition: image.h:24
char * cmdline
Command line to pass to image.
Definition: image.h:43
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:310
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:42
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
union @391 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 59 of file image.c.

Referenced by first_image(), initrd_dump(), initrd_load(), initrd_region(), initrd_squash_high(), initrd_startup(), initrd_swap(), lkrn_exec(), and register_image().

◆ __image_tag

struct image_tag current_image __image_tag
Initial value:
= {
.name = "SELECTED",
}

Image selected for execution.

The downloaded flattened device tree tag.

Currently-executing image.

Definition at line 62 of file image.c.

◆ require_trusted_images

int require_trusted_images = 0
static

Current image trust requirement.

Definition at line 72 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 75 of file image.c.

Referenced by image_set_trust().