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 4.9KB

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