iPXE
image.h
Go to the documentation of this file.
1 #ifndef _IPXE_IMAGE_H
2 #define _IPXE_IMAGE_H
3 
4 /**
5  * @file
6  *
7  * Executable images
8  *
9  */
10 
11 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
12 
13 #include <ipxe/tables.h>
14 #include <ipxe/list.h>
15 #include <ipxe/refcnt.h>
16 
17 struct uri;
18 struct pixel_buffer;
19 struct asn1_cursor;
20 struct image_type;
21 
22 /** An executable image */
23 struct image {
24  /** Reference count */
25  struct refcnt refcnt;
26 
27  /** List of registered images */
28  struct list_head list;
29 
30  /** URI of image */
31  struct uri *uri;
32  /** Name
33  *
34  * If the @c IMAGE_STATIC_NAME flag is set, then this is a
35  * statically allocated string.
36  */
37  char *name;
38  /** Flags */
39  unsigned int flags;
40 
41  /** Command line to pass to image */
42  char *cmdline;
43  /** Raw file image
44  *
45  * If the @c IMAGE_STATIC flag is set, then this is a
46  * statically allocated image.
47  */
48  union {
49  /** Read-only data */
50  const void *data;
51  /** Writable data */
52  void *rwdata;
53  };
54  /** Length of raw file image */
55  size_t len;
56 
57  /** Image type, if known */
58  struct image_type *type;
59 
60  /** Replacement image
61  *
62  * An image wishing to replace itself with another image (in a
63  * style similar to a Unix exec() call) should return from its
64  * exec() method with the replacement image set to point to
65  * the new image.
66  *
67  * If an image unregisters itself as a result of being
68  * executed, it must make sure that its replacement image (if
69  * any) is registered, otherwise the replacement is likely to
70  * be freed before it can be executed.
71  */
72  struct image *replacement;
73 };
74 
75 /** Image is registered */
76 #define IMAGE_REGISTERED 0x0001
77 
78 /** Image is trusted */
79 #define IMAGE_TRUSTED 0x0002
80 
81 /** Image will be automatically unregistered after execution */
82 #define IMAGE_AUTO_UNREGISTER 0x0004
83 
84 /** Image will be hidden from enumeration */
85 #define IMAGE_HIDDEN 0x0008
86 
87 /** Image is statically allocated */
88 #define IMAGE_STATIC 0x0010
89 
90 /** Image name is statically allocated */
91 #define IMAGE_STATIC_NAME 0x0020
92 
93 /** An executable image type */
94 struct image_type {
95  /** Name of this image type */
96  char *name;
97  /**
98  * Probe image
99  *
100  * @v image Image
101  * @ret rc Return status code
102  *
103  * Return success if the image is of this image type.
104  */
105  int ( * probe ) ( struct image *image );
106  /**
107  * Execute image
108  *
109  * @v image Image
110  * @ret rc Return status code
111  */
112  int ( * exec ) ( struct image *image );
113  /**
114  * Create pixel buffer from image
115  *
116  * @v image Image
117  * @v pixbuf Pixel buffer to fill in
118  * @ret rc Return status code
119  */
120  int ( * pixbuf ) ( struct image *image, struct pixel_buffer **pixbuf );
121  /**
122  * Extract ASN.1 object from image
123  *
124  * @v image Image
125  * @v offset Offset within image
126  * @v cursor ASN.1 cursor to fill in
127  * @ret next Offset to next image, or negative error
128  *
129  * The caller is responsible for eventually calling free() on
130  * the allocated ASN.1 cursor.
131  */
132  int ( * asn1 ) ( struct image *image, size_t offset,
133  struct asn1_cursor **cursor );
134  /**
135  * Extract archive image
136  *
137  * @v image Image
138  * @v extracted Extracted image
139  * @ret rc Return status code
140  */
141  int ( * extract ) ( struct image *image, struct image *extracted );
142 };
143 
144 /**
145  * Multiboot image probe priority
146  *
147  * Multiboot images are also valid executables in another format
148  * (e.g. ELF), so we must perform the multiboot probe first.
149  */
150 #define PROBE_MULTIBOOT 01
151 
152 /**
153  * Normal image probe priority
154  */
155 #define PROBE_NORMAL 02
156 
157 /**
158  * PXE image probe priority
159  *
160  * PXE images have no signature checks, so will claim all image files.
161  * They must therefore be tried last in the probe order list.
162  */
163 #define PROBE_PXE 03
164 
165 /** Executable image type table */
166 #define IMAGE_TYPES __table ( struct image_type, "image_types" )
167 
168 /** An executable image type */
169 #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
170 
171 /** An image tag */
172 struct image_tag {
173  /** Name */
174  const char *name;
175  /** Image (weak reference, nullified when image is freed) */
176  struct image *image;
177 };
178 
179 /** Image tag table */
180 #define IMAGE_TAGS __table ( struct image_tag, "image_tags" )
181 
182 /** An image tag */
183 #define __image_tag __table_entry ( IMAGE_TAGS, 01 )
184 
185 extern struct list_head images;
186 extern struct image_tag current_image;
187 extern struct image_tag selected_image;
188 
189 /** Iterate over all registered images */
190 #define for_each_image( image ) \
191  list_for_each_entry ( (image), &images, list )
192 
193 /** Iterate over all registered images, safe against deletion */
194 #define for_each_image_safe( image, tmp ) \
195  list_for_each_entry_safe ( (image), (tmp), &images, list )
196 
197 /**
198  * Retrieve first image
199  *
200  * @ret image Image, or NULL
201  */
202 static inline struct image * first_image ( void ) {
203  return list_first_entry ( &images, struct image, list );
204 }
205 
206 extern void free_image ( struct refcnt *refcnt );
207 extern struct image * alloc_image ( struct uri *uri );
208 extern int image_set_uri ( struct image *image, struct uri *uri );
209 extern int image_set_name ( struct image *image, const char *name );
210 extern char * image_strip_suffix ( struct image *image );
211 extern int image_set_cmdline ( struct image *image, const char *cmdline );
212 extern int image_set_len ( struct image *image, size_t len );
213 extern int image_set_data ( struct image *image, const void *data,
214  size_t len );
215 extern int register_image ( struct image *image );
216 extern void unregister_image ( struct image *image );
217 extern struct image * find_image ( const char *name );
218 extern struct image * find_image_tag ( struct image_tag *tag );
219 extern int image_exec ( struct image *image );
220 extern int image_replace ( struct image *replacement );
221 extern int image_select ( struct image *image );
222 extern int image_set_trust ( int require_trusted, int permanent );
223 extern struct image * image_memory ( const char *name, const void *data,
224  size_t len );
225 extern const char * image_argument ( struct image *image, const char *key );
226 extern int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf );
227 extern int image_asn1 ( struct image *image, size_t offset,
228  struct asn1_cursor **cursor );
229 extern int image_extract ( struct image *image, const char *name,
230  struct image **extracted );
231 extern int image_extract_exec ( struct image *image );
232 
233 /**
234  * Increment reference count on an image
235  *
236  * @v image Image
237  * @ret image Image
238  */
239 static inline struct image * image_get ( struct image *image ) {
240  ref_get ( &image->refcnt );
241  return image;
242 }
243 
244 /**
245  * Decrement reference count on an image
246  *
247  * @v image Image
248  */
249 static inline void image_put ( struct image *image ) {
250  ref_put ( &image->refcnt );
251 }
252 
253 /**
254  * Clear image command line
255  *
256  * @v image Image
257  */
258 static inline void image_clear_cmdline ( struct image *image ) {
260 }
261 
262 /**
263  * Set image as trusted
264  *
265  * @v image Image
266  */
267 static inline void image_trust ( struct image *image ) {
269 }
270 
271 /**
272  * Set image as untrusted
273  *
274  * @v image Image
275  */
276 static inline void image_untrust ( struct image *image ) {
278 }
279 
280 /**
281  * Mark image as hidden
282  *
283  * @v image Image
284  */
285 static inline void image_hide ( struct image *image ) {
287 }
288 
289 /**
290  * Tag image
291  *
292  * @v image Image
293  * @v tag Image tag
294  * @ret prev Previous tagged image (if any)
295  */
296 static inline struct image * image_tag ( struct image *image,
297  struct image_tag *tag ) {
298  struct image *prev = tag->image;
299 
300  tag->image = image;
301  return prev;
302 }
303 
304 #endif /* _IPXE_IMAGE_H */
const char * image_argument(struct image *image, const char *key)
Find argument within image command line.
Definition: image.c:652
struct image_tag selected_image
unsigned int flags
Flags.
Definition: image.h:39
const char * name
Definition: ath9k_hw.c:1984
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:225
An image tag.
Definition: image.h:172
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:392
int register_image(struct image *image)
Register executable image.
Definition: image.c:314
const void * data
Read-only data.
Definition: image.h:50
struct image_type * type
Image type, if known.
Definition: image.h:58
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:239
An executable image type.
Definition: image.h:94
An executable image.
Definition: image.h:23
struct image * image
Image (weak reference, nullified when image is freed)
Definition: image.h:176
int(* exec)(struct image *image)
Execute image.
Definition: image.h:112
char * name
Name of this image type.
Definition: image.h:96
char * cmdline
Command line to pass to image.
Definition: image.h:42
A doubly-linked list entry (or list head)
Definition: list.h:18
int image_set_len(struct image *image, size_t len)
Set image length.
Definition: image.c:244
A reference counter.
Definition: refcnt.h:26
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
struct image_tag current_image
const char * replacement
Definition: editstring.h:53
ring len
Length.
Definition: dwmac.h:231
int image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: asn1.c:1015
int image_set_data(struct image *image, const void *data, size_t len)
Set image data.
Definition: image.c:269
int(* asn1)(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: image.h:132
int image_set_uri(struct image *image, struct uri *uri)
Set image URI.
Definition: image.c:152
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition: image.c:123
static struct image * first_image(void)
Retrieve first image.
Definition: image.h:202
Linked lists.
struct image * find_image(const char *name)
Find image by name.
Definition: image.c:375
static void image_untrust(struct image *image)
Set image as untrusted.
Definition: image.h:276
size_t len
Length of raw file image.
Definition: image.h:55
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
char * image_strip_suffix(struct image *image)
Strip dot suffix from image name, if present.
Definition: image.c:205
A pixel buffer.
Definition: pixbuf.h:16
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:85
#define IMAGE_TRUSTED
Image is trusted.
Definition: image.h:79
struct uri * uri
URI of image.
Definition: image.h:31
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:249
int(* extract)(struct image *image, struct image *extracted)
Extract archive image.
Definition: image.h:141
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition: archive.c:44
int image_extract_exec(struct image *image)
Extract and execute image.
Definition: archive.c:107
int image_pixbuf(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: pixbuf.c:99
void free_image(struct refcnt *refcnt)
Free executable image.
Definition: image.c:85
void * rwdata
Writable data.
Definition: image.h:52
int(* pixbuf)(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: image.h:120
int image_set_trust(int require_trusted, int permanent)
Change image trust requirement.
Definition: image.c:583
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition: image.h:258
Reference counting.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct image * replacement
Replacement image.
Definition: image.h:72
Linker tables.
int image_select(struct image *image)
Select image for execution.
Definition: image.c:564
int(* probe)(struct image *image)
Probe image.
Definition: image.h:105
A Uniform Resource Identifier.
Definition: uri.h:64
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static void image_trust(struct image *image)
Set image as trusted.
Definition: image.h:267
struct list_head list
List of registered images.
Definition: image.h:28
static struct image * image_tag(struct image *image, struct image_tag *tag)
Tag image.
Definition: image.h:296
uint32_t cmdline
Definition: multiboot.h:16
struct image * image_memory(const char *name, const void *data, size_t len)
Create registered image from block of memory.
Definition: image.c:608
uint64_t tag
Identity tag.
Definition: edd.h:30
int image_exec(struct image *image)
Execute image.
Definition: image.c:413
char * name
Name.
Definition: image.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
const char * name
Name.
Definition: image.h:174
An ASN.1 object cursor.
Definition: asn1.h:20
union @391 key
Sense key.
Definition: scsi.h:17
int image_replace(struct image *replacement)
Set replacement image.
Definition: image.c:528
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
struct list_head images
List of registered images.
Definition: image.c:58
static void image_hide(struct image *image)
Mark image as hidden.
Definition: image.h:285
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:180
struct refcnt refcnt
Reference count.
Definition: image.h:25