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