You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

image.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef _IPXE_IMAGE_H
  2. #define _IPXE_IMAGE_H
  3. /**
  4. * @file
  5. *
  6. * Executable images
  7. *
  8. */
  9. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  10. #include <ipxe/tables.h>
  11. #include <ipxe/list.h>
  12. #include <ipxe/uaccess.h>
  13. #include <ipxe/refcnt.h>
  14. struct uri;
  15. struct pixel_buffer;
  16. struct asn1_cursor;
  17. struct image_type;
  18. /** An executable image */
  19. struct image {
  20. /** Reference count */
  21. struct refcnt refcnt;
  22. /** List of registered images */
  23. struct list_head list;
  24. /** URI of image */
  25. struct uri *uri;
  26. /** Name */
  27. char *name;
  28. /** Flags */
  29. unsigned int flags;
  30. /** Command line to pass to image */
  31. char *cmdline;
  32. /** Raw file image */
  33. userptr_t data;
  34. /** Length of raw file image */
  35. size_t len;
  36. /** Image type, if known */
  37. struct image_type *type;
  38. /** Replacement image
  39. *
  40. * An image wishing to replace itself with another image (in a
  41. * style similar to a Unix exec() call) should return from its
  42. * exec() method with the replacement image set to point to
  43. * the new image.
  44. *
  45. * If an image unregisters itself as a result of being
  46. * executed, it must make sure that its replacement image (if
  47. * any) is registered, otherwise the replacement is likely to
  48. * be freed before it can be executed.
  49. */
  50. struct image *replacement;
  51. };
  52. /** Image is registered */
  53. #define IMAGE_REGISTERED 0x00001
  54. /** Image is selected for execution */
  55. #define IMAGE_SELECTED 0x0002
  56. /** Image is trusted */
  57. #define IMAGE_TRUSTED 0x0004
  58. /** Image will be automatically unregistered after execution */
  59. #define IMAGE_AUTO_UNREGISTER 0x0008
  60. /** An executable image type */
  61. struct image_type {
  62. /** Name of this image type */
  63. char *name;
  64. /**
  65. * Probe image
  66. *
  67. * @v image Image
  68. * @ret rc Return status code
  69. *
  70. * Return success if the image is of this image type.
  71. */
  72. int ( * probe ) ( struct image *image );
  73. /**
  74. * Execute image
  75. *
  76. * @v image Image
  77. * @ret rc Return status code
  78. */
  79. int ( * exec ) ( struct image *image );
  80. /**
  81. * Create pixel buffer from image
  82. *
  83. * @v image Image
  84. * @v pixbuf Pixel buffer to fill in
  85. * @ret rc Return status code
  86. */
  87. int ( * pixbuf ) ( struct image *image, struct pixel_buffer **pixbuf );
  88. /**
  89. * Extract ASN.1 object from image
  90. *
  91. * @v image Image
  92. * @v offset Offset within image
  93. * @v cursor ASN.1 cursor to fill in
  94. * @ret next Offset to next image, or negative error
  95. *
  96. * The caller is responsible for eventually calling free() on
  97. * the allocated ASN.1 cursor.
  98. */
  99. int ( * asn1 ) ( struct image *image, size_t offset,
  100. struct asn1_cursor **cursor );
  101. };
  102. /**
  103. * Multiboot image probe priority
  104. *
  105. * Multiboot images are also valid executables in another format
  106. * (e.g. ELF), so we must perform the multiboot probe first.
  107. */
  108. #define PROBE_MULTIBOOT 01
  109. /**
  110. * Normal image probe priority
  111. */
  112. #define PROBE_NORMAL 02
  113. /**
  114. * PXE image probe priority
  115. *
  116. * PXE images have no signature checks, so will claim all image files.
  117. * They must therefore be tried last in the probe order list.
  118. */
  119. #define PROBE_PXE 03
  120. /** Executable image type table */
  121. #define IMAGE_TYPES __table ( struct image_type, "image_types" )
  122. /** An executable image type */
  123. #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
  124. extern struct list_head images;
  125. extern struct image *current_image;
  126. /** Iterate over all registered images */
  127. #define for_each_image( image ) \
  128. list_for_each_entry ( (image), &images, list )
  129. /** Iterate over all registered images, safe against deletion */
  130. #define for_each_image_safe( image, tmp ) \
  131. list_for_each_entry_safe ( (image), (tmp), &images, list )
  132. /**
  133. * Test for existence of images
  134. *
  135. * @ret existence Some images exist
  136. */
  137. static inline int have_images ( void ) {
  138. return ( ! list_empty ( &images ) );
  139. }
  140. /**
  141. * Retrieve first image
  142. *
  143. * @ret image Image, or NULL
  144. */
  145. static inline struct image * first_image ( void ) {
  146. return list_first_entry ( &images, struct image, list );
  147. }
  148. extern struct image * alloc_image ( struct uri *uri );
  149. extern int image_set_uri ( struct image *image, struct uri *uri );
  150. extern int image_set_name ( struct image *image, const char *name );
  151. extern int image_set_cmdline ( struct image *image, const char *cmdline );
  152. extern int register_image ( struct image *image );
  153. extern void unregister_image ( struct image *image );
  154. struct image * find_image ( const char *name );
  155. extern int image_exec ( struct image *image );
  156. extern int image_replace ( struct image *replacement );
  157. extern int image_select ( struct image *image );
  158. extern struct image * image_find_selected ( void );
  159. extern int image_set_trust ( int require_trusted, int permanent );
  160. extern int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf );
  161. extern int image_asn1 ( struct image *image, size_t offset,
  162. struct asn1_cursor **cursor );
  163. /**
  164. * Increment reference count on an image
  165. *
  166. * @v image Image
  167. * @ret image Image
  168. */
  169. static inline struct image * image_get ( struct image *image ) {
  170. ref_get ( &image->refcnt );
  171. return image;
  172. }
  173. /**
  174. * Decrement reference count on an image
  175. *
  176. * @v image Image
  177. */
  178. static inline void image_put ( struct image *image ) {
  179. ref_put ( &image->refcnt );
  180. }
  181. /**
  182. * Clear image command line
  183. *
  184. * @v image Image
  185. */
  186. static inline void image_clear_cmdline ( struct image *image ) {
  187. image_set_cmdline ( image, NULL );
  188. }
  189. /**
  190. * Set image as trusted
  191. *
  192. * @v image Image
  193. */
  194. static inline void image_trust ( struct image *image ) {
  195. image->flags |= IMAGE_TRUSTED;
  196. }
  197. /**
  198. * Set image as untrusted
  199. *
  200. * @v image Image
  201. */
  202. static inline void image_untrust ( struct image *image ) {
  203. image->flags &= ~IMAGE_TRUSTED;
  204. }
  205. #endif /* _IPXE_IMAGE_H */