iPXE
image.h File Reference

Executable images. More...

#include <ipxe/tables.h>
#include <ipxe/list.h>
#include <ipxe/refcnt.h>

Go to the source code of this file.

Data Structures

struct  image
 An executable image. More...
struct  image_type
 An executable image type. More...
struct  image_tag
 An image tag. More...

Macros

#define IMAGE_REGISTERED   0x0001
 Image is registered.
#define IMAGE_TRUSTED   0x0002
 Image is trusted.
#define IMAGE_AUTO_UNREGISTER   0x0004
 Image will be automatically unregistered after execution.
#define IMAGE_HIDDEN   0x0008
 Image will be hidden from enumeration.
#define IMAGE_STATIC   0x0010
 Image is statically allocated.
#define IMAGE_STATIC_NAME   0x0020
 Image name is statically allocated.
#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 image type table.
#define __image_type(probe_order)
 An executable image type.
#define IMAGE_TAGS   __table ( struct image_tag, "image_tags" )
 Image tag table.
#define __image_tag   __table_entry ( IMAGE_TAGS, 01 )
 An image tag.
#define for_each_image(image)
 Iterate over all registered images.
#define for_each_image_safe(image, tmp)
 Iterate over all registered images, safe against deletion.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static struct imagefirst_image (void)
 Retrieve first image.
void free_image (struct refcnt *refcnt)
 Free executable image.
struct imagealloc_image (struct uri *uri)
 Allocate executable image.
int image_set_uri (struct image *image, struct uri *uri)
 Set image URI.
int image_set_name (struct image *image, const char *name)
 Set image name.
char * image_strip_suffix (struct image *image)
 Strip dot suffix from image name, if present.
int image_set_cmdline (struct image *image, const char *cmdline)
 Set image command line.
int image_set_len (struct image *image, size_t len)
 Set image length.
int image_set_data (struct image *image, const void *data, size_t len)
 Set image data.
int register_image (struct image *image)
 Register executable image.
void unregister_image (struct image *image)
 Unregister executable image.
struct imagefind_image (const char *name)
 Find image by name.
struct imagefind_image_tag (struct image_tag *tag)
 Find image by tag.
int image_exec (struct image *image)
 Execute image.
int image_replace (struct image *replacement)
 Set replacement image.
int image_select (struct image *image)
 Select image for execution.
int image_set_trust (int require_trusted, int permanent)
 Change image trust requirement.
struct imageimage_memory (const char *name, const void *data, size_t len)
 Create registered image from block of memory.
const char * image_argument (struct image *image, const char *key)
 Find argument within image command line.
int image_pixbuf (struct image *image, struct pixel_buffer **pixbuf)
 Create pixel buffer from image.
int image_asn1 (struct image *image, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from image.
int image_extract (struct image *image, const char *name, struct image **extracted)
 Extract archive image.
int image_extract_exec (struct image *image)
 Extract and execute 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 void image_clear_cmdline (struct image *image)
 Clear image command line.
static void image_trust (struct image *image)
 Set image as trusted.
static void image_untrust (struct image *image)
 Set image as untrusted.
static void image_hide (struct image *image)
 Mark image as hidden.
static struct imageimage_tag (struct image *image, struct image_tag *tag)
 Tag image.

Variables

struct list_head images
 List of registered images.
struct image_tag current_image
struct image_tag selected_image
const char empty_image_data []
 Empty image terminating NUL.

Detailed Description

Executable images.

Definition in file image.h.

Macro Definition Documentation

◆ IMAGE_REGISTERED

#define IMAGE_REGISTERED   0x0001

Image is registered.

Definition at line 84 of file image.h.

Referenced by cms_decrypt(), image_exec(), image_replace(), register_image(), and unregister_image().

◆ IMAGE_TRUSTED

#define IMAGE_TRUSTED   0x0002

Image is trusted.

Definition at line 87 of file image.h.

Referenced by cms_verify_fail_okx(), cms_verify_okx(), image_exec(), image_extract(), image_trust(), image_untrust(), and imgstat().

◆ IMAGE_AUTO_UNREGISTER

#define IMAGE_AUTO_UNREGISTER   0x0004

Image will be automatically unregistered after execution.

Definition at line 90 of file image.h.

Referenced by image_exec(), image_extract_exec(), imgsingle_exec(), imgstat(), and uriboot().

◆ IMAGE_HIDDEN

#define IMAGE_HIDDEN   0x0008

Image will be hidden from enumeration.

Definition at line 93 of file image.h.

Referenced by efi_file_install(), efi_file_read_dir(), efi_file_read_initrd(), efi_image_exec(), image_hide(), imgstat(), initrd_load(), and multiboot_add_modules().

◆ IMAGE_STATIC

#define IMAGE_STATIC   0x0010

Image is statically allocated.

Definition at line 96 of file image.h.

Referenced by free_image(), image_set_len(), lkrn_exec(), and register_image().

◆ IMAGE_STATIC_NAME

#define IMAGE_STATIC_NAME   0x0020

Image name is statically allocated.

Definition at line 99 of file image.h.

Referenced by free_image(), image_set_name(), lkrn_exec(), and register_image().

◆ PROBE_MULTIBOOT

#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 158 of file image.h.

Referenced by __image_type().

◆ PROBE_NORMAL

#define PROBE_NORMAL   02

Normal image probe priority.

Definition at line 163 of file image.h.

Referenced by __image_type().

◆ PROBE_PXE

#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 171 of file image.h.

Referenced by __image_type().

◆ IMAGE_TYPES

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

Executable image type table.

Definition at line 174 of file image.h.

Referenced by image_probe().

◆ __image_type

#define __image_type ( probe_order)
Value:
__table_entry ( IMAGE_TYPES, probe_order )
#define IMAGE_TYPES
Executable image type table.
Definition image.h:174
#define __table_entry(table, idx)
Declare a linker table entry.
Definition tables.h:239

An executable image type.

Definition at line 177 of file image.h.

Referenced by __image_type(), __image_type(), and __image_type().

◆ IMAGE_TAGS

#define IMAGE_TAGS   __table ( struct image_tag, "image_tags" )

Image tag table.

Definition at line 188 of file image.h.

Referenced by free_image(), and imgstat().

◆ __image_tag

struct image_tag current_image __image_tag   __table_entry ( IMAGE_TAGS, 01 )

An image tag.

Currently-executing image.

Definition at line 191 of file image.h.

Referenced by FILE_SECBOOT().

◆ for_each_image

#define for_each_image ( image)
Value:
struct list_head images
List of registered images.
Definition image.c:59
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
An executable image.
Definition image.h:24

Iterate over all registered images.

Definition at line 199 of file image.h.

199#define for_each_image( image ) \
200 list_for_each_entry ( (image), &images, list )

Referenced by efi_file_find(), efi_file_get_info(), efi_file_install(), efi_file_read_dir(), efi_file_read_initrd(), find_image(), find_image_tag(), initrd_dump(), initrd_load_all(), initrd_squash_high(), initrd_swap_any(), and multiboot_add_modules().

◆ for_each_image_safe

#define for_each_image_safe ( image,
tmp )
Value:
unsigned long tmp
Definition linux_pci.h:65
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition list.h:459

Iterate over all registered images, safe against deletion.

Definition at line 203 of file image.h.

203#define for_each_image_safe( image, tmp ) \
204 list_for_each_entry_safe ( (image), (tmp), &images, list )

Referenced by imgmulti_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ first_image()

struct image * first_image ( void )
inlinestatic

Retrieve first image.

Return values
imageImage, or NULL

Definition at line 211 of file image.h.

211 {
212 return list_first_entry ( &images, struct image, list );
213}
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition list.h:334

References images, image::list, and list_first_entry.

Referenced by ipxe().

◆ free_image()

void free_image ( struct refcnt * refcnt)
extern

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

89 {
90 struct image *image = container_of ( refcnt, struct image, refcnt );
91 struct image_tag *tag;
92
93 /* Sanity check: free_image() should not be called directly on
94 * dynamically allocated images.
95 */
96 assert ( refcnt->count < 0 );
97 DBGC ( image, "IMAGE %s freed\n", image->name );
98
99 /* Clear any tag weak references */
101 if ( tag->image == image )
102 tag->image = NULL;
103 }
104
105 /* Free dynamic allocations used by both static and dynamic images */
106 free ( image->cmdline );
107 uri_put ( image->uri );
109
110 /* Free image name, if dynamically allocated */
111 if ( ! ( image->flags & IMAGE_STATIC_NAME ) )
112 free ( image->name );
113
114 /* Free image data and image itself, if dynamically allocated */
115 if ( ! ( image->flags & IMAGE_STATIC ) ) {
116 image_set_len ( image, 0 );
117 free ( image );
118 }
119}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
uint64_t tag
Identity tag.
Definition edd.h:1
#define DBGC(...)
Definition compiler.h:530
int image_set_len(struct image *image, size_t len)
Set image length.
Definition image.c:249
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition image.h:99
static void image_put(struct image *image)
Decrement reference count on an image.
Definition image.h:258
#define IMAGE_TAGS
Image tag table.
Definition image.h:188
#define IMAGE_STATIC
Image is statically allocated.
Definition image.h:96
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
An image tag.
Definition image.h:180
unsigned int flags
Flags.
Definition image.h:40
struct image * replacement
Replacement image.
Definition image.h:80
char * name
Name.
Definition image.h:38
struct uri * uri
URI of image.
Definition image.h:32
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 for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition uri.h:206

References assert, image::cmdline, container_of, refcnt::count, DBGC, image::flags, for_each_table_entry, free, image_put(), image_set_len(), IMAGE_STATIC, IMAGE_STATIC_NAME, IMAGE_TAGS, image::name, NULL, image::replacement, tag, 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)
extern

Allocate executable image.

Parameters
uriURI, or NULL
Return values
imageExecutable image

Definition at line 127 of file image.c.

127 {
128 struct image *image;
129 int rc;
130
131 /* Allocate image */
132 image = zalloc ( sizeof ( *image ) );
133 if ( ! image )
134 goto err_alloc;
135
136 /* Initialise image */
138 if ( uri && ( ( rc = image_set_uri ( image, uri ) ) != 0 ) )
139 goto err_set_uri;
141
142 return image;
143
144 err_set_uri:
145 image_put ( image );
146 err_alloc:
147 return NULL;
148}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
const char empty_image_data[]
Empty image terminating NUL.
Definition image.c:78
void free_image(struct refcnt *refcnt)
Free executable image.
Definition image.c:89
int image_set_uri(struct image *image, struct uri *uri)
Set image URI.
Definition image.c:157
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
struct refcnt refcnt
Reference count.
Definition image.h:26
const void * data
Read-only data.
Definition image.h:51
A Uniform Resource Identifier.
Definition uri.h:65

References image::data, empty_image_data, 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 )
extern

Set image URI.

Parameters
imageImage
uriNew image URI
Return values
rcReturn status code

Definition at line 157 of file image.c.

157 {
158 const char *name;
159 int rc;
160
161 /* Set name, if image does not already have one */
162 if ( ! ( image->name && image->name[0] ) ) {
163 name = ( uri->path ? uri->path : uri->opaque );
164 if ( name ) {
165 name = basename ( ( char * ) name );
166 if ( ( rc = image_set_name ( image, name ) ) != 0 )
167 return rc;
168 }
169 }
170
171 /* Update image URI */
172 uri_put ( image->uri );
173 image->uri = uri_get ( uri );
174
175 return 0;
176}
const char * name
Definition ath9k_hw.c:1986
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:185
const char * path
Path (after URI decoding).
Definition uri.h:81
const char * opaque
Opaque part.
Definition uri.h:71
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition uri.h:195

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 )
extern

Set image name.

Parameters
imageImage
nameNew image name
Return values
rcReturn status code

Definition at line 185 of file image.c.

185 {
186 char *name_copy;
187
188 /* Duplicate name */
189 name_copy = strdup ( name );
190 if ( ! name_copy )
191 return -ENOMEM;
192
193 /* Free existing name, if not statically allocated */
194 if ( ! ( image->flags & IMAGE_STATIC_NAME ) )
195 free ( image->name );
196
197 /* Replace existing name */
198 image->name = name_copy;
200
201 return 0;
202}
#define ENOMEM
Not enough space.
Definition errno.h:535
char * strdup(const char *src)
Duplicate string.
Definition string.c:425

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)
extern

Strip dot suffix from image name, if present.

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

Definition at line 210 of file image.c.

210 {
211 char *dot;
212
213 /* Locate and strip suffix, if present */
214 if ( image->name &&
215 ( ( dot = strrchr ( image->name, '.' ) ) != NULL ) ) {
216 *dot = '\0';
217 return dot;
218 }
219
220 return NULL;
221}
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition string.c:304

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 )
extern

Set image command line.

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

Definition at line 230 of file image.c.

230 {
231
232 free ( image->cmdline );
233 image->cmdline = NULL;
234 if ( cmdline ) {
236 if ( ! image->cmdline )
237 return -ENOMEM;
238 }
239 return 0;
240}
uint32_t cmdline
Definition multiboot.h:4

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 )
extern

Set image length.

Parameters
imageImage
lenLength of image data
Return values
rcReturn status code

Definition at line 249 of file image.c.

249 {
250 size_t alloc_len;
251 void *data;
252 void *new;
253 char *nul;
254
255 /* Refuse to reallocate static images */
256 if ( image->flags & IMAGE_STATIC )
257 return -ENOTTY;
258
259 /* Calculate allocation length (including a terminating NUL) */
260 alloc_len = ( len ? ( len + 1 ) : 0 );
261 if ( alloc_len < len )
262 return -ERANGE;
263
264 /* (Re)allocate image data */
265 data = image->rwdata;
266 if ( data == empty_image_data )
267 data = NULL;
268 new = urealloc ( data, alloc_len );
269 if ( ! new )
270 return -ENOMEM;
271 image->data = ( len ? new : empty_image_data );
272 image->len = len;
273
274 /* Add terminating NUL (if not the empty image) */
275 if ( len ) {
276 nul = ( image->rwdata + len );
277 *nul = '\0';
278 }
279
280 return 0;
281}
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define ERANGE
Result too large.
Definition errno.h:640
#define ENOTTY
Inappropriate I/O control operation.
Definition errno.h:595
void * urealloc(void *ptr, size_t new_size)
Reallocate external memory.
size_t len
Length of raw file image.
Definition image.h:63
void * rwdata
Writable data.
Definition image.h:53

References data, image::data, empty_image_data, ENOMEM, ENOTTY, ERANGE, image::flags, IMAGE_STATIC, image::len, len, NULL, image::rwdata, and urealloc().

Referenced by cpio_okx(), free_image(), gzip_extract(), image_set_data(), mime_decode_base64(), mime_decode_identity(), mime_extract_part(), xferbuf_image_realloc(), and zlib_deflate().

◆ image_set_data()

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

Set image data.

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

Definition at line 291 of file image.c.

291 {
292 int rc;
293
294 /* Set image length */
295 if ( ( rc = image_set_len ( image, len ) ) != 0 )
296 return rc;
297
298 /* Copy in new image data */
299 memcpy ( image->rwdata, data, len );
300
301 return 0;
302}
void * memcpy(void *dest, const void *src, size_t len) __nonnull

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

Referenced by image_memory(), and zimg_extract().

◆ register_image()

int register_image ( struct image * image)
extern

Register executable image.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 336 of file image.c.

336 {
337 static unsigned int imgindex = 0;
338 char name[8]; /* "imgXXXX" */
339 int rc;
340
341 /* Sanity checks */
342 if ( image->flags & IMAGE_STATIC ) {
343 assert ( ( image->name == NULL ) ||
345 assert ( image->cmdline == NULL );
346 }
347
348 /* Create image name if it doesn't already have one */
349 if ( ! image->name ) {
350 snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
351 if ( ( rc = image_set_name ( image, name ) ) != 0 )
352 return rc;
353 }
354
355 /* Check for terminating NUL (required for text-based images) */
356 if ( ! image->data ) {
357 DBGC ( image, "IMAGE %s has no data\n", image->name );
358 return -EINVAL;
359 }
360 if ( image->text[image->len] != '\0' ) {
361 DBGC ( image, "IMAGE %s missing terminating NUL\n",
362 image->name );
363 return -EINVAL;
364 }
365
366 /* Add to image list */
367 image_get ( image );
370 DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
371 image->name, virt_to_phys ( image->data ),
372 ( virt_to_phys ( image->data ) + image->len ) );
373
374 /* Try to detect image type, if applicable. Ignore failures,
375 * since we expect to handle some unrecognised images
376 * (e.g. kernel initrds, multiboot modules, random files
377 * provided via our EFI virtual filesystem, etc).
378 */
379 if ( ! image->type )
380 image_probe ( image );
381
382 return 0;
383}
#define EINVAL
Invalid argument.
Definition errno.h:429
static int image_probe(struct image *image)
Determine image type.
Definition image.c:310
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition image.h:248
#define IMAGE_REGISTERED
Image is registered.
Definition image.h:84
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
struct image_type * type
Image type, if known.
Definition image.h:66
struct list_head list
List of registered images.
Definition image.h:29
const char * text
Read-only text (guaranteed to be NUL terminated).
Definition image.h:60
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383

References assert, image::cmdline, image::data, DBGC, EINVAL, 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(), image::text, 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)
extern

Unregister executable image.

Parameters
imageExecutable image

Definition at line 390 of file image.c.

390 {
391
392 /* Do nothing unless image is registered */
393 if ( ! ( image->flags & IMAGE_REGISTERED ) )
394 return;
395
396 DBGC ( image, "IMAGE %s unregistered\n", image->name );
397 list_del ( &image->list );
399 image_put ( image );
400}
#define list_del(list)
Delete an entry from a list.
Definition list.h:120

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

Referenced by archive_okx(), 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(), image_exec(), image_extract(), image_extract_exec(), imgdecrypt_exec(), imgextract_exec(), imgfree_exec(), imgset_exec(), imgverify_exec(), and pixbuf_okx().

◆ find_image()

struct image * find_image ( const char * name)
extern

Find image by name.

Parameters
nameImage name
Return values
imageExecutable image, or NULL

Definition at line 408 of file image.c.

408 {
409 struct image *image;
410
412 if ( strcmp ( image->name, name ) == 0 )
413 return image;
414 }
415
416 return NULL;
417}
#define for_each_image(image)
Iterate over all registered images.
Definition image.h:199
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174

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)
extern

Find image by tag.

Parameters
tagImage tag
Return values
imageExecutable image, or NULL

Definition at line 425 of file image.c.

425 {
426 struct image *image;
427
429 if ( tag->image == image )
430 return image;
431 }
432
433 return NULL;
434}

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)
extern

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

446 {
447 struct image *saved_current_image;
448 struct image *replacement = NULL;
449 struct uri *old_cwuri;
450 int rc;
451
452 /* Sanity check */
454
455 /* Switch current working directory to be that of the image
456 * itself, if applicable
457 */
458 old_cwuri = uri_get ( cwuri );
459 if ( image->uri )
460 churi ( image->uri );
461
462 /* Set as currently running image */
463 saved_current_image = image_tag ( image, &current_image );
464
465 /* Take out a temporary reference to the image, so that it
466 * does not get freed when temporarily unregistered.
467 */
468 image_get ( image );
469
470 /* Check that this image can be executed */
471 if ( ! ( image->type && image->type->exec ) ) {
472 rc = -ENOEXEC;
473 goto err;
474 }
475
476 /* Check that image is trusted (if applicable) */
478 DBGC ( image, "IMAGE %s is not trusted\n", image->name );
480 goto err;
481 }
482
483 /* Record boot attempt */
484 syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
485
486 /* Temporarily unregister the image during its execution */
488
489 /* Try executing the image */
490 if ( ( rc = image->type->exec ( image ) ) != 0 ) {
491 DBGC ( image, "IMAGE %s could not execute: %s\n",
492 image->name, strerror ( rc ) );
493 /* Do not return yet; we still have clean-up to do */
494 }
495
496 /* Record result of boot attempt */
497 if ( rc == 0 ) {
498 syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
499 image->name );
500 } else {
501 syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
502 image->name, strerror ( rc ) );
503 }
504
505 /* Re-register image (unless due to be replaced) */
506 if ( ! image->replacement )
508
509 /* Pick up replacement image before we drop the original
510 * image's temporary reference. The replacement image must
511 * already be registered, so we don't need to hold a temporary
512 * reference (which would complicate the tail-recursion).
513 */
515 if ( replacement )
517
518 /* Clear any recorded replacement image */
521
522 err:
523 /* Unregister image if applicable */
526
527 /* Debug message for tail-recursion. Placed here because the
528 * image_put() may end up freeing the image.
529 */
530 if ( replacement ) {
531 DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
532 image->name, replacement->name );
533 }
534
535 /* Drop temporary reference to the original image */
536 image_put ( image );
537
538 /* Restore previous currently-running image */
539 image_tag ( saved_current_image, &current_image );
540
541 /* Reset current working directory */
542 churi ( old_cwuri );
543 uri_put ( old_cwuri );
544
545 /* Tail-recurse into replacement image, if one exists */
546 if ( replacement )
547 return image_exec ( replacement );
548
549 return rc;
550}
void churi(struct uri *uri)
Change working URI.
Definition cwuri.c:46
struct uri * cwuri
Current working URI.
Definition cwuri.c:39
const char * replacement
Definition editstring.h:54
#define ENOEXEC
Exec format error.
Definition errno.h:520
#define LOG_ERR
Error: error conditions.
Definition syslog.h:36
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition syslog.h:42
#define EACCES_UNTRUSTED
Definition image.c:49
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:390
int image_exec(struct image *image)
Execute image.
Definition image.c:446
static int require_trusted_images
Current image trust requirement.
Definition image.c:72
int register_image(struct image *image)
Register executable image.
Definition image.c:336
struct image_tag current_image
#define IMAGE_TRUSTED
Image is trusted.
Definition image.h:87
static struct image * image_tag(struct image *image, struct image_tag *tag)
Tag image.
Definition image.h:305
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition image.h:90
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int(* exec)(struct image *image)
Execute image.
Definition image.h:120
#define syslog(priority, fmt,...)
Write message to system log.
Definition syslog.h:94

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(), image::replacement, 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)
extern

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

561 {
562 struct image *image = current_image.image;
563 int rc;
564
565 /* Sanity check */
567
568 /* Fail unless there is a currently-executing image */
569 if ( ! image ) {
570 rc = -ENOTTY;
571 DBGC ( replacement, "IMAGE %s cannot replace non-existent "
572 "image: %s\n", replacement->name, strerror ( rc ) );
573 return rc;
574 }
575
576 /* Check that the replacement image can be executed */
577 if ( ! ( replacement->type && replacement->type->exec ) )
578 return -ENOEXEC;
579
580 /* Clear any existing replacement */
582
583 /* Set replacement */
585 DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
586 image->name, replacement->name );
587
588 return 0;
589}

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

Referenced by comboot_fetch_kernel(), image_extract_exec(), and imgexec().

◆ image_select()

int image_select ( struct image * image)
extern

Select image for execution.

Parameters
imageExecutable image
Return values
rcReturn status code

Definition at line 597 of file image.c.

597 {
598
599 /* Check that this image can be executed */
600 if ( ! ( image->type && image->type->exec ) )
601 return -ENOEXEC;
602
603 /* Mark image as selected */
605
606 return 0;
607}
struct image_tag selected_image

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 )
extern

Change image trust requirement.

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

Definition at line 616 of file image.c.

616 {
617
618 /* Update trust requirement, if permitted to do so */
620 require_trusted_images = require_trusted;
622 }
623
624 /* Fail if we attempted to change the trust requirement but
625 * were not permitted to do so.
626 */
627 if ( require_trusted_images != require_trusted )
628 return -EACCES_PERMANENT;
629
630 return 0;
631}
static int require_trusted_images_permanent
Prevent changes to image trust requirement.
Definition image.c:75
#define EACCES_PERMANENT
Definition image.c:53

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 )
extern

Create registered image from block of memory.

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

Definition at line 641 of file image.c.

642 {
643 struct image *image;
644 int rc;
645
646 /* Allocate image */
647 image = alloc_image ( NULL );
648 if ( ! image ) {
649 rc = -ENOMEM;
650 goto err_alloc_image;
651 }
652
653 /* Set name */
654 if ( ( rc = image_set_name ( image, name ) ) != 0 )
655 goto err_set_name;
656
657 /* Set data */
658 if ( ( rc = image_set_data ( image, data, len ) ) != 0 )
659 goto err_set_data;
660
661 /* Register image */
662 if ( ( rc = register_image ( image ) ) != 0 )
663 goto err_register;
664
665 /* Drop local reference to image */
666 image_put ( image );
667
668 return image;
669
670 err_register:
671 err_set_data:
672 err_set_name:
673 image_put ( image );
674 err_alloc_image:
675 return NULL;
676}
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition image.c:127
int image_set_data(struct image *image, const void *data, size_t len)
Set image data.
Definition image.c:291

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

Referenced by archive_okx(), fdt_test_exec(), imgmem(), and initrd_init().

◆ image_argument()

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

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

685 {
686 const char *cmdline = image->cmdline;
687 const char *search;
688 const char *match;
689 const char *next;
690
691 /* Find argument */
692 for ( search = cmdline ; search ; search = next ) {
693
694 /* Find next occurrence, if any */
695 match = strstr ( search, key );
696 if ( ! match )
697 break;
698 next = ( match + strlen ( key ) );
699
700 /* Check preceding delimiter, if any */
701 if ( ( match == cmdline ) || isspace ( match[-1] ) )
702 return next;
703 }
704
705 return NULL;
706}
union @162305117151260234136356364136041353210355154177 key
int isspace(int character)
Check to see if character is a space.
Definition ctype.c:42
uint32_t next
Next descriptor address.
Definition dwmac.h:11
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition string.c:324
size_t strlen(const char *src)
Get length of string.
Definition string.c:244

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

Referenced by bzimage_parse_cmdline(), and cpio_parse_cmdline().

◆ image_pixbuf()

int image_pixbuf ( struct image * image,
struct pixel_buffer ** pixbuf )
extern

Create pixel buffer from image.

Parameters
imageImage
pixbufPixel buffer to fill in
Return values
rcReturn status code

Definition at line 100 of file pixbuf.c.

100 {
101 int rc;
102
103 /* Check that this image can be used to create a pixel buffer */
104 if ( ! ( image->type && image->type->pixbuf ) )
105 return -ENOTSUP;
106
107 /* Try creating pixel buffer */
108 if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
109 DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
110 image->name, strerror ( rc ) );
111 return rc;
112 }
113
114 return 0;
115}
#define ENOTSUP
Operation not supported.
Definition errno.h:590
int(* pixbuf)(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition image.h:128

References DBGC, ENOTSUP, image::name, image_type::pixbuf, rc, strerror(), and image::type.

Referenced by console_exec(), pixbuf_okx(), and REQUIRING_SYMBOL().

◆ image_asn1()

int image_asn1 ( struct image * image,
size_t offset,
struct asn1_cursor ** cursor )
extern

Extract ASN.1 object from image.

Parameters
imageImage
offsetOffset within image
cursorASN.1 cursor to fill in
Return values
nextOffset to next image, or negative error

The caller is responsible for eventually calling free() on the allocated ASN.1 cursor.

Definition at line 1098 of file asn1.c.

1099 {
1100 int next;
1101 int rc;
1102
1103 /* Sanity check */
1105
1106 /* Check that this image can be used to extract an ASN.1 object */
1107 if ( ! ( image->type && image->type->asn1 ) )
1108 return -ENOTSUP;
1109
1110 /* Try creating ASN.1 cursor */
1111 next = image->type->asn1 ( image, offset, cursor );
1112 if ( next < 0 ) {
1113 rc = next;
1114 DBGC ( image, "IMAGE %s could not extract ASN.1 object: %s\n",
1115 image->name, strerror ( rc ) );
1116 return rc;
1117 }
1118
1119 return next;
1120}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
int(* asn1)(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition image.h:140

References image_type::asn1, assert, DBGC, ENOTSUP, len, image::name, next, offset, rc, strerror(), and image::type.

Referenced by asn1_okx(), cms_message(), image_x509(), and REQUIRING_SYMBOL().

◆ image_extract()

int image_extract ( struct image * image,
const char * name,
struct image ** extracted )
extern

Extract archive image.

Parameters
imageImage
nameExtracted image name
extractedExtracted image to fill in
Return values
rcReturn status code

Definition at line 45 of file archive.c.

46 {
47 int rc;
48
49 /* Check that this image can be used to extract an archive image */
50 if ( ! ( image->type && image->type->extract ) ) {
51 rc = -ENOTSUP;
52 goto err_unsupported;
53 }
54
55 /* Allocate new image */
56 *extracted = alloc_image ( image->uri );
57 if ( ! *extracted ) {
58 rc = -ENOMEM;
59 goto err_alloc;
60 }
61
62 /* Set image name */
63 if ( ( rc = image_set_name ( *extracted,
64 ( name ? name : image->name ) ) ) != 0 ) {
65 goto err_set_name;
66 }
67
68 /* Strip any archive or compression suffix from implicit name */
69 if ( ! name )
70 image_strip_suffix ( *extracted );
71
72 /* Try extracting archive image */
73 if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) {
74 DBGC ( image, "IMAGE %s could not extract image: %s\n",
75 image->name, strerror ( rc ) );
76 goto err_extract;
77 }
78
79 /* Register image */
80 if ( ( rc = register_image ( *extracted ) ) != 0 )
81 goto err_register;
82
83 /* Propagate trust flag */
84 if ( image->flags & IMAGE_TRUSTED )
85 image_trust ( *extracted );
86
87 /* Drop local reference to image */
88 image_put ( *extracted );
89
90 return 0;
91
92 unregister_image ( *extracted );
93 err_register:
94 err_extract:
95 err_set_name:
96 image_put ( *extracted );
97 err_alloc:
98 err_unsupported:
99 return rc;
100}
char * image_strip_suffix(struct image *image)
Strip dot suffix from image name, if present.
Definition image.c:210
static void image_trust(struct image *image)
Set image as trusted.
Definition image.h:276
int(* extract)(struct image *image, struct image *extracted)
Extract archive image.
Definition image.h:149

References alloc_image(), DBGC, ENOMEM, ENOTSUP, image_type::extract, image::flags, image_put(), image_set_name(), image_strip_suffix(), image_trust(), IMAGE_TRUSTED, image::name, name, rc, register_image(), strerror(), image::type, unregister_image(), and image::uri.

Referenced by archive_okx(), image_extract_exec(), imgextract(), and REQUIRING_SYMBOL().

◆ image_extract_exec()

int image_extract_exec ( struct image * image)
extern

Extract and execute image.

Parameters
imageImage
Return values
rcReturn status code

Definition at line 108 of file archive.c.

108 {
109 struct image *extracted;
110 int rc;
111
112 /* Extract image */
113 if ( ( rc = image_extract ( image, NULL, &extracted ) ) != 0 )
114 goto err_extract;
115
116 /* Set image command line */
117 if ( ( rc = image_set_cmdline ( extracted, image->cmdline ) ) != 0 )
118 goto err_set_cmdline;
119
120 /* Set auto-unregister flag */
121 extracted->flags |= IMAGE_AUTO_UNREGISTER;
122
123 /* Replace current image */
124 if ( ( rc = image_replace ( extracted ) ) != 0 )
125 goto err_replace;
126
127 /* Return to allow replacement image to be executed */
128 return 0;
129
130 err_replace:
131 err_set_cmdline:
132 unregister_image ( extracted );
133 err_extract:
134 return rc;
135}
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition archive.c:45
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition image.c:230
int image_replace(struct image *replacement)
Set replacement image.
Definition image.c:561

References image::cmdline, image::flags, IMAGE_AUTO_UNREGISTER, image_extract(), image_replace(), image_set_cmdline(), NULL, rc, and unregister_image().

Referenced by __image_type().

◆ image_get()

struct image * image_get ( struct image * image)
inlinestatic

Increment reference count on an image.

Parameters
imageImage
Return values
imageImage

Definition at line 248 of file image.h.

248 {
249 ref_get ( &image->refcnt );
250 return image;
251}
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93

References ref_get, and image::refcnt.

Referenced by bzimage_exec(), cms_decrypt(), create_downloader(), efi_file_open_image(), image_exec(), image_replace(), and register_image().

◆ image_put()

void image_put ( struct image * image)
inlinestatic

Decrement reference count on an image.

Parameters
imageImage

Definition at line 258 of file image.h.

258 {
259 ref_put ( &image->refcnt );
260}
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107

References ref_put, and image::refcnt.

Referenced by alloc_image(), cmdline_init(), cms_decrypt(), cpio_okx(), downloader_free(), efi_cmdline_init(), efi_file_free(), free_image(), image_exec(), image_extract(), image_memory(), image_replace(), imgdownload(), and unregister_image().

◆ image_clear_cmdline()

void image_clear_cmdline ( struct image * image)
inlinestatic

Clear image command line.

Parameters
imageImage

Definition at line 267 of file image.h.

267 {
269}
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition image.c:230

References image_set_cmdline(), and NULL.

◆ image_trust()

void image_trust ( struct image * image)
inlinestatic

Set image as trusted.

Parameters
imageImage

Definition at line 276 of file image.h.

276 {
278}

References image::flags, and IMAGE_TRUSTED.

Referenced by cms_verify(), embedded_init(), and image_extract().

◆ image_untrust()

void image_untrust ( struct image * image)
inlinestatic

Set image as untrusted.

Parameters
imageImage

Definition at line 285 of file image.h.

285 {
287}

References image::flags, and IMAGE_TRUSTED.

Referenced by cms_decrypt(), and cms_verify().

◆ image_hide()

void image_hide ( struct image * image)
inlinestatic

Mark image as hidden.

Parameters
imageImage

Definition at line 294 of file image.h.

294 {
296}
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition image.h:93

References image::flags, and IMAGE_HIDDEN.

Referenced by imgfdt(), and shim().

◆ image_tag()

struct image * image_tag ( struct image * image,
struct image_tag * tag )
inlinestatic

Tag image.

Parameters
imageImage
tagImage tag
Return values
prevPrevious tagged image (if any)

Definition at line 305 of file image.h.

306 {
307 struct image *prev = tag->image;
308
309 tag->image = image;
310 return prev;
311}

References tag.

Referenced by fdt_test_exec(), image_exec(), image_select(), imgfdt(), and shim().

Variable Documentation

◆ images

struct list_head images
extern

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().

◆ current_image

struct image_tag current_image
extern

◆ selected_image

struct image_tag selected_image
extern

◆ empty_image_data

const char empty_image_data[]
extern

Empty image terminating NUL.

Definition at line 78 of file image.c.

78{ '\0' };

Referenced by alloc_image(), and image_set_len().